Parameter.cxx
Go to the documentation of this file.
1 //
2 // Created by Brian Rebel on 8/17/20.
3 //
4 
8 
9 #include "cetlib_except/exception.h"
10 
11 namespace cmf{
12 
13  //----------------------------------------------------------------------------
14  size_t ParameterSpaceLoc::count(std::string const& parName) const
15  {
16  size_t cnt = 0;
17  for(auto const& itr : fPars) if(itr.Name() == parName) ++cnt;
18 
19  if(cnt > 1)
20  throw cet::exception("ParameterSpaceLoc")
21  << "Parameter "
22  << parName
23  << " shows up multiple times for detector "
25 
26  return cnt;
27  }
28 
29  //----------------------------------------------------------------------------
31  {
32  for(auto const& itr : fPars){
33  if(itr.Name() == parName){
34  return itr;
35  }
36  }
37 
38  // if we got here, we didn't find the parameter
39  throw cet::exception("ParameterSpaceLoc")
40  << "no parameter corresponding to "
41  << parName
42  << " found for detector "
44 
45  // make the compiler happy, although we will never get here
46  return fPars.front();
47  }
48 
49  //----------------------------------------------------------------------------
51  double const& val)
52  {
53  bool foundPar = false;
54  for(auto & itr : fPars){
55  if(itr.Name() == parName){
56  itr.SetValue(val);
57  foundPar = true;
58  }
59  }
60 
61  if(!foundPar)
62  throw cet::exception("ParameterSpaceLoc")
63  << "no parameter corresponding to "
64  << parName
65  << " found for detector "
67  << " to set to value "
68  << val;
69 
70  return;
71  }
72 
73  //----------------------------------------------------------------------------
75  {
76  // first check that we are looking at the correct detector and
77  // if the parameter is already in there
78  if(par.Detector() != fDetector) return;
79 
80  for(auto const& itr : fPars){
81  if(itr.Name() == par.Name()) return;
82  }
83 
84  fPars.emplace_back(par);
85  }
86 
87  //------------------------------------------------------------
88  std::ostream& operator<<(std::ostream& o, cmf::OscPar const& op) {
90  << " "
91  << op.Value();
92 
93  return o;
94  }
95 
96  //----------------------------------------------------------------------------
98  : fDetector (cmf::kFARDET )
99  , fVal (cmf::kGarbageDouble)
100  , fCentralVal (cmf::kGarbageFloat )
101  , fSigmaSize (1.e-5 )
102  , fVariance (1.e-5 )
103  , fIsFixed (false )
104  , fIsNuisance (false )
105  , fIsConstrained(false )
106  , fLowerBound (cmf::kGarbageFloat )
107  , fUpperBound (cmf::kGarbageFloat )
108  , fParType (cmf::kUnknownPar )
109  {
110  }
111 
112  //----------------------------------------------------------------------------
114  cmf::DetType_t const& det,
115  double const& val,
116  bool const& fixed,
117  float const& sigmaSize,
118  float const& cv,
119  float const& variance,
120  bool const& nuisance,
121  bool const& constrained,
122  float const& lowerBound,
123  float const& upperBound)
124  : fDetector (det )
125  , fVal (val )
126  , fCentralVal (cv )
127  , fSigmaSize (sigmaSize )
128  , fVariance (variance )
129  , fIsFixed (fixed )
130  , fIsNuisance (nuisance )
131  , fIsConstrained(constrained )
132  , fLowerBound (lowerBound )
133  , fUpperBound (upperBound )
135  {
136  if(fVariance < 1.e-5) fVariance = 1.e-5;
137 
138  if(cmf::IsOscillationParameter(name)){
141  }
142  else{
143  fKey = cmf::VarNameToKey(name);
150  else if (fKey >= kFirstEShiftVar && fKey <= cmf::kLastEShiftVar)
154  else
156  }
157 
158 
159  // sanity check warning - if the parameter is fixed and labeled as nuisance,
160  // tell the user that the fixed part wins
161  if(fIsFixed && fIsNuisance){
162  MF_LOG_WARNING("Parameter")
163  << name
164  << " is set to be fixed but is also set to be a nuisance parameter, "
165  << "assume that leaving it fixed is correct and set IsNuisance to "
166  << " false";
167 
168  fIsNuisance = false;
169  }
170 
171  }
172 
173  //----------------------------------------------------------------------------
175  return fKey < other.Key();
176  }
177 
178  //----------------------------------------------------------------------------
180  return fKey == other.Key();
181  }
182 
183  //------------------------------------------------------------
184  std::ostream& operator<<(std::ostream& o, cmf::Parameter const& par) {
185 
186  o << "-- parameter: "
187  << std::setw(30) << par.Name() << std::setw(10)
188  << " val: " << par.Value()
189  << " fixed: " << par.IsFixed()
190  << " oscillation " << par.IsOscPar()
191  << " nuisance " << par.IsNuisance()
192  << " constrained " << par.IsConstrained()
193  << " lower: " << par.LowerBound()
194  << " upper: " << par.UpperBound()
195  << std::endl;
196 
197  return o;
198  }
199 
200  //------------------------------------------------------------
201  std::ostream& operator<<(std::ostream& o, cmf::Location const& loc)
202  {
203  auto const& detLocs = loc.DetectorLocations();
204 
205  std::set<std::string> parsDone;
206 
207  o << std::endl;
208  for(auto const& itr : detLocs){
209  for(auto const& parItr : itr.Parameters()){
210 
211  if(parsDone.find(parItr.Name()) != parsDone.end()) continue;
212 
213  parsDone.insert(parItr.Name());
214 
215  o << " -- parameter: "
216  << std::setw(30)
217  << parItr.Name()
218  << std::setw(15)
219  << " val "
220  << cmf::cDetType_Strings[itr.Detector()]
221  << ": "
222  << parItr.Value()
223  << " fixed: "
224  << std::setw(5)
225  << parItr.IsFixed();
226 
227  // see if this parameter is set for other detectors too
228  for(auto const& itr2 : detLocs){
229  if(itr2.Detector() == itr.Detector()) continue;
230  if(itr2.count(parItr.Name()) > 0){
231  auto const& par2 = itr2.Parameter(parItr.Name());
232  o << std::setw(15)
233  << " val "
234  << cmf::cDetType_Strings[itr2.Detector()]
235  << ": "
236  << std::setw(15)
237  << " "
238  << par2.Value()
239  << " fixed: "
240  << std::setw(5)
241  << par2.IsFixed();
242  }
243  } // end loop over other detectors
244  o << std::endl;
245  } // end loop over parameters
246  o << std::endl;
247  } // end loop over the current detector
248 
249  return o;
250  }
251 
252  //----------------------------------------------------------------------------
253  Location::Location(std::set<cmf::DetType_t> const& dets)
254  {
255  for(auto const& det : dets) fDetLocs.emplace_back(det, std::vector<cmf::Parameter>());
256  }
257 
258  //----------------------------------------------------------------------------
260  std::set<cmf::DetType_t> const& dets)
261  : Location(dets)
262  {
263  std::map<cmf::DetType_t, double> detBaseline({{cmf::kNEARDET, 1. },
264  {cmf::kFARDET, 810.},
265  {cmf::kMINOSNEAR, 1.02},
266  {cmf::kMINOSFAR, 735.}});
267 
268  float parVal;
269 
270  for(auto const& itr : oscPars){
271  parVal = itr.second;
272  for(auto & locItr : fDetLocs){
273  if(itr.first == cmf::kL) parVal = detBaseline.find(locItr.Detector())->second;
274 
275  locItr.InsertParameter(cmf::Parameter(cmf::cOscParams_Strings[itr.first],
276  locItr.Detector(),
277  parVal,
278  true,
279  0,
280  parVal,
281  0,
282  false,
283  false,
284  0.,
285  1.));
286  } // end loop over locations
287  } // end loop over oscillation parameters
288  }
289 
290  //----------------------------------------------------------------------------
292  {
293  for(auto & itr : fDetLocs){
294  if(itr.Detector() == par.Detector()){
295  if(itr.count(par.Name()) < 1) itr.InsertParameter(par);
296  else itr.SetParameterValue(par.Name(), par.Value());
297  }
298  }
299  }
300 
301  //----------------------------------------------------------------------------
303  double const& value,
304  cmf::DetType_t const& det)
305  {
306  for(auto & itr : fDetLocs){
307  if(itr.Detector() == det ||
308  cmf::kALLDET == det ){
309  if(itr.count(parName) > 0)
310  itr.SetParameterValue(parName, value);
311  else
312  MF_LOG_WARNING("Location")
313  << parName
314  << " is not a recognized parameter for the "
316  }
317  } // end loop over the ParameterSpaceLocs
318  }
319 
320  //----------------------------------------------------------------------------
322  {
324 
325  // The oscillation parameters should be the same for all detectors,
326  // except of course the baseline. Make the map with the NOvA far detector
327  // by default
328  for(auto const& itr : this->DetectorLocation(det).Parameters()){
329  if(!itr.IsOscPar()) continue;
330 
331  oscMap.emplace((cmf::OscParm_t)itr.Key(),
332  itr.Value());
333  }
334 
335  return std::move(oscMap);
336  }
337 
338  //----------------------------------------------------------------------------
340  {
341  for(auto const& itr : fDetLocs){
342  if(itr.Detector() == det) return itr;
343  }
344 
345  // if we got here there is a problem because the detector is not in the
346  // collection
347  throw cet::exception("Location")
348  << "no cmf::ParameterSpaceLoc for "
350 
351  // the compiler will complain if we don't pretend to return what we
352  // say we will
353  return fDetLocs.front();
354  }
355 
356  //----------------------------------------------------------------------------
357  std::vector<Parameter> Location::OscillationParameters(cmf::DetType_t const& det) const
358  {
359  std::vector<Parameter> oscPars;
360 
361  MF_LOG_DEBUG("Location")
362  << "Get oscillation parameters from collection of "
363  << fDetLocs.size()
364  << " detector locations";
365 
366  for(auto const& itr : fDetLocs){
367  if(itr.Detector() == det){
368  MF_LOG_DEBUG("Location")
369  << "Oscillation Parameters for "
370  << cmf::cDetType_Strings[itr.Detector()];
371  for(auto const& parItr : itr.Parameters()){
372  if(parItr.IsOscPar()){
373  oscPars.emplace_back(parItr);
374  MF_LOG_DEBUG("Location")
375  << parItr;
376  }
377  }
378  }
379  }
380 
381  return std::move(oscPars);
382  }
383 
384  //----------------------------------------------------------------------------
385  std::vector<Parameter> Location::SystematicParameters(cmf::DetType_t const& det) const
386  {
387  std::vector<Parameter> systPars;
388 
389  for(auto const& itr : fDetLocs){
390  if(itr.Detector() == det){
391  for(auto const& parItr : itr.Parameters()){
392  if(!parItr.IsOscPar()) systPars.emplace_back(parItr);
393  }
394  }
395  }
396 
397  return std::move(systPars);
398  }
399 
400 
401 }// namespace
const XML_Char * name
Definition: expat.h:151
float const & LowerBound() const
Definition: Parameter.h:144
enum cmf::osc_params OscParm_t
std::vector< Parameter > SystematicParameters(cmf::DetType_t const &det=cmf::kFARDET) const
Definition: Parameter.cxx:385
bool const & IsConstrained() const
Definition: Parameter.h:142
float fSigmaSize
size of 1 sigma
Definition: Parameter.h:159
const std::vector< std::string > cDetType_Strings({"UnknownDet","NearDet","FarDet","MINOSNear","MINOSFar","AllDetectors"})
static const double kGarbageDouble
Definition: Constants.h:18
float fCentralVal
central value for the parameter
Definition: Parameter.h:158
Int_t par
Definition: SimpleIterate.C:24
bool const & IsFixed() const
Definition: Parameter.h:140
bool fIsConstrained
is this parameter constrained in allowed values?
Definition: Parameter.h:163
enum cmf::det_type DetType_t
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
float fUpperBound
upper bound for the constrained parameter
Definition: Parameter.h:165
bool IsOscPar() const
Definition: Parameter.h:141
cmf::OscillationParameterMap AsOscillationParameterMap(cmf::DetType_t const &det=cmf::kFARDET) const
Definition: Parameter.cxx:321
std::ostream & operator<<(std::ostream &o, cmf::Event const &event)
Definition: Event.cxx:27
cmf::Parameter const & Parameter(std::string const &parName) const
Definition: Parameter.cxx:30
size_t count(std::string const &parName) const
Definition: Parameter.cxx:14
static uint8_t VarNameToKey(std::string const &name)
Definition: StaticFuncs.h:395
std::map< cmf::OscParm_t, float > OscillationParameterMap
Definition: Constants.h:748
const XML_Char int const XML_Char * value
Definition: expat.h:331
void SetParameterValue(std::string const &parName, double const &val)
Definition: Parameter.cxx:50
float fLowerBound
lower bound for the constrained parameter
Definition: Parameter.h:164
static const float kGarbageFloat
Definition: Constants.h:19
const std::string cv[Ncv]
bool const & IsNuisance() const
Definition: Parameter.h:143
friend std::ostream & operator<<(std::ostream &o, cmf::Parameter const &par)
Definition: Parameter.cxx:184
uint8_t fDetector
which detector is this parameter for?
Definition: Parameter.h:156
cmf::DetType_t const Detector() const
Definition: Parameter.h:136
void InsertParameter(cmf::Parameter const &par)
Definition: Parameter.cxx:291
float const & Value() const
Definition: Parameter.h:72
uint8_t const & Key() const
Definition: Parameter.h:135
bool fIsNuisance
is this parameter treated as a nuisance parameter?
Definition: Parameter.h:162
bool operator<(cmf::Parameter const &other) const
Definition: Parameter.cxx:174
double fVal
current value for the parameter in the fit
Definition: Parameter.h:157
static bool IsOscillationParameter(std::string const &str)
float fVariance
amount parameter can vary when making random universes
Definition: Parameter.h:160
Module to combine a set of results into a single file currently only does one data product type at a ...
Definition: Event.cxx:24
cmf::OscParm_t const & Param() const
Definition: Parameter.h:71
std::vector< ParameterSpaceLoc > const & DetectorLocations() const
Definition: Parameter.h:193
bool fIsFixed
is this parameter fixed during fits?
Definition: Parameter.h:161
float const & UpperBound() const
Definition: Parameter.h:145
std::vector< cmf::Parameter > fPars
Definition: Parameter.h:49
bool operator==(cmf::Parameter const &other) const
Definition: Parameter.cxx:179
uint8_t fKey
key for this parameter
Definition: Parameter.h:155
#define MF_LOG_DEBUG(id)
cmf::ParameterSpaceLoc const & DetectorLocation(cmf::DetType_t const &det) const
Definition: Parameter.cxx:339
std::vector< Parameter > OscillationParameters(cmf::DetType_t const &det=cmf::kFARDET) const
Definition: Parameter.cxx:357
double const & Value() const
Definition: Parameter.h:134
std::string const Name() const
Definition: Parameter.h:132
#define MF_LOG_WARNING(category)
Float_t e
Definition: plot.C:35
static cmf::OscParm_t StringToOscParmType(std::string const &str)
std::vector< ParameterSpaceLoc > fDetLocs
Definition: Parameter.h:203
void SetParameterValue(std::string const &parName, double const &value, cmf::DetType_t const &det)
Definition: Parameter.cxx:302
uint8_t fParType
type of paramter this is
Definition: Parameter.h:166
void InsertParameter(cmf::Parameter const &par)
Definition: Parameter.cxx:74
const std::vector< std::string > cOscParams_Strings({"L","Rho","Dmsq21","Dmsq32","Th12","Th13","Th23","dCP","Th14","Th24","Th34","Dmsq41","d24","Eps_ee","Eps_emu","Eps_etau","Eps_mumu","Eps_mutau","Eps_tautau","Delta_emu","Delta_etau","Delta_mutau"})
enum BeamMode string