ParameterUtility.cxx
Go to the documentation of this file.
1 //
2 // Created by Brian Rebel on 8/17/20.
3 //
4 // NB: This code assumes the SelectionUtility is available to it
5 
8 
9 namespace cmf{
10 
11  //-----------------------------------------------------------------------------
13  cmf::DetType_t const& det,
15  {
16  // nothing to be done with an empty parameter set
17  if(pset.is_empty()) return;
18 
20  bool fixed;
21  bool nuisance;
22  bool constraint;
23  float centralVal;
24  float variance;
25  float sigma;
26  double val;
27  float lowerBound;
28  float upperBound;
29  std::string parstr;
30  std::string detstr;
31 
32  auto const& parNames = pset.get_names();
33 
34  for(auto const& itr : parNames){
35 
36  param = pset.get<fhicl::ParameterSet>(itr);
37 
38  MF_LOG_DEBUG("ParameterSetToLocation")
39  << param.to_indented_string();
40 
41  detstr = param.get<std::string>("Detector", "BOTH");
42  parstr = param.get<std::string>("Parameter" );
43  fixed = param.get<bool >("Fixed", true );
44  val = param.get<double >("Value", 0. );
45  nuisance = param.get<bool >("Nuisance", false );
46  sigma = param.get<float >("Sigma", 1.e6 );
47  centralVal = param.get<float >("CentralValue", 0. );
48  variance = param.get<float >("Variance", 1.e-5 );
49  constraint = param.get<bool >("Constraint", false );
50  lowerBound = param.get<float >("LoVal", 0. );
51  upperBound = param.get<float >("HiVal", 1.e6 );
52 
53  // remove any Wgt from the parameter name
54  if(parstr.find("Wgt") != std::string::npos) parstr.erase(parstr.find("Wgt"), 3);
55 
56  if(detstr == "BOTH" || cmf::StringToDetectorType(detstr) == det){
58  det,
59  val,
60  fixed,
61  sigma,
62  centralVal,
63  variance,
64  nuisance,
65  constraint,
66  lowerBound,
67  upperBound));
68 
69  MF_LOG_DEBUG("ParameterSpaceLoc")
70  << "inserting parameter "
71  << parstr;
72  } // end if using the correct detector
73  } // end loop over parameter names
74 
75  MF_LOG_DEBUG("ParameterSpaceLoc")
76  << " full location is "
77  << loc;
78 
79  }
80 
81  static ParameterUtility *gParUtil = nullptr;
82 
83  //-----------------------------------------------------------------------------
85  if(gParUtil == nullptr)
86  gParUtil = new ParameterUtility();
87 
88  return gParUtil;
89  }
90 
91  //-----------------------------------------------------------------------------
93  {
94  }
95 
96  //-----------------------------------------------------------------------------
98  {
99  // have default empty vectors for these which is what we expect if
100  // making the event lists
101  std::vector<std::string> psetNames({"OscillationParameters",
102  "SystematicParameters"});
103 
105  for(auto const& detItr : dets){
106  for(auto const& nameItr : psetNames){
107  cmf::Location loc(dets);
108  cmf::ParameterSetToLocation(pset.get<fhicl::ParameterSet>(nameItr), detItr, loc);
109 
110  if(nameItr.find("Oscillation") != std::string::npos)
111  fDetOscParameters.emplace_back(detItr, loc.DetectorLocation(detItr).Parameters());
112  else if(nameItr.find("Systematic") != std::string::npos)
113  fDetSysParameters.emplace_back(detItr, loc.DetectorLocation(detItr).Parameters());
114 
115  } // end loop over parameter set names
116  } // end loop over detectors
117 
118  MF_LOG_VERBATIM("cmf::CovarianceMatrixMaker")
119  << "\n-------------------------------------------------------"
120  << "\nPARAMETER SUMMARY"
121  << "\n-------------------------------------------------------";
122 
123  for(auto const& itr : fDetSysParameters){
124  MF_LOG_VERBATIM("ParameterUtility")
125  << "\n=======================================================\n"
126  << cmf::cDetType_Strings[itr.Detector()]
127  << "\n======================================================="
128  << "\n\nRunning with systematic parameters set to \n";
129 
130  for(auto const& parItr : itr.Parameters()){
131  MF_LOG_VERBATIM("ParameterUtility")
132  << " -- parameter: "
133  << std::setw(30)
134  << parItr.Name()
135  << " val: "
136  << std::setw(15)
137  << parItr.Value()
138  << " fixed: "
139  << std::setw(3)
140  << parItr.IsFixed();
141  } // end loop over parameters for detector
142 
143  MF_LOG_VERBATIM("ParameterUtility")
144  << "\nRunning with oscillation parameters set to \n";
145  for(auto const& odItr : fDetOscParameters){
146  if(odItr.Detector() != itr.Detector()) continue;
147  for(auto const& oscItr : odItr.Parameters()){
148  MF_LOG_VERBATIM("ParameterUtility")
149  << " -- parameter: "
150  << std::setw(30)
151  << oscItr.Name()
152  << " val: "
153  << std::setw(15)
154  << oscItr.Value()
155  << " fixed: "
156  << std::setw(3)
157  << oscItr.IsFixed();
158  } // end loop over oscillation parameters
159  } // end loop over detectors for oscillation parameters
160  } // end loop over systematic parameter detectors
161  }
162 
163  //-----------------------------------------------------------------------------
165  cmf::DetType_t const& det) const
166  {
167  // TODO this method returns the first parameter from any detector matching the name
168  // we should probably match the detector too, but as of March 2021, nothing calls it
169  for(auto const& dopItr : fDetOscParameters){
170  if(dopItr.Detector() != det) continue;
171 
172  if(dopItr.count(parName) < 1)
173  throw cet::exception("ParameterUtility")
174  << "oscillation parameter "
175  << parName
176  << " is not in the list for the "
178  << " detector";
179 
180  return dopItr.Parameter(parName);
181  } // end loop over detectors
182 
183  // we should never get here, but make the compiler happy
184  return fDetOscParameters.front().Parameter(parName);
185  }
186 
187  //-----------------------------------------------------------------------------
189  {
190  // TODO this method returns the first parameter from any detector matching the name
191  // we should probably match the detector too, but as of March 2021, nothing calls it
192  for(auto const& dspItr : fDetSysParameters){
193  if(dspItr.count(parName) < 1)
194  throw cet::exception("ParameterUtility")
195  << "systematic parameter "
196  << parName
197  << " is not in the list";
198 
199  return dspItr.Parameter(parName);
200  } // end loop over detectors
201 
202  // we should never get here, but make the compiler happy
203  return fDetSysParameters.front().Parameter(parName);
204  }
205 
206  //-----------------------------------------------------------------------------
208  {
209  // the oscillation parameters are the same in all detectors, so the
210  // default of the NOvA FD is fine
211  if(cmf::IsOscillationParameter(parName))
212  return this->OscParameterInfo(parName);
213 
214  // if we don't have an oscillation parameter it must be a systematic parameter
215  return this->SysParameterInfo(parName);
216  }
217 
218  //-----------------------------------------------------------------------------
220  {
221  // the systematic parameters could be different, so check all the lists
222  for(auto const& dspItr : fDetSysParameters){
223  if(dspItr.count(parName) > 0 ) return true;
224  }
225 
226  return false;
227  }
228 
229  //-----------------------------------------------------------------------------
231  {
232 
233  // loop over the detectors we are using and combine the systematic
234  // and oscillation parameters
235  std::vector<cmf::ParameterSpaceLoc> pslVec;
236 
237  for(auto const& itr : fDetOscParameters){
238  std::vector<cmf::Parameter> detPars;
239 
240  for(auto const& oscParItr : itr.Parameters()){
241  detPars.emplace_back(oscParItr);
242  }
243 
244  for(auto const& sysDetItr : fDetSysParameters){
245  if(sysDetItr.Detector() == itr.Detector()){
246  for(auto const& sysParItr : sysDetItr.Parameters()){
247  detPars.emplace_back(sysParItr);
248  }
249  }
250  } // end loop to get systematic parameters for the detector
251 
252  pslVec.emplace_back(itr.Detector(), detPars);
253  }
254 
255  cmf::Location loc(pslVec);
256 
257  return std::move(loc);
258  }
259 
260  //-----------------------------------------------------------------------------
261  std::set<std::string> ParameterUtility::SysParNames() const
262  {
263  std::set<std::string> parNames;
264  for(auto const& itr : fDetSysParameters){
265  for(auto const& parItr : itr.Parameters())
266  parNames.emplace(parItr.Name());
267  }
268 
269  return std::move(parNames);
270  }
271 
272  //-----------------------------------------------------------------------------
273  std::set<uint8_t> ParameterUtility::SysParKeys() const
274  {
275  std::set<uint8_t> parKeys;
276  for(auto const& itr : fDetSysParameters){
277  for(auto const& parItr : itr.Parameters())
278  parKeys.emplace(parItr.Key());
279  }
280 
281  return std::move(parKeys);
282  }
283 
284  //-----------------------------------------------------------------------------
285  std::vector<cmf::Parameter> const& ParameterUtility::SysParameters(cmf::DetType_t const& det) const
286  {
287  for(auto const& itr : fDetSysParameters){
288  if(itr.Detector() == det) return itr.Parameters();
289  }
290 
291  // if there are no systematic parameters for this detector return an
292  // empty vector.
293  std::vector<cmf::Parameter> emptyParVec;
294  return std::move(emptyParVec);
295  }
296 
297 } // end namespace
std::vector< cmf::ParameterSpaceLoc > fDetOscParameters
values for the oscillation parameters in each detector
std::vector< cmf::Parameter > const & Parameters() const
Definition: Parameter.h:43
void Initialize(fhicl::ParameterSet const &pset)
bool is_empty() const
std::set< std::string > SysParNames() const
const std::vector< std::string > cDetType_Strings({"UnknownDet","NearDet","FarDet","MINOSNear","MINOSFar","AllDetectors"})
cmf::Parameter const & OscParameterInfo(std::string const &parName, cmf::DetType_t const &det=cmf::kFARDET) const
cmf::Parameter const & ParameterInfo(std::string const &parName) const
static SelectionUtility * Instance()
std::set< uint8_t > SysParKeys() const
enum cmf::det_type DetType_t
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
std::vector< cmf::ParameterSpaceLoc > fDetSysParameters
values for the systematic parameters in each detector
std::vector< cmf::Parameter > const & SysParameters(cmf::DetType_t const &det=cmf::kFARDET) const
static ParameterUtility * Instance()
bool UseSysPar(std::string const &parName)
T get(std::string const &key) const
Definition: ParameterSet.h:231
static cmf::DetType_t StringToDetectorType(std::string const &str)
Definition: StaticFuncs.h:267
void InsertParameter(cmf::Parameter const &par)
Definition: Parameter.cxx:291
static ParameterUtility * gParUtil
std::vector< std::string > get_names() const
double sigma(TH1F *hist, double percentile)
static bool IsOscillationParameter(std::string const &str)
Module to combine a set of results into a single file currently only does one data product type at a ...
Definition: Event.cxx:24
void ParameterSetToLocation(fhicl::ParameterSet const &pset, cmf::DetType_t const &det, cmf::Location &loc)
std::set< cmf::DetType_t > const & DetectorsToUse()
std::string to_indented_string() const
#define MF_LOG_VERBATIM(category)
#define MF_LOG_DEBUG(id)
cmf::Location ParametersAsLocation()
cmf::ParameterSpaceLoc const & DetectorLocation(cmf::DetType_t const &det) const
Definition: Parameter.cxx:339
cmf::Parameter const & SysParameterInfo(std::string const &parName) const
enum BeamMode string