EMCNNCheckCosmics_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: EMCNNCheckCosmics
3 // Plugin Type: analyzer (art v3_03_01)
4 // File: EMCNNCheckCosmics_module.cc
5 //
6 // Generated at Wed Jan 8 21:50:23 2020 by Tingjun Yang using cetskelgen
7 // from cetlib version v3_08_00.
8 ////////////////////////////////////////////////////////////////////////
9 
16 #include "art_root_io/TFileService.h"
18 #include "canvas/Persistency/Common/FindManyP.h"
19 #include "fhiclcpp/ParameterSet.h"
21 
27 
33 
36 
37 #include "TTree.h"
38 
39 #include <iostream>
40 
41 namespace pdsp {
42  class EMCNNCheckCosmics;
43 }
44 
45 
47 public:
48  explicit EMCNNCheckCosmics(fhicl::ParameterSet const& p);
49  // The compiler-generated destructor is fine for non-base
50  // classes without bare pointers or other resource use.
51 
52  // Plugins should not be copied or assigned.
53  EMCNNCheckCosmics(EMCNNCheckCosmics const&) = delete;
57 
58  // Required functions.
59  void analyze(art::Event const& e) override;
60 
61  void beginJob() override;
62 
63 private:
64 
65  void ResetVars();
66 
67  //int fselectpdg;
70 
71  TTree *ftree;
72  int run;
73  int subrun;
74  int event;
75  int beampdg;
79  double track_endz;
82  double vtxx, vtxy, vtxz;
83  double endx, endy, endz;
84  double dirx, diry, dirz;
85  std::vector<short> channel;
86  std::vector<short> tpc;
87  std::vector<short> plane;
88  std::vector<short> wire;
89  std::vector<double> charge;
90  std::vector<double> peakt;
91  std::vector<double> score_em;
92  std::vector<double> score_trk;
93  std::vector<double> score_mic;
94 
95  std::vector<short> daughter_channel;
96  std::vector<short> daughter_tpc;
97  std::vector<short> daughter_plane;
98  std::vector<short> daughter_wire;
99  std::vector<double> daughter_charge;
100  std::vector<double> daughter_peakt;
101  std::vector<double> daughter_score_em;
102  std::vector<double> daughter_score_trk;
103  std::vector<double> daughter_score_mic;
104 
105  std::vector<int> pdg;
106  std::vector<int> origin;
107  std::vector<std::string> process;
108 
109 };
110 
111 
113  : EDAnalyzer{p},
114 //fselectpdg(p.get<int>("selectpdg")),
115  fGeneratorTag(p.get<std::string>("GeneratorTag")),
116  fCNNTag(p.get<std::string>("CNNTag","emtrkmichelid:emtrkmichel"))
117  {
118  }
119 
121 {
122  beampdg = 0;
123  average_score_em = 0.;
124  average_score_trk = 0.;
125  average_score_mic = 0.;
126  track_endz = -1;
127  ndaughterhits = 0;
129  vtxx = -9999;
130  vtxy = -9999;
131  vtxz = -9999;
132  endx = -9999;
133  endy = -9999;
134  endz = -9999;
135  dirx = -9999;
136  diry = -9999;
137  dirz = -9999;
138  channel.clear();
139  tpc.clear();
140  plane.clear();
141  wire.clear();
142  charge.clear();
143  peakt.clear();
144  score_em.clear();
145  score_trk.clear();
146  score_mic.clear();
147 
148  daughter_channel.clear();
149  daughter_tpc.clear();
150  daughter_plane.clear();
151  daughter_wire.clear();
152 
153  daughter_charge.clear();
154  daughter_peakt.clear();
155  daughter_score_em.clear();
156  daughter_score_trk.clear();
157  daughter_score_mic.clear();
158 
159  pdg.clear();
160  origin.clear();
161  process.clear();
162 
163 }
164 
166 {
167  run = e.run();
168  subrun = e.subRun();
169  event = e.id().event();
170 
171  //Services
174  auto const* SCE = lar::providerFrom<spacecharge::SpaceChargeService>();
175  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(e);
176  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService>()->DataFor(e, clockData);
178 
179  std::vector < art::Ptr < recob::Slice > > sliceList;
180  auto sliceListHandle = e.getHandle < std::vector < recob::Slice > >("pandora");
181  if (sliceListHandle) {
182  art::fill_ptr_vector(sliceList, sliceListHandle);
183  }
184  else return;
185 
186  // Get all pfparticles
187  std::vector < art::Ptr < recob::PFParticle > > pfpList;
188  auto pfpListHandle = e.getHandle < std::vector < recob::PFParticle > >("pandora");
189  if (pfpListHandle) {
190  art::fill_ptr_vector(pfpList, pfpListHandle);
191  }
192 
193  // Get all clusters
194  std::vector < art::Ptr < recob::Cluster > > cluList;
195  auto cluListHandle = e.getHandle < std::vector < recob::Cluster > >("pandora");
196  if (cluListHandle) {
197  art::fill_ptr_vector(cluList, cluListHandle);
198  }
199 
200  // Get all tracks
201  std::vector < art::Ptr < recob::Track > > trkList;
202  auto trkListHandle = e.getHandle < std::vector < recob::Track > >("pandoraTrack");
203  if (trkListHandle) {
204  art::fill_ptr_vector(trkList, trkListHandle);
205  }
206 
207  // Get all hits
208  std::vector < art::Ptr < recob::Hit > > hitList;
209  auto hitListHandle = e.getHandle < std::vector < recob::Hit > >("hitpdune");
210  if (hitListHandle) {
211  art::fill_ptr_vector(hitList, hitListHandle);
212  }
213 
214  // Get hits-track association
215  art::FindManyP<recob::Cluster> fmcpfp(pfpListHandle, e, "pandora");
216 
217  // Get vertex-PFParticle association
218  art::FindManyP<recob::Vertex> fmvpfp(pfpListHandle, e, "pandora");
219 
220  // Get hit-cluster association
221  art::FindManyP<recob::Hit> fmhc(cluListHandle, e, "pandora");
222 
223  art::FindManyP <recob::Hit> hitsFromSlice(sliceListHandle, e, "pandora");
224 
225  // Get track-hit association
226  art::FindManyP<recob::Hit, recob::TrackHitMeta> fmthm(trkListHandle, e,"pandoraTrack"); // to associate tracks and hits
227 
228  // Get hit-track association
229  art::FindManyP<recob::Hit> thass(trkListHandle, e, "pandoraTrack"); //to associate hit just trying
230 
231  anab::MVAReader<recob::Hit,4> hitResults(e, fCNNTag);
232 
233  // Get the PFParticle utility
235  const unsigned int beamSlice{pfpUtil.GetBeamSlice(e,"pandora")};
236 
237  const std::map<unsigned int,std::vector<const recob::PFParticle*>> sliceToPFParticleMap = pfpUtil.GetPFParticleSliceMap(e,"pandora");
238  std::vector<unsigned int> tracksToUse;
239  for (auto const & element : sliceToPFParticleMap)
240  {
241  if (element.first == beamSlice)
242  continue;
243 
244  for (const recob::PFParticle *part : element.second)
245  {
246  // We only want track-like particles
247  const recob::Track *track{pfpUtil.GetPFParticleTrack(*part,e,"pandora","pandoraTrack")};
248  if (track == nullptr)
249  continue;
250 
251  // Check that the track is long enough to be considered a cosmic muon
252  if (track->Length() < 100.)
253  continue;
254 
255  // Ignore tracks that start or end near the front face of the TPC
256  if (track->Start<TVector3>().Z() < 50.0 || track->End<TVector3>().Z() < 50.0)
257  continue;
258 
259  // Angular cut for very steep tracks
260  if (track->VertexDirection<TVector3>().Y() < TMath::Cos(165. * TMath::Pi() / 180.))
261  continue;
262 
263  // Now we should be in the situation where we have a good track
264  tracksToUse.push_back(track->ID());
265  }
266  }
267 
268  // We can now look at these particles
269  int trackid = -1;
270 // int endwire = -1;
271 // int endpeakt = -1;
272 // int endtpc = -1;
273  std::vector<int> wirekeys;
274  for(unsigned int t = 0; t < tracksToUse.size(); ++t){
275 
276  const art::Ptr<recob::Track> thisTrack = trkList.at(tracksToUse.at(t));
277  if (thisTrack){
278  this->ResetVars();
279  //if (!beam_cuts.IsBeamlike(*thisTrack, e, "1")) return;
280  // Track ID
281  trackid = thisTrack->ID();
282  // Track end point z
283  track_endz = thisTrack->End().Z();
284  vtxx = thisTrack->Vertex().X();
285  vtxy = thisTrack->Vertex().Y();
286  vtxz = thisTrack->Vertex().Z();
287  if (!geom->FindTPCAtPosition(geo::Point_t(vtxx, vtxy, vtxz)).isValid) return;
288  auto offset = SCE->GetCalPosOffsets(geo::Point_t(vtxx, vtxy, vtxz), (geom->FindTPCAtPosition(geo::Point_t(vtxx, vtxy, vtxz))).TPC);
289 // std::cout<<"track "<<offset.X()<<" "<<offset.Y()<<" "<<offset.Z()<<std::endl;
290  vtxx -= offset.X();
291  vtxy += offset.Y();
292  vtxz += offset.Z();
293  endx = thisTrack->End().X();
294  endy = thisTrack->End().Y();
295  endz = thisTrack->End().Z();
296  if (!geom->FindTPCAtPosition(geo::Point_t(endx, endy, endz)).isValid) return;
297  offset = SCE->GetCalPosOffsets(geo::Point_t(endx, endy, endz), (geom->FindTPCAtPosition(geo::Point_t(endx, endy, endz))).TPC);
298  endx -= offset.X();
299  endy += offset.Y();
300  endz += offset.Z();
301  TVector3 dir(endx-vtxx, endy-vtxy, endz-vtxz);
302  dir = dir.Unit();
303  dirx = dir.X();
304  diry = dir.Y();
305  dirz = dir.Z();
306  // Find the last wire number and peak time on the track
307  if (fmthm.isValid()){
308  float zlast0=-99999;
309  auto vhit=fmthm.at(trackid);
310  auto vmeta=fmthm.data(trackid);
311  for (size_t ii = 0; ii<vhit.size(); ++ii){ //loop over all meta data hit
312  bool fBadhit = false;
313  if (vmeta[ii]->Index() == static_cast<unsigned int>(std::numeric_limits<int>::max())){
314  fBadhit = true;
315  continue;
316  }
317  if (vmeta[ii]->Index()>=thisTrack->NumberTrajectoryPoints()){
318  throw cet::exception("Calorimetry_module.cc") << "Requested track trajectory index "<<vmeta[ii]->Index()<<" exceeds the total number of trajectory points "<<thisTrack->NumberTrajectoryPoints()<<" for track index "<<trackid<<". Something is wrong with the track reconstruction. Please contact tjyang@fnal.gov!!";
319  }
320  if (!thisTrack->HasValidPoint(vmeta[ii]->Index())){
321  fBadhit = true;
322  continue;
323  }
324  auto loc = thisTrack->LocationAtPoint(vmeta[ii]->Index());
325  if (fBadhit) continue; //HY::If BAD hit, skip this hit and go next
326  if (loc.Z()<-100) continue; //hit not on track
327  if(vhit[ii]->WireID().Plane==2){
328  wirekeys.push_back(vhit[ii].key());
329  float zlast=loc.Z();
330  if(zlast>zlast0){
331  zlast0=zlast;
332 // endwire=vhit[ii]->WireID().Wire;
333 // endpeakt=vhit[ii]->PeakTime();
334 // endtpc=vhit[ii]->WireID().TPC;
335  }
336  }
337  }
338  }
339 
340  // Now we can loop over the hits
341  auto const &hits = thass.at(trackid);
342  for (auto & hit : hits){
343  std::array<float,4> cnn_out = hitResults.getOutput(hit);
344  if (hit->WireID().Plane == 2){
345  channel.push_back(hit->Channel());
346  tpc.push_back(hit->WireID().TPC);
347  plane.push_back(hit->WireID().Plane);
348  wire.push_back(hit->WireID().Wire);
349  charge.push_back(hit->Integral());
350  peakt.push_back(hit->PeakTime());
351  score_em.push_back(cnn_out[hitResults.getIndex("em")]);
352  score_trk.push_back(cnn_out[hitResults.getIndex("track")]);
353  score_mic.push_back(cnn_out[hitResults.getIndex("michel")]);
354  }
355  }
356  }
357 
358  // Get the average of the collection plane scores
359  unsigned int nCollectionHits = 0;
360  for(unsigned int h = 0; h < plane.size(); ++h){
361  if(plane.at(h) == 2){
362  ++nCollectionHits;
363  average_score_em += score_em.at(h);
366  }
367  }
368 
369  if(nCollectionHits > 0){
370  average_score_em /= static_cast<double>(nCollectionHits);
371  average_score_trk /= static_cast<double>(nCollectionHits);
372  average_score_mic /= static_cast<double>(nCollectionHits);
373  }
374 
375  if (!channel.empty())
376  {
377  std::cout << "Filling output tree" << std::endl;
378  ftree->Fill();
379  }
380  }
381 }
382 
384 
385  art::ServiceHandle<art::TFileService> fileServiceHandle;
386  ftree = fileServiceHandle->make<TTree>("ftree", "hit info");
387  ftree->Branch("run", &run, "run/I");
388  ftree->Branch("event", &event, "event/I");
389  ftree->Branch("beampdg", &beampdg, "beampdg/I");
390  ftree->Branch("average_score_em" , &average_score_em , "average_score_em/D");
391  ftree->Branch("average_score_trk", &average_score_trk, "average_score_trk/D");
392  ftree->Branch("average_score_mic", &average_score_mic, "average_score_mic/D");
393  ftree->Branch("track_endz", &track_endz, "track_endz/D");
394  ftree->Branch("ndaughterhits", &ndaughterhits, "ndaughterhits/I");
395  ftree->Branch("average_daughter_score_mic", &average_daughter_score_mic, "average_daughter_score_mic/D");
396  ftree->Branch("vtxx", &vtxx, "vtxx/D");
397  ftree->Branch("vtxy", &vtxy, "vtxy/D");
398  ftree->Branch("vtxz", &vtxz, "vtxz/D");
399  ftree->Branch("endx", &endx, "endx/D");
400  ftree->Branch("endy", &endy, "endy/D");
401  ftree->Branch("endz", &endz, "endz/D");
402  ftree->Branch("dirx", &dirx, "dirx/D");
403  ftree->Branch("diry", &diry, "diry/D");
404  ftree->Branch("dirz", &dirz, "dirz/D");
405  ftree->Branch("channel", &channel);
406  ftree->Branch("tpc", &tpc);
407  ftree->Branch("plane", &plane);
408  ftree->Branch("wire", &wire);
409  ftree->Branch("charge", &charge);
410  ftree->Branch("peakt", &peakt);
411  ftree->Branch("score_em", &score_em);
412  ftree->Branch("score_trk", &score_trk);
413  ftree->Branch("score_mic", &score_mic);
414 
415  ftree->Branch("daughter_channel", &daughter_channel);
416  ftree->Branch("daughter_tpc", &daughter_tpc);
417  ftree->Branch("daughter_plane", &daughter_plane);
418  ftree->Branch("daughter_wire", &daughter_wire);
419  ftree->Branch("daughter_charge", &daughter_charge);
420  ftree->Branch("daughter_peakt", &daughter_peakt);
421  ftree->Branch("daughter_score_em", &daughter_score_em);
422  ftree->Branch("daughter_score_trk", &daughter_score_trk);
423  ftree->Branch("daughter_score_mic", &daughter_score_mic);
424 
425  ftree->Branch("pdg", &pdg);
426  ftree->Branch("origin", &origin);
427  ftree->Branch("process", &process);
428 
429 }
430 
431 
std::vector< double > daughter_score_trk
std::vector< double > daughter_score_mic
std::vector< double > score_trk
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
Point_t const & LocationAtPoint(size_t i) const
Definition: Track.h:126
bool isValid
Whether this ID points to a valid element.
Definition: geo_types.h:211
bool HasValidPoint(size_t i) const
Definition: Track.h:111
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
Definition: Track.h:102
Class to keep data related to recob::Hit associated with recob::Track.
int getIndex(const std::string &name) const
Index of column with given name, or -1 if name not found.
Definition: MVAReader.h:82
std::vector< short > daughter_wire
std::vector< double > score_mic
string dir
unsigned int Index
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
std::vector< double > daughter_score_em
geo::TPCID FindTPCAtPosition(double const worldLoc[3]) const
Returns the ID of the TPC at specified location.
std::vector< double > daughter_peakt
void analyze(art::Event const &e) override
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
def key(type, name=None)
Definition: graph.py:13
unsigned short GetBeamSlice(art::Event const &evt, const std::string particleLabel) const
Try to get the slice tagged as beam. Returns 9999 if no beam slice was found.
const recob::Track * GetPFParticleTrack(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel, const std::string trackLabel) const
Get the track associated to this particle. Returns a null pointer if not found.
Point_t const & Vertex() const
Definition: Track.h:124
std::vector< short > daughter_plane
p
Definition: test.py:223
const std::map< unsigned int, std::vector< const recob::PFParticle * > > GetPFParticleSliceMap(art::Event const &evt, const std::string particleLabel) const
Get a map of slice index to the primary PFParticles within it.
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
RunNumber_t run() const
Definition: DataViewImpl.cc:71
static int max(int a, int b)
Detector simulation of raw signals on wires.
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
int ID() const
Definition: Track.h:198
std::vector< short > daughter_tpc
Hierarchical representation of particle flow.
Definition: PFParticle.h:44
std::vector< short > daughter_channel
EMCNNCheckCosmics & operator=(EMCNNCheckCosmics const &)=delete
EMCNNCheckCosmics(fhicl::ParameterSet const &p)
EventNumber_t event() const
Definition: EventID.h:116
Point_t const & End() const
Definition: Track.h:125
std::vector< std::string > process
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
std::vector< double > daughter_charge
std::array< float, N > getOutput(size_t key) const
Get copy of the MVA output vector at index "key".
Definition: MVAReader.h:129
EventID id() const
Definition: Event.cc:34
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49
std::vector< double > score_em
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
Event finding and building.