HighLAND
SystematicCovariance.cxx
1 #include "SystematicCovariance.hxx"
2 
3 
4 //***********************************************************
5 SystematicCovariance::SystematicCovariance(){
6 //***********************************************************
7 
8 }
9 
10 //***********************************************************
11 SystematicCovariance::~SystematicCovariance() {
12 //***********************************************************
13 
14 }
15 
16 
17 //********************************************************************
19 //********************************************************************
20 
21  std::cout << " constructing things! " << std::endl;
22 
23  int nTotalParameters = 0;
24 
25  int nVarSys = 0;
26  EventVariationBase** varArr = man->GetVariationSystematics(nVarSys);
27  std::cout << "nVarSys = " << nVarSys << std::endl;
28  for (int it = 0; it < nVarSys; it++)
29  nTotalParameters += varArr[it]->GetNParameters();
30 
31  int nSys = 0;
32  EventWeightBase** weightArr = man->GetWeightSystematics(nSys);
33  for (int it = 0; it < nSys; it++)
34  nTotalParameters += weightArr[it]->GetNParameters();
35 
36  nSys = 0;
37  weightArr = man->GetFluxSystematics(nSys);
38  for (int it = 0; it < nSys; it++)
39  nTotalParameters += weightArr[it]->GetNParameters();
40 
41 
42  std::cout << " Total parameters: " << nTotalParameters << std::endl;
43 
44  _covarianceMatrix = new TMatrixT<double>(nTotalParameters,nTotalParameters);
45 
46  nSys = 0;
47  for (int it = 0; it < nVarSys; it++)
48  FillLinks(varArr[it]);
49 
50  nSys = 0;
51  weightArr = man->GetWeightSystematics(nSys);
52  for (int it = 0; it < nSys; it++)
53  FillLinks(weightArr[it]);
54 
55  nSys = 0;
56  weightArr = man->GetFluxSystematics(nSys);
57  for (int it = 0; it < nSys; it++)
58  FillLinks(weightArr[it]);
59 
60  MapIndices();
61 
62  for(unsigned int i=0; i<covariances.size(); i++)
63  for(unsigned int j=0; j<covlinks[i].inputindex.size(); j++)
64  for(unsigned int k=0; k<covlinks[i].inputindex.size(); k++)
65  (*_covarianceMatrix)(covlinks[i].covarianceindex[j],covlinks[i].covarianceindex[k]) = (*covariances[i])(covlinks[i].inputindex[j],covlinks[i].inputindex[k]);
66 
67  for(unsigned int i=0; i<links.size(); i++)
68  if(links[i].inputcov == -1)
69  (*_covarianceMatrix)(links[i].covarianceindex,links[i].covarianceindex) = 1;
70 
71 // _covarianceMatrix->Print();
72 
73 }
74 
75 //********************************************************************
76 bool SystematicCovariance::IsInList(std::string name, std::vector< std::string > list, int& index){
77 //********************************************************************
78 
79  index=0;
80  for (std::vector< std::string >::iterator it = list.begin(); it != list.end(); it++, index++)
81  if( (*it) == name)
82  return true;
83  return false;
84 }
85 
86 //********************************************************************
87 void SystematicCovariance::FillLinks(SystematicBase* syst){
88 //********************************************************************
89 
90  std::cout << "filling links" << std::endl;
91 
92  TString parname = syst->GetName();
93  parname.Remove(0,1);//get rid of the initial "k";
94  char paramcov[200];
95  sprintf(paramcov,"psycheSystematics.%s.CovFile",parname.Data());
96 // std::cout << paramcov << std::endl;
97 
98 
99  //if we don't assign an input covariance file
100  if(!ND::params().HasParameter(paramcov))
101  {
102  for(int i=0; i<(int)syst->GetNParameters(); i++)
103  {
104  indexlink temp;
105  temp.covarianceindex = links.size();
106  temp.inputindex = -1;
107  temp.inputcov=-1;
108  links.push_back(temp);
109  }
110 
111  }
112  else
113  {
114  int index;
115  if(!IsInList(ND::params().GetParameterS(paramcov),listoffiles,index))
116  {
117  listoffiles.push_back(ND::params().GetParameterS(paramcov));
118  char dirname[300];
119  sprintf(dirname,"%s/data/%s",getenv("PSYCHESYSTEMATICSROOT"),ND::params().GetParameterS(paramcov).c_str());
120 // std::cout << dirname << std::endl;
121  TFile file(dirname);
122 
123  char paramcovname[300];
124  sprintf(paramcovname,"psycheSystematics.%s.CovFileCov",parname.Data());
125 // std::cout << paramcovname << std::endl;
126 
127  TMatrixT<double>* tempcov = (TMatrixT<double>*)file.Get(ND::params().GetParameterS(paramcovname).c_str());
128  covariances.push_back(tempcov);
129  }
130  char paramcovlo[200];
131  sprintf(paramcovlo,"psycheSystematics.%s.CovLowIndex",parname.Data());
132 // std::cout << paramcovlo << std::endl;
133  int loindex = ND::params().GetParameterI(paramcovlo);
134 
135  sprintf(paramcovlo,"psycheSystematics.%s.CovHighIndex",parname.Data());
136 // std::cout << paramcovlo << std::endl;
137  int hiindex = ND::params().GetParameterI(paramcovlo);
138 
139  if(hiindex-loindex+1 != (int) syst->GetNParameters())
140  std::cerr << " Parameter Number mismatch!!!!!!" << std::endl;
141 
142  for(int i=0; i<(int)syst->GetNParameters(); i++)
143  {
144  indexlink temp;
145  temp.covarianceindex = (int) links.size();
146  temp.inputindex = loindex+i;
147  temp.inputcov=index;
148  links.push_back(temp);
149  }
150 
151  }
152 
153 
154 
155 }
156 
157 
158 //********************************************************************
159 void SystematicCovariance::MapIndices(){
160 //********************************************************************
161 
162  for(unsigned int i=0; i<covariances.size(); i++)
163  {
164  covlink temp;
165  temp.inputindex = std::vector<double>();
166  temp.covarianceindex = std::vector<double>();
167  covlinks.push_back(temp);
168  }
169 
170 
171  for(std::vector<indexlink>::iterator it = links.begin(); it != links.end(); it++)
172  {
173  if((*it).inputcov!=-1)
174  {
175  covlinks[(*it).inputcov].inputindex.push_back((*it).inputindex);
176  covlinks[(*it).inputcov].covarianceindex.push_back((*it).covarianceindex);
177  }
178  }
179 
180 
181 
182 }
void ConstructCovarianceMatrix(SystematicManager *man)
Creates the covariance matrix for the enabled parameters.
int GetParameterI(std::string)
Get parameter. Value is returned as integer.
Definition: Parameters.cxx:217
EventVariationBase ** GetVariationSystematics(int &nSys)
Get the vector of variationSystematics.
std::string GetParameterS(std::string)
Get parameter. Value is returned as string.
Definition: Parameters.cxx:242
std::vector< std::string > listoffiles
list of file names for covariances
The maximum number of systematics that is supported.
EventWeightBase ** GetWeightSystematics(int &nSys)
Get the vector of weightsSystematics.
bool HasParameter(std::string)
Check if Parameter is stored in database.
Definition: Parameters.cxx:202
EventWeightBase ** GetFluxSystematics(int &nSys)
Get the vector of fluxSystematics.
TMatrixT< double > * _covarianceMatrix
The covariance matrix for the enabled systematics.
std::vector< TMatrixT< double > * > covariances
list of covariances
std::vector< covlink > covlinks
vector of covariance links
std::vector< indexlink > links
vector of index links
UInt_t GetNParameters() const
Returns the number of systematic parameters associated to this systematic.
virtual const char * GetName() const
Return the name of this systematic. This overrides the TObject::GetName() interface.