ShowerCalibrationGaloreTests.h
Go to the documentation of this file.
1 /**
2  * @file ShowerCalibrationGaloreTests.h
3  * @brief Test functions for ShowerCalibrationGalore service providers
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date May 4th, 2016
6  *
7  * This is a template only header.
8  * It provides:
9  * - ShowerCalibrationTableTest(): prints correction table for different showers
10  *
11  * It requires linking with:
12  * - lardata_RecoBase
13  */
14 
15 
16 // LArSoft libraries
19 
20 // C/C++ standard libraries
21 #include <string>
22 #include <algorithm> // std::max()
23 #include <tuple> // std::pair<>, std::get()
24 #include <vector>
25 #include <iomanip>
26 
27 
28 namespace lar {
29 namespace example {
30 namespace tests {
31 
32 /// Returns a string padding s to be centered in a width w
34  (std::string const& s, unsigned int width, char pad = ' ')
35 {
36  unsigned int const left
37  = std::max(0U, (width - (unsigned int) s.length()) / 2);
38  unsigned int const right
39  = std::max(0U, width - left - (unsigned int) s.length());
40  return std::string(left, pad) + s + std::string(right, pad);
41 } // centerString()
42 
43 
44 //------------------------------------------------------------------------------
45 
46 
47 inline recob::Shower MakeShower(float E, int bestPlane = 2, int ID = 1) {
48  // a shower on a 3-plane detector
49  return recob::Shower(
50  { 0., 0., 1. }, // direction (cosines) at vertex: along z axis
51  { 1e-3, 1e-3, 1e-3 }, // uncertainty on the above
52  { 0., 0., 0. }, // start vertex (somewhere; we don't use geometry)
53  { 1e-3, 1e-3, 1e-3 }, // uncertainty on the above
54  { 1., 1., 1. }, // consistent measurement of 1 (GeV?) on all planes
55  { 1e-1, 1e-1, 1e-1 }, // uncertainty on the above (10%)
56  { E, E, E }, // consistent measurement of 1 (GeV?) on all planes
57  { 0.1*E, 0.1*E, 0.1*E }, // uncertainty on the above (10%)
58  bestPlane, // elect the last as the best plane
59  ID // ID
60  );
61 } // MakeShower()
62 
63 //------------------------------------------------------------------------------
64 /**
65  * @brief Synthetic test: prints corrections for showers in a energy range
66  * @tparam Stream type of output stream
67  * @param out output stream
68  * @param calibration service provider
69  * @param Emin lower shower energy for the printout [GeV] (default: 0)
70  * @param Emax upper shower energy for the printout [GeV] (default: 2.5)
71  * @param Estep energy step size for the printout [GeV] (default: have 10 steps)
72  * @param pids use these PIDs (default: { 11, 13, 111, 2212, 22 })
73  * @return number of detected errors (currently always 0)
74  *
75  * The corrections are printed in a table like:
76  * ~~~~
77  * E [GeV] particle1 particle2 ...
78  * 0.000 1.000 +/- 0.000 1.023 +/- 0.003 ...
79  * ...
80  * ~~~~
81  */
82 template <typename Stream>
84  Stream&& out,
85  lar::example::ShowerCalibrationGalore const* calibration,
86  float Emin = 0.0, float Emax = 2.5, float Estep = 0.1,
87  std::vector<lar::example::ShowerCalibrationGalore::PDGID_t> const& pids
88  = { 11, 13, 111, 2212, 22 }
89 ) {
90 
91  // a shower with 2 GeV of energy
93 
94  using PIDInfo_t
95  = std::pair<lar::example::ShowerCalibrationGalore::PDGID_t, std::string>;
96  std::vector<PIDInfo_t> const KnownPIDs = {
97  { 11, "e-" },
98  { 13, "mu-" },
99  { -11, "e+" },
100  { -13, "mu+" },
101  { 211, "pi+" },
102  { 111, "pi0" },
103  { 2112, "n" },
104  { 2212, "p" },
105  { 22, "photon" },
106  { 0, "default" }
107  };
108  PIDInfo_t const UnknownPID = { 0, "<unnamed>" };
109 
110  // compute a default Estep to have 10 steps
111  if (Estep == 0.) Estep = std::abs(Emax - Emin) / 10;
112  if (Emax < Emin) Estep = -std::abs(Estep);
113  const unsigned int nSteps
114  = (Emax == Emin)? 1U: (unsigned int)((Emax - Emin)/Estep);
115 
116  constexpr unsigned int widthE = 7;
117  constexpr unsigned int widthF = 5;
118  constexpr unsigned int widthFtoErr = 5;
119  constexpr unsigned int widthFerr = 5;
120  constexpr unsigned int widthCorr = widthF + widthFtoErr + widthFerr;
121  const std::string sep = " ";
122 
123  // table header
124  out << centerString("E [GeV]", widthE);
125  for (auto pid: pids) {
126  auto iKnown = std::find_if(KnownPIDs.begin(), KnownPIDs.end(),
127  [pid](PIDInfo_t const& info){ return pid == std::get<0>(info); }
128  );
129  out << sep;
130  if (iKnown == KnownPIDs.end()) {
131  out << std::setw(widthF) << "PID="
132  << std::left << std::setw(widthFtoErr + widthFerr) << pid
133  << std::right;
134  }
135  else {
136  out << centerString(std::get<1>(*iKnown), widthCorr);
137  }
138  } // for
139 
140  // print a line of corrections for each energy
141  for (unsigned int i = 0; i <= nSteps; ++i) {
142 
143  float const E = Emin + i * Estep;
144 
145  // set the same energy from every of the three planes
146  shower.set_total_energy({ E, E, E });
147 
148  out << "\n"
149  << std::fixed << std::setw(widthE) << std::setprecision(3) << E;
150 
151  for (auto pid: pids) {
152  auto corr = calibration->correction(shower, pid);
153 
154  out << sep
155  << std::fixed << std::setw(widthF) << corr.factor
156  << std::setw(widthFtoErr) << " +/- "
157  << std::fixed << std::setw(widthFerr) << corr.error;
158 
159  } // for (pid)
160 
161  } // for (i)
162  out << "\n";
163 
164  return 0; // no real error detection here, sorry
165 } // ShowerCalibrationTest()
166 
167 //------------------------------------------------------------------------------
168 
169 
170 } // namespace tests
171 } // namespace example
172 } // namespace lar
173 
174 
175 //------------------------------------------------------------------------------
176 
std::string string
Definition: nybbler.cc:12
unsigned int ID
void set_total_energy(const std::vector< double > &q)
Definition: Shower.h:129
recob::Shower MakeShower(float E, int bestPlane=2, int ID=1)
virtual Correction_t correction(recob::Shower const &shower, PDGID_t PDGID=unknownID) const =0
Returns the correction for a given reconstructed shower.
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
T abs(T value)
const double e
static int max(int a, int b)
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::string centerString(std::string const &s, unsigned int width, char pad= ' ')
Returns a string padding s to be centered in a width w.
Interface for a shower calibration service provider.
LArSoft-specific namespace.
unsigned int ShowerCalibrationTableTest(Stream &&out, lar::example::ShowerCalibrationGalore const *calibration, float Emin=0.0, float Emax=2.5, float Estep=0.1, std::vector< lar::example::ShowerCalibrationGalore::PDGID_t > const &pids={11, 13, 111, 2212, 22})
Synthetic test: prints corrections for showers in a energy range.
E
Definition: 018_def.c:13
static QCString * s
Definition: config.cpp:1042
Interface for a shower calibration service provider.