All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbndcode/sbndcode/CosmicId/Algs/CosmicIdAlg.cc
Go to the documentation of this file.
1 #include "CosmicIdAlg.h"
2 
4 
5 namespace sbnd{
6 
8 
9  this->reconfigure(config);
10 
11 }
12 
13 
15 
16 }
17 
18 
20 
21 }
22 
23 
24 void CosmicIdAlg::reconfigure(const Config& config){
25 
27  fPandoraLabel = config.PandoraLabel();
31 
41 
51 
55  fMinMergeAngle = config.MinMergeAngle();
56 
57  fvTag = config.FVTagAlg();
58  spTag = config.SPTagAlg();
59  ccTag = config.CCTagAlg();
60  acTag = config.ACTagAlg();
61  chTag = config.CHTagAlg();
62  ctTag = config.CTTagAlg();
63  ptTag = config.PTTagAlg();
64  pnTag = config.PNTagAlg();
65 
66  fBeamTimeMin = config.BeamTimeLimits().BeamTimeMin();
67  fBeamTimeMax = config.BeamTimeLimits().BeamTimeMax();
68 
69  return;
70 }
71 
72 // Change which cuts are run
73 void CosmicIdAlg::SetCuts(bool FV, bool SP, bool Geo, bool CC, bool AC, bool CT, bool CH, bool PT, bool PN){
74 
75  fApplyFiducialCut = FV;
76  fApplyStoppingCut = SP;
77  fApplyGeometryCut = Geo;
79  fApplyApaCrossCut = AC;
80  fApplyCrtTrackCut = CT;
81  fApplyCrtHitCut = CH;
82  fApplyPandoraT0Cut = PT;
84 
85 }
86 
87 // Reset which cuts are run from fhicl parameters
89 
99 
100 }
101 
102 // Run cuts to decide if track looks like a cosmic
103 bool CosmicIdAlg::CosmicId(recob::Track track, const art::Event& event, std::vector<double> t0Tpc0, std::vector<double> t0Tpc1){
104 
105  // Get associations between tracks and hit collections
106  auto tpcTrackHandle = event.getValidHandle<std::vector<recob::Track>>(fTpcTrackModuleLabel);
107  art::FindManyP<recob::Hit> findManyHits(tpcTrackHandle, event, fTpcTrackModuleLabel);
108  std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(track.ID());
109  // Get associations between track and calorimetry collections
110  art::FindManyP<anab::Calorimetry> findManyCalo(tpcTrackHandle, event, fCaloModuleLabel);
111 
112  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(event);
113 
114  // Tag cosmics from pandora T0 associations
116  if(pnTag.PandoraNuScoreCosmicId(track, event)) return true;
117  }
118 
119  // Tag cosmics from pandora T0 associations
120  if(fApplyPandoraT0Cut){
121  if(ptTag.PandoraT0CosmicId(track, event)) return true;
122  }
123 
124  // Tag cosmics which enter and exit the TPC
125  if(fApplyFiducialCut){
126  if(fvTag.FiducialVolumeCosmicId(track)) return true;
127  }
128 
129  // Tag cosmics which enter the TPC and stop
130  if(fApplyStoppingCut){
131  std::vector<art::Ptr<anab::Calorimetry>> calos = findManyCalo.at(track.ID());
132  if(spTag.StoppingParticleCosmicId(track, calos)) return true;
133  }
134 
135  // Tag cosmics in other TPC to beam activity
136  if(fApplyGeometryCut){
137  bool tpc0Flash = CosmicIdUtils::BeamFlash(t0Tpc0, fBeamTimeMin, fBeamTimeMax);
138  bool tpc1Flash = CosmicIdUtils::BeamFlash(t0Tpc1, fBeamTimeMin, fBeamTimeMax);
139  if(geoTag.GeometryCosmicId(track, hits, tpc0Flash, tpc1Flash)) return true;
140  }
141 
142  // Tag cosmics which cross the CPA
143  if(fApplyCpaCrossCut){
144  std::vector<recob::Track> tracks;
145  for(auto const& tpcTrack : (*tpcTrackHandle)){
146  tracks.push_back(tpcTrack);
147  }
148 
149  if(ccTag.CpaCrossCosmicId(detProp, track, tracks, findManyHits)) return true;
150  }
151 
152  // Tag cosmics which cross the APA
153  if(fApplyApaCrossCut){
154  if(acTag.ApaCrossCosmicId(detProp, track, hits, t0Tpc0, t0Tpc1)) return true;
155  }
156 
157  // Tag cosmics which match CRT tracks
158  if(fApplyCrtTrackCut){
159  auto crtTrackHandle = event.getValidHandle<std::vector<sbn::crt::CRTTrack>>(fCrtTrackModuleLabel);
160  std::vector<sbn::crt::CRTTrack> crtTracks = (*crtTrackHandle);
161 
162  if(ctTag.CrtTrackCosmicId(detProp, track, crtTracks, event)) return true;
163  }
164 
165  // Tag cosmics which match CRT hits
166  if(fApplyCrtHitCut){
167  auto crtHitHandle = event.getValidHandle<std::vector<sbn::crt::CRTHit>>(fCrtHitModuleLabel);
168  std::vector<sbn::crt::CRTHit> crtHits = (*crtHitHandle);
169 
170  if(chTag.CrtHitCosmicId(detProp, track, crtHits, event)) return true;
171  }
172 
173  return false;
174 
175 }
176 
177 // Run cuts to decide if PFParticle looks like a cosmic
179  recob::PFParticle pfparticle, std::map< size_t, art::Ptr<recob::PFParticle> > pfParticleMap, const art::Event& event, std::vector<double> t0Tpc0, std::vector<double> t0Tpc1){
180 
181  // Get associations between pfparticles and tracks
182  art::Handle< std::vector<recob::PFParticle> > pfParticleHandle;
183  event.getByLabel(fPandoraLabel, pfParticleHandle);
184  art::FindManyP< recob::Track > pfPartToTrackAssoc(pfParticleHandle, event, fTpcTrackModuleLabel);
185 
186  // Get associations between tracks and hits/calorimetry collections
187  auto tpcTrackHandle = event.getValidHandle<std::vector<recob::Track>>(fTpcTrackModuleLabel);
188  art::FindManyP<recob::Hit> findManyHits(tpcTrackHandle, event, fTpcTrackModuleLabel);
189  art::FindManyP<anab::Calorimetry> findManyCalo(tpcTrackHandle, event, fCaloModuleLabel);
190 
191  // Loop over all the daughters of the PFParticles and get associated tracks
192  std::vector<recob::Track> nuTracks;
193  for (const size_t daughterId : pfparticle.Daughters()){
194 
195  // Get tracks associated with daughter
196  art::Ptr<recob::PFParticle> pParticle = pfParticleMap.at(daughterId);
197  const std::vector< art::Ptr<recob::Track> > associatedTracks(pfPartToTrackAssoc.at(pParticle.key()));
198  if(associatedTracks.size() != 1) continue;
199 
200  recob::Track track = *associatedTracks.front();
201  nuTracks.push_back(track);
202 
203 
204  }
205 
206  // Tag cosmics from pandora MVA score
208  if(pnTag.PandoraNuScoreCosmicId(pfparticle, pfParticleMap, event)) return true;
209  }
210 
211  // Tag cosmics from pandora T0 associations
212  if(fApplyPandoraT0Cut){
213  if(ptTag.PandoraT0CosmicId(pfparticle, pfParticleMap, event)) return true;
214  }
215 
216  // Not a cosmic if there are only showers assiciated with PFParticle
217  if(nuTracks.size() == 0) return false;
218 
219  // Sort all daughter tracks by length
220  std::sort(nuTracks.begin(), nuTracks.end(), [](auto& left, auto& right){
221  return left.Length() > right.Length();});
222 
223  // Select longest track as the cosmic candidate
224  recob::Track track = nuTracks[0];
225  std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(track.ID());
226 
227  // Tag cosmics which enter and exit the TPC
228  if(fApplyFiducialCut){
229  if(fvTag.FiducialVolumeCosmicId(track)) return true;
230  }
231 
232  // Tag cosmics in other TPC to beam activity
233  if(fApplyGeometryCut){
234  bool tpc0Flash = CosmicIdUtils::BeamFlash(t0Tpc0, fBeamTimeMin, fBeamTimeMax);
235  bool tpc1Flash = CosmicIdUtils::BeamFlash(t0Tpc1, fBeamTimeMin, fBeamTimeMax);
236  if(geoTag.GeometryCosmicId(track, hits, tpc0Flash, tpc1Flash)) return true;
237  }
238 
239  // Tag cosmics which match CRT tracks
240  if(fApplyCrtTrackCut){
241  auto crtTrackHandle = event.getValidHandle<std::vector<sbn::crt::CRTTrack>>(fCrtTrackModuleLabel);
242  std::vector<sbn::crt::CRTTrack> crtTracks = (*crtTrackHandle);
243 
244  if(ctTag.CrtTrackCosmicId(detProp, track, crtTracks, event)) return true;
245  }
246 
247  // Tag cosmics which cross the CPA
248  if(fApplyCpaCrossCut){
249  std::vector<recob::Track> tracks;
250  for(auto const& tpcTrack : (*tpcTrackHandle)){
251  tracks.push_back(tpcTrack);
252  }
253 
254  if(ccTag.CpaCrossCosmicId(detProp, track, tracks, findManyHits)) return true;
255  }
256 
257  // Find second longest particle if trying to merge tracks
258  std::vector<std::pair<recob::Track, double>> secondaryTracks;
259  if(fUseTrackAngleVeto && nuTracks.size() > 1){
260  TVector3 start = track.Vertex<TVector3>();
261  TVector3 end = track.End<TVector3>();
262 
263  // Loop over the secondary tracks
264  // Find smallest angle between primary track and any secondary tracks above a certain length
265  for(size_t i = 1; i < nuTracks.size(); i++){
266  recob::Track track2 = nuTracks[i];
267  // Only consider secondary tracks longer than some limit (try to exclude michel electrons)
268  if(track2.Length() < fMinSecondTrackLength) continue;
269  TVector3 start2 = track2.Vertex<TVector3>();
270  TVector3 end2 = track2.End<TVector3>();
271  // Do they share the same vertex? (no delta rays)
272  if((start-start2).Mag() < fMinVertexDistance){
273  double angle = (end - start).Angle(end2 - start2);
274  secondaryTracks.push_back(std::make_pair(track2, angle));
275  }
276  }
277  }
278 
279  // If there is a valid secondary track
280  if(secondaryTracks.size() > 0){
281  // Sort tracks by smallest angle
282  std::sort(secondaryTracks.begin(), secondaryTracks.end(), [](auto& left, auto& right){
283  return left.second < right.second;});
284  // If secondary track angle is compatible with split track (near 180) then try to merge
285  if(secondaryTracks[0].second > fMinMergeAngle){
286  recob::Track track2 = secondaryTracks[0].first;
287 
288  // Check fiducial volume containment assuming merged track
289  if(fApplyFiducialCut){
290  if(!fvTag.InFiducial(track.End()) && !fvTag.InFiducial(track2.End())) return true;
291  }
292 
293  // Check if stopping applies to merged track
294  if(fApplyStoppingCut){
295  // Apply stopping cut to the longest track
296  std::vector<art::Ptr<anab::Calorimetry>> calos = findManyCalo.at(track.ID());
297  if(spTag.StoppingParticleCosmicId(track, calos)) return true;
298  // Apply stopping cut assuming the tracks are split
299  std::vector<art::Ptr<anab::Calorimetry>> calos2 = findManyCalo.at(track2.ID());
300  if(spTag.StoppingParticleCosmicId(track, track2, calos, calos2)) return true;
301  }
302 
303  // Check if either track crosses APA
304  if(fApplyApaCrossCut){
305  // Apply apa crossing cut to the longest track
306  if(acTag.ApaCrossCosmicId(detProp, track, hits, t0Tpc0, t0Tpc1)) return true;
307  // Also apply to secondary track FIXME need to check primary track doesn't go out of bounds
308  std::vector<art::Ptr<recob::Hit>> hits2 = findManyHits.at(track2.ID());
309  if(acTag.ApaCrossCosmicId(detProp, track2, hits2, t0Tpc0, t0Tpc1)) return true;
310  }
311 
312  // Check if either track matches CRT hit
313  if(fApplyCrtHitCut){
314  // Apply crt hit match cut to both tracks
315  auto crtHitHandle = event.getValidHandle<std::vector<sbn::crt::CRTHit>>(fCrtHitModuleLabel);
316  std::vector<sbn::crt::CRTHit> crtHits = (*crtHitHandle);
317  if(chTag.CrtHitCosmicId(detProp, track, crtHits, event)) return true;
318  if(chTag.CrtHitCosmicId(detProp, track2, crtHits, event)) return true;
319  }
320  }
321  // Don't apply other cuts if angle between tracks is consistent with neutrino interaction
322  }
323 
324  // If there's only one track from vertex then apply cuts as normal
325  if(secondaryTracks.size() == 0){
326 
327  // Tag cosmics which enter the TPC and stop
328  if(fApplyStoppingCut){
329  std::vector<art::Ptr<anab::Calorimetry>> calos = findManyCalo.at(track.ID());
330  if(spTag.StoppingParticleCosmicId(track, calos)) return true;
331  }
332 
333  // Tag cosmics which cross the APA
334  if(fApplyApaCrossCut){
335  if(acTag.ApaCrossCosmicId(detProp, track, hits, t0Tpc0, t0Tpc1)) return true;
336  }
337 
338  // Tag cosmics which match CRT hits
339  if(fApplyCrtHitCut){
340  auto crtHitHandle = event.getValidHandle<std::vector<sbn::crt::CRTHit>>(fCrtHitModuleLabel);
341  std::vector<sbn::crt::CRTHit> crtHits = (*crtHitHandle);
342 
343  if(chTag.CrtHitCosmicId(detProp, track, crtHits, event)) return true;
344  }
345  }
346 
347  return false;
348 
349 }
350 
351 
352 }
const std::vector< size_t > & Daughters() const
Returns the collection of daughter particles.
Definition: PFParticle.h:114
fhicl::Table< CpaCrossCosmicIdAlg::Config > CCTagAlg
ClusterModuleLabel join with tracks
bool BeamFlash(std::vector< double > flashes, double beamTimeMin, double beamTimeMax)
bool CpaCrossCosmicId(detinfo::DetectorPropertiesData const &detProp, recob::Track track, std::vector< recob::Track > tracks, art::FindManyP< recob::Hit > hitAssoc)
walls no right
Definition: selectors.fcl:105
bool PandoraT0CosmicId(recob::Track track, const art::Event &event)
fhicl::Table< PandoraT0CosmicIdAlg::Config > PTTagAlg
fhicl::Table< FiducialVolumeCosmicIdAlg::Config > FVTagAlg
bool CosmicId(recob::Track track, const art::Event &event, std::vector< double > t0Tpc0, std::vector< double > t0Tpc1)
process_name use argoneut_mc_hitfinder track
fhicl::Table< CrtTrackCosmicIdAlg::Config > CTTagAlg
double Length(size_t p=0) const
Access to various track properties.
bool StoppingParticleCosmicId(recob::Track track, std::vector< art::Ptr< anab::Calorimetry >> calos)
bool CrtTrackCosmicId(detinfo::DetectorPropertiesData const &detProp, recob::Track track, std::vector< sbn::crt::CRTTrack > crtTracks, const art::Event &event)
Point_t const & Vertex() const
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
fhicl::Table< CrtHitCosmicIdAlg::Config > CHTagAlg
walls no left
Definition: selectors.fcl:105
bool CrtHitCosmicId(detinfo::DetectorPropertiesData const &detProp, recob::Track track, std::vector< sbn::crt::CRTHit > crtHits, const art::Event &event)
bool GeometryCosmicId(recob::Track track, std::vector< art::Ptr< recob::Hit >> hits, bool tpc0Flash, bool tpc1Flash)
fhicl::Table< StoppingParticleCosmicIdAlg::Config > SPTagAlg
process_name showerreco Particles Coinciding wih the Vertex services ScanOptions nu_mu CC
fhicl::Table< ApaCrossCosmicIdAlg::Config > ACTagAlg
Hierarchical representation of particle flow.
Definition: PFParticle.h:44
bool PandoraNuScoreCosmicId(recob::Track track, const art::Event &event)
fhicl::Table< PandoraNuScoreCosmicIdAlg::Config > PNTagAlg
Point_t const & End() const
bool ApaCrossCosmicId(detinfo::DetectorPropertiesData const &detProp, recob::Track track, std::vector< art::Ptr< recob::Hit >> hits, std::vector< double > t0Tpc0, std::vector< double > t0Tpc1)
stream1 can override from command line with o or output services user sbnd
finds tracks best matching by angle
auto const detProp
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track:
void SetCuts(bool FV, bool SP, bool Geo, bool CC, bool AC, bool CT, bool CH, bool PT, bool PN)