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 
27 #include "fhiclcpp/ParameterSet.h"
28 
29 #include "art_root_io/TFileService.h"
30 
31 #include <string>
32 
38 
39 #include "TTree.h"
40 
41 namespace hit {
42  class HitAnaModule;
43 }
44 
46 public:
47  explicit HitAnaModule(fhicl::ParameterSet const& p);
48 
49 private:
50  void analyze(art::Event const& e) override;
51 
52  void beginJob() override;
53 
55 
56  void createAssocVector(HitWireAssns_t const&, std::vector<std::vector<int>>&);
57 
58  void createAssocVector(std::vector<recob::Wire> const&,
59  std::vector<recob::Hit> const&,
60  std::vector<std::vector<int>>&);
61 
62  void createMCAssocVector(std::vector<recob::Wire> const&,
63  std::vector<sim::MCHitCollection> const&,
64  std::vector<std::vector<int>>&);
65 
66  // Declare member data here.
67  std::vector<std::string> fHitModuleLabels;
70 
71  TTree* wireDataTree;
72  std::vector<TTree*> hitDataTree;
73 
75 };
76 
78 // More initializers here.
79 {
80  fHitModuleLabels = p.get<std::vector<std::string>>("HitModuleLabels");
81  fWireModuleLabel = p.get<std::string>("WireModuleLabel");
82  fMCHitModuleLabel = p.get<std::string>("MCHitModuleLabel");
83 }
84 
85 void
87  std::vector<std::vector<int>>& WireHitAssocVector)
88 {
89  // WireHitAssocVector: for each wire, indices of all the hits associated to it
90 
91  // the iteration to art::Assns<Hit, Wire> points to a art::Ptr pair (assn_t)
92  // with a hit as first element ("left") and a wire as the second one ("right")
93  for (HitWireAssns_t::assn_t const& assn : HitToWire)
94  WireHitAssocVector.at(assn.second.key()).push_back(assn.first.key());
95 }
96 
97 void
98 hit::HitAnaModule::createAssocVector(std::vector<recob::Wire> const& wireVector,
99  std::vector<recob::Hit> const& hitVector,
100  std::vector<std::vector<int>>& WireHitAssocVector)
101 {
102  WireHitAssocVector.resize(wireVector.size());
103  for (size_t iwire = 0; iwire < wireVector.size(); iwire++) {
104  for (size_t ihit = 0; ihit < hitVector.size(); ihit++) {
105  if (hitVector[ihit].Channel() == wireVector[iwire].Channel())
106  WireHitAssocVector[iwire].push_back(ihit);
107  }
108  }
109 }
110 
111 void
112 hit::HitAnaModule::createMCAssocVector(std::vector<recob::Wire> const& wireVector,
113  std::vector<sim::MCHitCollection> const& mcHitVector,
114  std::vector<std::vector<int>>& WireMCHitAssocVector)
115 {
116 
117  WireMCHitAssocVector.clear();
118  WireMCHitAssocVector.resize(wireVector.size());
119 
120  //first, store all the MCHitCollection indices in a map keyed on channel
121  //then, loop through wires, and lookup mchitcollections based on the wire's channel
122 
123  std::map<unsigned int, std::vector<int>> mcHitIndicesByChannel;
124  for (unsigned int icol = 0; icol < mcHitVector.size(); icol++)
125  mcHitIndicesByChannel[mcHitVector[icol].Channel()].push_back(icol);
126 
127  for (unsigned int iwire = 0; iwire < wireVector.size(); iwire++)
128  WireMCHitAssocVector[iwire].insert(WireMCHitAssocVector[iwire].end(),
129  mcHitIndicesByChannel[wireVector[iwire].Channel()].begin(),
130  mcHitIndicesByChannel[wireVector[iwire].Channel()].end());
131 }
132 
133 void
135 {
136  //get event and run numbers
137  unsigned int eventNumber = e.id().event();
138  unsigned int runNumber = e.run();
139 
141 
142  // get the data
143  auto const& wireVector = *e.getValidHandle<std::vector<recob::Wire>>(fWireModuleLabel);
144  auto const& mcHitVector = *e.getValidHandle<std::vector<sim::MCHitCollection>>(fMCHitModuleLabel);
145 
146  // make the association vector. First index is wire index, second is
147  // mcHitCollection index
148  std::vector<std::vector<int>> WireMCHitAssocVector;
149  createMCAssocVector(wireVector, mcHitVector, WireMCHitAssocVector);
150 
151  //get the hit data
152  size_t nHitModules = fHitModuleLabels.size();
153  std::vector<art::Handle<std::vector<recob::Hit>>> hitHandles(nHitModules);
154  // for each hit module output (first index), for each wire (second index)
155  // the list of hits associated with that wire is stored
156  std::vector<std::vector<std::vector<int>>> WireHitAssocVectors(nHitModules);
157  for (size_t iter = 0; iter < nHitModules; iter++) {
158 
159  e.getByLabel(fHitModuleLabels[iter], hitHandles[iter]);
160 
161  //create association vectors by hand for now
162  //art::ValidHandle<HitWireAssns_t> HitToWireAssns
163  //= e.getValidHandle<HitWireAssns_t>(fHitModuleLabels[iter]);
164  //WireHitAssocVectors[iter].resize(wireVector.size());
165  //createAssocVector(*HitToWireAssns,WireHitAssocVectors[iter]);
166 
167  WireHitAssocVectors[iter].resize(wireVector.size());
168  art::Handle<HitWireAssns_t> HitToWireAssns;
169  if (e.getByLabel(fHitModuleLabels[iter], HitToWireAssns))
170  createAssocVector(*HitToWireAssns, WireHitAssocVectors[iter]);
171  else
172  createAssocVector(wireVector, *(hitHandles[iter]), WireHitAssocVectors[iter]);
173 
174  //load in this hit/assoc pair
176  *(hitHandles[iter]), WireHitAssocVectors[iter], fHitModuleLabels[iter]);
177  }
178 
179  // get time service
180  auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e);
181 
182  // run the analzyer alg
184  wireVector, mcHitVector, WireMCHitAssocVector, clock_data, eventNumber, runNumber);
185 }
186 
187 void
189 {
191  wireDataTree = tfs->make<TTree>("wireDataTree", "WireDataTree");
193 
194  // The below creates a Tree with one branch - a recob::Hit branch - for each
195  // Hit module specified in the fcl file. So, don't run this module once per Hit
196  // Finder as one normally would. Just run it once and specify all HitFinders.
197  // This was the design for the WireDataTree; we follow it here for the
198  // Hit trees.
199  for (auto const& label : fHitModuleLabels) {
200  std::string firstArg("hitData_");
201  firstArg += label;
202  std::string secArg("HitDataTree_");
203  secArg += label;
204  TTree* intermediateTree = tfs->make<TTree>(firstArg.c_str(), secArg.c_str());
205  hitDataTree.push_back(intermediateTree);
206  }
208 }
209 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void LoadHitAssocPair(std::vector< recob::Hit > const &, std::vector< std::vector< int >> const &, std::string const &)
Definition: HitAnaAlg.cxx:97
void createMCAssocVector(std::vector< recob::Wire > const &, std::vector< sim::MCHitCollection > const &, std::vector< std::vector< int >> &)
void SetHitDataTree(std::vector< TTree * > &trees)
Definition: HitAnaAlg.cxx:34
std::vector< std::string > fHitModuleLabels
void beginJob() override
std::string fWireModuleLabel
std::string string
Definition: nybbler.cc:12
std::vector< TTree * > hitDataTree
struct vector vector
std::string fMCHitModuleLabel
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
void SetWireDataTree(TTree *)
Definition: HitAnaAlg.cxx:27
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
fInnerVessel push_back(Point(-578.400000, 0.000000, 0.000000))
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
T get(std::string const &key) const
Definition: ParameterSet.h:271
details::FindAllP< recob::Hit, recob::Wire > HitToWire
Query object connecting a hit to a wire.
Definition: HitUtils.h:56
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
void AnalyzeWires(std::vector< recob::Wire > const &, std::vector< sim::MCHitCollection > const &, std::vector< std::vector< int >> const &, detinfo::DetectorClocksData const &, unsigned int, unsigned int)
Definition: HitAnaAlg.cxx:109
RunNumber_t run() const
Definition: DataViewImpl.cc:71
Detector simulation of raw signals on wires.
Declaration of signal hit object.
HitAnaModule(fhicl::ParameterSet const &p)
void createAssocVector(HitWireAssns_t const &, std::vector< std::vector< int >> &)
EventNumber_t event() const
Definition: EventID.h:116
Declaration of basic channel signal object.
void analyze(art::Event const &e) override
void ClearHitModules()
Definition: HitAnaAlg.cxx:89
EventID id() const
Definition: Event.cc:34