30 #include "cetlib_except/exception.h" 45 #include "larevt/CalibrationDBI/Interface/ChannelStatusProvider.h" 46 #include "larevt/CalibrationDBI/Interface/ChannelStatusService.h" 52 #include "CLHEP/Random/RandFlat.h" 81 class RawWaveformDump;
101 void endJob()
override;
136 std::vector<track_id_to_string> track_id_map;
137 std::set<std::string> generator_names;
138 bool isSorted =
false;
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); });
148 void add(
const int& track_id,
const std::string& gname)
150 this->track_id_map.push_back(std::make_pair(track_id, gname));
151 generator_names.emplace(gname);
154 bool has_gen(std::string gname) {
return static_cast<bool>(generator_names.count(gname)); };
157 if (!isSorted) { this->sort_now(); }
158 return std::lower_bound(track_id_map.begin(),
161 [](
const auto& a,
const auto& b) {
return (a.first < b); })
168 std::string
const instanceName =
"RawWaveformDump";
194 -> declareEngine(instanceName, p,
"SeedForRawWaveformDump"),
198 if (std::getenv(
"CLUSTER") && std::getenv(
"PROCESS")) {
200 string(std::getenv(
"CLUSTER")) +
"-" + string(std::getenv(
"PROCESS")) +
"-";
205 <<
"Both DigitModuleLabel and WireProducerLabel are empty";
210 <<
"Only one of DigitModuleLabel and WireProducerLabel should be set";
225 for (
unsigned int i = 0; i < 5; i++) {
226 std::ostringstream name;
261 for (
unsigned int i = 0;
264 std::ostringstream name;
282 std::unique_ptr<genFinder> gf(
new genFinder());
286 std::vector<art::Ptr<raw::RawDigit>> rawdigitlist;
293 std::vector<art::Ptr<recob::Wire>> wirelist;
298 if (rawdigitlist.empty() && wirelist.empty())
return;
299 if (rawdigitlist.size() && wirelist.size())
return;
302 lariov::ChannelStatusProvider
const& channelStatus =
310 unsigned int dataSize;
311 if (rawdigitlist.size()) {
313 dataSize = digitVec0->
Samples();
316 dataSize = (wirelist[0]->Signal()).
size();
318 if (dataSize != detProp.ReadOutWindowSize()) {
319 std::cout <<
"!!!!! Bad dataSize: " << dataSize << std::endl;
324 std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap;
326 if (rawdigitlist.size()) {
327 for (
size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) {
331 rawdigitMap[chnum] = digitVec;
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) {
341 wireMap[chnum] = wire;
349 <<
" No simb::MCParticle objects in this event - " 350 <<
" Line " << __LINE__ <<
" in file " << __FILE__ << std::endl;
356 if (!simChannelHandle->size())
return;
362 auto mcHandles = evt.
getMany<std::vector<simb::MCTruth>>();
363 std::vector<std::pair<int, std::string>> track_id_to_label;
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);
370 int track_id = ptr->TrackId();
371 gf->
add(track_id, sModuleLabel);
375 std::string dummystr6 =
"none ";
376 std::string dummystr7 =
"none ";
380 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>> Ch2TrkWSInfoMap;
383 std::map<int, std::vector<raw::ChannelID_t>> Trk2ChVecMap;
386 for (
auto const& channel : (*simChannelHandle)) {
393 bool selectThisChannel =
false;
396 std::map<int, WireSigInfo> Trk2WSInfoMap;
399 auto const& timeSlices = channel.TDCIDEMap();
400 for (
auto const& timeSlice : timeSlices) {
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;
408 for (
auto const& energyDeposit : energyDeposits) {
410 if (!energyDeposit.trackID)
continue;
411 int trkid = energyDeposit.trackID;
419 if (!eve_id)
continue;
420 std::string genlab = gf->
get_gen(eve_id);
422 if (Trk2WSInfoMap.find(trkid) == Trk2WSInfoMap.end()) {
427 wsinf.
tdcmin = dataSize - 1;
431 Trk2WSInfoMap.insert(std::pair<int, WireSigInfo>(trkid, wsinf));
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;
440 if (!Trk2WSInfoMap.empty()) {
441 for (std::pair<int, WireSigInfo> itmap : Trk2WSInfoMap) {
451 itmap.second.genlab.resize(6,
' ');
452 itmap.second.procid.resize(7,
' ');
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));
464 Trk2ChVecMap.at(trkid).push_back(ch1);
465 selectThisChannel =
true;
469 if (selectThisChannel) {
470 Ch2TrkWSInfoMap.insert(
471 std::pair<
raw::ChannelID_t, std::map<int, WireSigInfo>>(ch1, Trk2WSInfoMap));
477 std::set<raw::ChannelID_t> selected_channels;
480 if (!Trk2ChVecMap.empty()) {
481 for (
auto const& ittrk : Trk2ChVecMap) {
483 ittrk.second.size());
484 chnum = ittrk.second[i];
486 if (not selected_channels.insert(chnum).second) {
continue; }
488 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>>
::iterator itchn;
489 itchn = Ch2TrkWSInfoMap.find(chnum);
490 if (itchn != Ch2TrkWSInfoMap.end()) {
492 std::vector<short> adcvec(dataSize);
494 if (rawdigitlist.size()) {
495 auto search = rawdigitMap.find(chnum);
496 if (search == rawdigitMap.end())
continue;
498 std::vector<short> rawadc(dataSize);
500 for (
size_t j = 0; j < rawadc.size(); ++j) {
504 else if (wirelist.size()) {
505 auto search = wireMap.find(chnum);
506 if (search == wireMap.end())
continue;
508 const auto& signal = wire->
Signal();
509 for (
size_t j = 0; j < adcvec.size(); ++j) {
510 adcvec[j] = signal[j];
525 unsigned int icnt = 0;
526 for (
auto const& it : itchn->second) {
538 if (icnt == 5)
break;
542 for (
unsigned int i = icnt; i < 5; ++i) {
553 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
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;
572 int sigtdc1, sigtdc2, sighwid, sigfwid, sigtdcm;
574 sigtdc1 = TDCMin - 14 / 2;
575 sigtdc2 = TDCMax + 3 * 14 / 2;
578 sigtdc1 = TDCMin - 32 / 2;
579 sigtdc2 = TDCMax + 32 / 2;
581 sigfwid = sigtdc2 - sigtdc1;
582 sighwid = sigfwid / 2;
583 sigtdcm = sigtdc1 + sighwid;
590 int dt = fShortWaveformSize - sigfwid;
591 start_tick = sigtdc1 - dt *
fRandFlat.fire(0, 1);
595 int mrgn = fShortWaveformSize / 20;
596 int dt = fShortWaveformSize - 2 * mrgn;
597 start_tick = sigtdcm - mrgn - dt *
fRandFlat.fire(0, 1);
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;
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];
617 unsigned int icnt = 0;
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)) {
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;
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;
637 stck_1[icnt] = mintdc - start_tick;
638 stck_2[icnt] = maxtdc - start_tick;
641 if (icnt == 5)
break;
647 for (
unsigned int i = 0; i < icnt; ++i) {
659 for (
unsigned int i = icnt; i < 5; ++i) {
683 int noisechancount = 0;
684 std::map<raw::ChannelID_t, bool> signalMap;
685 for (
auto const& channel : (*simChannelHandle)) {
686 signalMap[channel.Channel()] =
true;
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));
696 for (
size_t rdIter = 0; rdIter < (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
701 std::vector<short> adcvec(dataSize);
702 if (rawdigitlist.size()) {
703 size_t ranIdx = randigitmap[rdIter];
705 if (signalMap[digitVec->
Channel()])
continue;
707 std::vector<short> rawadc(dataSize);
710 for (
size_t j = 0; j < rawadc.size(); ++j) {
718 else if (wirelist.size()) {
719 size_t ranIdx = randigitmap[rdIter];
721 if (signalMap[wire->
Channel()])
continue;
722 if (channelStatus.IsBad(wire->
Channel()))
continue;
724 const auto& signal = wire->
Signal();
725 for (
size_t j = 0; j < adcvec.size(); ++j) {
726 adcvec[j] = signal[j];
734 for (
unsigned int i = 0; i < 5; ++i) {
746 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
752 for (
unsigned int itck = start_tick; itck < (start_tick +
fShortWaveformSize); ++itck) {
759 std::cout <<
"Total number of noise channels " << noisechancount << std::endl;
double E(const int i=0) const
float GetPedestal() const
int c2numpy_init(c2numpy_writer *writer, const std::string outputFilePrefix, int32_t numRowsPerFile)
base_engine_t & createEngine(seed_t seed)
bool has_gen(std::string gname)
int c2numpy_int32(c2numpy_writer *writer, int32_t data)
const ADCvector_t & ADCs() const
Reference to the compressed ADC count vector.
static std::string ViewName(geo::View_t view)
Returns the name of the specified view.
ULong64_t Samples() const
Number of samples in the uncompressed ADC data.
void add(const int &track_id, const std::string &gname)
Declaration of signal hit object.
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition of basic raw digits.
std::string Process() const
int c2numpy_close(c2numpy_writer *writer)
std::pair< int, std::string > track_id_to_string
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
int TrackIdToEveTrackId(int tid) const
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
int c2numpy_addcolumn(c2numpy_writer *writer, const std::string name, c2numpy_type type)
#define DEFINE_ART_MODULE(klass)
raw::ChannelID_t Channel() const
Returns the ID of the channel (or InvalidChannelID)
Collect all the RawData header files together.
int c2numpy_uint32(c2numpy_writer *writer, uint32_t data)
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.
int c2numpy_float32(c2numpy_writer *writer, float data)
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
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)
object containing MC truth information necessary for making RawDigits and doing back tracking ...
int c2numpy_int16(c2numpy_writer *writer, int16_t data)
EventNumber_t event() const
Declaration of basic channel signal object.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
second_as<> second
Type of time stored in seconds, in double precision.
std::string get_gen(int tid)
SubRunNumber_t subRun() const
int c2numpy_string(c2numpy_writer *writer, const char *data)
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const