All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RunningT0Tagging_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: RunningT0TaggingAna
3 // Module Type: analyzer
4 // File: RunningT0TaggingAna_module.cc
5 //
6 // Example code for running t0 tagging within an analysis script
7 //
8 // Tom Brooks (tbrooks@fnal.gov)
9 ////////////////////////////////////////////////////////////////////////
10 
11 // sbndcode includes
17 
18 // LArSoft includes
22 #include "nusimdata/SimulationBase/MCParticle.h"
23 
24 // Framework includes
25 #include "art/Framework/Core/EDAnalyzer.h"
26 #include "art/Framework/Principal/Event.h"
27 #include "art/Framework/Principal/Handle.h"
28 #include "art_root_io/TFileService.h"
29 #include "canvas/Persistency/Common/FindManyP.h"
32 
33 // Utility libraries
34 #include "fhiclcpp/ParameterSet.h"
35 #include "fhiclcpp/types/Table.h"
36 #include "fhiclcpp/types/Atom.h"
37 
38 // C++ includes
39 #include <map>
40 #include <vector>
41 
42 namespace sbnd {
43 
44  class RunningT0Tagging : public art::EDAnalyzer {
45  public:
46 
47  // Describes configuration parameters of the module
48  struct Config {
49  using Name = fhicl::Name;
50  using Comment = fhicl::Comment;
51 
52  // One Atom for each parameter
53  fhicl::Atom<art::InputTag> SimModuleLabel {
54  Name("SimModuleLabel"),
55  Comment("tag of detector simulation data product")
56  };
57 
58  fhicl::Atom<art::InputTag> CRTHitLabel {
59  Name("CRTHitLabel"),
60  Comment("tag of CRT hit producer data product")
61  };
62 
63  fhicl::Atom<art::InputTag> CRTTrackLabel {
64  Name("CRTTrackLabel"),
65  Comment("tag of CRT track producer data product")
66  };
67 
68  fhicl::Atom<art::InputTag> TPCTrackLabel {
69  Name("TPCTrackLabel"),
70  Comment("tag of TPC track producer data product")
71  };
72 
73  fhicl::Atom<bool> Verbose {
74  Name("Verbose"),
75  Comment("Print information about what's going on")
76  };
77 
78  fhicl::Table<CRTT0MatchAlg::Config> CRTHitMatch {
79  Name("CRTHitMatch"),
80  };
81 
82  fhicl::Table<CRTTrackMatchAlg::Config> CRTTrackMatch {
83  Name("CRTTrackMatch"),
84  };
85 
86  }; // Config
87 
88  using Parameters = art::EDAnalyzer::Table<Config>;
89 
90  // Constructor: configures module
91  explicit RunningT0Tagging(Parameters const& config);
92 
93  // Called once, at start of the job
94  virtual void beginJob() override;
95 
96  // Called once per event
97  virtual void analyze(const art::Event& event) override;
98 
99  // Called once, at end of the job
100  virtual void endJob() override;
101 
102  private:
103 
104  // fcl file parameters
105  art::InputTag fSimModuleLabel; ///< name of detsim producer
106  art::InputTag fCRTHitLabel; ///< name of CRT hit producer
107  art::InputTag fCRTTrackLabel; ///< name of CRT track producer
108  art::InputTag fTPCTrackLabel; ///< name of TPC track producer
109  bool fVerbose; ///< print information about what's going on
110 
111  CRTT0MatchAlg fCRTHitMatch; ///< CRT hit - TPC track matching algorithms
112  CRTTrackMatchAlg fCRTTrackMatch; ///< CRT track - TPC track matching algorithms
113 
114  // Performance counters
115  int nTotal = 0;
116  int nCrtHitMatched = 0;
117  int nCrtHitCorrect = 0;
120 
121  }; // class RunningT0Tagging
122 
123 
124  // Constructor
126  : EDAnalyzer(config)
127  , fSimModuleLabel (config().SimModuleLabel())
128  , fCRTHitLabel (config().CRTHitLabel())
129  , fCRTTrackLabel (config().CRTTrackLabel())
130  , fTPCTrackLabel (config().TPCTrackLabel())
131  , fVerbose (config().Verbose())
132  , fCRTHitMatch (config().CRTHitMatch())
133  , fCRTTrackMatch (config().CRTTrackMatch())
134  {
135 
136  } //RunningT0Tagging()
137 
138 
140  {
141 
142  // Initial output
143  if(fVerbose) std::cout<<"----------------- Running CRT t0 tagging example -------------------"<<std::endl;
144 
145  } // RunningT0Tagging::beginJob()
146 
147 
148  void RunningT0Tagging::analyze(const art::Event& event)
149  {
150 
151  // Fetch basic event info
152  if(fVerbose){
153  std::cout<<"============================================"<<std::endl
154  <<"Run = "<<event.run()<<", SubRun = "<<event.subRun()<<", Event = "<<event.id().event()<<std::endl
155  <<"============================================"<<std::endl;
156  }
157 
158  //----------------------------------------------------------------------------------------------------------
159  // GETTING PRODUCTS
160  //----------------------------------------------------------------------------------------------------------
161 
162  // Get g4 particles
163  auto particleHandle = event.getValidHandle<std::vector<simb::MCParticle>>(fSimModuleLabel);
164  // Put into a map for easier access
165  std::map<int, simb::MCParticle> particles;
166  for (auto const& particle: (*particleHandle)){
167  int partID = particle.TrackId();
168  particles[partID] = particle;
169  }
170 
171  // Get CRT hits from the event
172  auto crtHitHandle = event.getValidHandle<std::vector<sbn::crt::CRTHit>>(fCRTHitLabel);
173  std::vector<sbn::crt::CRTHit> crtHits;
174  for(auto const& hit : (*crtHitHandle)){
175  crtHits.push_back(hit);
176  }
177 
178  // Get CRT tracks from the event
179  auto crtTrackHandle = event.getValidHandle<std::vector<sbn::crt::CRTTrack>>(fCRTTrackLabel);
180  std::vector<sbn::crt::CRTTrack> crtTracks;
181  for(auto const& track : (*crtTrackHandle)){
182  crtTracks.push_back(track);
183  }
184 
185  // Get reconstructed tracks from the event
186  auto tpcTrackHandle = event.getValidHandle<std::vector<recob::Track>>(fTPCTrackLabel);
187 
188  // Get the associated hits
189  art::FindManyP<recob::Hit> findManyHits(tpcTrackHandle, event, fTPCTrackLabel);
190 
191  //----------------------------------------------------------------------------------------------------------
192  // RUNNING THE T0 TAGGING
193  //----------------------------------------------------------------------------------------------------------
194 
195  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(event);
196  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(event, clockData);
197 
198  // Loop over reconstructed tracks
199  for (auto const& tpcTrack : (*tpcTrackHandle)){
200  // Get the associated hits
201  std::vector<art::Ptr<recob::Hit>> hits = findManyHits.at(tpcTrack.ID());
202 
203  // Get the true particle
204  int trackTrueID = RecoUtils::TrueParticleIDFromTotalRecoHits(clockData, hits, false);
205  if(particles.find(trackTrueID) == particles.end()) continue;
206 
207  // Only consider primary muons
208  if(!(std::abs(particles[trackTrueID].PdgCode()) == 13 && particles[trackTrueID].Mother() == 0)) continue;
209 
210  // Only consider particles inside the reco hit time window
211  double trueTime = particles[trackTrueID].T() * 1e-3; // [us]
212  if(fVerbose) std::cout<<"True track time = "<<trueTime<<" us\n";
213 
214  nTotal++;
215 
216  // Calculate t0 from CRT hit matching
217  double hitT0 = fCRTHitMatch.T0FromCRTHits(detProp, tpcTrack, crtHits, event);
218  if(hitT0 == -99999){
219  if(fVerbose) std::cout<<"Couldn't match to CRT hit.\n";
220  }
221  else{
222  nCrtHitMatched++;
223  if(fVerbose) std::cout<<"Hit matched time = "<<hitT0<<" us\n";
224  if(std::abs(trueTime - hitT0) < 2) nCrtHitCorrect++;
225  }
226  // Calculate t0 from CRT track matching
227  double trackT0 = fCRTTrackMatch.T0FromCRTTracks(detProp, tpcTrack, crtTracks, event);
228  if(trackT0 == -99999){
229  if(fVerbose) std::cout<<"Couldn't match to CRT track.\n";
230  }
231  else{
233  if(fVerbose) std::cout<<"Track matched time = "<<trackT0<<" us\n";
234  if(std::abs(trueTime - trackT0) < 2) nCrtTrackCorrect++;
235  }
236 
237  if(fVerbose) std::cout<<"\n";
238 
239  }
240 
241 
242  } // RunningT0Tagging::analyze()
243 
244 
246 
247  std::cout<<"Running CRT t0 tagging:\n"
248  <<"-- CRT hit matching:\n"
249  <<"---- Efficiency = "<<((double)nCrtHitMatched/nTotal)*100<<"%\n"
250  <<"---- Purity = "<<((double)nCrtHitCorrect/nCrtHitMatched)*100<<"%\n"
251  <<"-- CRT track matching:\n"
252  <<"---- Efficiency = "<<((double)nCrtTrackMatched/nTotal)*100<<"%\n"
253  <<"---- Purity = "<<((double)nCrtTrackCorrect/nCrtTrackMatched)*100<<"%\n";
254 
255  } // RunningT0Tagging::endJob()
256 
257 
258  DEFINE_ART_MODULE(RunningT0Tagging)
259 } // namespace sbnd
fhicl::Atom< art::InputTag > CRTHitLabel
BEGIN_PROLOG Verbose
fhicl::Atom< art::InputTag > TPCTrackLabel
art::EDAnalyzer::Table< Config > Parameters
virtual void analyze(const art::Event &event) override
virtual void beginJob() override
fhicl::Atom< art::InputTag > CRTTrackLabel
Declaration of signal hit object.
process_name use argoneut_mc_hitfinder track
process_name hit
Definition: cheaterreco.fcl:51
fhicl::Table< CRTTrackMatchAlg::Config > CRTTrackMatch
art::InputTag fCRTHitLabel
name of CRT hit producer
fhicl::Atom< art::InputTag > SimModuleLabel
T abs(T value)
RunningT0Tagging(Parameters const &config)
art::InputTag fSimModuleLabel
name of detsim producer
BEGIN_PROLOG vertical distance to the surface Name
int TrueParticleIDFromTotalRecoHits(detinfo::DetectorClocksData const &clockData, const std::vector< art::Ptr< recob::Hit > > &hits, bool rollup_unsaved_ids=1)
CRTT0MatchAlg fCRTHitMatch
CRT hit - TPC track matching algorithms.
BEGIN_PROLOG don t mess with this TPCTrackLabel
Provides recob::Track data product.
CRTTrackMatchAlg fCRTTrackMatch
CRT track - TPC track matching algorithms.
double T0FromCRTHits(detinfo::DetectorPropertiesData const &detProp, recob::Track tpcTrack, std::vector< sbn::crt::CRTHit > crtHits, const art::Event &event)
bool fVerbose
print information about what&#39;s going on
virtual void endJob() override
do i e
stream1 can override from command line with o or output services user sbnd
double T0FromCRTTracks(detinfo::DetectorPropertiesData const &detProp, recob::Track tpcTrack, std::vector< sbn::crt::CRTTrack > crtTracks, const art::Event &event)
art::InputTag fTPCTrackLabel
name of TPC track producer
art::InputTag fCRTTrackLabel
name of CRT track producer
fhicl::Table< CRTT0MatchAlg::Config > CRTHitMatch
BEGIN_PROLOG could also be cout
auto const detProp