All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CosmicTrackTagger_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CosmicTagger
3 // Module Type: producer
4 // File: CosmicTrackTagger_module.cc
5 //
6 // Generated at Mon Sep 24 18:21:00 2012 by Sarah Lockwitz using artmod
7 // from art v1_02_02.
8 // artmod -e beginJob -e reconfigure -e endJob producer trkf::CosmicTrackTagger
9 ////////////////////////////////////////////////////////////////////////
10 
11 #include "art/Framework/Core/EDProducer.h"
12 #include "art/Framework/Core/ModuleMacros.h"
13 #include "art/Framework/Principal/Event.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "canvas/Persistency/Common/FindManyP.h"
16 
17 #include <iostream>
18 
23 
28 
29 #include "TStopwatch.h"
30 
31 namespace cosmic {
32  class CosmicTrackTagger;
33 }
34 
35 class cosmic::CosmicTrackTagger : public art::EDProducer {
36 public:
37  explicit CosmicTrackTagger(fhicl::ParameterSet const& p);
38 
39  void produce(art::Event& e) override;
40 
41 private:
42  std::string fTrackModuleLabel;
48 };
49 
50 cosmic::CosmicTrackTagger::CosmicTrackTagger(fhicl::ParameterSet const& p) : EDProducer{p}
51 {
52  auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
53  auto const detp =
54  art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataForJob(clock_data);
55  auto const* geo = lar::providerFrom<geo::Geometry>();
56 
57  fDetHalfHeight = geo->DetHalfHeight();
58  fDetWidth = 2. * geo->DetHalfWidth();
59  fDetLength = geo->DetLength();
60 
61  float fSamplingRate = sampling_rate(clock_data);
62 
63  fTrackModuleLabel = p.get<std::string>("TrackModuleLabel", "track");
64  fEndTickPadding = p.get<int>("EndTickPadding", 50);
65 
66  fTPCXBoundary = p.get<float>("TPCXBoundary", 5);
67  fTPCYBoundary = p.get<float>("TPCYBoundary", 5);
68  fTPCZBoundary = p.get<float>("TPCZBoundary", 5);
69 
70  const double driftVelocity = detp.DriftVelocity(detp.Efield(), detp.Temperature()); // cm/us
71 
72  fDetectorWidthTicks =
73  2 * geo->DetHalfWidth() / (driftVelocity * fSamplingRate / 1000); // ~3200 for uB
74  fMinTickDrift = clock_data.Time2Tick(clock_data.TriggerTime());
75  fMaxTickDrift = fMinTickDrift + fDetectorWidthTicks + fEndTickPadding;
76 
77  produces<std::vector<anab::CosmicTag>>();
78  produces<art::Assns<recob::Track, anab::CosmicTag>>();
79 }
80 
81 void
83 {
84  // Implementation of required member function here.
85 
86  std::unique_ptr<std::vector<anab::CosmicTag>> cosmicTagTrackVector(
87  new std::vector<anab::CosmicTag>);
88  std::unique_ptr<art::Assns<recob::Track, anab::CosmicTag>> assnOutCosmicTagTrack(
89  new art::Assns<recob::Track, anab::CosmicTag>);
90 
91  TStopwatch ts;
92 
93  art::Handle<std::vector<recob::Track>> Trk_h;
94  e.getByLabel(fTrackModuleLabel, Trk_h);
95  std::vector<art::Ptr<recob::Track>> TrkVec;
96  art::fill_ptr_vector(TrkVec, Trk_h);
97 
98  /////////////////////////////////
99  // LOOPING OVER INSPILL TRACKS
100  /////////////////////////////////
101 
102  art::FindManyP<recob::Hit> hitsSpill(Trk_h, e, fTrackModuleLabel);
103 
104  for (unsigned int iTrack = 0; iTrack < Trk_h->size(); iTrack++) {
105 
106  int isCosmic = 0;
108 
109  art::Ptr<recob::Track> tTrack = TrkVec.at(iTrack);
110  std::vector<art::Ptr<recob::Hit>> HitVec = hitsSpill.at(iTrack);
111 
112  if (iTrack != tTrack.key()) { std::cout << "Mismatch in track index/key" << std::endl; }
113 
114  // A BETTER WAY OF FINDING END POINTS:
115  auto tVector1 = tTrack->Vertex();
116  auto tVector2 = tTrack->End();
117 
118  float trackEndPt1_X = tVector1.X();
119  float trackEndPt1_Y = tVector1.Y();
120  float trackEndPt1_Z = tVector1.Z();
121  float trackEndPt2_X = tVector2.X();
122  float trackEndPt2_Y = tVector2.Y();
123  float trackEndPt2_Z = tVector2.Z();
124 
125  if (trackEndPt1_X != trackEndPt1_X || trackEndPt1_Y != trackEndPt1_Y ||
126  trackEndPt1_Z != trackEndPt1_Z || trackEndPt2_X != trackEndPt2_X ||
127  trackEndPt2_Y != trackEndPt2_Y || trackEndPt2_Z != trackEndPt2_Z) {
128  std::cerr << "!!! FOUND A PROBLEM... the length is: " << tTrack->Length()
129  << " np: " << tTrack->NumberTrajectoryPoints() << " id: " << tTrack->ID() << " "
130  << tTrack << std::endl;
131  std::vector<float> tempPt1, tempPt2;
132  tempPt1.push_back(-999);
133  tempPt1.push_back(-999);
134  tempPt1.push_back(-999);
135  tempPt2.push_back(-999);
136  tempPt2.push_back(-999);
137  tempPt2.push_back(-999);
138  cosmicTagTrackVector->emplace_back(tempPt1, tempPt2, -999, tag_id);
139  util::CreateAssn(*this, e, *cosmicTagTrackVector, tTrack, *assnOutCosmicTagTrack);
140  continue; // I don't want to deal with these "tracks"
141  }
142 
143  /////////////////////////////////////
144  // Getting first and last ticks
145  /////////////////////////////////////
146  float tick1 = 9999;
147  float tick2 = -9999;
148  bool dumpMe(false);
149 
150  for (unsigned int p = 0; p < HitVec.size(); p++) {
151  if (dumpMe) {
152  std::cout << "###>> Hit key: " << HitVec[p].key()
153  << ", peak - RMS: " << HitVec[p]->PeakTimeMinusRMS()
154  << ", peak + RMS: " << HitVec[p]->PeakTimePlusRMS() << std::endl;
155  }
156  if (HitVec[p]->PeakTimeMinusRMS() < tick1) tick1 = HitVec[p]->PeakTimeMinusRMS();
157  if (HitVec[p]->PeakTimePlusRMS() > tick2) tick2 = HitVec[p]->PeakTimePlusRMS();
158  }
159 
160  /////////////////////////////////////////////////////////
161  // Are any of the ticks outside of the ReadOutWindow ?
162  /////////////////////////////////////////////////////////
163  if (tick1 < fMinTickDrift || tick2 > fMaxTickDrift) {
164  isCosmic = 1;
166  }
167 
168  /////////////////////////////////
169  // Now check Y & Z boundaries:
170  /////////////////////////////////
171  int nBdY = 0, nBdZ = 0;
172  if (isCosmic == 0) {
173 
174  // Checking lower side of TPC
175  if (fabs(fDetHalfHeight + trackEndPt1_Y) < fTPCYBoundary ||
176  fabs(fDetHalfHeight + trackEndPt2_Y) < fTPCYBoundary || trackEndPt1_Y < -fDetHalfHeight ||
177  trackEndPt2_Y < -fDetHalfHeight)
178  nBdY++;
179 
180  // Checking upper side of TPC
181  if (fabs(fDetHalfHeight - trackEndPt1_Y) < fTPCYBoundary ||
182  fabs(fDetHalfHeight - trackEndPt2_Y) < fTPCYBoundary || trackEndPt1_Y > fDetHalfHeight ||
183  trackEndPt2_Y > fDetHalfHeight)
184  nBdY++;
185 
186  if (fabs(trackEndPt1_Z - fDetLength) < fTPCZBoundary ||
187  fabs(trackEndPt2_Z - fDetLength) < fTPCZBoundary)
188  nBdZ++;
189  if (fabs(trackEndPt1_Z) < fTPCZBoundary || fabs(trackEndPt2_Z) < fTPCZBoundary) nBdZ++;
190  if ((nBdY + nBdZ) > 1) {
191  isCosmic = 2;
192  if (nBdY > 1)
194  else if (nBdZ > 1)
196  else
198  }
199  else if ((nBdY + nBdZ) == 1) {
200  isCosmic = 3;
201  if (nBdY == 1)
203  else if (nBdZ == 1)
205  }
206  }
207 
208  std::vector<float> endPt1;
209  std::vector<float> endPt2;
210  endPt1.push_back(trackEndPt1_X);
211  endPt1.push_back(trackEndPt1_Y);
212  endPt1.push_back(trackEndPt1_Z);
213  endPt2.push_back(trackEndPt2_X);
214  endPt2.push_back(trackEndPt2_Y);
215  endPt2.push_back(trackEndPt2_Z);
216 
217  float cosmicScore = isCosmic > 0 ? 1 : 0;
218  if (isCosmic == 3) cosmicScore = 0.5;
219 
220  ///////////////////////////////////////////////////////
221  // Doing a very basic check on X boundaries
222  // this gets the types of tracks that go through both X boundaries of the detector
223  if (fabs(trackEndPt1_X - trackEndPt2_X) > fDetWidth - fTPCXBoundary) {
224  cosmicScore = 1;
225  isCosmic = 4;
227  }
228 
229  cosmicTagTrackVector->emplace_back(endPt1, endPt2, cosmicScore, tag_id);
230 
231  util::CreateAssn(*this, e, *cosmicTagTrackVector, tTrack, *assnOutCosmicTagTrack);
232  }
233  // END OF LOOPING OVER INSPILL TRACKS
234 
235  ///////////////////////////////////////////////////////////////////////////////////////////////
236  //////TAGGING DELTA RAYS (and other stub) ASSOCIATED TO A ALREADY TAGGED COSMIC TRACK//////////
237  ///////////////////////////////////////////////////////////////////////////////////////////////
238  float dE = 0, dS = 0, temp = 0, IScore = 0;
239  unsigned int IndexE = 0, iTrk1 = 0, iTrk = 0;
241 
242  for (iTrk = 0; iTrk < Trk_h->size(); iTrk++) {
243  art::Ptr<recob::Track> tTrk = TrkVec.at(iTrk);
244  if ((*cosmicTagTrackVector)[iTrk].CosmicScore() == 0) {
245  auto tStart = tTrk->Vertex();
246  auto tEnd = tTrk->End();
247  unsigned int l = 0;
248  for (iTrk1 = 0; iTrk1 < Trk_h->size(); iTrk1++) {
249  art::Ptr<recob::Track> tTrk1 = TrkVec.at(iTrk1);
250  float getScore = (*cosmicTagTrackVector)[iTrk1].CosmicScore();
251  if (getScore == 1 || getScore == 0.5) {
252  anab::CosmicTagID_t getType = (*cosmicTagTrackVector)[iTrk1].CosmicType();
253  auto tStart1 = tTrk1->Vertex();
254  auto tEnd1 = tTrk1->End();
255  auto NumE = (tEnd - tStart1).Cross(tEnd - tEnd1);
256  auto DenE = tEnd1 - tStart1;
257  dE = NumE.R() / DenE.R();
258  if (l == 0) {
259  temp = dE;
260  IndexE = iTrk1;
261  IScore = getScore;
262  IType = getType;
263  }
264  if (dE < temp) {
265  temp = dE;
266  IndexE = iTrk1;
267  IScore = getScore;
268  IType = getType;
269  }
270  l++;
271  }
272  } //End Trk1 loop
273  art::Ptr<recob::Track> tTrkI = TrkVec.at(IndexE);
274  auto tStartI = tTrkI->Vertex();
275  auto tEndI = tTrkI->End();
276  auto NumS = (tStart - tStartI).Cross(tStart - tEndI);
277  auto DenS = tEndI - tStartI;
278  dS = NumS.R() / DenS.R();
279  if (((dS < 5 && temp < 5) || (dS < temp && dS < 5)) && (tTrk->Length() < 60)) {
280  (*cosmicTagTrackVector)[iTrk].CosmicScore() = IScore - 0.05;
281  (*cosmicTagTrackVector)[iTrk].CosmicType() = IType;
282  }
283  } // end cosmicScore==0 loop
284  } // end iTrk loop
285 
286  e.put(std::move(cosmicTagTrackVector));
287  e.put(std::move(assnOutCosmicTagTrack));
288 
289  TrkVec.clear();
290 
291 } // end of produce
292 
293 DEFINE_ART_MODULE(cosmic::CosmicTrackTagger)
Utilities related to art service access.
BEGIN_PROLOG could also be cerr
enum anab::cosmic_tag_id CosmicTagID_t
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
Provides recob::Track data product.
void produce(art::Event &e) override
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
BEGIN_PROLOG cosmic
do i e
CosmicTrackTagger(fhicl::ParameterSet const &p)
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
art framework interface to geometry description
BEGIN_PROLOG could also be cout