LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
ShowerCalibrationGaloreFromPID.h
Go to the documentation of this file.
1 
13 #ifndef LAREXAMPLES_SERVICES_SHOWERCALIBRATIONGALORE_PROVIDERS_SHOWERCALIBRATIONGALOREFROMPID_H
14 #define LAREXAMPLES_SERVICES_SHOWERCALIBRATIONGALORE_PROVIDERS_SHOWERCALIBRATIONGALOREFROMPID_H
15 
16 
17 // LArSoft libraries
19 
21 #include "fhiclcpp/types/Atom.h"
22 #include "fhiclcpp/types/Table.h"
23 #include "cetlib_except/exception.h"
24 
25 // ROOT libraries
26 #include "Math/Interpolator.h"
27 #include "TDirectory.h"
28 #include "TClass.h"
29 #include "TH1.h"
30 #include "TObject.h"
31 
32 // C/C++ standard libraries
33 #include <string>
34 #include <vector>
35 #include <memory> // std::unique_ptr()
36 #include <type_traits> // std::is_base_of<>
37 #include <initializer_list>
38 
39 
40 // forward declarations
41 class TGraph; // from ROOT
42 
43 
44 namespace lar {
45  namespace example {
46 
47  // BEGIN ShowerCalibrationGalore -----------------------------------------
50 
51  namespace details {
52 
55  template <typename ROOTobj>
56  std::unique_ptr<ROOTobj> readROOTobject
57  (TDirectory* sourceDir, std::string name);
58 
59  } // namespace details
60 
61 
63  std::pair<std::string, std::string> splitROOTpath(std::string path);
64 
65 
122  public:
123 
124  //---------------------------------------------------------------------
126  struct Config {
127  using Name = fhicl::Name;
129 
130  fhicl::Atom<std::string> CalibrationFile {
131  Name("CalibrationFile"),
132  Comment(
133  "path to calibration file and ROOT directory"
134  " (e.g. path/to/file.root:Dir/Dir)"
135  )
136  };
137 
138  }; // struct Config
139 
142 
143 
144  //---------------------------------------------------------------------
147  { readCalibration(config.CalibrationFile()); }
148 
149  //---------------------------------------------------------------------
153  (parameters_type(pset, { "service_type", "service_provider" })())
154  {}
155 
156 
159 
160  //---------------------------------------------------------------------
170  virtual float correctionFactor
171  (recob::Shower const&, PDGID_t = unknownID) const override;
172 
182  virtual Correction_t correction
183  (recob::Shower const&, PDGID_t = unknownID) const override;
184 
186 
188  virtual std::string report() const override
189  { std::ostringstream sstr; reportTo(sstr); return sstr.str(); }
190 
191 
193  void readCalibration(std::string path);
194 
195 
197  template <typename Stream>
198  void reportTo(Stream&& out) const;
199 
200 
203  static void verifyOrder(TGraph const* graph);
204 
205 
206  //---------------------------------------------------------------------
207  private:
210  std::vector<PDGID_t> appliesTo;
211  double minE = -1.;
212  double maxE = -1.;
213 
215  std::unique_ptr<ROOT::Math::Interpolator> factor;
216 
218  std::unique_ptr<ROOT::Math::Interpolator> error;
219 
220  double evalFactor(double E) const;
221  double evalError(double E) const;
222 
223  bool present() const { return maxE >= 0.; }
224  bool uniform() const { return minE == maxE; }
225 
226  CalibrationInfo_t& applyTo(PDGID_t id);
227  CalibrationInfo_t& applyTo(std::initializer_list<PDGID_t> ids);
228 
230  template <typename Stream>
231  void reportTo(Stream&& out) const;
232  }; // CalibrationInfo_t
233 
234 
240 
241 
243  CalibrationInfo_t const& selectCorrection(PDGID_t id) const;
244 
246  CalibrationInfo_t readParticleCalibration
247  (TDirectory* SourceDir, std::string GraphName) const;
248 
251  CalibrationInfo_t readParticleCalibration
252  (TDirectory* SourceDir, std::string GraphName, PDGID_t id) const;
253 
256  CalibrationInfo_t readParticleCalibration(
257  TDirectory* SourceDir, std::string GraphName,
258  std::initializer_list<PDGID_t> ids
259  ) const;
260 
262  static TDirectory* OpenROOTdirectory(std::string path);
263 
265  static std::unique_ptr<ROOT::Math::Interpolator>
266  createInterpolator(unsigned int N, double const* x, double const* y);
267 
268  }; // class ShowerCalibrationGaloreFromPID
269 
271  // END ShowerCalibrationGalore -----------------------------------------------
272 
273 
274  } // namespace example
275 } // namespace lar
276 
277 
278 
279 //------------------------------------------------------------------------------
280 //--- template implementation
281 //---
282 template <typename ROOTobj>
283 std::unique_ptr<ROOTobj> lar::example::details::readROOTobject
284  (TDirectory* sourceDir, std::string name)
285 {
286  if (!sourceDir) {
287  throw cet::exception("readROOTobject")
288  << "Invalid source ROOT directory\n";
289  }
290 
291  // read the object and immediately claim its ownership
292  TObject* pObj = sourceDir->Get(name.c_str());
293  if (!pObj) {
294  throw cet::exception("readROOTobject")
295  << "No object '" << name << "' in ROOT directory '"
296  << sourceDir->GetPath() << "'\n";
297  }
298 
299  if (std::is_base_of<TH1, std::decay_t<ROOTobj>>::value)
300  static_cast<TH1*>(pObj)->SetDirectory(nullptr);
301 // if (std::is_base_of<TTree, std::decay_t<ROOTobj>>::value)
302 // static_cast<TTree*>(pObj)->SetDirectory(nullptr);
303  std::unique_ptr<TObject> obj(pObj);
304 
305  // use ROOT to investigate whether this is the right type
306  if (!obj->InheritsFrom(ROOTobj::Class())) {
307  throw cet::exception("readROOTobject")
308  << "Object '" << name << "' in ROOT directory '"
309  << sourceDir->GetPath() << "' is a " << obj->IsA()->GetName()
310  << ", not derived from " << ROOTobj::Class()->GetName() << "\n";
311  }
312 
313  // now that we are sure obj is of the right type, we transfer ownership
314  // from obj to the return value (that here is implicitly constructed)
315  return std::unique_ptr<ROOTobj>(static_cast<ROOTobj*>(obj.release()));
316 } // lar::example::details::readROOTobject()
317 
318 //------------------------------------------------------------------------------
319 //--- lar::example::ShowerCalibrationGaloreFromPID
320 //---
321 template <typename Stream>
323 {
324  out << "Corrections for:";
325  out << "\n - neutral pion: ";
326  Calibration_pi0.reportTo(std::forward<Stream>(out));
327  out << "\n - photon: ";
328  Calibration_photon.reportTo(std::forward<Stream>(out));
329  out << "\n - electron/positron: ";
330  Calibration_electron.reportTo(std::forward<Stream>(out));
331  out << "\n - muon/antimuon: ";
332  Calibration_muon.reportTo(std::forward<Stream>(out));
333  out << "\n - other (default): ";
334  Calibration_other.reportTo(std::forward<Stream>(out));
335  out << "\n";
336 } // lar::example::ShowerCalibrationGaloreFromPID::report()
337 
338 
339 //------------------------------------------------------------------------------
340 template <typename Stream>
342  (Stream&& out) const
343 {
344  if (!present()) {
345  out << "not present";
346  return;
347  }
348  if (uniform()) {
349  out << "uniform correction " << factor->Eval(minE) << " +/- "
350  << error->Eval(minE) << " for all energies";
351  }
352  else {
353  out << "correction valid from E=" << minE
354  << " GeV (" << factor->Eval(minE) << " +/- " << error->Eval(minE)
355  << ") to E=" << maxE
356  << " GeV (" << factor->Eval(maxE) << " +/- " << error->Eval(maxE)
357  << ")";
358  }
359  if (!appliesTo.empty()) {
360  out << "; covers particles ID={";
361  for (auto id: appliesTo) out << " " << id;
362  out << " }";
363  }
364 
365 } // lar::example::ShowerCalibrationGaloreFromPID::CalibrationInfo_t::report()
366 
367 
368 //------------------------------------------------------------------------------
369 
370 
371 #endif // LAREXAMPLES_SERVICES_SHOWERCALIBRATIONGALORE_PROVIDERS_SHOWERCALIBRATIONGALOREFROMPID_H
372 
CalibrationInfo_t Calibration_pi0
neutral pion calibration
Float_t x
Definition: compare.C:6
CalibrationInfo_t Calibration_electron
electron/positron calibration
Internal structure containing the calibration information.
Float_t E
Definition: plot.C:23
std::unique_ptr< ROOT::Math::Interpolator > error
parametrisation of the correction uncertainty
Float_t y
Definition: compare.C:6
Collection of configuration parameters for the service.
A correction factor with global uncertainty.
Shower calibration service provider correcting according to PID.
CalibrationInfo_t Calibration_photon
photon calibration
ShowerCalibrationGaloreFromPID(fhicl::ParameterSet const &pset)
Constructor from a parameter set.
CalibrationInfo_t Calibration_other
default calibration
CalibrationInfo_t Calibration_muon
muon/antimuon calibration
std::unique_ptr< ROOT::Math::Interpolator > factor
parametrisation of the correction factor
std::string value(boost::any const &)
int PDGID_t
A type representing a particle ID in Particle Data Group convention.
Interface for a shower calibration service provider.
LArSoft-specific namespace.
virtual std::string report() const override
Returns a string with a short report of the current corrections.
double minE
Definition: plot_hist.C:7
std::unique_ptr< ROOTobj > readROOTobject(TDirectory *sourceDir, std::string name)
double maxE
Definition: plot_hist.C:8
std::pair< std::string, std::string > splitROOTpath(std::string path)
Splits path into ROOT file name and directory path.
void reportTo(Stream &&out) const
Prints a short report of this correction.
ShowerCalibrationGaloreFromPID(Config const &config)
Constructor from the complete configuration object.
ProductStatus present()
Definition: ProductStatus.h:16
void reportTo(Stream &&out) const
Prints a short report of the current corrections.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Interface for a shower calibration service provider.