LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Calorimetry_module.cc
Go to the documentation of this file.
1 //
3 // Calorimetry class
4 //
5 // maddalena.antonello@lngs.infn.it
6 // ornella.palamara@lngs.infn.it
7 // ART port echurch@fnal.gov
8 // This algorithm is designed to perform the calorimetric reconstruction
9 // of the 3D reconstructed tracks
11 #ifndef CALO_H
12 #define CALO_H
13 
14 
15 extern "C" {
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 }
19 #include <vector>
20 #include <string>
21 #include <math.h>
22 #include <algorithm>
23 #include <iostream>
24 #include <fstream>
25 
30 
38 #include "larevt/CalibrationDBI/Interface/ChannelStatusService.h"
39 #include "larevt/CalibrationDBI/Interface/ChannelStatusProvider.h"
40 #include "lardata/ArtDataHelper/TrackUtils.h" // lar::util::TrackPitchInView()
43 #include "larcorealg/CoreUtils/NumericUtils.h" // util::absDiff()
44 
45 // ROOT includes
46 #include <TROOT.h>
47 #include <TFile.h>
48 #include <TTree.h>
49 #include <TBranch.h>
50 #include <TH1F.h>
51 #include <TH2F.h>
52 #include <TMath.h>
53 #include <TGraph.h>
54 #include <TF1.h>
55 #include <TVector3.h>
56 
57 // Framework includes
62 #include "fhiclcpp/ParameterSet.h"
70 
71 
73 namespace calo {
74 
75  class Calorimetry : public art::EDProducer {
76 
77  public:
78 
79  explicit Calorimetry(fhicl::ParameterSet const& pset);
80  virtual ~Calorimetry();
81 
82  void beginJob();
83  // void endJob();
84 
85  void produce(art::Event& evt);
86 
87  private:
88 
89  void ReadCaloTree();
90 
92  bool EndsOnBoundary(art::Ptr<recob::Track> lar_track);
93 
94  void GetPitch(art::Ptr<recob::Hit> hit, std::vector<double> trkx, std::vector<double> trky, std::vector<double> trkz, std::vector<double> trkw, std::vector<double> trkx0, double *xyz3d, double &pitch, double TickT0);
95 
96  std::string fTrackModuleLabel;
98  std::string fT0ModuleLabel;
99  bool fUseArea;
100  bool fFlipTrack_dQdx; //flip track direction if significant rise of dQ/dx at the track start
102 
103  int fnsps;
104  std::vector<int> fwire;
105  std::vector<double> ftime;
106  std::vector<double> fstime;
107  std::vector<double> fetime;
108  std::vector<double> fMIPs;
109  std::vector<double> fdQdx;
110  std::vector<double> fdEdx;
111  std::vector<double> fResRng;
112  std::vector<float> fpitch;
113  std::vector<TVector3> fXYZ;
114 
115  protected:
116 
117 
118  }; // class Calorimetry
119 
120 }
121 
122 #endif // CALO_H
123 
124 //-------------------------------------------------
126  : fTrackModuleLabel(pset.get< std::string >("TrackModuleLabel") ),
127  fSpacePointModuleLabel (pset.get< std::string >("SpacePointModuleLabel") ),
128  fT0ModuleLabel (pset.get< std::string >("T0ModuleLabel") ),
129  fUseArea(pset.get< bool >("UseArea") ),
130  fFlipTrack_dQdx(pset.get< bool >("FlipTrack_dQdx",true)),
131  caloAlg(pset.get< fhicl::ParameterSet >("CaloAlg"))
132 {
133  produces< std::vector<anab::Calorimetry> >();
134  produces< art::Assns<recob::Track, anab::Calorimetry> >();
135 }
136 
137 //-------------------------------------------------
139 {
140 
141 }
142 
143 //-------------------------------------------------
145 {
146  return;
147 }
148 
149 //------------------------------------------------------------------------------------//
151 {
152  auto const* detprop = lar::providerFrom<detinfo::DetectorPropertiesService>();
153 
154  art::Handle< std::vector<recob::Track> > trackListHandle;
155  std::vector<art::Ptr<recob::Track> > tracklist;
156  if (evt.getByLabel(fTrackModuleLabel,trackListHandle))
157  art::fill_ptr_vector(tracklist, trackListHandle);
158 
159  // Get Geometry
161 
162  // channel quality
163  lariov::ChannelStatusProvider const& channelStatus
165 
166  size_t nplanes = geom->Nplanes();
167 
168  //create anab::Calorimetry objects and make association with recob::Track
169  std::unique_ptr< std::vector<anab::Calorimetry> > calorimetrycol(new std::vector<anab::Calorimetry>);
170  std::unique_ptr< art::Assns<recob::Track, anab::Calorimetry> > assn(new art::Assns<recob::Track, anab::Calorimetry>);
171 
172  //art::FindManyP<recob::SpacePoint> fmsp(trackListHandle, evt, fTrackModuleLabel);
173  art::FindManyP<recob::Hit> fmht(trackListHandle, evt, fTrackModuleLabel);
174  art::FindManyP<recob::Hit, recob::TrackHitMeta> fmthm(trackListHandle, evt, fTrackModuleLabel); //this has more information about hit-track association, only available in PMA for now
175  art::FindManyP<anab::T0> fmt0(trackListHandle, evt, fT0ModuleLabel);
176 
177  for(size_t trkIter = 0; trkIter < tracklist.size(); ++trkIter){
178 
179  decltype(auto) larEnd = tracklist[trkIter]->Trajectory().End();
180 
181  // Some variables for the hit
182  float time; //hit time at maximum
183  float stime; //hit start time
184  float etime; //hit end time
185  uint32_t channel = 0;//channel number
186  unsigned int cstat = 0; //hit cryostat number
187  unsigned int tpc = 0; //hit tpc number
188  unsigned int wire = 0; //hit wire number
189  unsigned int plane = 0; //hit plane number
190 
191  std::vector< art::Ptr<recob::Hit> > allHits = fmht.at(trkIter);
192  double T0 =0;
193  double TickT0 =0;
194  if ( fmt0.isValid() ) {
195  std::vector< art::Ptr<anab::T0> > allT0 = fmt0.at(trkIter);
196  if ( allT0.size() ) T0 = allT0[0]->Time();
197  TickT0 = T0 / detprop->SamplingRate();
198  }
199 
200  std::vector< std::vector<unsigned int> > hits(nplanes);
201 
203  for (size_t ah = 0; ah< allHits.size(); ++ah){
204  hits[allHits[ah]->WireID().Plane].push_back(ah);
205  }
206  //get hits in each plane
207  for (size_t ipl = 0; ipl < nplanes; ++ipl){//loop over all wire planes
208 
209  geo::PlaneID planeID;//(cstat,tpc,ipl);
210 
211  fwire.clear();
212  ftime.clear();
213  fstime.clear();
214  fetime.clear();
215  fMIPs.clear();
216  fdQdx.clear();
217  fdEdx.clear();
218  fpitch.clear();
219  fResRng.clear();
220  fXYZ.clear();
221 
222  float Kin_En = 0.;
223  float Trk_Length = 0.;
224  std::vector<float> vdEdx;
225  std::vector<float> vresRange;
226  std::vector<float> vdQdx;
227  std::vector<float> deadwire; //residual range for dead wires
228  std::vector<TVector3> vXYZ;
229 
230  //range of wire signals
231  unsigned int wire0 = 100000;
232  unsigned int wire1 = 0;
233  double PIDA = 0;
234  int nPIDA = 0;
235 
236  // determine track direction. Fill residual range array
237  bool GoingDS = true;
238  // find the track direction by comparing US and DS charge BB
239  double USChg = 0;
240  double DSChg = 0;
241  // temp array holding distance betweeen space points
242  std::vector<double> spdelta;
243  //int nht = 0; //number of hits
244  fnsps = 0; //number of space points
245  std::vector<double> ChargeBeg;
246  std::stack<double> ChargeEnd;
247 
248  // find track pitch
249  double fTrkPitch = 0;
250  for (size_t itp = 0; itp < tracklist[trkIter]->NumberTrajectoryPoints(); ++itp){
251  const auto& pos = tracklist[trkIter]->LocationAtPoint(itp);
252  const double Position[3] = { pos.X(), pos.Y(), pos.Z() };
253  geo::TPCID tpcid = geom->FindTPCAtPosition ( Position );
254  if (tpcid.isValid) {
255  try{
256  fTrkPitch = lar::util::TrackPitchInView(*tracklist[trkIter], geom->Plane(ipl).View(), itp);
257  }
258  catch( cet::exception &e){
259  mf::LogWarning("Calorimetry") << "caught exception "
260  << e << "\n setting pitch (C) to "
261  << util::kBogusD;
262  fTrkPitch = 0;
263  }
264  break;
265  }
266  }
267 
268  // find the separation between all space points
269  double xx = 0.,yy = 0.,zz = 0.;
270 
271  //save track 3d points
272  std::vector<double> trkx;
273  std::vector<double> trky;
274  std::vector<double> trkz;
275  std::vector<double> trkw;
276  std::vector<double> trkx0;
277  for (size_t i = 0; i<hits[ipl].size(); ++i){
278  //Get space points associated with the hit
279  std::vector< art::Ptr<recob::SpacePoint> > sptv = fmspts.at(hits[ipl][i]);
280  for (size_t j = 0; j < sptv.size(); ++j){
281 
282  double t = allHits[hits[ipl][i]]->PeakTime() - TickT0; // Want T0 here? Otherwise ticks to x is wrong?
283  double x = detprop->ConvertTicksToX(t, allHits[hits[ipl][i]]->WireID().Plane, allHits[hits[ipl][i]]->WireID().TPC, allHits[hits[ipl][i]]->WireID().Cryostat);
284  double w = allHits[hits[ipl][i]]->WireID().Wire;
285  if (TickT0){
286  trkx.push_back(sptv[j]->XYZ()[0]-detprop->ConvertTicksToX(TickT0, allHits[hits[ipl][i]]->WireID().Plane, allHits[hits[ipl][i]]->WireID().TPC, allHits[hits[ipl][i]]->WireID().Cryostat));
287  }
288  else{
289  trkx.push_back(sptv[j]->XYZ()[0]);
290  }
291  trky.push_back(sptv[j]->XYZ()[1]);
292  trkz.push_back(sptv[j]->XYZ()[2]);
293  trkw.push_back(w);
294  trkx0.push_back(x);
295  }
296  }
297  for (size_t ihit = 0; ihit < hits[ipl].size(); ++ihit){//loop over all hits on each wire plane
298 
299  //std::cout<<ihit<<std::endl;
300 
301  if (!planeID.isValid){
302  plane = allHits[hits[ipl][ihit]]->WireID().Plane;
303  tpc = allHits[hits[ipl][ihit]]->WireID().TPC;
304  cstat = allHits[hits[ipl][ihit]]->WireID().Cryostat;
305  planeID.Cryostat = cstat;
306  planeID.TPC = tpc;
307  planeID.Plane = plane;
308  planeID.isValid = true;
309  }
310 
311  wire = allHits[hits[ipl][ihit]]->WireID().Wire;
312  time = allHits[hits[ipl][ihit]]->PeakTime(); // What about here? T0
313  stime = allHits[hits[ipl][ihit]]->PeakTimeMinusRMS();
314  etime = allHits[hits[ipl][ihit]]->PeakTimePlusRMS();
315 
316  double charge = allHits[hits[ipl][ihit]]->PeakAmplitude();
317  if (fUseArea) charge = allHits[hits[ipl][ihit]]->Integral();
318  //get 3d coordinate and track pitch for the current hit
319  //not all hits are associated with space points, the method uses neighboring spacepts to interpolate
320  double xyz3d[3];
321  double pitch;
322  bool fBadhit = false;
323  if (fmthm.isValid()){
324  auto vhit = fmthm.at(trkIter);
325  auto vmeta = fmthm.data(trkIter);
326  for (size_t ii = 0; ii<vhit.size(); ++ii){
327  if (vhit[ii].key() == allHits[hits[ipl][ihit]].key()){
328  if (vmeta[ii]->Index() == std::numeric_limits<int>::max()){
329  fBadhit = true;
330  continue;
331  }
332  if (vmeta[ii]->Index()>=tracklist[trkIter]->NumberTrajectoryPoints()){
333  throw cet::exception("Calorimetry_module.cc") << "Requested track trajectory index "<<vmeta[ii]->Index()<<" exceeds the total number of trajectory points "<<tracklist[trkIter]->NumberTrajectoryPoints()<<" for track index "<<trkIter<<". Something is wrong with the track reconstruction. Please contact tjyang@fnal.gov";
334  }
335  if (!tracklist[trkIter]->HasValidPoint(vmeta[ii]->Index())){
336  fBadhit = true;
337  continue;
338  }
339  double angleToVert = geom->WireAngleToVertical(vhit[ii]->View(), vhit[ii]->WireID().TPC, vhit[ii]->WireID().Cryostat) - 0.5*::util::pi<>();
340  const auto& dir = tracklist[trkIter]->DirectionAtPoint(vmeta[ii]->Index());
341  double cosgamma = std::abs(std::sin(angleToVert)*dir.Y() + std::cos(angleToVert)*dir.Z());
342  if (cosgamma){
343  pitch = geom->WirePitch(0)/cosgamma;
344  }
345  else{
346  pitch = 0;
347  }
348  auto loc = tracklist[trkIter]->LocationAtPoint(vmeta[ii]->Index());
349  xyz3d[0] = loc.X();
350  xyz3d[1] = loc.Y();
351  xyz3d[2] = loc.Z();
352  break;
353  }
354  }
355  }
356  else
357  GetPitch(allHits[hits[ipl][ihit]], trkx, trky, trkz, trkw, trkx0, xyz3d, pitch, TickT0);
358 
359  if (fBadhit) continue;
360  if (xyz3d[2]<-100) continue; //hit not on track
361  if (pitch<=0) pitch = fTrkPitch;
362  if (!pitch) continue;
363 
364  if(fnsps == 0) {
365  xx = xyz3d[0];
366  yy = xyz3d[1];
367  zz = xyz3d[2];
368  spdelta.push_back(0);
369  } else {
370  double dx = xyz3d[0] - xx;
371  double dy = xyz3d[1] - yy;
372  double dz = xyz3d[2] - zz;
373  spdelta.push_back(sqrt(dx*dx + dy*dy + dz*dz));
374  Trk_Length += spdelta.back();
375  xx = xyz3d[0];
376  yy = xyz3d[1];
377  zz = xyz3d[2];
378  }
379 
380  ChargeBeg.push_back(charge);
381  ChargeEnd.push(charge);
382 
383  double MIPs = charge;
384  double dQdx = MIPs/pitch;
385  double dEdx = 0;
386  if (fUseArea) dEdx = caloAlg.dEdx_AREA(allHits[hits[ipl][ihit]], pitch, T0);
387  else dEdx = caloAlg.dEdx_AMP(allHits[hits[ipl][ihit]], pitch, T0);
388 
389  Kin_En = Kin_En + dEdx * pitch;
390 
391  if (allHits[hits[ipl][ihit]]->WireID().Wire < wire0) wire0 = allHits[hits[ipl][ihit]]->WireID().Wire;
392  if (allHits[hits[ipl][ihit]]->WireID().Wire > wire1) wire1 = allHits[hits[ipl][ihit]]->WireID().Wire;
393 
394  fMIPs.push_back(MIPs);
395  fdEdx.push_back(dEdx);
396  fdQdx.push_back(dQdx);
397  fwire.push_back(wire);
398  ftime.push_back(time);
399  fstime.push_back(stime);
400  fetime.push_back(etime);
401  fpitch.push_back(pitch);
402  TVector3 v(xyz3d[0],xyz3d[1],xyz3d[2]);
403  //std::cout << "Adding these positions to v and then fXYZ " << xyz3d[0] << " " << xyz3d[1] << " " << xyz3d[2] << "\n" <<std::endl;
404  fXYZ.push_back(v);
405  ++fnsps;
406  }
407  if (!fnsps){
408  //std::cout << "Adding the aforementioned positions..." << std::endl;
409  calorimetrycol->push_back(anab::Calorimetry(util::kBogusD,
410  vdEdx,
411  vdQdx,
412  vresRange,
413  deadwire,
415  fpitch,
417  planeID));
418  util::CreateAssn(*this, evt, *calorimetrycol, tracklist[trkIter], *assn);
419  continue;
420  }
421  for (int isp = 0; isp<fnsps; ++isp){
422  if (isp>3) break;
423  USChg += ChargeBeg[isp];
424  }
425  int countsp = 0;
426  while (!ChargeEnd.empty()){
427  if (countsp>3) break;
428  DSChg += ChargeEnd.top();
429  ChargeEnd.pop();
430  ++countsp;
431  }
432  // Going DS if charge is higher at the end
433  GoingDS = (DSChg > USChg) || (!fFlipTrack_dQdx);
434  // determine the starting residual range and fill the array
435  fResRng.resize(fnsps);
436  if(GoingDS) {
437  fResRng[fnsps - 1] = spdelta[fnsps - 1] / 2;
438  for(int isp = fnsps - 2; isp > -1; isp--) {
439  fResRng[isp] = fResRng[isp+1] + spdelta[isp+1];
440  }
441  } else {
442  fResRng[0] = spdelta[1] / 2;
443  for(int isp = 1; isp < fnsps; isp++) {
444  fResRng[isp] = fResRng[isp-1] + spdelta[isp];
445  }
446  }
447 
448  LOG_DEBUG("CaloPrtHit") << " pt wire time ResRng MIPs pitch dE/dx Ai X Y Z\n";
449 
450  double Ai = -1;
451  for (int i = 0; i < fnsps; ++i){//loop over all 3D points
452  vresRange.push_back(fResRng[i]);
453  vdEdx.push_back(fdEdx[i]);
454  vdQdx.push_back(fdQdx[i]);
455  vXYZ.push_back(fXYZ[i]);
456  if (i!=0 && i!= fnsps-1){//ignore the first and last point
457  // Calculate PIDA
458  Ai = fdEdx[i] * pow(fResRng[i],0.42);
459  nPIDA++;
460  PIDA += Ai;
461  }
462  LOG_DEBUG("CaloPrtHit") <<std::setw(4)<< trkIter
463  //std::cout<<std::setw(4)<< trkIter
464  <<std::setw(4)<< ipl
465  <<std::setw(4) << i
466  <<std::setw(4) << fwire[i]
467  << std::setw(6) << (int)ftime[i]
468  << std::setiosflags(std::ios::fixed | std::ios::showpoint)
469  << std::setprecision(2)
470  << std::setw(8) << fResRng[i]
471  << std::setprecision(1)
472  << std::setw(8) << fMIPs[i]
473  << std::setprecision(2)
474  << std::setw(8) << fpitch[i]
475  << std::setw(8) << fdEdx[i]
476  << std::setw(8) << Ai
477  << std::setw(8) << fXYZ[i].x()
478  << std::setw(8) << fXYZ[i].y()
479  << std::setw(8) << fXYZ[i].z()
480  << "\n";
481  }//end looping over 3D points
482  if(nPIDA > 0) {
483  PIDA = PIDA / (double)nPIDA;
484  }
485  else {
486  PIDA = -1;
487  }
488  LOG_DEBUG("CaloPrtTrk") << "Plane # "<< ipl
489  << "TrkPitch= "
490  << std::setprecision(2) << fTrkPitch
491  << " nhits= " << fnsps
492  << "\n"
493  << std::setiosflags(std::ios::fixed | std::ios::showpoint)
494  << "Trk Length= " << std::setprecision(1)
495  << Trk_Length << " cm,"
496  << " KE calo= " << std::setprecision(1)
497  << Kin_En << " MeV,"
498  << " PIDA= " << PIDA
499  << "\n";
500 
501  // look for dead wires
502  for (unsigned int iw = wire0; iw<wire1+1; ++iw){
503  plane = allHits[hits[ipl][0]]->WireID().Plane;
504  tpc = allHits[hits[ipl][0]]->WireID().TPC;
505  cstat = allHits[hits[ipl][0]]->WireID().Cryostat;
506  channel = geom->PlaneWireToChannel(plane,iw,tpc,cstat);
507  if (channelStatus.IsBad(channel)){
508  LOG_DEBUG("Calorimetry") << "Found dead wire at Plane = " << plane
509  << " Wire =" << iw;
510  unsigned int closestwire = 0;
511  unsigned int endwire = 0;
512  unsigned int dwire = 100000;
513  double mindis = 100000;
514  double goodresrange = 0;
515  //hitCtr = 0;
516  for (size_t ihit = 0; ihit <hits[ipl].size(); ++ihit){
517  // for(art::PtrVector<recob::Hit>::const_iterator hitIter = hitsV.begin();
518  // hitIter != hitsV.end();
519  // ++hitCtr, hitIter++){
520  channel = allHits[hits[ipl][ihit]]->Channel();
521  if (channelStatus.IsBad(channel)) continue;
522  // grab the space points associated with this hit
523  std::vector< art::Ptr<recob::SpacePoint> > sppv = fmspts.at(hits[ipl][ihit]);
524  if(sppv.size() < 1) continue;
525  // only use the first space point in the collection, really each hit should
526  // only map to 1 space point
527  const recob::Track::Point_t xyz{sppv[0]->XYZ()[0],
528  sppv[0]->XYZ()[1],
529  sppv[0]->XYZ()[2]};
530  double dis1 = (larEnd - xyz).Mag2();
531  if (dis1) dis1 = std::sqrt(dis1);
532  if (dis1 < mindis){
533  endwire = allHits[hits[ipl][ihit]]->WireID().Wire;
534  mindis = dis1;
535  }
536  if (util::absDiff(wire, iw) < dwire){
537  closestwire = allHits[hits[ipl][ihit]]->WireID().Wire;
538  dwire = util::absDiff(allHits[hits[ipl][ihit]]->WireID().Wire, iw);
539  goodresrange = dis1;
540  }
541  }
542  if (closestwire){
543  if (iw < endwire){
544  deadwire.push_back(goodresrange+(int(closestwire)-int(iw))*fTrkPitch);
545  }
546  else{
547  deadwire.push_back(goodresrange+(int(iw)-int(closestwire))*fTrkPitch);
548  }
549  }
550  }
551  }
552  //std::cout << "Adding at the end but still same fXYZ" << std::endl;
553  calorimetrycol->push_back(anab::Calorimetry(Kin_En,
554  vdEdx,
555  vdQdx,
556  vresRange,
557  deadwire,
558  Trk_Length,
559  fpitch,
561  planeID));
562  util::CreateAssn(*this, evt, *calorimetrycol, tracklist[trkIter], *assn);
563 
564  }//end looping over planes
565  }//end looping over tracks
566 
567  evt.put(std::move(calorimetrycol));
568  evt.put(std::move(assn));
569 
570  return;
571 }
572 
573 void calo::Calorimetry::GetPitch(art::Ptr<recob::Hit> hit, std::vector<double> trkx, std::vector<double> trky, std::vector<double> trkz, std::vector<double> trkw, std::vector<double> trkx0, double *xyz3d, double &pitch, double TickT0){
574  //Get 3d coordinates and track pitch for each hit
575  //Find 5 nearest space points and determine xyz and curvature->track pitch
576 
577  //std::cout << "Start of get pitch" << std::endl;
578 
579  // Get services
581  auto const* dp = lar::providerFrom<detinfo::DetectorPropertiesService>();
582 
583  //save distance to each spacepoint sorted by distance
584  std::map<double,size_t> sptmap;
585  //save the sign of distance
586  std::map<size_t, int> sptsignmap;
587 
588  double wire_pitch = geom->WirePitch(0);
589 
590  double t0 = hit->PeakTime() - TickT0;
591  double x0 = dp->ConvertTicksToX(t0, hit->WireID().Plane, hit->WireID().TPC, hit->WireID().Cryostat);
592  double w0 = hit->WireID().Wire;
593 
594  for (size_t i = 0; i<trkx.size(); ++i){
595  double distance = pow((trkw[i]-w0)*wire_pitch,2)+pow(trkx0[i]-x0,2);
596  if (distance>0) distance = sqrt(distance);
597  //std::cout << "Dis " << distance << ", sqaured " << distance*distance << " = " << wire_pitch*wire_pitch <<"("<<trkw[i]<<"-"<<w0<<")^2 + ("<<trkx0[i]<<"-"<<x0<<")^2"<<std::endl;
598  sptmap.insert(std::pair<double,size_t>(distance,i));
599  if (w0-trkw[i]>0) sptsignmap.insert(std::pair<size_t,int>(i,1));
600  else sptsignmap.insert(std::pair<size_t,int>(i,-1));
601  }
602 
603  //x,y,z vs distance
604  std::vector<double> vx;
605  std::vector<double> vy;
606  std::vector<double> vz;
607  std::vector<double> vs;
608 
609  double kx = 0, ky = 0, kz = 0;
610 
611  int np = 0;
612  for (auto isp = sptmap.begin(); isp!=sptmap.end(); isp++){
613 // const double *xyz = new double[3];
614 // xyz = isp->second->XYZ();
615  double xyz[3];
616  xyz[0] = trkx[isp->second];
617  xyz[1] = trky[isp->second];
618  xyz[2] = trkz[isp->second];
619 
620  double distancesign = sptsignmap[isp->second];
621  //std::cout<<np<<" "<<xyz[0]<<" "<<xyz[1]<<" "<<xyz[2]<<" "<<(*isp).first<<std::endl;
622  if (np==0&&isp->first>30){//hit not on track
623  xyz3d[0] = -1000;
624  xyz3d[1] = -1000;
625  xyz3d[2] = -1000;
626  pitch = -1;
627  return;
628  }
629  //std::cout<<np<<" "<<xyz[0]<<" "<<xyz[1]<<" "<<xyz[2]<<" "<<(*isp).first<<" Plane " << hit->WireID().Plane << " TPC " << hit->WireID().TPC << std::endl;
630  if (np<5) {
631  vx.push_back(xyz[0]);
632  vy.push_back(xyz[1]);
633  vz.push_back(xyz[2]);
634  vs.push_back(isp->first*distancesign);
635  }
636  else {
637  break;
638  }
639  np++;
640  //delete [] xyz;
641  }
642  //std::cout<<"np="<<np<<std::endl;
643  if (np>=2){//at least two points
644  //std::cout << "At least 2 points.."<<std::endl;
645  TGraph *xs = new TGraph(np,&vs[0],&vx[0]);
646  //for (int i = 0; i<np; i++) std::cout<<i<<" "<<vs[i]<<" "<<vx[i]<<" "<<vy[i]<<" "<<vz[i]<<std::endl;
647  try{
648  if (np>2){
649  xs->Fit("pol2","Q");
650  }
651  else{
652  xs->Fit("pol1","Q");
653  }
654  TF1 *pol = 0;
655  if (np>2) pol = (TF1*) xs->GetFunction("pol2");
656  else pol = (TF1*) xs->GetFunction("pol1");
657  xyz3d[0] = pol->Eval(0);
658  kx = pol->GetParameter(1);
659  //std::cout<<"X fit "<<xyz3d[0]<<" "<<kx<<std::endl;
660  }
661  catch(...){
662  mf::LogWarning("Calorimetry::GetPitch") <<"Fitter failed";
663  xyz3d[0] = vx[0];
664  }
665  delete xs;
666  TGraph *ys = new TGraph(np,&vs[0],&vy[0]);
667  try{
668  if (np>2){
669  ys->Fit("pol2","Q");
670  }
671  else{
672  ys->Fit("pol1","Q");
673  }
674  TF1 *pol = 0;
675  if (np>2) pol = (TF1*) ys->GetFunction("pol2");
676  else pol = (TF1*) ys->GetFunction("pol1");
677  xyz3d[1] = pol->Eval(0);
678  ky = pol->GetParameter(1);
679  //std::cout<<"Y fit "<<xyz3d[1]<<" "<<ky<<std::endl;
680  }
681  catch(...){
682  mf::LogWarning("Calorimetry::GetPitch") <<"Fitter failed";
683  xyz3d[1] = vy[0];
684  }
685  delete ys;
686  TGraph *zs = new TGraph(np,&vs[0],&vz[0]);
687  try{
688  if (np>2){
689  zs->Fit("pol2","Q");
690  }
691  else{
692  zs->Fit("pol1","Q");
693  }
694  TF1 *pol = 0;
695  if (np>2) pol = (TF1*) zs->GetFunction("pol2");
696  else pol = (TF1*) zs->GetFunction("pol1");
697  xyz3d[2] = pol->Eval(0);
698  kz = pol->GetParameter(1);
699  //std::cout<<"Z fit "<<xyz3d[2]<<" "<<kz<<std::endl;
700  }
701  catch(...){
702  mf::LogWarning("Calorimetry::GetPitch") <<"Fitter failed";
703  xyz3d[2] = vz[0];
704  }
705  delete zs;
706  }
707  else if (np){
708  xyz3d[0] = vx[0];
709  xyz3d[1] = vy[0];
710  xyz3d[2] = vz[0];
711  }
712  else{
713  xyz3d[0] = -1000;
714  xyz3d[1] = -1000;
715  xyz3d[2] = -1000;
716  pitch = -1;
717  return;
718  }
719  pitch = -1;
720  if (kx*kx+ky*ky+kz*kz){
721  double tot = sqrt(kx*kx+ky*ky+kz*kz);
722  kx /= tot;
723  ky /= tot;
724  kz /= tot;
725  //get pitch
726  double wirePitch = geom->WirePitch(hit->WireID().Plane,hit->WireID().TPC,hit->WireID().Cryostat);
727  double angleToVert = geom->Plane(hit->WireID().Plane,hit->WireID().TPC,hit->WireID().Cryostat).Wire(0).ThetaZ(false) - 0.5*TMath::Pi();
728  double cosgamma = TMath::Abs(TMath::Sin(angleToVert)*ky+TMath::Cos(angleToVert)*kz);
729  if (cosgamma>0) pitch = wirePitch/cosgamma;
730 
731  }
732  //std::cout << "At end of get pitch " << xyz3d[0] << " " << xyz3d[1] << " " << xyz3d[2] << " " << x0 << " " << std::endl;
733 }
734 
735 
736 namespace calo{
737 
739 
740 } // end namespace
741 
Float_t x
Definition: compare.C:6
code to link reconstructed objects back to the MC truth information
Calorimetry(fhicl::ParameterSet const &pset)
Functions to help with numbers.
std::vector< double > fdEdx
Double_t xx
Definition: macro.C:12
PlaneGeo const & Plane(unsigned int const p, unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified wire.
void produce(art::Event &evt)
double dEdx_AREA(art::Ptr< recob::Hit > hit, double pitch, double T0=0) const
geo::WireID WireID() const
Initial tdc tick for hit.
Definition: Hit.h:234
Declaration of signal hit object.
The data type to uniquely identify a Plane.
Definition: geo_types.h:250
bool isValid
Whether this ID points to a valid element.
Definition: geo_types.h:129
Class to keep data related to recob::Hit associated with recob::Track.
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:130
STL namespace.
Double_t zz
Definition: plot.C:279
WireID_t Wire
Index of the wire within its plane.
Definition: geo_types.h:313
data_const_reference data(size_type i) const
Definition: FindManyP.h:458
std::vector< TVector3 > fXYZ
geo::Length_t WirePitch(geo::PlaneID const &planeid) const
Returns the distance between two consecutive wires.
std::vector< double > fResRng
double ThetaZ() const
Angle of the wires from positive z axis; .
Definition: PlaneGeo.cxx:728
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
geo::TPCID FindTPCAtPosition(double const worldLoc[3]) const
Returns the ID of the TPC at specified location.
bool BeginsOnBoundary(art::Ptr< recob::Track > lar_track)
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:171
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
Int_t max
Definition: plot.C:27
void hits()
Definition: readHits.C:15
std::string fT0ModuleLabel
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
Provides recob::Track data product.
std::string fSpacePointModuleLabel
parameter set interface
std::vector< double > ftime
std::vector< double > fetime
bool EndsOnBoundary(art::Ptr< recob::Track > lar_track)
void GetPitch(art::Ptr< recob::Hit > hit, std::vector< double > trkx, std::vector< double > trky, std::vector< double > trkz, std::vector< double > trkw, std::vector< double > trkx0, double *xyz3d, double &pitch, double TickT0)
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
std::vector< Point_t > convertCollToPoint(std::vector< Point > const &coll)
Definition: TrackingTypes.h:55
The data type to uniquely identify a TPC.
Definition: geo_types.h:195
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:258
std::vector< double > fMIPs
Detector simulation of raw signals on wires.
constexpr auto absDiff(A const &a, B const &b)
Returns the absolute value of the difference between two values.
Definition: NumericUtils.h:43
CalorimetryAlg caloAlg
Encapsulate the geometry of a wire.
raw::ChannelID_t PlaneWireToChannel(WireID const &wireid) const
Returns the ID of the TPC channel connected to the specified wire.
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:219
Utility object to perform functions of association.
Encapsulate the construction of a single detector plane.
TDirectory * dir
Definition: macro.C:5
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::vector< double > fdQdx
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
tracking::Point_t Point_t
Definition: Track.h:56
std::vector< int > fwire
#define LOG_DEBUG(id)
std::string fTrackModuleLabel
constexpr double kBogusD
obviously bogus double value
double TrackPitchInView(recob::Track const &track, geo::View_t view, size_t trajectory_point=0)
Provides projected wire pitch for the view.
Definition: TrackUtils.cxx:76
std::vector< double > fstime
TCEvent evt
Definition: DataStructs.cxx:5
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:203
std::vector< float > fpitch
Float_t e
Definition: plot.C:34
recob::tracking::Plane Plane
Definition: TrackState.h:17
Float_t w
Definition: plot.C:23
Utility functions to extract information from recob::Track.
double WireAngleToVertical(geo::View_t view, geo::TPCID const &tpcid) const
Returns the angle of the wires in the specified view from vertical.
double dEdx_AMP(art::Ptr< recob::Hit > hit, double pitch, double T0=0) const
calorimetry
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33