LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
RawWaveformDump_module.cc
Go to the documentation of this file.
1 //
3 // Module to dump raw data for ROI training
4 //
5 // mwang@fnal.gov
6 // tjyang@fnal.gov
7 // wwu@fnal.gov
8 //
9 // This version:
10 // a) uses RawDigit or WireProducer
11 // b) saves full waveform or short waveform
12 // - for short waveform, picks signal
13 // associated with largest energy
14 // deposit & saves only tdcmin/tdcmaxes
15 // that are within window extents
16 //
18 
19 // Framework includes
30 #include "cetlib_except/exception.h"
31 #include "fhiclcpp/ParameterSet.h"
33 
34 // LArSoft libraries
37 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t
41 #include "lardataobj/RawData/raw.h"
45 #include "larevt/CalibrationDBI/Interface/ChannelStatusProvider.h"
46 #include "larevt/CalibrationDBI/Interface/ChannelStatusService.h"
51 
52 #include "CLHEP/Random/RandFlat.h"
53 #include "c2numpy.h"
54 
55 #include <algorithm>
56 #include <chrono>
57 #include <cstdlib>
58 #include <map>
59 #include <random>
60 #include <set>
61 #include <string>
62 #include <utility>
63 #include <vector>
64 
65 using std::cout;
66 using std::endl;
67 using std::ofstream;
68 using std::string;
69 
70 struct WireSigInfo {
71  int pdgcode;
72  std::string genlab;
73  std::string procid;
74  unsigned int tdcmin;
75  unsigned int tdcmax;
76  int numel;
77  double edep;
78 };
79 
80 namespace nnet {
81  class RawWaveformDump;
82 }
83 
85 
86 public:
87  explicit RawWaveformDump(fhicl::ParameterSet const& p);
88 
89  // Plugins should not be copied or assigned.
90  RawWaveformDump(RawWaveformDump const&) = delete;
91  RawWaveformDump(RawWaveformDump&&) = delete;
92  RawWaveformDump& operator=(RawWaveformDump const&) = delete;
93  RawWaveformDump& operator=(RawWaveformDump&&) = delete;
94 
95  // Required functions.
96  void analyze(art::Event const& e) override;
97 
98  //void reconfigure(fhicl::ParameterSet const & p);
99 
100  void beginJob() override;
101  void endJob() override;
102 
103 private:
105 
107  std::string fSimChannelLabel;
108  std::string fDigitModuleLabel;
109  std::string fWireProducerLabel;
111  unsigned int fShortWaveformSize;
112 
113  std::string fSelectGenLabel;
114  std::string fSelectProcID;
116  std::string fPlaneToDump;
126 
127  CLHEP::RandFlat fRandFlat;
128 
130 };
131 
132 //-----------------------------------------------------------------------
133 struct genFinder {
134 private:
135  typedef std::pair<int, std::string> track_id_to_string;
136  std::vector<track_id_to_string> track_id_map;
137  std::set<std::string> generator_names;
138  bool isSorted = false;
139 
140 public:
141  void sort_now()
142  {
143  std::sort(this->track_id_map.begin(),
144  this->track_id_map.end(),
145  [](const auto& a, const auto& b) { return (a.first < b.first); });
146  isSorted = true;
147  }
148  void add(const int& track_id, const std::string& gname)
149  {
150  this->track_id_map.push_back(std::make_pair(track_id, gname));
151  generator_names.emplace(gname);
152  isSorted = false;
153  }
154  bool has_gen(std::string gname) { return static_cast<bool>(generator_names.count(gname)); };
155  std::string get_gen(int tid)
156  {
157  if (!isSorted) { this->sort_now(); }
158  return std::lower_bound(track_id_map.begin(),
159  track_id_map.end(),
160  tid,
161  [](const auto& a, const auto& b) { return (a.first < b); })
162  ->second;
163  };
164 };
165 
166 // Create the random number generator
167 namespace {
168  std::string const instanceName = "RawWaveformDump";
169 }
170 
171 //-----------------------------------------------------------------------
173  : EDAnalyzer{p}
174  , fDumpWaveformsFileName(p.get<std::string>("DumpWaveformsFileName", "dumpwaveforms"))
175  , fSimulationProducerLabel(p.get<std::string>("SimulationProducerLabel", "larg4Main"))
176  , fSimChannelLabel(p.get<std::string>("SimChannelLabel", "elecDrift"))
177  , fDigitModuleLabel(p.get<std::string>("DigitModuleLabel", "simWire"))
178  , fWireProducerLabel(p.get<std::string>("WireProducerLabel"))
179  , fUseFullWaveform(p.get<bool>("UseFullWaveform", true))
180  , fShortWaveformSize(p.get<unsigned int>("ShortWaveformSize"))
181  , fSelectGenLabel(p.get<std::string>("SelectGenLabel", "ANY"))
182  , fSelectProcID(p.get<std::string>("SelectProcID", "ANY"))
183  , fSelectPDGCode(p.get<int>("SelectPDGCode", 0))
184  , fPlaneToDump(p.get<std::string>("PlaneToDump"))
185  , fMinParticleEnergyGeV(p.get<double>("MinParticleEnergyGeV", 0.))
186  , fMinEnergyDepositedMeV(p.get<double>("MinEnergyDepositedMeV", 0.))
187  , fMinNumberOfElectrons(p.get<int>("MinNumberOfElectrons", 600))
188  , fMaxNumberOfElectrons(p.get<int>("MaxNumberOfElectrons", -1))
189  , fSaveSignal(p.get<bool>("SaveSignal", true))
190  , fMaxNoiseChannelsPerEvent(p.get<int>("MaxNoiseChannelsPerEvent"))
191  , fCollectionPlaneLabel(p.get<std::string>("CollectionPlaneLabel"))
194  -> declareEngine(instanceName, p, "SeedForRawWaveformDump"),
195  "HepJamesRandom",
196  instanceName)}
197 {
198  if (std::getenv("CLUSTER") && std::getenv("PROCESS")) {
200  string(std::getenv("CLUSTER")) + "-" + string(std::getenv("PROCESS")) + "-";
201  }
202 
203  if (fDigitModuleLabel.empty() && fWireProducerLabel.empty()) {
204  throw cet::exception("RawWaveformDump")
205  << "Both DigitModuleLabel and WireProducerLabel are empty";
206  }
207 
208  if ((!fDigitModuleLabel.empty()) && (!fWireProducerLabel.empty())) {
209  throw cet::exception("RawWaveformDump")
210  << "Only one of DigitModuleLabel and WireProducerLabel should be set";
211  }
212 }
213 
214 //-----------------------------------------------------------------------
216 {
217  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataForJob();
218 
224 
225  for (unsigned int i = 0; i < 5; i++) {
226  std::ostringstream name;
227 
228  name.str("");
229  name << "tid" << i;
230  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_INT32);
231 
232  name.str("");
233  name << "pdg" << i;
234  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_INT32);
235 
236  name.str("");
237  name << "gen" << i;
238  c2numpy_addcolumn(&npywriter, name.str().c_str(), (c2numpy_type)((int)C2NUMPY_STRING + 6));
239 
240  name.str("");
241  name << "pid" << i;
242  c2numpy_addcolumn(&npywriter, name.str().c_str(), (c2numpy_type)((int)C2NUMPY_STRING + 7));
243 
244  name.str("");
245  name << "edp" << i;
246  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_FLOAT32);
247 
248  name.str("");
249  name << "nel" << i;
250  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_UINT32);
251 
252  name.str("");
253  name << "sti" << i;
254  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_UINT16);
255 
256  name.str("");
257  name << "stf" << i;
258  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_UINT16);
259  }
260 
261  for (unsigned int i = 0;
262  i < (fUseFullWaveform ? detProp.ReadOutWindowSize() : fShortWaveformSize);
263  i++) {
264  std::ostringstream name;
265  name << "tck_" << i;
266  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_INT16);
267  }
268 }
269 
270 //-----------------------------------------------------------------------
272 {
274 }
275 
276 //-----------------------------------------------------------------------
278 {
279  cout << "Event "
280  << " " << evt.id().run() << " " << evt.id().subRun() << " " << evt.id().event() << endl;
281 
282  std::unique_ptr<genFinder> gf(new genFinder());
283 
284  // ... Read in the digit List object(s).
286  std::vector<art::Ptr<raw::RawDigit>> rawdigitlist;
287  if (evt.getByLabel(fDigitModuleLabel, digitVecHandle)) {
288  art::fill_ptr_vector(rawdigitlist, digitVecHandle);
289  }
290 
291  // ... Read in the wire List object(s).
292  art::Handle<std::vector<recob::Wire>> wireListHandle;
293  std::vector<art::Ptr<recob::Wire>> wirelist;
294  if (evt.getByLabel(fWireProducerLabel, wireListHandle)) {
295  art::fill_ptr_vector(wirelist, wireListHandle);
296  }
297 
298  if (rawdigitlist.empty() && wirelist.empty()) return;
299  if (rawdigitlist.size() && wirelist.size()) return;
300 
301  // channel status
302  lariov::ChannelStatusProvider const& channelStatus =
304 
305  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
306  auto const detProp =
308 
309  // ... Use the handle to get a particular (0th) element of collection.
310  unsigned int dataSize;
311  if (rawdigitlist.size()) {
312  art::Ptr<raw::RawDigit> digitVec0(digitVecHandle, 0);
313  dataSize = digitVec0->Samples(); //size of raw data vectors
314  }
315  else {
316  dataSize = (wirelist[0]->Signal()).size();
317  }
318  if (dataSize != detProp.ReadOutWindowSize()) {
319  std::cout << "!!!!! Bad dataSize: " << dataSize << std::endl;
320  return;
321  }
322 
323  // ... Build a map from channel number -> rawdigitVec
324  std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap;
325  raw::ChannelID_t chnum = raw::InvalidChannelID; // channel number
326  if (rawdigitlist.size()) {
327  for (size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) {
328  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, rdIter);
329  chnum = digitVec->Channel();
330  if (chnum == raw::InvalidChannelID) continue;
331  rawdigitMap[chnum] = digitVec;
332  }
333  }
334  // ... Build a map from channel number -> wire
335  std::map<raw::ChannelID_t, art::Ptr<recob::Wire>> wireMap;
336  if (wirelist.size()) {
337  for (size_t ich = 0; ich < wirelist.size(); ++ich) {
338  art::Ptr<recob::Wire> wire = wirelist[ich];
339  chnum = wire->Channel();
340  if (chnum == raw::InvalidChannelID) continue;
341  wireMap[chnum] = wire;
342  }
343  }
344 
345  // ... Read in MC particle list
347  if (!evt.getByLabel(fSimulationProducerLabel, particleHandle)) {
348  throw cet::exception("AnalysisExample")
349  << " No simb::MCParticle objects in this event - "
350  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
351  }
352 
353  // ... Read in sim channel list
354  auto simChannelHandle = evt.getValidHandle<std::vector<sim::SimChannel>>(fSimChannelLabel);
355 
356  if (!simChannelHandle->size()) return;
357 
358  // ... Create a map of track IDs to generator labels
359  //Get a list of generator names.
360  //std::vector<art::Handle<std::vector<simb::MCTruth>>> mcHandles;
361  //evt.getManyByType(mcHandles);
362  auto mcHandles = evt.getMany<std::vector<simb::MCTruth>>();
363  std::vector<std::pair<int, std::string>> track_id_to_label;
364 
365  for (auto const& mcHandle : mcHandles) {
366  const std::string& sModuleLabel = mcHandle.provenance()->moduleLabel();
368  std::vector<art::Ptr<simb::MCParticle>> mcParts = findMCParts.at(0);
369  for (const art::Ptr<simb::MCParticle> ptr : mcParts) {
370  int track_id = ptr->TrackId();
371  gf->add(track_id, sModuleLabel);
372  }
373  }
374 
375  std::string dummystr6 = "none ";
376  std::string dummystr7 = "none ";
377 
378  if (fSaveSignal) {
379  // .. create a channel number to trackid-wire signal info map
380  std::map<raw::ChannelID_t, std::map<int, WireSigInfo>> Ch2TrkWSInfoMap;
381 
382  // .. create a track ID to vector of channel numbers (in w/c this track deposited energy) map
383  std::map<int, std::vector<raw::ChannelID_t>> Trk2ChVecMap;
384 
385  // ... Loop over simChannels
386  for (auto const& channel : (*simChannelHandle)) {
387 
388  // .. get simChannel channel number
389  const raw::ChannelID_t ch1 = channel.Channel();
390  if (ch1 == raw::InvalidChannelID) continue;
391  if (geo::PlaneGeo::ViewName(fgeom->View(ch1)) != fPlaneToDump[0]) continue;
392 
393  bool selectThisChannel = false;
394 
395  // .. create a track ID to wire signal info map
396  std::map<int, WireSigInfo> Trk2WSInfoMap;
397 
398  // ... Loop over all ticks with ionization energy deposited
399  auto const& timeSlices = channel.TDCIDEMap();
400  for (auto const& timeSlice : timeSlices) {
401 
402  auto const& energyDeposits = timeSlice.second;
403  auto const tpctime = timeSlice.first;
404  unsigned int tdctick = static_cast<unsigned int>(clockData.TPCTDC2Tick(double(tpctime)));
405  if (tdctick < 0 || tdctick > (dataSize - 1)) continue;
406 
407  // ... Loop over all energy depositions in this tick
408  for (auto const& energyDeposit : energyDeposits) {
409 
410  if (!energyDeposit.trackID) continue;
411  int trkid = energyDeposit.trackID;
412  simb::MCParticle particle = PIS->TrackIdToMotherParticle(trkid);
413  //std::cout << energyDeposit.trackID << " " << trkid << " " << particle.TrackId() << std::endl;
414 
415  // .. ignore this energy deposition if incident particle energy below some threshold
416  if (particle.E() < fMinParticleEnergyGeV) continue;
417 
418  int eve_id = PIS->TrackIdToEveTrackId(trkid);
419  if (!eve_id) continue;
420  std::string genlab = gf->get_gen(eve_id);
421 
422  if (Trk2WSInfoMap.find(trkid) == Trk2WSInfoMap.end()) {
423  WireSigInfo wsinf;
424  wsinf.pdgcode = particle.PdgCode();
425  wsinf.genlab = genlab;
426  wsinf.procid = particle.Process();
427  wsinf.tdcmin = dataSize - 1;
428  wsinf.tdcmax = 0;
429  wsinf.edep = 0.;
430  wsinf.numel = 0;
431  Trk2WSInfoMap.insert(std::pair<int, WireSigInfo>(trkid, wsinf));
432  }
433  if (tdctick < Trk2WSInfoMap.at(trkid).tdcmin) Trk2WSInfoMap.at(trkid).tdcmin = tdctick;
434  if (tdctick > Trk2WSInfoMap.at(trkid).tdcmax) Trk2WSInfoMap.at(trkid).tdcmax = tdctick;
435  Trk2WSInfoMap.at(trkid).edep += energyDeposit.energy;
436  Trk2WSInfoMap.at(trkid).numel += energyDeposit.numElectrons;
437  }
438  }
439 
440  if (!Trk2WSInfoMap.empty()) {
441  for (std::pair<int, WireSigInfo> itmap : Trk2WSInfoMap) {
442  if (fSelectGenLabel != "ANY") {
443  if (itmap.second.genlab != fSelectGenLabel) continue;
444  }
445  if (fSelectProcID != "ANY") {
446  if (itmap.second.procid != fSelectProcID) continue;
447  }
448  if (fSelectPDGCode != 0) {
449  if (itmap.second.pdgcode != fSelectPDGCode) continue;
450  }
451  itmap.second.genlab.resize(6, ' ');
452  itmap.second.procid.resize(7, ' ');
453  if (itmap.second.numel >= fMinNumberOfElectrons &&
454  itmap.second.edep >= fMinEnergyDepositedMeV) {
455  if (fMaxNumberOfElectrons >= 0 && itmap.second.numel >= fMaxNumberOfElectrons) {
456  continue;
457  }
458  else {
459  int trkid = itmap.first;
460  if (Trk2ChVecMap.find(trkid) == Trk2ChVecMap.end()) {
461  std::vector<raw::ChannelID_t> chvec;
462  Trk2ChVecMap.insert(std::pair<int, std::vector<raw::ChannelID_t>>(trkid, chvec));
463  }
464  Trk2ChVecMap.at(trkid).push_back(ch1);
465  selectThisChannel = true;
466  }
467  }
468  } // loop over Trk2WSinfoMap
469  if (selectThisChannel) {
470  Ch2TrkWSInfoMap.insert(
471  std::pair<raw::ChannelID_t, std::map<int, WireSigInfo>>(ch1, Trk2WSInfoMap));
472  }
473  } // if Trk2WSInfoMap not empty
474 
475  } // loop over SimChannels
476 
477  std::set<raw::ChannelID_t> selected_channels;
478 
479  // ... Now write out the signal waveforms for each track
480  if (!Trk2ChVecMap.empty()) {
481  for (auto const& ittrk : Trk2ChVecMap) {
482  int i = fRandFlat.fireInt(
483  ittrk.second.size()); // randomly select one channel with a signal from this particle
484  chnum = ittrk.second[i];
485 
486  if (not selected_channels.insert(chnum).second) { continue; }
487 
488  std::map<raw::ChannelID_t, std::map<int, WireSigInfo>>::iterator itchn;
489  itchn = Ch2TrkWSInfoMap.find(chnum);
490  if (itchn != Ch2TrkWSInfoMap.end()) {
491 
492  std::vector<short> adcvec(dataSize); // vector to hold zero-padded full waveform
493 
494  if (rawdigitlist.size()) {
495  auto search = rawdigitMap.find(chnum);
496  if (search == rawdigitMap.end()) continue;
497  art::Ptr<raw::RawDigit> rawdig = (*search).second;
498  std::vector<short> rawadc(dataSize); // vector to hold uncompressed adc values later
499  raw::Uncompress(rawdig->ADCs(), rawadc, rawdig->GetPedestal(), rawdig->Compression());
500  for (size_t j = 0; j < rawadc.size(); ++j) {
501  adcvec[j] = rawadc[j] - rawdig->GetPedestal();
502  }
503  }
504  else if (wirelist.size()) {
505  auto search = wireMap.find(chnum);
506  if (search == wireMap.end()) continue;
507  art::Ptr<recob::Wire> wire = (*search).second;
508  const auto& signal = wire->Signal();
509  for (size_t j = 0; j < adcvec.size(); ++j) {
510  adcvec[j] = signal[j];
511  }
512  }
513 
514  // .. write out info for each peak
515  // a full waveform has at least one peak; the output will save up to 5 peaks (if there is
516  // only 1 peak, will fill the other 4 with 0);
517  // for fShortWaveformSize: only use the first peak's start_tick
518 
519  if (fUseFullWaveform) {
520 
521  c2numpy_uint32(&npywriter, evt.id().event());
522  c2numpy_uint32(&npywriter, chnum);
524  c2numpy_uint16(&npywriter, itchn->second.size()); // size of Trk2WSInfoMap, or #peaks
525  unsigned int icnt = 0;
526  for (auto const& it : itchn->second) {
527  c2numpy_int32(&npywriter, it.first); // trackid
528  c2numpy_int32(&npywriter, it.second.pdgcode); // pdgcode
529  c2numpy_string(&npywriter, it.second.genlab.c_str()); // genlab
530  c2numpy_string(&npywriter, it.second.procid.c_str()); // procid
531  c2numpy_float32(&npywriter, it.second.edep); // edepo
532  c2numpy_uint32(&npywriter, it.second.numel); // numelec
533 
534  c2numpy_uint16(&npywriter, it.second.tdcmin); // stck1
535  c2numpy_uint16(&npywriter, it.second.tdcmax); // stc2
536 
537  icnt++;
538  if (icnt == 5) break;
539  }
540 
541  // .. pad with 0's if number of peaks less than 5
542  for (unsigned int i = icnt; i < 5; ++i) {
545  c2numpy_string(&npywriter, dummystr6.c_str());
546  c2numpy_string(&npywriter, dummystr7.c_str());
551  }
552 
553  for (unsigned int itck = 0; itck < dataSize; ++itck) {
554  c2numpy_int16(&npywriter, adcvec[itck]);
555  }
556  }
557  else {
558 
559  // .. first loop to find largest signal
560  double EDep = 0.;
561  unsigned int TDCMin, TDCMax;
562  bool foundmaxsig = false;
563  for (auto& it : itchn->second) {
564  if (it.second.edep > EDep && it.second.numel > 0) {
565  EDep = it.second.edep;
566  TDCMin = it.second.tdcmin;
567  TDCMax = it.second.tdcmax;
568  foundmaxsig = true;
569  }
570  }
571  if (foundmaxsig) {
572  int sigtdc1, sigtdc2, sighwid, sigfwid, sigtdcm;
574  sigtdc1 = TDCMin - 14 / 2;
575  sigtdc2 = TDCMax + 3 * 14 / 2;
576  }
577  else {
578  sigtdc1 = TDCMin - 32 / 2;
579  sigtdc2 = TDCMax + 32 / 2;
580  }
581  sigfwid = sigtdc2 - sigtdc1;
582  sighwid = sigfwid / 2;
583  sigtdcm = sigtdc1 + sighwid;
584 
585  int start_tick = -1;
586  int end_tick = -1;
587  // .. set window edges to contain the largest signal
588  if (sigfwid < (int)fShortWaveformSize) {
589  // --> case 1: signal range fits within window
590  int dt = fShortWaveformSize - sigfwid;
591  start_tick = sigtdc1 - dt * fRandFlat.fire(0, 1);
592  }
593  else {
594  // --> case 2: signal range larger than window
595  int mrgn = fShortWaveformSize / 20;
596  int dt = fShortWaveformSize - 2 * mrgn;
597  start_tick = sigtdcm - mrgn - dt * fRandFlat.fire(0, 1);
598  }
599  if (start_tick < 0) start_tick = 0;
600  end_tick = start_tick + fShortWaveformSize - 1;
601  if (end_tick > int(dataSize - 1)) {
602  end_tick = dataSize - 1;
603  start_tick = end_tick - fShortWaveformSize + 1;
604  }
605 
606  c2numpy_uint32(&npywriter, evt.id().event());
607  c2numpy_uint32(&npywriter, chnum);
609 
610  // .. second loop to select only signals that are within the window
611 
612  int it_trk[5], it_pdg[5], it_nel[5];
613  unsigned int stck_1[5], stck_2[5];
614  std::string it_glb[5], it_prc[5];
615  double it_edp[5];
616 
617  unsigned int icnt = 0;
618 
619  for (auto& it : itchn->second) {
620  if ((it.second.tdcmin >= (unsigned int)start_tick &&
621  it.second.tdcmin < (unsigned int)end_tick) ||
622  (it.second.tdcmax > (unsigned int)start_tick &&
623  it.second.tdcmax <= (unsigned int)end_tick)) {
624 
625  it_trk[icnt] = it.first;
626  it_pdg[icnt] = it.second.pdgcode;
627  it_glb[icnt] = it.second.genlab;
628  it_prc[icnt] = it.second.procid;
629  it_edp[icnt] = it.second.edep;
630  it_nel[icnt] = it.second.numel;
631 
632  unsigned int mintdc = it.second.tdcmin;
633  unsigned int maxtdc = it.second.tdcmax;
634  if (mintdc < (unsigned int)start_tick) mintdc = start_tick;
635  if (maxtdc > (unsigned int)end_tick) maxtdc = end_tick;
636 
637  stck_1[icnt] = mintdc - start_tick;
638  stck_2[icnt] = maxtdc - start_tick;
639 
640  icnt++;
641  if (icnt == 5) break;
642  }
643  }
644 
645  c2numpy_uint16(&npywriter, icnt); // number of peaks
646 
647  for (unsigned int i = 0; i < icnt; ++i) {
648  c2numpy_int32(&npywriter, it_trk[i]); // trackid
649  c2numpy_int32(&npywriter, it_pdg[i]); // pdgcode
650  c2numpy_string(&npywriter, it_glb[i].c_str()); // genlab
651  c2numpy_string(&npywriter, it_prc[i].c_str()); // procid
652  c2numpy_float32(&npywriter, it_edp[i]); // edepo
653  c2numpy_uint32(&npywriter, it_nel[i]); // numelec
654  c2numpy_uint16(&npywriter, stck_1[i]); // stck1
655  c2numpy_uint16(&npywriter, stck_2[i]); // stck2
656  }
657 
658  // .. pad with 0's if number of peaks less than 5
659  for (unsigned int i = icnt; i < 5; ++i) {
662  c2numpy_string(&npywriter, dummystr6.c_str());
663  c2numpy_string(&npywriter, dummystr7.c_str());
668  }
669 
670  for (unsigned int itck = start_tick; itck < (start_tick + fShortWaveformSize);
671  ++itck) {
672  c2numpy_int16(&npywriter, adcvec[itck]);
673  }
674 
675  } // foundmaxsig
676  }
677  }
678  }
679  }
680  }
681  else {
682  //save noise
683  int noisechancount = 0;
684  std::map<raw::ChannelID_t, bool> signalMap;
685  for (auto const& channel : (*simChannelHandle)) {
686  signalMap[channel.Channel()] = true;
687  }
688  // .. create a vector for shuffling the wire channel indices
689  auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
690  size_t nchan = (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
691  std::vector<size_t> randigitmap;
692  for (size_t i = 0; i < nchan; ++i)
693  randigitmap.push_back(i);
694  std::shuffle(randigitmap.begin(), randigitmap.end(), std::mt19937(seed));
695 
696  for (size_t rdIter = 0; rdIter < (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
697  ++rdIter) {
698 
699  if (noisechancount == fMaxNoiseChannelsPerEvent) break;
700 
701  std::vector<short> adcvec(dataSize); // vector to wire adc values
702  if (rawdigitlist.size()) {
703  size_t ranIdx = randigitmap[rdIter];
704  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, ranIdx);
705  if (signalMap[digitVec->Channel()]) continue;
706 
707  std::vector<short> rawadc(dataSize); // vector to hold uncompressed adc values later
708  if (geo::PlaneGeo::ViewName(fgeom->View(digitVec->Channel())) != fPlaneToDump[0]) continue;
709  raw::Uncompress(digitVec->ADCs(), rawadc, digitVec->GetPedestal(), digitVec->Compression());
710  for (size_t j = 0; j < rawadc.size(); ++j) {
711  adcvec[j] = rawadc[j] - digitVec->GetPedestal();
712  }
713  c2numpy_uint32(&npywriter, evt.id().event());
714  c2numpy_uint32(&npywriter, digitVec->Channel());
716  geo::PlaneGeo::ViewName(fgeom->View(digitVec->Channel())).c_str());
717  }
718  else if (wirelist.size()) {
719  size_t ranIdx = randigitmap[rdIter];
720  art::Ptr<recob::Wire> wire = wirelist[ranIdx];
721  if (signalMap[wire->Channel()]) continue;
722  if (channelStatus.IsBad(wire->Channel())) continue;
723  if (geo::PlaneGeo::ViewName(fgeom->View(wire->Channel())) != fPlaneToDump[0]) continue;
724  const auto& signal = wire->Signal();
725  for (size_t j = 0; j < adcvec.size(); ++j) {
726  adcvec[j] = signal[j];
727  }
728  c2numpy_uint32(&npywriter, evt.id().event());
729  c2numpy_uint32(&npywriter, wire->Channel());
731  }
732 
733  c2numpy_uint16(&npywriter, 0); //number of peaks
734  for (unsigned int i = 0; i < 5; ++i) {
737  c2numpy_string(&npywriter, dummystr6.c_str());
738  c2numpy_string(&npywriter, dummystr7.c_str());
743  }
744 
745  if (fUseFullWaveform) {
746  for (unsigned int itck = 0; itck < dataSize; ++itck) {
747  c2numpy_int16(&npywriter, adcvec[itck]);
748  }
749  }
750  else {
751  int start_tick = int((dataSize - fShortWaveformSize) * fRandFlat.fire(0, 1));
752  for (unsigned int itck = start_tick; itck < (start_tick + fShortWaveformSize); ++itck) {
753  c2numpy_int16(&npywriter, adcvec[itck]);
754  }
755  }
756 
757  ++noisechancount;
758  }
759  std::cout << "Total number of noise channels " << noisechancount << std::endl;
760  }
761 }
double E(const int i=0) const
Definition: MCParticle.h:234
float GetPedestal() const
Definition: RawDigit.h:221
int c2numpy_init(c2numpy_writer *writer, const std::string outputFilePrefix, int32_t numRowsPerFile)
Definition: c2numpy.h:140
std::string fDigitModuleLabel
module that made digits
base_engine_t & createEngine(seed_t seed)
bool has_gen(std::string gname)
int c2numpy_int32(c2numpy_writer *writer, int32_t data)
Definition: c2numpy.h:298
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
Definition: RawDigit.h:209
int PdgCode() const
Definition: MCParticle.h:213
static std::string ViewName(geo::View_t view)
Returns the name of the specified view.
Definition: PlaneGeo.cxx:682
ULong64_t Samples() const
Number of samples in the uncompressed ADC data.
Definition: RawDigit.h:217
c2numpy_type
Definition: c2numpy.h:29
void add(const int &track_id, const std::string &gname)
RawWaveformDump(fhicl::ParameterSet const &p)
Declaration of signal hit object.
std::string fSimChannelLabel
module that made simchannels
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition: RawDigit.h:213
Definition of basic raw digits.
std::string Process() const
Definition: MCParticle.h:216
Particle class.
void analyze(art::Event const &e) override
RunNumber_t run() const
Definition: EventID.h:98
int c2numpy_close(c2numpy_writer *writer)
Definition: c2numpy.h:425
art::ServiceHandle< geo::Geometry > fgeom
std::pair< int, std::string > track_id_to_string
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
Definition: RawTypes.h:31
int c2numpy_addcolumn(c2numpy_writer *writer, const std::string name, c2numpy_type type)
Definition: c2numpy.h:157
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
void beginJob()
Definition: Breakpoints.cc:14
long seed
Definition: chem4.cc:67
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
Definition: Wire.h:223
Collect all the RawData header files together.
Definition: EmTrack.h:40
int c2numpy_uint32(c2numpy_writer *writer, uint32_t data)
Definition: c2numpy.h:334
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
std::vector< float > Signal() const
Return a zero-padded full length vector filled with RoI signal.
Definition: Wire.cxx:30
int c2numpy_float32(c2numpy_writer *writer, float data)
Definition: c2numpy.h:369
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
Definition: RawDigit.h:229
simb::MCParticle TrackIdToMotherParticle(int const id) const
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Encapsulate the construction of a single detector plane.
View_t View(PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
int c2numpy_uint16(c2numpy_writer *writer, uint16_t data)
Definition: c2numpy.h:325
object containing MC truth information necessary for making RawDigits and doing back tracking ...
int c2numpy_int16(c2numpy_writer *writer, int16_t data)
Definition: c2numpy.h:289
EventNumber_t event() const
Definition: EventID.h:116
Declaration of basic channel signal object.
art::ServiceHandle< cheat::ParticleInventoryService > PIS
TCEvent evt
Definition: DataStructs.cxx:8
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:306
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:744
Float_t e
Definition: plot.C:35
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
std::string fSimulationProducerLabel
producer that tracked simulated part. through detector
std::string get_gen(int tid)
SubRunNumber_t subRun() const
Definition: EventID.h:110
int c2numpy_string(c2numpy_writer *writer, const char *data)
Definition: c2numpy.h:411
EventID id() const
Definition: Event.cc:23
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const