CentralValuesAndUncertainties.cpp
Go to the documentation of this file.
1 
2 
4 #include <set>
5 #include <string>
6 #include <iostream>
7 #include <sstream>
8 
9 #include <boost/property_tree/ptree.hpp>
10 #include <boost/property_tree/xml_parser.hpp>
11 
12 #include "TRandom3.h"
13 
14 //using namespace std;
15 
16 namespace NeutrinoFluxReweight{
17 
18  CentralValuesAndUncertainties* CentralValuesAndUncertainties::instance = 0;
19 
21  //! Setting the seed to zero.
22  r3=new TRandom3(0);
23  baseSeed = 0;
24  }
25 
27  using boost::property_tree::ptree;
28  ptree top;
29 
30  read_xml(filename,top,2); // option 2 removes comment strings
31 
32  //! Reading the uncorrelated parameters:
33  ptree& uncorrelated = top.get_child("pars.uncorrelated");
34  ptree::iterator it = uncorrelated.begin();
35  for(; it!=uncorrelated.end(); ++it){
36  // it->first is the name
37  // it->second is the child property tree
38  double cv=it->second.get<double>("cv");
39  double err=it->second.get<double>("err");
40  Parameter p(it->first,cv);
42  }
43 
44  //! Reading the uncorrelated list:
45  ptree& uncorrelated_list = top.get_child("pars.uncorrelated_list");
46  it = uncorrelated_list.begin();
47  for(; it!=uncorrelated_list.end(); ++it){
48  // it->first is the name
49  // it->second is the child property tree
50 
51  std::string cvs_string=it->second.get<std::string>("cvs");
52  std::string errs_string=it->second.get<std::string>("errs");
53  double cv=0;
54  int ii=0;
56  std::stringstream ss(cvs_string);
57  std::vector<Parameter> tmp_par;
58  while(ss >> cv){
59  std::stringstream sID;
60  sID << ii;
61  std::string nameID = sID.str();
62  name = it->first + "_" + nameID;
63  Parameter p(name,cv);
64  tmp_par.push_back(p);
65  ii++;
66  };
67 
68  double err=0;
69  std::stringstream sserr(errs_string);
70  ii=0;
71  while(sserr >> err){
73  ii++;
74  }
75 
76  }
77 
78  //! Reading the correlated parameters:
79  ptree& correlated = top.get_child("pars.correlated");
80  it = correlated.begin();
81  for(; it!=correlated.end(); ++it){
82  // it->first is the name
83  // it->second is the child property tree
84  std::string cvs_string=it->second.get<std::string>("cvs");
85  std::string covmx_string=it->second.get<std::string>("covmx");
86 
87  //filling the Parameter table:
88  std::stringstream ss(cvs_string);
89  double cv=0;
90  int ii=0;
92  ParameterTable ptable;
93  while(ss >> cv){
94  std::stringstream sID;
95  sID << ii;
96  std::string nameID = sID.str();
97  name = it->first + "_" +nameID;
98  Parameter p(name,cv);
99  ptable.setParameter(p);
100  ii++;
101  };
102  TMatrixD mcov(ii,ii);
103  //filling the matrix:
104  std::stringstream ssmx(covmx_string);
105  double err;
106  int idx = 0;
107  while(ssmx >> err){
108  mcov(idx/ii,idx%ii) = err;
109  idx++;
110  }
112  }
113 
114  //Auxiliar parameter.
115  //cv=1.0 and error 100%:
116  Parameter par_aux("aux_parameter",1.0);
118 
119  }
120 
123  uncorrelated_errors[cv_par.first] = uncertainty;
124  }
125 
126  // void CentralValuesAndUncertainties::addCorrelated(ParameterTable& cv_pars, MatrixClass& cov_mx){
128  correlated_par_tables.push_back(cv_pars);
129  covariance_matrices.push_back(cov_mx);
130  }
131 
133  baseSeed = val;
134  }
136 
137  //If universe = -1, then it is the central value:
138  double cvfactor = 1.0;
139  if(universe==-1)cvfactor = 0.0;
140  int univ_seed = baseSeed + universe;
141  r3->SetSeed(univ_seed);
142 
143  ParameterTable ptable;
144 
145  //! We are going to use 100% correlated bin-to-bin for systematic errors in thin target data:
146  double sigma_pc_pip = r3->Gaus(0.0,1.0);
147  double sigma_pc_pim = r3->Gaus(0.0,1.0);
148  double sigma_pc_kap = r3->Gaus(0.0,1.0);
149  double sigma_pc_kam = r3->Gaus(0.0,1.0);
150  double sigma_pc_p = r3->Gaus(0.0,1.0);
151  double sigma_pc_n = r3->Gaus(0.0,1.0);
152 
153  const boost::interprocess::flat_map<std::string, double>& table_uncorr_pars = uncorrelated_pars.getMap();
155  for(;it!=table_uncorr_pars.end();++it){
156  double sigma = r3->Gaus(0.0,1.0);
157  //redefining sigma for 100% correlation:
158  if((it->first).find("ThinTarget_pC_pip_sys")<10)sigma = sigma_pc_pip;
159  if((it->first).find("ThinTarget_pC_pim_sys")<10)sigma = sigma_pc_pim;
160  if((it->first).find("ThinTargetLowxF_pC_kap_sys")<10)sigma = sigma_pc_kap;
161  if((it->first).find("ThinTargetLowxF_pC_kam_sys")<10)sigma = sigma_pc_kam;
162  if((it->first).find("ThinTarget_pC_p_sys")<10) sigma = sigma_pc_p;
163  if((it->first).find("ThinTarget_pC_n_sys")<10) sigma = sigma_pc_n;
164  double new_val = it->second + cvfactor*sigma*uncorrelated_errors[it->first];
165  Parameter p(it->first,new_val);
166  ptable.setParameter(p);
167  }
168 
169  TDecompChol *decomp;
170 
171  for(size_t ii=0;ii<covariance_matrices.size();++ii){
172 
173  decomp=new TDecompChol(covariance_matrices[ii],0.0);
174 
175  bool isPosDef=decomp->Decompose();
176  TMatrixD MxU = decomp->GetU();
177  delete decomp;
178  TMatrixD MxV(MxU);
179  MxV.Transpose(MxU);
180  int nmat = MxV.GetNcols();
181  TVectorD vsigma(nmat);
182  for(int jj=0;jj<nmat;jj++){
183  vsigma[jj]=cvfactor*(r3->Gaus(0.0,1.0));
184  }
185  TVectorD vecDShift = MxV*vsigma;
186 
187  const boost::interprocess::flat_map<std::string, double>& tb = (correlated_par_tables[ii]).getMap();
189 
190  for(;it_tb != tb.end();++it_tb){
191  std::string tmp_name = it_tb->first;
192  std::string snID = tmp_name.substr((it_tb->first).rfind("_")+1,(it_tb->first).length());
193  std::stringstream ssID(snID);
194  int nID;
195  ssID >> nID;
196  double new_val = it_tb->second + vecDShift[nID];
197  Parameter p(it_tb->first,new_val);
198  if(isPosDef)ptable.setParameter(p);
199  }
200 
201  }
202 
203  return ptable;
204 
205  }
206 
208 
209  ParameterTable ptableCV;
210 
211  const boost::interprocess::flat_map<std::string, double>& table_pars=uncorrelated_pars.getMap();
213  for(;it!=table_pars.end();it++){
214  Parameter p(it->first,it->second);
215  ptableCV.setParameter(p);
216  }
217 
218  //Correlated:
219  for(size_t ii=0;ii<covariance_matrices.size();ii++){
220  const boost::interprocess::flat_map<std::string, double>& corr_table_pars = correlated_par_tables[ii].getMap();
221  it = corr_table_pars.begin();
222  for(;it != corr_table_pars.end();it++){
223  Parameter p(it->first,it->second);
224  ptableCV.setParameter(p);
225  }
226 
227  }
228 
229  return ptableCV;
230 
231  }
232 
235  return instance;
236  }
237 
238 }
239 
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
A list/table of parameter names and values.
const boost::interprocess::flat_map< std::string, double > & getMap() const
std::string string
Definition: nybbler.cc:12
intermediate_table::const_iterator const_iterator
std::pair< std::string, double > Parameter
string filename
Definition: train.py:213
ParameterTable calculateParsForUniverse(int universe)
Calculate a table of randomly varied parameters for a particular universe i. The universe number is u...
p
Definition: test.py:223
void addUncorrelated(Parameter &cv_par, double uncertainty)
Add a parameter with its central value and its uncertainty. The parameter is specified as uncorrelate...
void err(const char *fmt,...)
Definition: message.cpp:226
boost::interprocess::flat_map< std::string, double > uncorrelated_errors
A class to manage parameter central values and their uncertanities.
ParameterTable getCVPars()
Get the central value parameters.
void addCorrelated(ParameterTable &cv_pars, TMatrixD &cov_mx)
Add a set of parameters with correlated uncertainties. The central values of the parameters must be p...
void setParameter(Parameter p)
add a parameter to the table or, if already there, reset its value
void readFromXML(const char *filename)
Read a xml file name to parse the parameters.
void setBaseSeed(int val)
Set a beggining/base seed to be used in generating random parameter shifts for the many universe meth...