CRTMatchTrackCaloAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTMatchTrackCaloAna
3 // Plugin Type: analyzer (art v3_02_06)
4 // File: CRTMatchTrackCaloAna_module.cc
5 // This code is an adaptation of Tingjun Yang's T0 code with adjustments so it pulls a crt matched track and the TPC dE/dx information
6 // Generated at Tue Jul 30 21:43:10 2019 by Tingjun Yang using cetskelgen
7 // from cetlib version v3_07_02.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "canvas/Persistency/Common/FindManyP.h"
19 #include "fhiclcpp/ParameterSet.h"
21 #include "art_root_io/TFileService.h"
37 
39 #include "TTree.h"
40 
41 #include <vector>
42 
43 namespace pdsp {
44  class CRTMatchTrackCaloAna;
45 }
46 
47 
49 public:
51  // The compiler-generated destructor is fine for non-base
52  // classes without bare pointers or other resource use.
53 
54  // Plugins should not be copied or assigned.
59  void beginJob() override;
60 
61  // Required functions.
62  void analyze(art::Event const& e) override;
63 
64 private:
66 
67  // Declare member data here.
68  TTree *crtCalo;
69  int run;
70  int event;
71  std::vector<int> trackid;
72  std::vector<double> t0crt2;
73  std::vector<double> t0pandora;
74  std::vector<double> t0anodep;
75  std::vector<double> t0truth;
76  std::vector<double> trackstartx;
77  std::vector<double> trackstarty;
78  std::vector<double> trackstartz;
79  std::vector<double> trackendx;
80  std::vector<double> trackendy;
81  std::vector<double> trackendz;
82  std::vector<double> trackstartx_sce;
83  std::vector<double> trackstarty_sce;
84  std::vector<double> trackstartz_sce;
85  std::vector<double> trackendx_sce;
86  std::vector<double> trackendy_sce;
87  std::vector<double> trackendz_sce;
88 
89  std::vector<double> crt2x0;
90  std::vector<double> crt2y0;
91  std::vector<double> crt2z0;
92  std::vector<double> crt2x1;
93  std::vector<double> crt2y1;
94  std::vector<double> crt2z1;
95  std::vector<double> trkhitx;
96  std::vector<double> trkhity;
97  std::vector<double> trkhitz;
98  std::vector<double> WireID;
99  std::vector<double> TPCID;
100  std::vector<double> trkpitch;
101  std::vector<double> trkdqdx;
102  std::vector<double> trkdedx;
103  std::vector<double> trkdedx_cali;
104  std::vector<double> trkresrange;
106  std::vector<double> rdtimestamp_digits;
109 };
110 
111 
113  : EDAnalyzer{p}, // ,
114  // More initializers here.
115  CalibrationPars(p.get<fhicl::ParameterSet>("CalorimetryParameters")),
116  fCalorimetryModuleLabel (p.get<std::string>("CalorimetryModuleLabel"))
117 {
119 }
120 
122 {
123  // Implementation of required member function here.
124  run = e.run();
125  event = e.id().event();
126 
127  rdtimestamp_evt = -1;
128  rdtimestamp_digits.clear();
129 
130  //Services
133  //Space charge service
134  auto const* SCE = lar::providerFrom<spacecharge::SpaceChargeService>();
135 
136  // Reconstruciton information
137  std::vector < art::Ptr < recob::Track > > trackList;
138  auto trackListHandle = e.getHandle < std::vector < recob::Track > >("pandoraTrack");
139  if (trackListHandle) {
140  art::fill_ptr_vector(trackList, trackListHandle);
141  }
142  else return;
143 
144  //Get hits associated with track
145  art::FindManyP < recob::Hit > hitsFromTrack(trackListHandle, e, "pandoraTrack");
146 
147  //Get PFParticles
148  auto pfpListHandle = e.getHandle< std::vector<recob::PFParticle> >("pandora");
149 
150  //Get Track-PFParticle association
151  art::FindManyP<recob::PFParticle> fmpfp(trackListHandle, e, "pandoraTrack");
152 
153  //Get Pandora T0-PFParticle association
154  art::FindManyP<anab::T0> fmt0pandora(pfpListHandle, e, "pandora");
155 
156  //Get Anode Piercing T0
157  art::FindManyP<anab::T0> fmt0anodepiercer(pfpListHandle, e, "anodepiercerst0");
158 
159  //Get 2-CRT T0
160  art::FindManyP<anab::T0> fmt0crt2(trackListHandle, e, "crtreco");
161  art::FindManyP<anab::CosmicTag> fmctcrt2(trackListHandle, e, "crtreco");
162  art::FindManyP<anab::Calorimetry> fmcal(trackListHandle, e, "pandoracalo");
163 
164 
165  std::vector<art::Ptr<recob::Hit>> hitlist;
166  auto hitListHandle = e.getHandle< std::vector<recob::Hit> >("hitpdune"); // to get information about the hits
167  if (hitListHandle) art::fill_ptr_vector(hitlist, hitListHandle);
168 
169  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(e);
170  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService>()->DataFor(e, clockData);
171 
172  for (auto const& track : trackList){
173 
174 
175  trackid.clear();
176  t0crt2.clear();
177  trackstartx.clear();
178  trackstarty.clear();
179  trackstartz.clear();
180  trackendx.clear();
181  trackendy.clear();
182  trackendz.clear();
183  trackstartx_sce.clear();
184  trackstarty_sce.clear();
185  trackstartz_sce.clear();
186  trackendx_sce.clear();
187  trackendy_sce.clear();
188  trackendz_sce.clear();
189 
190  crt2x0.clear();
191  crt2y0.clear();
192  crt2z0.clear();
193  crt2x1.clear();
194  crt2y1.clear();
195  crt2z1.clear();
196  trkpitch.clear();
197  WireID.clear();
198  TPCID.clear();
199  trkdedx_cali.clear();
200  trkdedx.clear();
201  trkpitch.clear();
202  trkhitx.clear(); trkhity.clear(); trkhitz.clear();
203  int this_trackid = track.key();
204  double this_t0crt2 = -DBL_MAX;
205  double this_t0pandora = -DBL_MAX;
206  double this_t0anodep = -DBL_MAX;
207  double this_t0truth = -DBL_MAX;
208  double this_trackstartx = -DBL_MAX;
209  double this_trackstarty = -DBL_MAX;
210  double this_trackstartz = -DBL_MAX;
211  double this_trackendx = -DBL_MAX;
212  double this_trackendy = -DBL_MAX;
213  double this_trackendz = -DBL_MAX;
214  double this_trackstartx_sce = -DBL_MAX;
215  double this_trackstarty_sce = -DBL_MAX;
216  double this_trackstartz_sce = -DBL_MAX;
217  double this_trackendx_sce = -DBL_MAX;
218  double this_trackendy_sce = -DBL_MAX;
219  double this_trackendz_sce = -DBL_MAX;
220  double this_crt2x0 = -DBL_MAX;
221  double this_crt2y0 = -DBL_MAX;
222  double this_crt2z0 = -DBL_MAX;
223  double this_crt2x1 = -DBL_MAX;
224  double this_crt2y1 = -DBL_MAX;
225  double this_crt2z1 = -DBL_MAX;
226  if (!fmctcrt2.isValid()) continue;
227  if (fmt0crt2.isValid()){
228  auto const& vt0crt2 = fmt0crt2.at(track.key());
229  if (!vt0crt2.empty()) this_t0crt2 = vt0crt2[0]->Time();
230  }
231 
232 
233  if (fmctcrt2.isValid()){
234  auto const& vctcrt2 = fmctcrt2.at(track.key());
235  if (!vctcrt2.empty()){
236  this_crt2x0 = vctcrt2[0]->EndPoint1()[0];
237  this_crt2y0 = vctcrt2[0]->EndPoint1()[1];
238  this_crt2z0 = vctcrt2[0]->EndPoint1()[2];
239  this_crt2x1 = vctcrt2[0]->EndPoint2()[0];
240  this_crt2y1 = vctcrt2[0]->EndPoint2()[1];
241  this_crt2z1 = vctcrt2[0]->EndPoint2()[2];
242  }
243  }
244 
245 
246 
247  if (this_t0crt2 > -DBL_MAX){
248 
249  auto const & allHits = hitsFromTrack.at(track.key());
250 
251 
252  this_trackstartx = track->Vertex().X();
253  this_trackstarty = track->Vertex().Y();
254  this_trackstartz = track->Vertex().Z();
255  this_trackendx = track->End().X();
256  this_trackendy = track->End().Y();
257  this_trackendz = track->End().Z();
258 
259  if (std::abs(this_t0pandora+DBL_MAX)<1e-10){
260  //no pandora t0 found, correct for t0
261  double ticksOffset = 0;
262  if (this_t0crt2 > -DBL_MAX) ticksOffset = this_t0crt2/500.+detProp.GetXTicksOffset(allHits[0]->WireID());
263  else if (this_t0anodep > -DBL_MAX) ticksOffset = this_t0anodep/500.+detProp.GetXTicksOffset(allHits[0]->WireID());
264  double xOffset = detProp.ConvertTicksToX(ticksOffset,allHits[0]->WireID());
265  this_trackstartx -= xOffset;
266  this_trackendx -= xOffset;
267  }
268 
269  auto const & posOffsets = SCE->GetCalPosOffsets(geo::Point_t(this_trackstartx, this_trackstarty, this_trackstartz), allHits[0]->WireID().TPC);
270  std::vector<art::Ptr<anab::Calorimetry>> calos=fmcal.at(track.key());
271 
272 
273  std::vector< float > calis = calibration.GetCalibratedCalorimetry(*track, e, "pandoraTrack", fCalorimetryModuleLabel, 2);
274  double trkHitX, trkHitY, trkHitZ;
275  for(size_t ical = 0; ical<calos.size(); ++ical){
276  if (calos[ical]->PlaneID().Plane!=2) continue;
277 
278  const size_t NHits=calos[ical]->dEdx().size();
279  for(size_t iHit = 0; iHit < NHits; ++iHit){
280 
281  const auto& TrkPos = (calos[ical] -> XYZ()[iHit]);
282 
283  trkdqdx.push_back(calos[ical]->dQdx()[iHit]);
284  size_t tpIndex=(calos[ical]->TpIndices()[iHit]);
285  //trkhitIntegral=hitlist[tpIndex]->Integral();
286  trkdedx_cali.push_back(calis[iHit]);
287  trkdedx.push_back(calos[ical]->dEdx()[iHit]);
288  trkHitX=TrkPos.X();
289  trkHitY=TrkPos.Y();
290  trkHitZ=TrkPos.Z();
291  //std::cout<<"Trk Hit Z:"<<iHit<<','<<trkHitZ<<std::endl;
292  trkpitch.push_back(calos[ical]->TrkPitchVec()[iHit]);
293  trkhitx.push_back(trkHitX);
294  trkhity.push_back(trkHitY);
295  trkhitz.push_back(trkHitZ);
296  WireID.push_back(hitlist[tpIndex]->WireID().Wire);
297  TPCID.push_back(hitlist[tpIndex]->WireID().TPC);
298  }
299 
300  }
301 
302 
303  this_trackstartx_sce = this_trackstartx - posOffsets.X();
304  this_trackstarty_sce = this_trackstarty + posOffsets.Y();
305  this_trackstartz_sce = this_trackstartz + posOffsets.Z();
306  this_trackendx_sce = this_trackendx - posOffsets.X();
307  this_trackendy_sce = this_trackendy + posOffsets.Y();
308  this_trackendz_sce = this_trackendz + posOffsets.Z();
309 
310  trackid.push_back(this_trackid);
311  t0crt2.push_back(this_t0crt2);
312  t0truth.push_back(this_t0truth);
313  trackstartx.push_back(this_trackstartx);
314  trackstarty.push_back(this_trackstarty);
315  trackstartz.push_back(this_trackstartz);
316  trackendx.push_back(this_trackendx);
317  trackendy.push_back(this_trackendy);
318  trackendz.push_back(this_trackendz);
319  trackstartx_sce.push_back(this_trackstartx_sce);
320  trackstarty_sce.push_back(this_trackstarty_sce);
321  trackstartz_sce.push_back(this_trackstartz_sce);
322  trackendx_sce.push_back(this_trackendx_sce);
323  trackendy_sce.push_back(this_trackendy_sce);
324  trackendz_sce.push_back(this_trackendz_sce);
325  crt2x0.push_back(this_crt2x0);
326  crt2y0.push_back(this_crt2y0);
327  crt2z0.push_back(this_crt2z0);
328  crt2x1.push_back(this_crt2x1);
329  crt2y1.push_back(this_crt2y1);
330  crt2z1.push_back(this_crt2z1);
331  if (!trackid.empty()) crtCalo->Fill();
332  }
333  }
334 
335 
336 }
337 
339  art::ServiceHandle<art::TFileService> fileServiceHandle;
340  crtCalo = fileServiceHandle->make<TTree>("crtCalo", "t0 info");
341  crtCalo->Branch("run", &run, "run/I");
342  crtCalo->Branch("event", &event, "event/I");
343  crtCalo->Branch("trackid", &trackid);
344  crtCalo->Branch("t0crt2", &t0crt2);
345  crtCalo->Branch("t0truth", &t0truth);
346  crtCalo->Branch("trackstartx",&trackstartx);
347  crtCalo->Branch("trackstarty",&trackstarty);
348  crtCalo->Branch("trackstartz",&trackstartz);
349  crtCalo->Branch("trackendx",&trackendx);
350  crtCalo->Branch("trackendy",&trackendy);
351  crtCalo->Branch("trackendz",&trackendz);
352  crtCalo->Branch("trackstartx_sce",&trackstartx_sce);
353  crtCalo->Branch("trackstarty_sce",&trackstarty_sce);
354  crtCalo->Branch("trackstartz_sce",&trackstartz_sce);
355  crtCalo->Branch("trackendx_sce",&trackendx_sce);
356  crtCalo->Branch("trackendy_sce",&trackendy_sce);
357  crtCalo->Branch("trackendz_sce",&trackendz_sce);
358  crtCalo->Branch("crt2x0",&crt2x0);
359  crtCalo->Branch("crt2y0",&crt2y0);
360  crtCalo->Branch("crt2z0",&crt2z0);
361  crtCalo->Branch("crt2x1",&crt2x1);
362  crtCalo->Branch("crt2y1",&crt2y1);
363  crtCalo->Branch("crt2z1",&crt2z1);
364 
365 
366  crtCalo->Branch("trkdedx_cali",&trkdedx_cali);
367  crtCalo->Branch("trkdedx",&trkdedx);
368  crtCalo->Branch("trkpitch",&trkpitch);
369  crtCalo->Branch("trkhitx",&trkhitx);
370  crtCalo->Branch("trkhity",&trkhity);
371  crtCalo->Branch("trkhitz",&trkhitz);
372  crtCalo->Branch("TPCID",&TPCID);
373  crtCalo->Branch("WireID",&WireID);
374 }
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
protoana::ProtoDUNECalibration calibration
T abs(T value)
double dEdx(float dqdx, float Efield)
Definition: doAna.cpp:21
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
p
Definition: test.py:223
RunNumber_t run() const
Definition: DataViewImpl.cc:71
Definition of data types for geometry description.
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
CRTMatchTrackCaloAna & operator=(CRTMatchTrackCaloAna const &)=delete
Declaration of signal hit object.
Provides recob::Track data product.
EventNumber_t event() const
Definition: EventID.h:116
Declaration of basic channel signal object.
detail::Node< FrameID, bool > PlaneID
Definition: CRTID.h:125
void analyze(art::Event const &e) override
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
std::vector< float > GetCalibratedCalorimetry(const recob::Track &track, art::Event const &evt, const std::string trackModule, const std::string caloModule, size_t planeID, double negativeZFix=0.)
CRTMatchTrackCaloAna(fhicl::ParameterSet const &p)
EventID id() const
Definition: Event.cc:34
Event finding and building.