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;
137 std::vector<track_id_to_string> track_id_map;
138 std::set<std::string> generator_names;
139 bool isSorted =
false;
144 std::sort(this->track_id_map.begin(),
145 this->track_id_map.end(),
146 [](
const auto& a,
const auto& b) {
return (a.first < b.first); });
149 void add(
const int& track_id,
const std::string& gname)
151 this->track_id_map.push_back(std::make_pair(track_id, gname));
152 generator_names.emplace(gname);
155 bool has_gen(std::string gname) {
return static_cast<bool>(generator_names.count(gname)); };
158 if (!isSorted) { this->sort_now(); }
159 return std::lower_bound(track_id_map.begin(),
162 [](
const auto& a,
const auto& b) {
return (a.first < b); })
169 std::string
const instanceName =
"RawWaveformDump";
195 -> declareEngine(instanceName, p,
"SeedForRawWaveformDump"),
199 if (std::getenv(
"CLUSTER") && std::getenv(
"PROCESS")) {
201 string(std::getenv(
"CLUSTER")) +
"-" + string(std::getenv(
"PROCESS")) +
"-";
206 <<
"Both DigitModuleLabel and WireProducerLabel are empty";
211 <<
"Only one of DigitModuleLabel and WireProducerLabel should be set";
226 for (
unsigned int i = 0; i < 5; i++) {
227 std::ostringstream name;
262 for (
unsigned int i = 0;
265 std::ostringstream name;
283 std::unique_ptr<genFinder> gf(
new genFinder());
287 std::vector<art::Ptr<raw::RawDigit>> rawdigitlist;
294 std::vector<art::Ptr<recob::Wire>> wirelist;
299 if (rawdigitlist.empty() && wirelist.empty())
return;
300 if (rawdigitlist.size() && wirelist.size())
return;
303 lariov::ChannelStatusProvider
const& channelStatus =
311 unsigned int dataSize;
312 if (rawdigitlist.size()) {
314 dataSize = digitVec0->
Samples();
317 dataSize = (wirelist[0]->Signal()).
size();
319 if (dataSize != detProp.ReadOutWindowSize()) {
320 std::cout <<
"!!!!! Bad dataSize: " << dataSize << std::endl;
325 std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap;
327 if (rawdigitlist.size()) {
328 for (
size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) {
332 rawdigitMap[chnum] = digitVec;
336 std::map<raw::ChannelID_t, art::Ptr<recob::Wire>> wireMap;
337 if (wirelist.size()) {
338 for (
size_t ich = 0; ich < wirelist.size(); ++ich) {
342 wireMap[chnum] = wire;
350 <<
" No simb::MCParticle objects in this event - " 351 <<
" Line " << __LINE__ <<
" in file " << __FILE__ << std::endl;
357 if (!simChannelHandle->size())
return;
360 auto mcHandles = evt.
getMany<std::vector<simb::MCTruth>>();
361 std::vector<std::pair<int, std::string>> track_id_to_label;
363 for (
auto const& mcHandle : mcHandles) {
364 const std::string& sModuleLabel = mcHandle.provenance()->moduleLabel();
366 std::vector<art::Ptr<simb::MCParticle>> mcParts = findMCParts.at(0);
368 int track_id = ptr->TrackId();
369 gf->
add(track_id, sModuleLabel);
373 std::string dummystr6 =
"none ";
374 std::string dummystr7 =
"none ";
378 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>> Ch2TrkWSInfoMap;
381 std::map<int, std::vector<raw::ChannelID_t>> Trk2ChVecMap;
384 for (
auto const& channel : (*simChannelHandle)) {
391 bool selectThisChannel =
false;
394 std::map<int, WireSigInfo> Trk2WSInfoMap;
397 auto const& timeSlices = channel.TDCIDEMap();
398 for (
auto const& timeSlice : timeSlices) {
400 auto const& energyDeposits = timeSlice.second;
401 auto const tpctime = timeSlice.first;
402 unsigned int tdctick =
static_cast<unsigned int>(clockData.TPCTDC2Tick(
double(tpctime)));
403 if (tdctick < 0 || tdctick > (dataSize - 1))
continue;
406 for (
auto const& energyDeposit : energyDeposits) {
408 if (!energyDeposit.trackID)
continue;
409 int trkid = energyDeposit.trackID;
416 if (!eve_id)
continue;
417 std::string genlab = gf->
get_gen(eve_id);
419 if (Trk2WSInfoMap.find(trkid) == Trk2WSInfoMap.end()) {
424 wsinf.
tdcmin = dataSize - 1;
428 Trk2WSInfoMap.insert(std::pair<int, WireSigInfo>(trkid, wsinf));
430 if (tdctick < Trk2WSInfoMap.at(trkid).tdcmin) Trk2WSInfoMap.at(trkid).tdcmin = tdctick;
431 if (tdctick > Trk2WSInfoMap.at(trkid).tdcmax) Trk2WSInfoMap.at(trkid).tdcmax = tdctick;
432 Trk2WSInfoMap.at(trkid).edep += energyDeposit.energy;
433 Trk2WSInfoMap.at(trkid).numel += energyDeposit.numElectrons;
437 if (!Trk2WSInfoMap.empty()) {
438 for (std::pair<int, WireSigInfo> itmap : Trk2WSInfoMap) {
448 itmap.second.genlab.resize(6,
' ');
449 itmap.second.procid.resize(7,
' ');
456 int trkid = itmap.first;
457 if (Trk2ChVecMap.find(trkid) == Trk2ChVecMap.end()) {
458 std::vector<raw::ChannelID_t> chvec;
459 Trk2ChVecMap.insert(std::pair<
int, std::vector<raw::ChannelID_t>>(trkid, chvec));
461 Trk2ChVecMap.at(trkid).push_back(ch1);
462 selectThisChannel =
true;
466 if (selectThisChannel) {
467 Ch2TrkWSInfoMap.insert(
468 std::pair<
raw::ChannelID_t, std::map<int, WireSigInfo>>(ch1, Trk2WSInfoMap));
474 std::set<raw::ChannelID_t> selected_channels;
477 if (!Trk2ChVecMap.empty()) {
478 for (
auto const& ittrk : Trk2ChVecMap) {
480 ittrk.second.size());
481 chnum = ittrk.second[i];
483 if (not selected_channels.insert(chnum).second) {
continue; }
485 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>>
::iterator itchn;
486 itchn = Ch2TrkWSInfoMap.find(chnum);
487 if (itchn != Ch2TrkWSInfoMap.end()) {
489 std::vector<short> adcvec(dataSize);
491 if (rawdigitlist.size()) {
492 auto search = rawdigitMap.find(chnum);
493 if (search == rawdigitMap.end())
continue;
495 std::vector<short> rawadc(dataSize);
497 for (
size_t j = 0; j < rawadc.size(); ++j) {
501 else if (wirelist.size()) {
502 auto search = wireMap.find(chnum);
503 if (search == wireMap.end())
continue;
505 const auto& signal = wire->
Signal();
506 for (
size_t j = 0; j < adcvec.size(); ++j) {
507 adcvec[j] = signal[j];
523 unsigned int icnt = 0;
524 for (
auto const& it : itchn->second) {
536 if (icnt == 5)
break;
540 for (
unsigned int i = icnt; i < 5; ++i) {
551 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
559 unsigned int TDCMin, TDCMax;
560 bool foundmaxsig =
false;
561 for (
auto& it : itchn->second) {
562 if (it.second.edep > EDep && it.second.numel > 0) {
563 EDep = it.second.edep;
564 TDCMin = it.second.tdcmin;
565 TDCMax = it.second.tdcmax;
570 int sigtdc1, sigtdc2, sighwid, sigfwid, sigtdcm;
572 sigtdc1 = TDCMin - 14 / 2;
573 sigtdc2 = TDCMax + 3 * 14 / 2;
576 sigtdc1 = TDCMin - 32 / 2;
577 sigtdc2 = TDCMax + 32 / 2;
579 sigfwid = sigtdc2 - sigtdc1;
580 sighwid = sigfwid / 2;
581 sigtdcm = sigtdc1 + sighwid;
588 int dt = fShortWaveformSize - sigfwid;
589 start_tick = sigtdc1 - dt *
fRandFlat.fire(0, 1);
593 int mrgn = fShortWaveformSize / 20;
594 int dt = fShortWaveformSize - 2 * mrgn;
595 start_tick = sigtdcm - mrgn - dt *
fRandFlat.fire(0, 1);
597 if (start_tick < 0) start_tick = 0;
598 end_tick = start_tick + fShortWaveformSize - 1;
599 if (end_tick >
int(dataSize - 1)) {
600 end_tick = dataSize - 1;
601 start_tick = end_tick - fShortWaveformSize + 1;
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];
616 unsigned int icnt = 0;
618 for (
auto& it : itchn->second) {
619 if ((it.second.tdcmin >= (
unsigned int)start_tick &&
620 it.second.tdcmin < (
unsigned int)end_tick) ||
621 (it.second.tdcmax > (
unsigned int)start_tick &&
622 it.second.tdcmax <= (
unsigned int)end_tick)) {
624 it_trk[icnt] = it.first;
625 it_pdg[icnt] = it.second.pdgcode;
626 it_glb[icnt] = it.second.genlab;
627 it_prc[icnt] = it.second.procid;
628 it_edp[icnt] = it.second.edep;
629 it_nel[icnt] = it.second.numel;
631 unsigned int mintdc = it.second.tdcmin;
632 unsigned int maxtdc = it.second.tdcmax;
633 if (mintdc < (
unsigned int)start_tick) mintdc = start_tick;
634 if (maxtdc > (
unsigned int)end_tick) maxtdc = end_tick;
636 stck_1[icnt] = mintdc - start_tick;
637 stck_2[icnt] = maxtdc - start_tick;
640 if (icnt == 5)
break;
646 for (
unsigned int i = 0; i < icnt; ++i) {
658 for (
unsigned int i = icnt; i < 5; ++i) {
682 int noisechancount = 0;
683 std::map<raw::ChannelID_t, bool> signalMap;
684 for (
auto const& channel : (*simChannelHandle)) {
685 signalMap[channel.Channel()] =
true;
688 auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
689 size_t nchan = (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
690 std::vector<size_t> randigitmap;
691 for (
size_t i = 0; i < nchan; ++i)
692 randigitmap.push_back(i);
693 std::shuffle(randigitmap.begin(), randigitmap.end(), std::mt19937(
seed));
695 for (
size_t rdIter = 0; rdIter < (rawdigitlist.empty() ? wirelist.size() : rawdigitlist.size());
700 std::vector<short> adcvec(dataSize);
701 if (rawdigitlist.size()) {
702 size_t ranIdx = randigitmap[rdIter];
704 if (signalMap[digitVec->
Channel()])
continue;
706 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;
725 const auto& signal = wire->
Signal();
726 for (
size_t j = 0; j < adcvec.size(); ++j) {
727 adcvec[j] = signal[j];
736 for (
unsigned int i = 0; i < 5; ++i) {
748 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
754 for (
unsigned int itck = start_tick; itck < (start_tick +
fShortWaveformSize); ++itck) {
761 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.
ULong64_t Samples() const
Number of samples in the uncompressed ADC data.
static std::string ViewName(View_t view)
Returns the name of the specified view.
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.
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
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)
Interface for a class providing readout channel mapping to geometry.
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(raw::ChannelID_t const channel) const
Returns the view (wire orientation) on the specified TPC channel.
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)
cet::coded_exception< error, detail::translate > exception
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const