All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCHitFinder_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: MCHitFinder
3 // Module Type: producer
4 // File: MCHitFinder_module.cc
5 //
6 // Generated at Tue Jun 24 07:30:08 2014 by Kazuhiro Terao using artmod
7 // from cetpkgsupport v1_05_04.
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "art/Framework/Core/EDProducer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "fhiclcpp/ParameterSet.h"
16 
17 #include <memory>
18 
20 
25 
26 namespace hit {
27 
28  class MCHitFinder;
29 
30  class MCHitFinder : public art::EDProducer {
31  public:
32  explicit MCHitFinder(fhicl::ParameterSet const & p);
33 
34  private:
35  void produce(art::Event & e) override;
36 
37  std::string fLArG4ModuleName;
38  bool fVerbose;
40 
41  };
42 
43 
44  MCHitFinder::MCHitFinder(fhicl::ParameterSet const & p)
45  : EDProducer{p}
46  {
47  fLArG4ModuleName = p.get<std::string>("LArG4ModuleName");
48  fMakeMCWire = p.get<bool>("MakeMCWire");
49  fVerbose = p.get<bool>("Verbose");
50  produces< std::vector< sim::MCHitCollection> >();
51  if(fMakeMCWire)
52  produces< std::vector< sim::MCWireCollection> >();
53  }
54 
55  void MCHitFinder::produce(art::Event & e)
56  {
57 
58  art::ServiceHandle<geo::Geometry const> geo;
59 
60  const unsigned int nch = geo->Nchannels();
61 
62  std::unique_ptr< std::vector<sim::MCHitCollection> > hits_v ( new std::vector<sim::MCHitCollection>() );
63  std::unique_ptr< std::vector<sim::MCWireCollection> > wires_v ( new std::vector<sim::MCWireCollection>() );
64 
65  hits_v->reserve(nch);
66  wires_v->reserve(nch);
67  for(size_t ch=0; ch<nch; ++ch) {
68 
69  hits_v->push_back(sim::MCHitCollection(ch));
70  wires_v->push_back(sim::MCWireCollection(ch));
71 
72  }
73 
74  art::Handle< std::vector<sim::SimChannel> > simchArray;
75  e.getByLabel(fLArG4ModuleName,simchArray);
76  if(!simchArray.isValid())
77  throw cet::exception(__PRETTY_FUNCTION__)
78  << "Did not find sim::SimChannel with a label: " << fLArG4ModuleName.c_str() << std::endl;
79 
80  // Loop over SimChannel
81  for(size_t simch_index=0; simch_index<simchArray->size(); ++simch_index) {
82 
83  const art::Ptr<sim::SimChannel> simch_ptr(simchArray,simch_index);
84 
85  size_t ch = simch_ptr->Channel();
86 
87  if(ch >= hits_v->size())
88 
89  throw cet::exception(__PRETTY_FUNCTION__)
90  << "Channel number " << ch << " exceeds total # of channels: " << nch << std::endl;
91 
92  auto &mchits = hits_v->at(ch);
93  auto &mcwires = wires_v->at(ch);
94 
95  std::map<sim::MCEnDep,sim::MCWire> edep_wire_map;
96  auto tdc_ide_map = simch_ptr->TDCIDEMap();
97 
98  ////////////////////////////////////////////////////////////
99  // Loop over stored data for a particular sim::SimChannel //
100  ////////////////////////////////////////////////////////////
101 
102  //
103  // Read & Convert
104  //
105  if(fVerbose)
106  std::cout<<std::endl<<"Processing Ch: "<<ch<<std::endl;
107 
108  for(auto const& tdc_ide_pair : tdc_ide_map) {
109 
110  auto const& tdc = tdc_ide_pair.first;
111  auto const& ide_v = tdc_ide_pair.second;
112 
113  for(auto const& ide : ide_v) {
114  sim::MCEnDep edep;
115  edep.SetVertex(ide.x,ide.y,ide.z);
116  edep.SetEnergy(ide.energy);
117  edep.SetTrackId(ide.trackID);
118 
119  sim::MCWire wire;
120  wire.SetStartTDC(tdc);
121 
122  auto edep_iter = edep_wire_map.insert(std::make_pair(edep,wire));
123 
124  //auto edep_iter = edep_wire_map.find(edep);
125 
126  auto last_tdc = (edep_iter).first->second.StartTDC() + (edep_iter).first->second.size() - 1;
127 
128  if(fVerbose) {
129 
130  if( edep_iter.second ) std::cout<<std::endl;
131 
132  std::cout<<" Track: "<<ide.trackID
133  <<" Vtx: " <<ide.x << " " << ide.y << " " <<ide.z << " " <<ide.energy
134  << " ... @ TDC = "<<tdc<<" ... "<<ide.numElectrons << std::endl;
135  }
136 
137  if( !(edep_iter.second) ) {
138 
139  if( last_tdc+1 != tdc ) {
140  /*
141  std::cerr
142  << "Found discontinuous TDC! "
143  << " Last (ADC @ TDC): " << (*(edep_iter.first->second.rbegin())) << " @ " << last_tdc
144  << " while current (ADC @ TDC): " << (ide.numElectrons * detp->ElectronsToADC()) << " @ " << tdc
145  << " ... skipping to store!"
146  << std::endl;
147  */
148  continue;
149  }
150  }
151  (edep_iter).first->second.push_back(ide.numElectrons);
152  }// Done looping over IDEs in a particular TDC
153  }// Done looping over TDC
154 
155  //
156  // Store
157  //
158  for(auto const& edep_wire_pair : edep_wire_map) {
159 
160  auto const& edep = edep_wire_pair.first;
161  auto const& wire = edep_wire_pair.second;
162 
163  // Create MCHit
165  float vtx[3] = {float(edep.Vertex()[0]),
166  float(edep.Vertex()[1]),
167  float(edep.Vertex()[2])};
168  hit.SetParticleInfo(vtx, edep.Energy(), edep.TrackId());
169 
170  double max_time = 0;
171  double qsum = 0;
172  double qmax = 0;
173  for(size_t wire_index=0; wire_index < wire.size(); ++wire_index) {
174 
175  auto q = wire.at(wire_index);
176 
177  qsum += q;
178 
179  if( q > qmax) { qmax = q; max_time = wire.StartTDC() + wire_index; }
180 
181  }
182  hit.SetCharge(qsum,qmax);
183  hit.SetTime(max_time,0);
184 
185  mchits.push_back(hit);
186 
187  if(!fMakeMCWire) continue;
188 
189  mcwires.push_back(wire);
190  } // End looping over all MCEnDep-MCWire pairs
191  } // End looping over all SimChannels
192 
193  e.removeCachedProduct(simchArray);
194 
195  std::sort((*hits_v).begin(),(*hits_v).end());
196  e.put(std::move(hits_v));
197 
198  if(fMakeMCWire) {
199  std::sort((*wires_v).begin(),(*wires_v).end());
200  e.put(std::move(wires_v));
201  }
202 
203  }
204 }
205 DEFINE_ART_MODULE(hit::MCHitFinder)
void SetCharge(float qsum, float amp)
Setter function for charge/amplitude.
Definition: MCHit.h:54
void SetEnergy(float e)
Definition: MCDataHolder.h:39
void SetParticleInfo(const float vtx[], const float energy, const int trackId)
Setter function for partile info.
Definition: MCHit.h:64
void SetTime(const float peak, const float width)
Setter function for time.
Definition: MCHit.h:57
pdgs p
Definition: selectors.fcl:22
void SetStartTDC(const unsigned int start)
Setter function for time.
Definition: MCWire.h:42
void produce(art::Event &e) override
void SetTrackId(unsigned int id)
Definition: MCDataHolder.h:41
process_name hit
Definition: cheaterreco.fcl:51
void SetVertex(float x, float y, float z)
Definition: MCDataHolder.h:32
std::string fLArG4ModuleName
object containing MC truth information necessary for making RawDigits and doing back tracking ...
do i e
MCHitFinder(fhicl::ParameterSet const &p)
art framework interface to geometry description
BEGIN_PROLOG could also be cout