Classes | Functions
lar::example::tests Namespace Reference

Tests for LArSoft examples. More...

Classes

class  AtomicNumberTest
 Tests AtomicNumberService. More...
 
class  CheckDataProductSize
 Checks the size of the specified collection. More...
 
class  DumpCheatTracks
 Dumps lar::example::CheatTrack data product and associations. More...
 
class  ParticleMaker
 Creates a collection of simulated particles. More...
 
class  ShowerCalibrationGaloreTest
 Tests ShowerCalibrationGaloreService. More...
 
class  SpacePointMaker
 Creates a collection of space points. More...
 

Functions

int CreateTestShowerCalibrationFromPID (std::string outputPath)
 Creates a test calibration file for ShowerCalibrationGaloreFromPID. More...
 
std::string centerString (std::string const &s, unsigned int width, char pad= ' ')
 Returns a string padding s to be centered in a width w. More...
 
recob::Shower MakeShower (float E, int bestPlane=2, int ID=1)
 
template<typename Stream >
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. More...
 
recob::SpacePoint MakeSpacePoint (int ID, double const *pos, double error=0.)
 Creates and returns a new space point. More...
 
unsigned int FillSpacePointGrid (std::vector< recob::SpacePoint > &spacePoints, geo::BoxBoundedGeo const &box, double stepSize)
 Creates space points distributed in a grid. More...
 

Detailed Description

Tests for LArSoft examples.

Function Documentation

std::string lar::example::tests::centerString ( std::string const &  s,
unsigned int  width,
char  pad = ' ' 
)
inline

Returns a string padding s to be centered in a width w.

Definition at line 34 of file ShowerCalibrationGaloreTests.h.

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()
uint length() const
Definition: qcstring.h:195
std::string string
Definition: nybbler.cc:12
static int max(int a, int b)
static QCString * s
Definition: config.cpp:1042
int lar::example::tests::CreateTestShowerCalibrationFromPID ( std::string  outputPath)

Creates a test calibration file for ShowerCalibrationGaloreFromPID.

Parameters
outputPathUNIX + ROOT path for the output ROOT directory
Returns
an error code, 0 on success

outputPath is a full ROOT directory path made of a UNIX path and a ROOT directory path. For example, "data/calibrations.root:Showers/ByType" will create a directory data and a calibrations.root ROOT file in it (or update it if exists), create a structure of two nested ROOT directories, Showers/ByType, and write all the calibration graphs in there.

It currently writes:

  • "Pi0" (TGraphErrors): neutral pion calibration vs. reconstructed energy, [ 0; 2 ] GeV range
  • "Photon" (TGraphErrors): photon calibration vs. reconstructed energy, [ 0; 2 ] GeV range
  • "Electron" (TGraphErrors): electron/positron calibration vs. reconstructed energy, [ 0; 2 ] GeV range
  • "Muon" (TGraphErrors): muon/antimuon calibration vs. reconstructed energy, [ 0; 2 ] GeV range
  • "Default" (TGraphErrors): other particle calibration vs. reconstructed energy, [ 0; 5 ] GeV range

Definition at line 328 of file CreateTestShowerCalibrationFromPID.cxx.

329 {
330 
331  //
332  // create output file
333  //
334  TDirectory* pOutputDir = CreateROOTdir(outputPath);
335  if (!pOutputDir) {
336  std::cerr << "Can't create ROOT directory '" << outputPath << "'"
337  << std::endl;
338  return 1;
339  }
340  TFile* pOutputFile = pOutputDir->GetFile();
341 
342  //
343  // create the calibration histograms
344  //
345  pOutputDir->cd();
346  try {
347  WriteCalibrationObject
348  (CreateNeutralPionCalibration(), "pion calibration");
349  WriteCalibrationObject(CreatePhotonCalibration(), "photon calibration");
350  WriteCalibrationObject
351  (CreateElectronCalibration(), "electron calibration");
352  WriteCalibrationObject(CreateMuonCalibration(), "muon calibration");
353  WriteCalibrationObject(CreateGeneralCalibration(), "generic calibration");
354  }
355  catch (std::runtime_error const& e) {
356  std::cerr << "An error occurred: " << e.what() << std::endl;
357  return 1;
358  }
359 
360  //
361  // close output and leave (deliberately ONLY if writing above was successful)
362  //
363  pOutputDir->Write();
364  pOutputFile->Write();
365  delete pOutputFile;
366 
367  return 0;
368 } // lar::example::tests::CreateTestShowerCalibrationFromPID()
const double e
QTextStream & endl(QTextStream &s)
unsigned int lar::example::tests::FillSpacePointGrid ( std::vector< recob::SpacePoint > &  spacePoints,
geo::BoxBoundedGeo const &  box,
double  stepSize 
)

