LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
HitCheater_module.cc
Go to the documentation of this file.
1 // Class: HitCheater
3 // Module Type: producer
4 // File: HitCheater_module.cc
5 //
6 // Generated at Tue Nov 8 09:41:20 2011 by Brian Rebel using artmod
7 // from art v1_00_02.
9 
10 // C/C++ standard libraries
11 #include <cmath> // std::sqrt()
12 #include <map>
13 #include <string>
14 
22 #include "cetlib_except/exception.h"
24 
26 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t
36 
37 namespace hit {
38  class HitCheater;
39 }
40 
42 public:
43  explicit HitCheater(fhicl::ParameterSet const& p);
44 
45 private:
46  void produce(art::Event& e) override;
47 
48  void FindHitsOnChannel(const sim::SimChannel* sc, std::vector<recob::Hit>& hits, int spill);
49 
50  std::string fG4ModuleLabel;
51  std::string fWireModuleLabel;
52  double fMinCharge;
53  double fElectronsToADC;
57  double fSamplingRate;
60 };
61 
62 //-------------------------------------------------------------------
64 {
65  fG4ModuleLabel = p.get<std::string>("G4ModuleLabel", "largeant");
66  fWireModuleLabel = p.get<std::string>("WireModuleLabel", "caldata");
67  fMinCharge = p.get<double>("MinimumCharge", 5.);
68  fNewHitTDCGap = p.get<int>("NewHitTDCGap", 1);
69 
70  auto const clock_data = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
71  auto const det_prop =
73  fElectronsToADC = det_prop.ElectronsToADC();
74  fSamplingRate = sampling_rate(clock_data);
75  fTriggerOffset = trigger_offset(clock_data);
76 
78  size_t pos = fWireModuleLabel.find(":");
79  if (pos != std::string::npos) {
80  fCalDataProductInstanceName = fWireModuleLabel.substr(pos + 1);
81  fWireModuleLabel = fWireModuleLabel.substr(0, pos);
82  }
83 
84  fReadOutWindowSize = det_prop.ReadOutWindowSize();
85  fNumberTimeSamples = det_prop.NumberTimeSamples();
86 
87  // let HitCollectionCreator declare that we are going to produce
88  // hits and associations with wires and raw digits
89  // (with no particular product label)
91 }
92 
93 //-------------------------------------------------------------------
95 {
96  // this object contains the hit collection
97  // and its associations to wires and raw digits:
99 
100  // Read in the wire List object(s).
103  e.getValidHandle<std::vector<recob::Wire>>(WireInputTag);
104 
105  int whatSpill = 1;
106  if (!fCalDataProductInstanceName.empty()) {
107  if (fCalDataProductInstanceName.find("ost") != std::string::npos)
108  whatSpill = 2;
109  else
110  whatSpill = 0;
111  }
112 
113  // also get the raw digits associated with the wires;
114  // we assume they have been created by the same module as the wires
115  art::FindOneP<raw::RawDigit> WireToRawDigits(wHandle, e, WireInputTag);
116 
117  // make a map of wires to channel numbers
118  std::map<raw::ChannelID_t, art::Ptr<recob::Wire>> wireMap;
119 
120  for (size_t wc = 0; wc < wHandle->size(); ++wc) {
121  art::Ptr<recob::Wire> wire(wHandle, wc);
122  wireMap[wire->Channel()] = wire;
123  }
124 
125  // get the sim::SimChannels out of the event
126  std::vector<const sim::SimChannel*> sccol;
127  e.getView(fG4ModuleLabel, sccol);
128 
129  // find the hits on each channel
130  for (sim::SimChannel const* sc : sccol) {
131  std::vector<recob::Hit> hits_on_channel;
132 
133  FindHitsOnChannel(sc, hits_on_channel, whatSpill);
134 
135  art::Ptr<recob::Wire> const& wire = wireMap[sc->Channel()];
136  art::Ptr<raw::RawDigit> rawdigits; // null by default
137  if (wire.isNonnull()) rawdigits = WireToRawDigits.at(wire.key());
138 
139  // add all the hits found on this channel to the data product,
140  // all associated to the same hit and wire
141  for (recob::Hit& hit : hits_on_channel)
142  hits.emplace_back(std::move(hit), wire, rawdigits);
143 
144  } // end loop over SimChannels
145 
146  // put the cheated hits into the event
147  MF_LOG_DEBUG("HitCheater") << "putting " << hits.size() << " hits into the event";
148  hits.put_into(e);
149 
150  return;
151 }
152 
153 //-------------------------------------------------------------------
155  std::vector<recob::Hit>& hits,
156  int spill)
157 {
159 
160  raw::ChannelID_t channel = sc->Channel();
161  geo::SigType_t signal_type = geo->SignalType(channel);
162  geo::View_t view = geo->View(channel);
163 
164  // determine the possible geo::WireIDs for this particular channel
165  // then make a map of tdc to electrons for each one of those geo::WireIDs
166  // then find hits on each geo::WireID
167  std::vector<geo::WireID> wireids = geo->ChannelToWire(channel);
168 
169  std::map<geo::WireID, std::map<unsigned int, double>> wireIDSignals;
170 
171  auto const& idemap = sc->TDCIDEMap();
172 
173  for (auto const& mapitr : idemap) {
174  unsigned short tdc = mapitr.first;
175 
177  if (tdc < spill * fReadOutWindowSize || tdc > (spill + 1) * fReadOutWindowSize) continue;
178  }
179  else {
180  if (tdc > fReadOutWindowSize) continue;
181  }
182 
183  // figure out which TPC we are in for each voxel
184 
185  for (auto const& ideitr : mapitr.second) {
186 
187  const float edep = ideitr.numElectrons;
188 
189  geo::Point_t const pos{ideitr.x, ideitr.y, ideitr.z};
190 
191  geo::TPCID tpcID = geo->FindTPCAtPosition(pos);
192  if (!tpcID.isValid) {
193  mf::LogWarning("HitCheater")
194  << "TPC for position ( " << pos.X() << " ; " << pos.Y() << " ; " << pos.Z() << " )"
195  << " in no TPC; move on to the next sim::IDE";
196  continue;
197  }
198  const unsigned int tpc = tpcID.TPC;
199  const unsigned int cstat = tpcID.Cryostat;
200 
201  for (auto const& wid : wireids) {
202  if (wid.TPC == tpc && wid.Cryostat == cstat) {
203  // in the right TPC, now figure out which wire we want
204  // this works because there is only one plane option for
205  // each WireID in each TPC
206  if (wid.Wire == geo->NearestWireID(pos, wid).Wire) wireIDSignals[wid][tdc] += edep;
207  } // end if in the correct TPC and Cryostat
208  } // end loop over wireids for this channel
209  } // end loop over ides for this channel
210  } // end loop over tdcs for this channel
211 
212  // now loop over each wire ID and determine where the hits are
213  for (auto const& widitr : wireIDSignals) {
214 
215  // get the first tdc in the
216  unsigned short prev = widitr.second.begin()->first;
217  unsigned short startTime = prev;
218  double totCharge = 0.;
219  double maxCharge = -1.;
220  double peakTime = 0.;
221  int multiplicity = 1;
222  lar::util::StatCollector<double> time; // reduce rounding errors
223 
224  // loop over all the tdcs for this geo::WireID
225  for (auto tdcitr : widitr.second) {
226  unsigned short tdc = tdcitr.first;
227  if (tdc < prev) {
228  throw art::Exception(art::errors::LogicError) << "SimChannel TDCs going backward!";
229  }
230 
231  // more than a one tdc gap between times with
232  // signal, start a new hit
233  if (tdc - prev > fNewHitTDCGap) {
234 
235  if (totCharge > fMinCharge) {
236  hits.emplace_back(channel, // channel
237  (raw::TDCtick_t)startTime, // start_tick
238  (raw::TDCtick_t)prev, // end_tick
239  peakTime, // peak_time
240  1., // sigma_peak_time
241  time.RMS(), // RMS
242  maxCharge, // peak_amplitude
243  std::sqrt(maxCharge), // sigma_peak_amplitude
244  totCharge, // summedADC
245  totCharge, // hit_integral
246  std::sqrt(totCharge), // hit_sigma_integral
247  multiplicity, // multiplicity
248  0, // local_index
249  1., // goodness_of_fit
250  0., // dof
251  view, // view
252  signal_type, // signal_type
253  widitr.first // wireID
254  );
255 
256  MF_LOG_DEBUG("HitCheater") << "new hit is " << hits.back();
257 
258  } // end if charge is large enough
259 
260  // reset the variables for each hit
261  startTime = tdc;
262  peakTime = tdc;
263  totCharge = 0.;
264  maxCharge = -1.;
265  time.clear();
266 
267  } // end if need to start a new hit
268 
269  const double adc = fElectronsToADC * tdcitr.second;
270 
271  totCharge += adc;
272  // use ADC as weight; the shift reduces the precision needed;
273  // average would need to be shifted back: time.Averatge() + startTime
274  // but RMS is not affected
275  time.add(tdc - startTime, adc);
276  if (adc > maxCharge) {
277  maxCharge = adc;
278  peakTime = tdc;
279  }
280 
281  prev = tdc;
282 
283  } // end loop over tdc values for the current geo::WireID
284 
285  // We might have missed the last hit, so do it now
286  if (totCharge > fMinCharge) {
287  hits.emplace_back(channel, // channel
288  (raw::TDCtick_t)startTime, // start_tick
289  (raw::TDCtick_t)prev + 1, // end_tick; prev included in the hit
290  peakTime, // peak_time
291  1., // sigma_peak_time
292  time.RMS(), // RMS
293  maxCharge, // peak_amplitude
294  std::sqrt(maxCharge), // sigma_peak_amplitude
295  totCharge, // summedADC
296  totCharge, // hit_integral
297  std::sqrt(totCharge), // hit_sigma_integral
298  multiplicity, // multiplicity
299  0, // local_index
300  1., // goodness_of_fit
301  0., // dof
302  view, // view
303  signal_type, // signal_type
304  widitr.first // wireID
305  );
306 
307  MF_LOG_DEBUG("HitCheater") << "last hit is " << hits.back();
308 
309  } // end if charge is large enough
310 
311  } // end loop over map of geo::WireID to map<tdc,electrons>
312 
313  return;
314 }
315 
int fTriggerOffset
from detinfo::DetectorPropertiesService
double fSamplingRate
from detinfo::DetectorPropertiesService
std::string fWireModuleLabel
label name for module making recob::Wires
Energy deposited on a readout channel by simulated tracks.
Definition: SimChannel.h:136
std::vector< WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
int fNumberTimeSamples
Number of total time samples (N*readoutwindowsize)
int fNewHitTDCGap
gap allowed in tdcs without charge before making a new hit
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
void FindHitsOnChannel(const sim::SimChannel *sc, std::vector< recob::Hit > &hits, int spill)
Declaration of signal hit object.
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
bool isValid
Whether this ID points to a valid element.
Definition: geo_types.h:210
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:211
Definition of basic raw digits.
WireID_t Wire
Index of the wire within its plane.
Definition: geo_types.h:563
std::string fCalDataProductInstanceName
label name for module making recob::Wires
Classes gathering simple statistics.
Weight_t RMS() const
Returns the root mean square.
double fMinCharge
Minimum charge required to make a hit.
static void declare_products(art::ProducesCollector &collector, std::string instance_name="", bool doWireAssns=true, bool doRawDigitAssns=true)
Declares the hit products we are going to fill.
Definition: HitCreator.cxx:248
int TDCtick_t
Type representing a TDC tick.
Definition: RawTypes.h:25
Helper functions to create a hit.
void hits()
Definition: readHits.C:15
TPCID FindTPCAtPosition(Point_t const &point) const
Returns the ID of the TPC at specified location.
A class handling a collection of hits and its associations.
Definition: HitCreator.h:481
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
std::string fG4ModuleLabel
label name for module making sim::SimChannels
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
Definition: Wire.h:223
enum geo::_plane_sigtype SigType_t
Enumerate the possible plane projections.
key_type key() const noexcept
Definition: Ptr.h:166
void emplace_back(recob::Hit &&hit, art::Ptr< recob::Wire > const &wire=art::Ptr< recob::Wire >(), art::Ptr< raw::RawDigit > const &digits=art::Ptr< raw::RawDigit >())
Adds the specified hit to the data collection.
Definition: HitCreator.cxx:286
The data type to uniquely identify a TPC.
Definition: geo_types.h:381
Double_t edep
Definition: macro.C:13
Definition of data types for geometry description.
void put_into(art::Event &)
Moves the data into an event.
Definition: HitCreator.h:614
Detector simulation of raw signals on wires.
ProducesCollector & producesCollector() noexcept
std::size_t getView(std::string const &moduleLabel, std::string const &productInstanceName, std::string const &processName, std::vector< ELEMENT const * > &result) const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
raw::ChannelID_t Channel() const
Returns the readout channel this object describes.
Definition: SimChannel.h:323
bool isNonnull() const noexcept
Definition: Ptr.h:204
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:180
size_t size() const
Returns the number of hits currently in the collection.
Definition: HitCreator.h:597
SigType_t SignalType(PlaneID const &pid) const
Returns the type of signal on the channels of specified TPC plane.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
WireID NearestWireID(Point_t const &point, PlaneID const &planeid) const
Returns the ID of wire closest to position in the specified TPC.
Float_t sc
Definition: plot.C:23
int fReadOutWindowSize
Number of samples in a readout window; NOT total samples.
View_t View(PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
#define MF_LOG_DEBUG(id)
object containing MC truth information necessary for making RawDigits and doing back tracking ...
void produce(art::Event &e) override
int trigger_offset(DetectorClocksData const &data)
HitCheater(fhicl::ParameterSet const &p)
Declaration of basic channel signal object.
TDCIDEs_t const & TDCIDEMap() const
Returns all the deposited energy information as stored.
Definition: SimChannel.h:319
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:46
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
double fElectronsToADC
Conversion factor of electrons to ADC counts.
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:399
Float_t e
Definition: plot.C:35
void clear()
Clears all the statistics.
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
Namespace collecting geometry-related classes utilities.
Collects statistics on a single quantity (weighted)
Definition: fwd.h:26
art framework interface to geometry description
void add(Data_t value, Weight_t weight=Weight_t(1.0))
Adds one entry with specified value and weight.