MakeLibrary_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file MakeLibrary_module.cc
3 // \brief Summarize ART events into LEM libary format
4 // \author Christopher Backhouse - bckhouse@caltech.edu
5 ////////////////////////////////////////////////////////////////////////
6 
8 #include "LEM/Util.h"
9 
10 // ROOT includes
11 #include "TH1.h"
12 #include "TTree.h"
13 
14 // NOvASoft includes
16 #include "Geometry/Geometry.h"
18 
19 #include "MCCheater/BackTracker.h"
20 
21 #include "RecoBase/CellHit.h"
22 #include "RecoBase/RecoHit.h"
23 #include "RecoBase/Cluster.h"
24 
25 #include "Simulation/Particle.h"
28 
29 #include "SummaryData/POTSum.h"
30 
31 // Framework includes
38 #include "fhiclcpp/ParameterSet.h"
39 
40 namespace lem
41 {
42  /// Summarize ART events into %LEM libary format
44  {
45  public:
46  explicit MakeLibrary(const fhicl::ParameterSet& pset);
47  ~MakeLibrary();
48 
49  virtual void produce(art::Event& evt);
50 
51  virtual void reconfigure(const fhicl::ParameterSet& pset);
52 
53  virtual void beginJob();
54  virtual void endSubRun(art::SubRun& sr);
55  virtual void endJob();
56  protected:
57  TVector3 SliceMeanPosEWeighted(const rb::Cluster& slice) const;
58 
59  int SliceOriginHelper(std::deque<int>& xs, double frac, int origin) const;
60  void SliceOrigin(const rb::Cluster& slc,
61  int& plane, int& cellX, int& cellY) const;
62 
63  TTree* fTree;
64  double fTotalPOT;
65 
69  };
70 
71  //......................................................................
73  : fTree(0), fTotalPOT(0)
74  {
75  reconfigure(pset);
76  }
77 
78  //......................................................................
80  {
81  }
82 
83  //......................................................................
85  {
86  fParticleLabel = pset.get<std::string>("ParticleLabel");
87  fSlicerLabel = pset.get<std::string>("SlicerLabel");
88  fMCTruthLabel = pset.get<std::string>("MCTruthLabel");
89 
90  // We want to store exactly the values of the hits, only rescale them later
91  // when actually doing matching.
94  }
95 
96  //......................................................................
98  {
100  fTree = tfs->make<TTree>("tree", "tree");
101 
103  }
104 
105  //......................................................................
107  {
109  TH1* hPOT = tfs->make<TH1F>("hTotalPOT", ";; POT", 1, 0, 1);
110  hPOT->Fill(.5, fTotalPOT);
111  }
112 
113  //----------------------------------------------------------------------
115  {
117  sr.getByLabel(fMCTruthLabel, pot);
118 
119  fTotalPOT += pot->totgoodpot;
120 
121  std::cout << "POT " << pot->totgoodpot << std::endl;
122  }
123 
124  //......................................................................
125  int MakeLibrary::SliceOriginHelper(std::deque<int>& xs,
126  double frac, int origin) const
127  {
128  if(xs.empty()) return 0;
129 
130  std::sort(xs.begin(), xs.end());
131 
132  // If the range is too big to fit drop outlier entries until it isn't
133  while(xs.back()-xs.front() > 255){
134  xs.pop_back();
135  if(xs.back()-xs.front() > 255) xs.pop_front();
136  }
137 
138  // Initial estimate is this far through the list
139  int x = xs[frac*xs.size()];
140 
141  // If that drops entries off the bottom nudge it up
142  if(xs.front() - x + origin < 0){
143  x = xs.front() - origin;
144  }
145  // If it drops entries off the top nudge it down
146  if(xs.back() - x + origin > 255){
147  x = xs.back() + origin - 255;
148  }
149 
150  return x;
151  }
152 
153  //......................................................................
154  int Clamp(int x)
155  {
156  return std::min(std::max(0, x), 255);
157  }
158 
159  //......................................................................
161  int& plane, int& cellX, int& cellY) const
162  {
163  cellX = cellY = 0; // Default in case of empty view
164 
165  std::deque<int> xcells, ycells, planes;
166 
167  const unsigned int N = slc.NCell();
168  for(unsigned int cellIdx = 0; cellIdx < N; ++cellIdx){
169  const art::Ptr<rb::CellHit> chit = slc.Cell(cellIdx);
170  planes.push_back(chit->Plane());
171  if(chit->View() == geo::kX)
172  xcells.push_back(chit->Cell());
173  else
174  ycells.push_back(chit->Cell());
175  }
176 
177  plane = SliceOriginHelper(planes, .05, lem::kVertexPlane);
178  cellX = SliceOriginHelper(xcells, .5, lem::kVertexCell);
179  cellY = SliceOriginHelper(ycells, .5, lem::kVertexCell);
180  }
181 
182  //......................................................................
184  {
187 
189  evt.getByLabel(fMCTruthLabel, fluxs);
190 
191  assert(fluxs->size() == 1);
192  const simb::MCFlux& flux = (*fluxs)[0];
193 
195  evt.getByLabel(fMCTruthLabel, truths);
196  assert(truths->size() == 1);
197  const simb::MCTruth& truth = (*truths)[0];
198 
199  const simb::MCNeutrino& nu = truth.GetNeutrino();
200  const TVector3 vtx = nu.Nu().Position(0).Vect();
201 
203  evt.getByLabel(fSlicerLabel, slices);
204 
205  // Find the biggest slice
206  int bestIdx = -1;
207  double bestADC = 0;
208  for(unsigned int sliceIdx = 0; sliceIdx < slices->size(); ++sliceIdx){
209  if((*slices)[sliceIdx].IsNoise()) continue;
210  const double adc = (*slices)[sliceIdx].TotalADC();
211  if(adc > bestADC){
212  bestADC = adc;
213  bestIdx = sliceIdx;
214  }
215  }
216 
217  if(bestIdx == -1) return;
218 
219  const rb::Cluster& slice = (*slices)[bestIdx];
220 
221  int originPlane, originCellX, originCellY;
222  SliceOrigin(slice, originPlane, originCellX, originCellY);
223 
224  // Collect all the track IDs that are ever primary for a hit
225  std::set<int> ids;
226  for(unsigned int cellIdx = 0; cellIdx < slice.NCell(); ++cellIdx){
227  const art::Ptr<rb::CellHit>& chit = slice.Cell(cellIdx);
228  const std::vector<cheat::TrackIDE> ides = bt->HitToTrackIDE(chit);
229  if(!ides.empty()) ids.insert(ides[0].trackID);
230  }
231  if(ids.size() > 255) mf::LogWarning("MakeLibrary") << "Track ID overflow";
232  // Map them onto small integers
233  std::map<int, unsigned char> idMap;
234  for(int id : ids){
235  const unsigned char next = std::min(idMap.size(), 255ul);
236  idMap[id] = next;
237  }
238 
239  // TODO: merge hits that fall on already hit cells. This apparently
240  // happens. Does it cause the self-energy to be wrong?
241  std::vector<LiteHit> hs;
242  for(unsigned int cellIdx = 0; cellIdx < slice.NCell(); ++cellIdx){
243  const art::Ptr<rb::CellHit>& chit = slice.Cell(cellIdx);
244 
245  const int originCell = (chit->View() == geo::kX ? originCellX : originCellY);
246 
247  const int plane = Clamp(chit->Plane()-originPlane+lem::kVertexPlane);
248 
249  // Get it central in the window
250  const int cell = Clamp(chit->Cell()-originCell+lem::kVertexCell);
251 
252  const std::vector<cheat::TrackIDE> ides = bt->HitToTrackIDE(chit);
253  const unsigned char idIdx = ides.empty() ? 255 : idMap[ides[0].trackID];
254 
255  const rb::RecoHit rhit = slice.RecoHit(chit);
256 
257  if(rhit.IsCalibrated())
258  hs.push_back(LiteHit(plane, cell, rhit.PECorr(), idIdx));
259  } // end for cellIdx
260 
261  std::vector<int> pdgs;
262  for(int id : ids){
263  if(id != 255){
264  const sim::Particle* part = bt->TrackIDToParticle(id);
265  if(part) pdgs.push_back(part->PdgCode());
266  }
267  }
268 
269  EventSummary sum(hs, -1, pdgs);
270 
271  sum.run = evt.run();
272  sum.subrun = evt.subRun();
273  sum.event = evt.event();
274 
275  sum.origPdg = flux.fntype;
276 
277  sum.pdg = nu.Nu().PdgCode();
278  sum.ccnc = nu.CCNC();
279  sum.mode = nu.Mode();
280  sum.y = nu.Y();
281  sum.trueEVis = nu.Nu().E()*(nu.CCNC() ? nu.Y() : 1);
282 
283 
284  // Convert the true vertex to an integer cell, and store in the summary
285 
287  // That call labelled the views by which plane the true vertex is in. But
288  // we need them labelled by which plane our origin is in.
289  if(sum.trueVtxPlane%2 != originPlane%2)
291 
292  const int originCell = (geom->Plane(originPlane)->View() == geo::kX) ? originCellX : originCellY;
293  const int originCellOther = (geom->Plane(originPlane)->View() == geo::kX) ? originCellY : originCellX;
294 
295  sum.trueVtxPlane = Clamp(sum.trueVtxPlane-originPlane+lem::kVertexPlane);
296 
297  // Get it central in the window
298  sum.trueVtxCell = Clamp(sum.trueVtxCell-originCell+lem::kVertexCell);
299 
300  sum.trueVtxCellOther = Clamp(sum.trueVtxCellOther-originCellOther+lem::kVertexCell);
301 
302  // Conversion length on photon pi0 children
304  evt.getByLabel(fParticleLabel, parts);
305  if(!parts.failedToGet()){
306  int pi0Id = -10;
307  std::map<double, double> photLs; // -energy, length, just for sorting
308  for(unsigned int n = 0; n < parts->size(); ++n){
309  const sim::Particle& p = (*parts)[n];
310  if(p.Process() == "Primary" && p.PdgCode() == 111) pi0Id = p.TrackId();
311  if(p.PdgCode() == 22 && p.Mother() == pi0Id){
312  photLs[-p.E()] = (p.EndPosition().Vect()-vtx).Mag();
313  }
314  }
315  if(!photLs.empty()){
316  sum.photE0 = -photLs.begin()->first;
317  sum.photL0 = photLs.begin()->second;
318 
319  photLs.erase(photLs.begin());
320  if(!photLs.empty()){
321  sum.photE1 = -photLs.begin()->first;
322  sum.photL1 = photLs.begin()->second;
323  }
324  }
325  }
326 
327  sum.ToTree(fTree);
328  }
329 
330  //......................................................................
331  TVector3 MakeLibrary::SliceMeanPosEWeighted(const rb::Cluster& slice) const
332  {
334 
335  double Ex = 0;
336  double Ey = 0;
337 
338  TVector3 mean;
339 
340  for(unsigned int i = 0; i < slice.NCell(); ++i){
341  const art::Ptr<rb::CellHit>& h = slice.Cell(i);
342  double xyz[3];
343  geom->Plane(h->Plane())->Cell(h->Cell())->GetCenter(xyz);
344  const double x = xyz[0];
345  const double y = xyz[1];
346  const double z = xyz[2];
347  const geo::View_t view = geom->Plane(h->Plane())->View();
348 
349  const rb::RecoHit rhit = slice.RecoHit(i);
350  if(!rhit.IsCalibrated()) continue;
351  const double E = rhit.PECorr();
352 
353  if(view == geo::kX){
354  mean.SetX(mean.X()+x*E);
355  Ex += E;
356  }
357  if(view == geo::kY){
358  mean.SetY(mean.Y()+y*E);
359  Ey += E;
360  }
361  mean.SetZ(mean.Z()+z*E);
362  }
363 
364  if(Ex) mean.SetX(mean.X()/Ex);
365  if(Ey) mean.SetY(mean.Y()/Ey);
366  if(Ex || Ey) mean.SetZ(mean.Z()/(Ex+Ey));
367 
368  return mean;
369  }
370 
372 
373 } //namespace lem
374 ////////////////////////////////////////////////////////////////////////
double E(const int i=0) const
Definition: MCParticle.h:232
::xsd::cxx::tree::id< char, ncname > id
Definition: Database.h:165
virtual void reconfigure(const fhicl::ParameterSet &pset)
TVector3 SliceMeanPosEWeighted(const rb::Cluster &slice) const
T max(const caf::Proxy< T > &a, T b)
SubRunNumber_t subRun() const
Definition: Event.h:72
back track the reconstruction to the simulation
const TLorentzVector & Position(const int i=0) const
Definition: MCParticle.h:218
int PdgCode() const
Definition: MCParticle.h:211
int CCNC() const
Definition: MCNeutrino.h:148
Simple representation of event for LEM use.
Definition: EventSummary.h:26
static double fgChargePower
Definition: EventSummary.h:76
unsigned int NCell(geo::View_t view) const
Number of cells in view view.
Definition: Cluster.cxx:134
const TLorentzVector & EndPosition() const
Definition: MCParticle.h:224
void SliceOrigin(const rb::Cluster &slc, int &plane, int &cellX, int &cellY) const
std::vector< TrackIDE > HitToTrackIDE(const rb::CellHit &hit, bool useBirksE=false) const
Convenience function. HitsToTrackIDE but for a single hit.
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
void VertexToPlaneAndCell(const TVector3 vtx, const rb::Cluster &slice, int &plane, int &cell, int &cellOtherView, bool reverse)
Definition: Util.cxx:60
unsigned short Plane() const
Definition: CellHit.h:39
int Mother() const
Definition: MCParticle.h:212
geo::View_t View() const
Definition: CellHit.h:41
const char * p
Definition: xmltok.h:285
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
virtual void endSubRun(art::SubRun &sr)
Vertical planes which measure X.
Definition: PlaneGeo.h:28
const int kVertexPlane
Definition: EventSummary.h:22
A collection of associated CellHits.
Definition: Cluster.h:47
std::string fMCTruthLabel
const PlaneGeo * Plane(unsigned int i) const
DEFINE_ART_MODULE(TestTMapFile)
std::string Process() const
Definition: MCParticle.h:214
Loaders::FluxType flux
Horizontal planes which measure Y.
Definition: PlaneGeo.h:29
int Clamp(int x)
object containing MC flux information
int TrackId() const
Definition: MCParticle.h:209
Calibrated quantities relying on position in the orthogonal view. To generate a rb::CellHit from a rb...
Definition: RecoHit.h:19
void swap(art::HLTGlobalStatus &lhs, art::HLTGlobalStatus &rhs)
PID
Definition: FillPIDs.h:14
unsigned short Cell() const
Definition: CellHit.h:40
static void InitToTree(TTree *tr)
TString part[npart]
Definition: Style.C:32
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:53
std::string fSlicerLabel
double Y() const
Definition: MCNeutrino.h:156
virtual void produce(art::Event &evt)
int SliceOriginHelper(std::deque< int > &xs, double frac, int origin) const
Float_t E
Definition: plot.C:20
const int kVertexCell
Definition: EventSummary.h:23
T get(std::string const &key) const
Definition: ParameterSet.h:231
int evt
#define pot
rb::RecoHit RecoHit(const art::Ptr< rb::CellHit > &chit) const
Return calibrated hit based on assumed W coordinate.
Definition: Cluster.cxx:259
bool IsCalibrated() const
You MUST check here before accessing PECorr, MIP or GeV.
Definition: RecoHit.cxx:35
caf::StandardRecord * sr
double frac(double x)
Fractional part.
EventNumber_t event() const
Definition: Event.h:67
void ToTree(TTree *tr) const
Definition: View.py:1
z
Definition: test.py:28
virtual void beginJob()
const sim::Particle * TrackIDToParticle(int const &id) const
Returns a pointer to the sim::Particle object corresponding to the given TrackID. ...
OStream cout
Definition: OStream.cxx:6
art::Ptr< rb::CellHit > Cell(geo::View_t view, unsigned int viewIdx) const
Get the ith cell from view view.
Definition: Cluster.cxx:145
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T * make(ARGS...args) const
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
virtual void endJob()
void geom(int which=0)
Definition: geom.C:163
float Mag() const
static double fgDecayRate
Definition: EventSummary.h:75
assert(nhit_max >=nhit_nbins)
float PECorr() const
Definition: RecoHit.cxx:47
Event generator information.
Definition: MCTruth.h:32
T min(const caf::Proxy< T > &a, T b)
Simple representation of event for LEM use.
Compressed hit info, basic component of LEM events.
Definition: LiteHit.h:18
double totgoodpot
normalized by 10^12 POT
Definition: POTSum.h:28
Double_t sum
Definition: plot.C:31
RunNumber_t run() const
Definition: Event.h:77
std::string fParticleLabel
Event generator information.
Definition: MCNeutrino.h:18
Summarize ART events into LEM libary format.
int Mode() const
Definition: MCNeutrino.h:149
void next()
Definition: show_event.C:84
MakeLibrary(const fhicl::ParameterSet &pset)
Encapsulate the geometry of one entire detector (near, far, ndos)
bool failedToGet() const
Definition: Handle.h:196