Creates space points distributed in a grid.

Parameters
spacePointsthe container to be filled
boxthe extend of volume to be filled
stepSizethe spacing between points
Returns
the number of space points added

The function adds space points to the specified collection. The centre of the box hosts a space point. The other space points are added shifting by multiples of stepSize in all directions. The IDs are incremental, starting from the ID next to the one from the last space point in the collection, or 1 if the collection is empty.

Points have an uncertainty of $ s/\sqrt{12} $, with $ s $ the step size.

Definition at line 40 of file SpacePointTestUtils.cxx.

44  {
45 
46  // determine the starting point
47  auto const indicesX = ComputeRangeIndices(box.MinX(), box.MaxX(), stepSize);
48  auto const indicesY = ComputeRangeIndices(box.MinY(), box.MaxY(), stepSize);
49  auto const indicesZ = ComputeRangeIndices(box.MinZ(), box.MaxZ(), stepSize);
50 
51  int ID = spacePoints.empty()? 1: spacePoints.back().ID() + 1;
52  size_t const origNPoints = spacePoints.size();
53  double const error = stepSize / std::sqrt(12.);
54 
55  // fill the grid;
56  // we don't use an increment (point[0] += stepping) to avoid rounding errors
57  std::array<double, 3> const center
58  {{ box.CenterX(), box.CenterY(), box.CenterZ() }};
59  std::array<double, 3> point;
60  for (int ix = indicesX.first; ix <= indicesX.second; ++ix) {
61  point[0] = center[0] + ix * stepSize;
62 
63  for (int iy = indicesY.first; iy <= indicesY.second; ++iy) {
64  point[1] = center[1] + iy * stepSize;
65 
66  for (int iz = indicesZ.first; iz <= indicesZ.second; ++iz) {
67  point[2] = center[2] + iz * stepSize;
68 
69  spacePoints.push_back(MakeSpacePoint(ID++, point.data(), error));
70 
71  } // for z
72  } // for y
73  } // for x
74 
75  return spacePoints.size() - origNPoints;
76 } // lar::example::tests::FillSpacePointGrid()
unsigned int ID
error
Definition: include.cc:26
recob::SpacePoint MakeSpacePoint(int ID, double const *pos, double error=0.)
Creates and returns a new space point.
std::pair< int, int > ComputeRangeIndices(double min, double max, double stepSize)
def center(depos, point)
Definition: depos.py:117
recob::Shower lar::example::tests::MakeShower ( float  E,
int  bestPlane = 2,
int  ID = 1 
)
inline

Definition at line 47 of file ShowerCalibrationGaloreTests.h.

47  {
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()
unsigned int ID
const double e
E
Definition: 018_def.c:13
recob::SpacePoint lar::example::tests::MakeSpacePoint ( int  ID,
double const *  pos,
double  error = 0. 
)

Creates and returns a new space point.

Parameters
IDspace point identifier
posspace point position: { x, y, z } [cm]
erroruncertainty on position (applies to all the coordinates)
Returns
a new space point

Points are uncorrelated.

Definition at line 26 of file SpacePointTestUtils.cxx.

27 {
28  // assume it's upper triangular; the documentation does not say
29  double const err[6] = { error, 0., 0., error, 0., error };
30  return {
31  pos,
32  err,
33  0.0, // chisq
34  ID
35  };
36 } // lar::example::tests::MakeSpacePoint()
unsigned int ID
error
Definition: include.cc:26
void err(const char *fmt,...)
Definition: message.cpp:226
template<typename Stream >
unsigned int lar::example::tests::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.

Template Parameters
Streamtype of output stream
Parameters
outoutput stream
calibrationservice provider
Eminlower shower energy for the printout [GeV] (default: 0)
Emaxupper shower energy for the printout GeV
Estepenergy step size for the printout [GeV] (default: have 10 steps)
pidsuse these PIDs (default: { 11, 13, 111, 2212, 22 })
Returns
number of detected errors (currently always 0)

The corrections are printed in a table like:

E [GeV] particle1 particle2 ...
0.000 1.000 +/- 0.000 1.023 +/- 0.003 ...
...

Definition at line 83 of file ShowerCalibrationGaloreTests.h.

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()
std::string string
Definition: nybbler.cc:12
void set_total_energy(const std::vector< double > &q)
Definition: Shower.h:129
recob::Shower MakeShower(float E, int bestPlane=2, int ID=1)
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
T abs(T value)
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.
E
Definition: 018_def.c:13