All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HitAnaModule_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: HitAnaModule
3 // Module Type: analyzer
4 // File: HitAnaModule_module.cc
5 //
6 // Generated at Sun Jun 22 08:40:08 2014 by Wesley Ketchum using artmod
7 // from cetpkgsupport v1_05_04.
8 ////////////////////////////////////////////////////////////////////////
9 
10 /*!
11  * Title: HitAnaModule
12  * Author: wketchum@lanl.gov
13  * Inputs: recob::Wire (calibrated), recob::Hit, Assns<recob::Wire, recob::Hit>
14  * Outputs: validation histograms
15  *
16  * Description:
17  * This module is intended to be yet another hit analyzer module. Its intention is
18  * (1) to compare hit-finding modules against each other, and eventually
19  * (2) to compare those to truth
20  */
21 
22 #include "art/Framework/Core/EDAnalyzer.h"
23 #include "art/Framework/Core/ModuleMacros.h"
24 #include "art/Framework/Principal/Event.h"
25 #include "art/Framework/Principal/Handle.h"
26 #include "art/Framework/Services/Registry/ServiceHandle.h"
27 #include "canvas/Persistency/Common/Assns.h"
28 #include "fhiclcpp/ParameterSet.h"
29 
30 #include "art_root_io/TFileService.h"
31 
32 #include <string>
33 
39 
40 #include "TTree.h"
41 
42 namespace hit {
43  class HitAnaModule;
44 }
45 
46 class hit::HitAnaModule : public art::EDAnalyzer {
47 public:
48  explicit HitAnaModule(fhicl::ParameterSet const& p);
49 
50 private:
51  void analyze(art::Event const& e) override;
52 
53  void beginJob() override;
54 
55  using HitWireAssns_t = art::Assns<recob::Hit, recob::Wire>;
56 
57  void createAssocVector(HitWireAssns_t const&, std::vector<std::vector<int>>&);
58 
59  void createAssocVector(std::vector<recob::Wire> const&,
60  std::vector<recob::Hit> const&,
61  std::vector<std::vector<int>>&);
62 
63  void createMCAssocVector(std::vector<recob::Wire> const&,
64  std::vector<sim::MCHitCollection> const&,
65  std::vector<std::vector<int>>&);
66 
67  // Declare member data here.
68  std::vector<std::string> fHitModuleLabels;
69  std::string fMCHitModuleLabel;
70  std::string fWireModuleLabel;
71 
72  TTree* wireDataTree;
73  std::vector<TTree*> hitDataTree;
74 
76 };
77 
78 hit::HitAnaModule::HitAnaModule(fhicl::ParameterSet const& p) : EDAnalyzer(p) // ,
79 // More initializers here.
80 {
81  fHitModuleLabels = p.get<std::vector<std::string>>("HitModuleLabels");
82  fWireModuleLabel = p.get<std::string>("WireModuleLabel");
83  fMCHitModuleLabel = p.get<std::string>("MCHitModuleLabel");
84 }
85 
86 void
88  std::vector<std::vector<int>>& WireHitAssocVector)
89 {
90  // WireHitAssocVector: for each wire, indices of all the hits associated to it
91 
92  // the iteration to art::Assns<Hit, Wire> points to a art::Ptr pair (assn_t)
93  // with a hit as first element ("left") and a wire as the second one ("right")
94  for (HitWireAssns_t::assn_t const& assn : HitToWire)
95  WireHitAssocVector.at(assn.second.key()).push_back(assn.first.key());
96 }
97 
98 void
99 hit::HitAnaModule::createAssocVector(std::vector<recob::Wire> const& wireVector,
100  std::vector<recob::Hit> const& hitVector,
101  std::vector<std::vector<int>>& WireHitAssocVector)
102 {
103  WireHitAssocVector.resize(wireVector.size());
104  for (size_t iwire = 0; iwire < wireVector.size(); iwire++) {
105  for (size_t ihit = 0; ihit < hitVector.size(); ihit++) {
106  if (hitVector[ihit].Channel() == wireVector[iwire].Channel())
107  WireHitAssocVector[iwire].push_back(ihit);
108  }
109  }
110 }
111 
112 void
113 hit::HitAnaModule::createMCAssocVector(std::vector<recob::Wire> const& wireVector,
114  std::vector<sim::MCHitCollection> const& mcHitVector,
115  std::vector<std::vector<int>>& WireMCHitAssocVector)
116 {
117 
118  WireMCHitAssocVector.clear();
119  WireMCHitAssocVector.resize(wireVector.size());
120 
121  //first, store all the MCHitCollection indices in a map keyed on channel
122  //then, loop through wires, and lookup mchitcollections based on the wire's channel
123 
124  std::map<unsigned int, std::vector<int>> mcHitIndicesByChannel;
125  for (unsigned int icol = 0; icol < mcHitVector.size(); icol++)
126  mcHitIndicesByChannel[mcHitVector[icol].Channel()].push_back(icol);
127 
128  for (unsigned int iwire = 0; iwire < wireVector.size(); iwire++)
129  WireMCHitAssocVector[iwire].insert(WireMCHitAssocVector[iwire].end(),
130  mcHitIndicesByChannel[wireVector[iwire].Channel()].begin(),
131  mcHitIndicesByChannel[wireVector[iwire].Channel()].end());
132 }
133 
134 void
135 hit::HitAnaModule::analyze(art::Event const& e)
136 {
137  //get event and run numbers
138  unsigned int eventNumber = e.id().event();
139  unsigned int runNumber = e.run();
140 
141  analysisAlg.ClearHitModules();
142 
143  // get the data
144  auto const& wireVector = *e.getValidHandle<std::vector<recob::Wire>>(fWireModuleLabel);
145  auto const& mcHitVector = *e.getValidHandle<std::vector<sim::MCHitCollection>>(fMCHitModuleLabel);
146 
147  // make the association vector. First index is wire index, second is
148  // mcHitCollection index
149  std::vector<std::vector<int>> WireMCHitAssocVector;
150  createMCAssocVector(wireVector, mcHitVector, WireMCHitAssocVector);
151 
152  //get the hit data
153  size_t nHitModules = fHitModuleLabels.size();
154  std::vector<art::Handle<std::vector<recob::Hit>>> hitHandles(nHitModules);
155  // for each hit module output (first index), for each wire (second index)
156  // the list of hits associated with that wire is stored
157  std::vector<std::vector<std::vector<int>>> WireHitAssocVectors(nHitModules);
158  for (size_t iter = 0; iter < nHitModules; iter++) {
159 
160  e.getByLabel(fHitModuleLabels[iter], hitHandles[iter]);
161 
162  //create association vectors by hand for now
163  //art::ValidHandle<HitWireAssns_t> HitToWireAssns
164  //= e.getValidHandle<HitWireAssns_t>(fHitModuleLabels[iter]);
165  //WireHitAssocVectors[iter].resize(wireVector.size());
166  //createAssocVector(*HitToWireAssns,WireHitAssocVectors[iter]);
167 
168  WireHitAssocVectors[iter].resize(wireVector.size());
169  art::Handle<HitWireAssns_t> HitToWireAssns;
170  if (e.getByLabel(fHitModuleLabels[iter], HitToWireAssns))
171  createAssocVector(*HitToWireAssns, WireHitAssocVectors[iter]);
172  else
173  createAssocVector(wireVector, *(hitHandles[iter]), WireHitAssocVectors[iter]);
174 
175  //load in this hit/assoc pair
176  analysisAlg.LoadHitAssocPair(
177  *(hitHandles[iter]), WireHitAssocVectors[iter], fHitModuleLabels[iter]);
178  }
179 
180  // get time service
181  auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e);
182 
183  // run the analzyer alg
184  analysisAlg.AnalyzeWires(
185  wireVector, mcHitVector, WireMCHitAssocVector, clock_data, eventNumber, runNumber);
186 }
187 
188 void
190 {
191  art::ServiceHandle<art::TFileService const> tfs;
192  wireDataTree = tfs->make<TTree>("wireDataTree", "WireDataTree");
193  analysisAlg.SetWireDataTree(wireDataTree);
194 
195  // The below creates a Tree with one branch - a recob::Hit branch - for each
196  // Hit module specified in the fcl file. So, don't run this module once per Hit
197  // Finder as one normally would. Just run it once and specify all HitFinders.
198  // This was the design for the WireDataTree; we follow it here for the
199  // Hit trees.
200  for (auto const& label : fHitModuleLabels) {
201  std::string firstArg("hitData_");
202  firstArg += label;
203  std::string secArg("HitDataTree_");
204  secArg += label;
205  TTree* intermediateTree = tfs->make<TTree>(firstArg.c_str(), secArg.c_str());
206  hitDataTree.push_back(intermediateTree);
207  }
208  analysisAlg.SetHitDataTree(hitDataTree);
209 }
210 
211 DEFINE_ART_MODULE(hit::HitAnaModule)
void createMCAssocVector(std::vector< recob::Wire > const &, std::vector< sim::MCHitCollection > const &, std::vector< std::vector< int >> &)
std::vector< std::string > fHitModuleLabels
void beginJob() override
std::string fWireModuleLabel
std::vector< TTree * > hitDataTree
Declaration of signal hit object.
pdgs p
Definition: selectors.fcl:22
std::string fMCHitModuleLabel
process_name hit
Definition: cheaterreco.fcl:51
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
details::FindAllP< recob::Hit, recob::Wire > HitToWire
Query object connecting a hit to a wire.
Definition: HitUtils.h:56
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
art::Assns< recob::Hit, recob::Wire > HitWireAssns_t
HitAnaModule(fhicl::ParameterSet const &p)
void createAssocVector(HitWireAssns_t const &, std::vector< std::vector< int >> &)
do i e
Declaration of basic channel signal object.
void analyze(art::Event const &e) override
art::ServiceHandle< art::TFileService > tfs