RawWaveformDump_module.cc
Go to the documentation of this file.
1 //////////////////////////////////////////////
2 //
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 //
17 //////////////////////////////////////////////
18 
19 #include <random>
20 
21 // Framework includes
29 #include "canvas/Persistency/Common/FindManyP.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"
48 
51 
52 #include "nurandom/RandomUtils/NuRandomService.h"
53 #include "CLHEP/Random/RandFlat.h"
54 #include "c2numpy.h"
55 
56 using std::cout;
57 using std::endl;
58 using std::ofstream;
59 using std::string;
60 
61 struct WireSigInfo {
62  int pdgcode;
65  unsigned int tdcmin;
66  unsigned int tdcmax;
67  int numel;
68  double edep;
69 };
70 
71 namespace nnet {
72  class RawWaveformDump;
73 }
74 
76 
77 public:
78  explicit RawWaveformDump(fhicl::ParameterSet const& p);
79 
80  // Plugins should not be copied or assigned.
81  RawWaveformDump(RawWaveformDump const&) = delete;
82  RawWaveformDump(RawWaveformDump&&) = delete;
83  RawWaveformDump& operator=(RawWaveformDump const&) = delete;
84  RawWaveformDump& operator=(RawWaveformDump&&) = delete;
85 
86  // Required functions.
87  void analyze(art::Event const& e) override;
88 
89  //void reconfigure(fhicl::ParameterSet const & p);
90 
91  void beginJob() override;
92  void endJob() override;
93 
94 private:
96 
97  std::string fSimulationProducerLabel; ///< producer that tracked simulated part. through detector
98  std::string fSimChannelLabel; ///< module that made simchannels
99  std::string fDigitModuleLabel; ///< module that made digits
102  unsigned int fShortWaveformSize;
103 
117 
118  CLHEP::RandFlat fRandFlat;
119 
121 };
122 
123 //-----------------------------------------------------------------------
124 struct genFinder {
125 private:
126  typedef std::pair<int, std::string> track_id_to_string;
127  std::vector<track_id_to_string> track_id_map;
128  std::set<std::string> generator_names;
129  bool isSorted = false;
130 
131 public:
132  void
134  {
135  std::sort(this->track_id_map.begin(),
136  this->track_id_map.end(),
137  [](const auto& a, const auto& b) { return (a.first < b.first); });
138  isSorted = true;
139  }
140  void
141  add(const int& track_id, const std::string& gname)
142  {
143  this->track_id_map.push_back(std::make_pair(track_id, gname));
144  generator_names.emplace(gname);
145  isSorted = false;
146  }
147  bool
149  {
150  return static_cast<bool>(generator_names.count(gname));
151  };
153  get_gen(int tid)
154  {
155  if (!isSorted) { this->sort_now(); }
156  return std::lower_bound(track_id_map.begin(),
157  track_id_map.end(),
158  tid,
159  [](const auto& a, const auto& b) { return (a.first < b); })
160  ->second;
161  };
162 };
163 
164 // Create the random number generator
165 namespace {
166  std::string const instanceName = "RawWaveformDump";
167 }
168 
169 //-----------------------------------------------------------------------
171  : EDAnalyzer{p}
172  , fDumpWaveformsFileName(p.get<std::string>("DumpWaveformsFileName", "dumpwaveforms"))
173  , fSimulationProducerLabel(p.get<std::string>("SimulationProducerLabel", "larg4Main"))
174  , fSimChannelLabel(p.get<std::string>("SimChannelLabel", "elecDrift"))
175  , fDigitModuleLabel(p.get<std::string>("DigitModuleLabel", "simWire"))
176  , fWireProducerLabel(p.get<std::string>("WireProducerLabel"))
177  , fUseFullWaveform(p.get<bool>("UseFullWaveform", true))
178  , fShortWaveformSize(p.get<unsigned int>("ShortWaveformSize"))
179  , fSelectGenLabel(p.get<std::string>("SelectGenLabel", "ANY"))
180  , fSelectProcID(p.get<std::string>("SelectProcID", "ANY"))
181  , fSelectPDGCode(p.get<int>("SelectPDGCode", 0))
182  , fPlaneToDump(p.get<std::string>("PlaneToDump"))
183  , fMinParticleEnergyGeV(p.get<double>("MinParticleEnergyGeV", 0.))
184  , fMinEnergyDepositedMeV(p.get<double>("MinEnergyDepositedMeV", 0.))
185  , fMinNumberOfElectrons(p.get<int>("MinNumberOfElectrons", 600))
186  , fMaxNumberOfElectrons(p.get<int>("MaxNumberOfElectrons", -1))
187  , fSaveSignal(p.get<bool>("SaveSignal", true))
188  , fMaxNoiseChannelsPerEvent(p.get<int>("MaxNoiseChannelsPerEvent"))
189  , fCollectionPlaneLabel(p.get<std::string>("CollectionPlaneLabel"))
191  instanceName, p, "SeedForRawWaveformDump"),
192  "HepJamesRandom", instanceName)}
193 {
194  if (std::getenv("CLUSTER") && std::getenv("PROCESS")) {
195  fDumpWaveformsFileName += string(std::getenv("CLUSTER")) + "-" + string(std::getenv("PROCESS")) + "-";
196  }
197 
198  if (fDigitModuleLabel.empty() && fWireProducerLabel.empty()) {
199  throw cet::exception("RawWaveformDump")
200  << "Both DigitModuleLabel and WireProducerLabel are empty";
201  }
202 
203  if ((!fDigitModuleLabel.empty()) && (!fWireProducerLabel.empty())) {
204  throw cet::exception("RawWaveformDump")
205  << "Only one of DigitModuleLabel and WireProducerLabel should be set";
206  }
207 }
208 
209 //-----------------------------------------------------------------------
210 void
212 {
213  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataForJob();
214 
220 
221  for (unsigned int i = 0; i < 5; i++) {
222  std::ostringstream name;
223 
224  name.str("");
225  name << "tid" << i;
226  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_INT32);
227 
228  name.str("");
229  name << "pdg" << i;
230  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_INT32);
231 
232  name.str("");
233  name << "gen" << i;
234  c2numpy_addcolumn(&npywriter, name.str().c_str(), (c2numpy_type)((int)C2NUMPY_STRING + 6));
235 
236  name.str("");
237  name << "pid" << i;
238  c2numpy_addcolumn(&npywriter, name.str().c_str(), (c2numpy_type)((int)C2NUMPY_STRING + 7));
239 
240  name.str("");
241  name << "edp" << i;
242  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_FLOAT32);
243 
244  name.str("");
245  name << "nel" << i;
246  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_UINT32);
247 
248  name.str("");
249  name << "sti" << i;
250  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_UINT16);
251 
252  name.str("");
253  name << "stf" << i;
254  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_UINT16);
255  }
256 
257  for (unsigned int i = 0;
258  i < (fUseFullWaveform ? detProp.ReadOutWindowSize() : fShortWaveformSize);
259  i++) {
260  std::ostringstream name;
261  name << "tck_" << i;
262  c2numpy_addcolumn(&npywriter, name.str().c_str(), C2NUMPY_INT16);
263  }
264 }
265 
266 //-----------------------------------------------------------------------
267 void
269 {
271 }
272 
273 //-----------------------------------------------------------------------
274 void
276 {
277  cout << "Event "
278  << " " << evt.id().run() << " " << evt.id().subRun() << " " << evt.id().event() << endl;
279 
280  std::unique_ptr<genFinder> gf(new genFinder());
281 
282  // ... Read in the digit List object(s).
284  std::vector<art::Ptr<raw::RawDigit>> rawdigitlist;
285  if (evt.getByLabel(fDigitModuleLabel, digitVecHandle)) {
286  art::fill_ptr_vector(rawdigitlist, digitVecHandle);
287  }
288 
289  // ... Read in the wire List object(s).
290  art::Handle<std::vector<recob::Wire>> wireListHandle;
291  std::vector<art::Ptr<recob::Wire>> wirelist;
292  if (evt.getByLabel(fWireProducerLabel, wireListHandle)) {
293  art::fill_ptr_vector(wirelist, wireListHandle);
294  }
295 
296  if (rawdigitlist.empty() && wirelist.empty()) return;
297  if (rawdigitlist.size() && wirelist.size()) return;
298 
299  // channel status
300  lariov::ChannelStatusProvider const& channelStatus =
302 
303  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
304  auto const detProp =
306 
307  // ... Use the handle to get a particular (0th) element of collection.
308  unsigned int dataSize;
309  if (rawdigitlist.size()) {
310  art::Ptr<raw::RawDigit> digitVec0(digitVecHandle, 0);
311  dataSize = digitVec0->Samples(); //size of raw data vectors
312  }
313  else {
314  dataSize = (wirelist[0]->Signal()).size();
315  }
316  if (dataSize != detProp.ReadOutWindowSize()) {
317  std::cout << "!!!!! Bad dataSize: " << dataSize << std::endl;
318  return;
319  }
320 
321  // ... Build a map from channel number -> rawdigitVec
322  std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap;
323  raw::ChannelID_t chnum = raw::InvalidChannelID; // channel number
324  if (rawdigitlist.size()) {
325  for (size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) {
326  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, rdIter);
327  chnum = digitVec->Channel();
328  if (chnum == raw::InvalidChannelID) continue;
329  rawdigitMap[chnum] = digitVec;
330  }
331  }
332  // ... Build a map from channel number -> wire
333  std::map<raw::ChannelID_t, art::Ptr<recob::Wire>> wireMap;
334  if (wirelist.size()) {
335  for (size_t ich = 0; ich < wirelist.size(); ++ich) {
336  art::Ptr<recob::Wire> wire = wirelist[ich];
337  chnum = wire->Channel();
338  if (chnum == raw::InvalidChannelID) continue;
339  wireMap[chnum] = wire;
340  }
341  }
342 
343  // ... Read in MC particle list
345  if (!evt.getByLabel(fSimulationProducerLabel, particleHandle)) {
346  throw cet::exception("AnalysisExample")
347  << " No simb::MCParticle objects in this event - "
348  << " Line " << __LINE__ << " in file " << __FILE__ << std::endl;
349  }
350 
351  // ... Read in sim channel list
352  auto simChannelHandle =
353  evt.getValidHandle<std::vector<sim::SimChannel>>(fSimChannelLabel);
354 
355  if (!simChannelHandle->size()) return;
356 
357  // ... Create a map of track IDs to generator labels
358  //Get a list of generator names.
359  //std::vector<art::Handle<std::vector<simb::MCTruth>>> mcHandles;
360  //evt.getManyByType(mcHandles);
361  auto mcHandles = evt.getMany<std::vector<simb::MCTruth>>();
362  std::vector<std::pair<int, std::string>> track_id_to_label;
363 
364  for (auto const& mcHandle : mcHandles) {
365  const std::string& sModuleLabel = mcHandle.provenance()->moduleLabel();
366  art::FindManyP<simb::MCParticle> findMCParts(mcHandle, evt, fSimulationProducerLabel);
367  std::vector<art::Ptr<simb::MCParticle>> mcParts = findMCParts.at(0);
368  for (const art::Ptr<simb::MCParticle> ptr : mcParts) {
369  int track_id = ptr->TrackId();
370  gf->add(track_id, sModuleLabel);
371  }
372  }
373 
374  std::string dummystr6 = "none ";
375  std::string dummystr7 = "none ";
376 
377  if (fSaveSignal) {
378  // .. create a channel number to trackid-wire signal info map
379  std::map<raw::ChannelID_t, std::map<int, WireSigInfo>> Ch2TrkWSInfoMap;
380 
381  // .. create a track ID to vector of channel numbers (in w/c this track deposited energy) map
382  std::map<int, std::vector<raw::ChannelID_t>> Trk2ChVecMap;
383 
384  // ... Loop over simChannels
385  for (auto const& channel : (*simChannelHandle)) {
386 
387  // .. get simChannel channel number
388  const raw::ChannelID_t ch1 = channel.Channel();
389  if (ch1 == raw::InvalidChannelID) continue;
390  if (geo::PlaneGeo::ViewName(fgeom->View(ch1)) != fPlaneToDump[0]) continue;
391 
392  bool selectThisChannel = false;
393 
394  // .. create a track ID to wire signal info map
395  std::map<int, WireSigInfo> Trk2WSInfoMap;
396 
397  // ... Loop over all ticks with ionization energy deposited
398  auto const& timeSlices = channel.TDCIDEMap();
399  for (auto const& timeSlice : timeSlices) {
400 
401  auto const& energyDeposits = timeSlice.second;
402  auto const tpctime = timeSlice.first;
403  unsigned int tdctick = static_cast<unsigned int>(clockData.TPCTDC2Tick(double(tpctime)));
404  if (tdctick < 0 || tdctick > (dataSize - 1)) continue;
405 
406  // ... Loop over all energy depositions in this tick
407  for (auto const& energyDeposit : energyDeposits) {
408 
409  if (!energyDeposit.trackID) continue;
410  int trkid = energyDeposit.trackID;
411  simb::MCParticle particle = PIS->TrackIdToMotherParticle(trkid);
412  //std::cout << energyDeposit.trackID << " " << trkid << " " << particle.TrackId() << std::endl;
413 
414  // .. ignore this energy deposition if incident particle energy below some threshold
415  if (particle.E() < fMinParticleEnergyGeV) continue;
416 
417  int eve_id = PIS->TrackIdToEveTrackId(trkid);
418  if (!eve_id) continue;
419  std::string genlab = gf->get_gen(eve_id);
420 
421  if (Trk2WSInfoMap.find(trkid) == Trk2WSInfoMap.end()) {
422  WireSigInfo wsinf;
423  wsinf.pdgcode = particle.PdgCode();
424  wsinf.genlab = genlab;
425  wsinf.procid = particle.Process();
426  wsinf.tdcmin = dataSize - 1;
427  wsinf.tdcmax = 0;
428  wsinf.edep = 0.;
429  wsinf.numel = 0;
430  Trk2WSInfoMap.insert(std::pair<int, WireSigInfo>(trkid, wsinf));
431  }
432  if (tdctick < Trk2WSInfoMap.at(trkid).tdcmin) Trk2WSInfoMap.at(trkid).tdcmin = tdctick;
433  if (tdctick > Trk2WSInfoMap.at(trkid).tdcmax) Trk2WSInfoMap.at(trkid).tdcmax = tdctick;
434  Trk2WSInfoMap.at(trkid).edep += energyDeposit.energy;
435  Trk2WSInfoMap.at(trkid).numel += energyDeposit.numElectrons;
436  }
437  }
438 
439  if (!Trk2WSInfoMap.empty()) {
440  for (std::pair<int, WireSigInfo> itmap : Trk2WSInfoMap) {
441  if (fSelectGenLabel != "ANY") {
442  if (itmap.second.genlab != fSelectGenLabel) continue;
443  }
444  if (fSelectProcID != "ANY") {
445  if (itmap.second.procid != fSelectProcID) continue;
446  }
447  if (fSelectPDGCode != 0) {
448  if (itmap.second.pdgcode != fSelectPDGCode) continue;
449  }
450  itmap.second.genlab.resize(6, ' ');
451  itmap.second.procid.resize(7, ' ');
452  if (itmap.second.numel >= fMinNumberOfElectrons &&
453  itmap.second.edep >= fMinEnergyDepositedMeV) {
454  if (fMaxNumberOfElectrons >= 0 && itmap.second.numel >= fMaxNumberOfElectrons) {
455  continue;
456  }
457  else {
458  int trkid = itmap.first;
459  if (Trk2ChVecMap.find(trkid) == Trk2ChVecMap.end()) {
460  std::vector<raw::ChannelID_t> chvec;
461  Trk2ChVecMap.insert(std::pair<int, std::vector<raw::ChannelID_t>>(trkid, chvec));
462  }
463  Trk2ChVecMap.at(trkid).push_back(ch1);
464  selectThisChannel = true;
465  }
466  }
467  } // loop over Trk2WSinfoMap
468  if (selectThisChannel) {
469  Ch2TrkWSInfoMap.insert(
470  std::pair<raw::ChannelID_t, std::map<int, WireSigInfo>>(ch1, Trk2WSInfoMap));
471  }
472  } // if Trk2WSInfoMap not empty
473 
474  } // loop over SimChannels
475 
476  std::set<raw::ChannelID_t> selected_channels;
477 
478  // ... Now write out the signal waveforms for each track
479  if (!Trk2ChVecMap.empty()) {
480  for (auto const& ittrk : Trk2ChVecMap) {
481  int i = fRandFlat.fireInt(ittrk.second.size()); // randomly select one channel with a signal from this particle
482  chnum = ittrk.second[i];
483 
484  if (not selected_channels.insert(chnum).second) {
485  continue;
486  }
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  } else {
577  sigtdc1 = TDCMin - 32/2;
578  sigtdc2 = TDCMax + 32/2;
579  }
580  sigfwid=sigtdc2 - sigtdc1;
581  sighwid=sigfwid/2;
582  sigtdcm=sigtdc1+sighwid;
583 
584  int start_tick = -1;
585  int end_tick = -1;
586  // .. set window edges to contain the largest signal
587  if (sigfwid < (int)fShortWaveformSize) {
588  // --> case 1: signal range fits within window
589  int dt = fShortWaveformSize - sigfwid;
590  start_tick = sigtdc1 - dt * fRandFlat.fire(0,1);
591  }
592  else {
593  // --> case 2: signal range larger than window
594  int mrgn = fShortWaveformSize/20;
595  int dt = fShortWaveformSize - 2*mrgn;
596  start_tick = sigtdcm - mrgn - dt * fRandFlat.fire(0,1);
597  }
598  if (start_tick < 0) start_tick = 0;
599  end_tick = start_tick + fShortWaveformSize - 1;
600  if (end_tick > int (dataSize - 1)) {
601  end_tick = dataSize - 1;
602  start_tick = end_tick - fShortWaveformSize + 1;
603  }
604 
605  c2numpy_uint32(&npywriter, evt.id().event());
606  c2numpy_uint32(&npywriter, chnum);
608 
609  // .. second loop to select only signals that are within the window
610 
611  int it_trk[5],it_pdg[5],it_nel[5];
612  unsigned int stck_1[5],stck_2[5];
613  std::string it_glb[5], it_prc[5];
614  double it_edp[5];
615 
616  unsigned int icnt = 0;
617 
618  for (auto & it : itchn->second) {
619  if (( it.second.tdcmin >= (unsigned int)start_tick && it.second.tdcmin < (unsigned int)end_tick) ||
620  ( it.second.tdcmax > (unsigned int)start_tick && it.second.tdcmax <= (unsigned int)end_tick)) {
621 
622  it_trk[icnt] = it.first;
623  it_pdg[icnt] = it.second.pdgcode;
624  it_glb[icnt] = it.second.genlab;
625  it_prc[icnt] = it.second.procid;
626  it_edp[icnt] = it.second.edep;
627  it_nel[icnt] = it.second.numel;
628 
629  unsigned int mintdc = it.second.tdcmin;
630  unsigned int maxtdc = it.second.tdcmax;
631  if (mintdc < (unsigned int)start_tick)mintdc = start_tick;
632  if (maxtdc > (unsigned int)end_tick)maxtdc = end_tick;
633 
634  stck_1[icnt] = mintdc - start_tick;
635  stck_2[icnt] = maxtdc - start_tick;
636 
637  icnt++;
638  if (icnt == 5) break;
639  }
640  }
641 
642  c2numpy_uint16(&npywriter, icnt); // number of peaks
643 
644  for (unsigned int i = 0; i < icnt; ++i) {
645  c2numpy_int32(&npywriter, it_trk[i]); // trackid
646  c2numpy_int32(&npywriter, it_pdg[i]); // pdgcode
647  c2numpy_string(&npywriter, it_glb[i].c_str()); // genlab
648  c2numpy_string(&npywriter, it_prc[i].c_str()); // procid
649  c2numpy_float32(&npywriter,it_edp[i]); // edepo
650  c2numpy_uint32(&npywriter, it_nel[i]); // numelec
651  c2numpy_uint16(&npywriter, stck_1[i]); // stck1
652  c2numpy_uint16(&npywriter, stck_2[i]); // stck2
653  }
654 
655  // .. pad with 0's if number of peaks less than 5
656  for (unsigned int i = icnt; i < 5; ++i) {
659  c2numpy_string(&npywriter, dummystr6.c_str());
660  c2numpy_string(&npywriter, dummystr7.c_str());
665  }
666 
667  for (unsigned int itck = start_tick; itck < (start_tick + fShortWaveformSize); ++itck) {
668  c2numpy_int16(&npywriter, adcvec[itck]);
669  }
670 
671  } // foundmaxsig
672  }
673  }
674  }
675  }
676  }
677  else {
678  //save noise
679  int noisechancount = 0;
680  std::map<raw::ChannelID_t, bool> signalMap;
681  for (auto const& channel : (*simChannelHandle)) {
682  signalMap[channel.Channel()] = true;
683  }
684  // .. create a vector for shuffling the wire channel indices
685  auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
686  size_t nchan = (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
687  std::vector<size_t> randigitmap;
688  for (size_t i=0; i<nchan; ++i) randigitmap.push_back(i);
689  std::shuffle ( randigitmap.begin(), randigitmap.end(), std::mt19937(seed) );
690 
691  for (size_t rdIter = 0; rdIter < (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
692  ++rdIter) {
693 
694  if (noisechancount==fMaxNoiseChannelsPerEvent)break;
695 
696  std::vector<short> adcvec(dataSize); // vector to wire adc values
697  if (rawdigitlist.size()) {
698  size_t ranIdx=randigitmap[rdIter];
699  art::Ptr<raw::RawDigit> digitVec(digitVecHandle, ranIdx);
700  if (signalMap[digitVec->Channel()]) continue;
701 
702  std::vector<short> rawadc(dataSize); // vector to hold uncompressed adc values later
703  if (geo::PlaneGeo::ViewName(fgeom->View(digitVec->Channel())) != fPlaneToDump[0]) continue;
704  raw::Uncompress(digitVec->ADCs(), rawadc, digitVec->GetPedestal(), digitVec->Compression());
705  for (size_t j = 0; j < rawadc.size(); ++j) {
706  adcvec[j] = rawadc[j] - digitVec->GetPedestal();
707  }
708  c2numpy_uint32(&npywriter, evt.id().event());
709  c2numpy_uint32(&npywriter, digitVec->Channel());
711  geo::PlaneGeo::ViewName(fgeom->View(digitVec->Channel())).c_str());
712  }
713  else if (wirelist.size()) {
714  size_t ranIdx=randigitmap[rdIter];
715  art::Ptr<recob::Wire> wire = wirelist[ranIdx];
716  if (signalMap[wire->Channel()]) continue;
717  if (channelStatus.IsBad(wire->Channel())) continue;
718  if (geo::PlaneGeo::ViewName(fgeom->View(wire->Channel())) != fPlaneToDump[0]) continue;
719  const auto& signal = wire->Signal();
720  for (size_t j = 0; j < adcvec.size(); ++j) {
721  adcvec[j] = signal[j];
722  }
723  c2numpy_uint32(&npywriter, evt.id().event());
724  c2numpy_uint32(&npywriter, wire->Channel());
726  }
727 
728  c2numpy_uint16(&npywriter, 0); //number of peaks
729  for (unsigned int i = 0; i < 5; ++i) {
732  c2numpy_string(&npywriter, dummystr6.c_str());
733  c2numpy_string(&npywriter, dummystr7.c_str());
738  }
739 
740  if (fUseFullWaveform) {
741  for (unsigned int itck = 0; itck < dataSize; ++itck) {
742  c2numpy_int16(&npywriter, adcvec[itck]);
743  }
744  }
745  else {
746  int start_tick = int((dataSize - fShortWaveformSize) * fRandFlat.fire(0, 1));
747  for (unsigned int itck = start_tick; itck < (start_tick + fShortWaveformSize); ++itck) {
748  c2numpy_int16(&npywriter, adcvec[itck]);
749  }
750  }
751 
752  ++noisechancount;
753  }
754  std::cout << "Total number of noise channels " << noisechancount << std::endl;
755  }
756 }
def analyze(root, level, gtrees, gbranches, doprint)
Definition: rootstat.py:69
static QCString name
Definition: declinfo.cpp:673
double E(const int i=0) const
Definition: MCParticle.h:233
virtual bool IsBad(raw::ChannelID_t channel) const =0
Returns whether the specified channel is bad in the current run.
float GetPedestal() const
Definition: RawDigit.h:214
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:290
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
Definition: RawDigit.h:210
int c2numpy_init(c2numpy_writer *writer, const std::string outputFilePrefix, int32_t numRowsPerFile)
Definition: c2numpy.h:142
int PdgCode() const
Definition: MCParticle.h:212
static std::string ViewName(geo::View_t view)
Returns the name of the specified view.
Definition: PlaneGeo.cxx:763
ULong64_t Samples() const
Number of samples in the uncompressed ADC data.
Definition: RawDigit.h:213
int c2numpy_int16(c2numpy_writer *writer, int16_t data)
Definition: c2numpy.h:282
void add(const int &track_id, const std::string &gname)
std::string string
Definition: nybbler.cc:12
RawWaveformDump(fhicl::ParameterSet const &p)
std::string fSimChannelLabel
module that made simchannels
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition: RawDigit.h:212
uint8_t channel
Definition: CRTFragment.hh:201
std::string Process() const
Definition: MCParticle.h:215
int c2numpy_close(c2numpy_writer *writer)
Definition: c2numpy.h:407
Particle class.
void analyze(art::Event const &e) override
int c2numpy_uint16(c2numpy_writer *writer, uint16_t data)
Definition: c2numpy.h:314
RunNumber_t run() const
Definition: EventID.h:98
art framework interface to geometry description
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:92
int c2numpy_addcolumn(c2numpy_writer *writer, const std::string name, c2numpy_type type)
Definition: c2numpy.h:158
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
Definition: RawTypes.h:32
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void beginJob()
Definition: Breakpoints.cc:14
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
Definition: Wire.h:231
const double a
std::string getenv(std::string const &name)
Definition: getenv.cc:15
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const
Definition: DataViewImpl.h:479
int c2numpy_uint32(c2numpy_writer *writer, uint32_t data)
Definition: c2numpy.h:322
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
Definition: search.py:1
Class providing information about the quality of channels.
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
std::vector< float > Signal() const
Return a zero-padded full length vector filled with RoI signal.
Definition: Wire.cxx:47
int c2numpy_string(c2numpy_writer *writer, const char *data)
Definition: c2numpy.h:394
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
Definition: RawDigit.h:216
c2numpy_type
Definition: c2numpy.h:29
simb::MCParticle TrackIdToMotherParticle(int const id) const
Declaration of signal hit object.
Encapsulate the construction of a single detector plane.
int c2numpy_float32(c2numpy_writer *writer, float data)
Definition: c2numpy.h:354
genFinder * gf
Interface for experiment-specific channel quality info provider.
static bool * b
Definition: config.cpp:1043
EventNumber_t event() const
Definition: EventID.h:116
Declaration of basic channel signal object.
art::ServiceHandle< cheat::ParticleInventoryService > PIS
TCEvent evt
Definition: DataStructs.cxx:7
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:297
Interface for experiment-specific service for channel quality info.
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:776
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
std::string fSimulationProducerLabel
producer that tracked simulated part. through detector
std::string get_gen(int tid)
SubRunNumber_t subRun() const
Definition: EventID.h:110
EventID id() const
Definition: Event.cc:34
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)