37 #include "canvas/Persistency/Common/FindManyP.h" 60 #include "nurandom/RandomUtils/NuRandomService.h" 61 #include "CLHEP/Random/RandFlat.h" 82 class RawWaveformClnSigDump;
102 void endJob()
override;
144 bool isSorted =
false;
150 std::sort(this->track_id_map.begin(),
151 this->track_id_map.end(),
152 [](
const auto&
a,
const auto&
b) {
return (
a.first <
b.first); });
158 this->track_id_map.push_back(std::make_pair(track_id, gname));
159 generator_names.emplace(gname);
165 return static_cast<bool>(generator_names.count(gname));
170 if (!isSorted) { this->sort_now(); }
171 return std::lower_bound(track_id_map.begin(),
174 [](
const auto&
a,
const auto&
b) {
return (a.first <
b); })
181 std::string const instanceName =
"RawWaveformClnSigDump";
210 instanceName,
p,
"SeedForRawWaveformDump"),
211 "HepJamesRandom", instanceName)}
220 <<
"Both DigitModuleLabel and CleanSignalModuleLabel are empty";
236 for (
unsigned int i = 0; i < 5; i++) {
237 std::ostringstream
name;
280 for (
unsigned int i = 0;
283 std::ostringstream
name;
291 for (
unsigned int i = 0;
294 std::ostringstream
name;
319 std::vector<art::Ptr<raw::RawDigit>> rawdigitlist;
327 std::vector<art::Ptr<raw::RawDigit>> rawdigitlist2;
333 if (rawdigitlist.empty() && rawdigitlist2.empty())
return;
340 unsigned int dataSize;
342 dataSize = digitVec0->
Samples();
343 if (dataSize != detProp.ReadOutWindowSize()) {
344 throw cet::exception(
"RawWaveformClnSigDump") <<
"Bad dataSize: " << dataSize;
347 unsigned int dataSize2 = digitVec20->
Samples();
348 if (dataSize != dataSize2) {
350 <<
"RawDigits from the 2 data products have different dataSizes: " << dataSize <<
"not eq to" << dataSize2;
354 std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap;
356 if (rawdigitlist.size()) {
357 for (
size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) {
361 rawdigitMap[chnum] = digitVec;
364 std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap2;
366 if (rawdigitlist2.size()) {
367 for (
size_t rdIter = 0; rdIter < digitVecHandle2->size(); ++rdIter) {
371 rawdigitMap2[chnum2] = digitVec2;
379 <<
" No simb::MCParticle objects in this event - " 380 <<
" Line " << __LINE__ <<
" in file " << __FILE__ <<
std::endl;
384 auto simChannelHandle =
387 if (!simChannelHandle->size())
return;
393 auto mcHandles = evt.
getMany<std::vector<simb::MCTruth>>();
394 std::vector<std::pair<int, std::string>> track_id_to_label;
396 for (
auto const& mcHandle : mcHandles) {
397 const std::string& sModuleLabel = mcHandle.provenance()->moduleLabel();
399 std::vector<art::Ptr<simb::MCParticle>> mcParts = findMCParts.at(0);
401 int track_id = ptr->TrackId();
402 gf->
add(track_id, sModuleLabel);
411 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>> Ch2TrkWSInfoMap;
414 std::map<int, std::vector<raw::ChannelID_t>> Trk2ChVecMap;
417 for (
auto const&
channel : (*simChannelHandle)) {
424 bool selectThisChannel =
false;
427 std::map<int, WireSigInfo> Trk2WSInfoMap;
430 auto const& timeSlices =
channel.TDCIDEMap();
431 for (
auto const& timeSlice : timeSlices) {
433 auto const& energyDeposits = timeSlice.second;
434 auto const tpctime = timeSlice.first;
435 unsigned int tdctick =
static_cast<unsigned int>(clockData.TPCTDC2Tick(
double(tpctime)));
436 if (tdctick < 0 || tdctick > (dataSize - 1))
continue;
439 for (
auto const& energyDeposit : energyDeposits) {
441 if (!energyDeposit.trackID)
continue;
442 int trkid = energyDeposit.trackID;
450 if (!eve_id)
continue;
453 if (Trk2WSInfoMap.find(trkid) == Trk2WSInfoMap.end()) {
458 wsinf.
tdcmin = dataSize - 1;
464 Trk2WSInfoMap.insert(std::pair<int, WireSigInfo>(trkid, wsinf));
466 if (tdctick < Trk2WSInfoMap.at(trkid).tdcmin) Trk2WSInfoMap.at(trkid).tdcmin = tdctick;
467 if (tdctick > Trk2WSInfoMap.at(trkid).tdcmax) Trk2WSInfoMap.at(trkid).tdcmax = tdctick;
468 Trk2WSInfoMap.at(trkid).edep += energyDeposit.energy;
469 Trk2WSInfoMap.at(trkid).numel += energyDeposit.numElectrons;
473 auto search2 = rawdigitMap2.find(ch1);
474 if (search2 == rawdigitMap2.end())
continue;
476 std::vector<short> rawadc(dataSize);
478 std::vector<short> adcvec2(dataSize);
479 for (
size_t j = 0; j < rawadc.size(); ++j) {
483 if (!Trk2WSInfoMap.empty()) {
484 for (std::pair<int, WireSigInfo> itmap : Trk2WSInfoMap) {
488 for (
size_t i = itmap.second.tdcmin; i <= itmap.second.tdcmax; i++) {
489 if (
abs(adcvec2[i]) >
abs(pkadc)) {
494 Trk2WSInfoMap.at(itmap.first).tdcpeak = pktdc;
495 Trk2WSInfoMap.at(itmap.first).adcpeak = pkadc;
506 itmap.second.genlab.resize(6,
' ');
507 itmap.second.procid.resize(7,
' ');
514 int trkid = itmap.first;
515 if (Trk2ChVecMap.find(trkid) == Trk2ChVecMap.end()) {
516 std::vector<raw::ChannelID_t> chvec;
517 Trk2ChVecMap.insert(std::pair<
int, std::vector<raw::ChannelID_t>>(trkid, chvec));
519 Trk2ChVecMap.at(trkid).push_back(ch1);
520 selectThisChannel =
true;
524 if (selectThisChannel) {
525 Ch2TrkWSInfoMap.insert(
526 std::pair<
raw::ChannelID_t, std::map<int, WireSigInfo>>(ch1, Trk2WSInfoMap));
532 std::set<raw::ChannelID_t> selected_channels;
535 if (!Trk2ChVecMap.empty()) {
536 for (
auto const& ittrk : Trk2ChVecMap) {
537 int i =
fRandFlat.fireInt(ittrk.second.size());
538 chnum = ittrk.second[i];
540 if (not selected_channels.insert(chnum).second) {
544 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>>
::iterator itchn;
545 itchn = Ch2TrkWSInfoMap.find(chnum);
546 if (itchn != Ch2TrkWSInfoMap.end()) {
548 std::vector<short> adcvec(dataSize);
550 auto search = rawdigitMap.find(chnum);
551 if (
search == rawdigitMap.end())
continue;
553 std::vector<short> rawadc(dataSize);
555 for (
size_t j = 0; j < rawadc.size(); ++j) {
559 std::vector<short> adcvec2(dataSize);
561 auto search2 = rawdigitMap2.find(chnum);
562 if (search2 == rawdigitMap2.end())
continue;
565 for (
size_t j = 0; j < rawadc.size(); ++j) {
580 unsigned int icnt = 0;
581 for (
auto & it : itchn->second) {
595 if (icnt == 5)
break;
599 for (
unsigned int i = icnt; i < 5; ++i) {
612 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
615 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
623 unsigned int TDCMin, TDCMax;
624 bool foundmaxsig =
false;
625 for (
auto & it : itchn->second) {
626 if (it.second.edep > EDep && it.second.adcpeak != 0 && it.second.numel > 0){
627 EDep = it.second.edep;
628 TDCMin = it.second.tdcmin;
629 TDCMax = it.second.tdcmax;
634 int sigtdc1, sigtdc2, sighwid, sigfwid, sigtdcm;
642 sigfwid=sigtdc2 - sigtdc1;
644 sigtdcm=sigtdc1+sighwid;
651 int dt = fShortWaveformSize - sigfwid;
652 start_tick = sigtdc1 - dt *
fRandFlat.fire(0,1);
656 int mrgn = fShortWaveformSize/20;
657 int dt = fShortWaveformSize - 2*mrgn;
658 start_tick = sigtdcm - mrgn - dt *
fRandFlat.fire(0,1);
660 if (start_tick < 0) start_tick = 0;
661 end_tick = start_tick + fShortWaveformSize - 1;
662 if (end_tick >
int (dataSize - 1)) {
663 end_tick = dataSize - 1;
664 start_tick = end_tick - fShortWaveformSize + 1;
673 int it_trk[5],it_pdg[5],it_nel[5],pk_tdc[5],pk_adc[5];
674 unsigned int stck_1[5],stck_2[5];
678 unsigned int icnt = 0;
680 for (
auto & it : itchn->second) {
682 if (( it.second.tdcmin >= (
unsigned int)start_tick && it.second.tdcmin < (
unsigned int)end_tick) ||
683 ( it.second.tdcmax > (
unsigned int)start_tick && it.second.tdcmax <= (
unsigned int)end_tick)) {
685 it_trk[icnt] = it.first;
686 it_pdg[icnt] = it.second.pdgcode;
687 it_glb[icnt] = it.second.genlab;
688 it_prc[icnt] = it.second.procid;
689 it_edp[icnt] = it.second.edep;
690 it_nel[icnt] = it.second.numel;
692 unsigned int mintdc = it.second.tdcmin;
693 unsigned int maxtdc = it.second.tdcmax;
694 if (mintdc < (
unsigned int)start_tick)mintdc = start_tick;
695 if (maxtdc > (
unsigned int)end_tick)maxtdc = end_tick;
697 stck_1[icnt] = mintdc - start_tick;
698 stck_2[icnt] = maxtdc - start_tick;
699 pk_tdc[icnt] = it.second.tdcpeak - start_tick;
700 pk_adc[icnt] = it.second.adcpeak;
703 if (icnt == 5)
break;
709 for (
unsigned int i = 0; i < icnt; ++i) {
723 for (
unsigned int i = icnt; i < 5; ++i) {
736 for (
unsigned int itck = start_tick; itck < (start_tick +
fShortWaveformSize); ++itck) {
739 for (
unsigned int itck = start_tick; itck < (start_tick +
fShortWaveformSize); ++itck) {
751 int noisechancount = 0;
752 std::map<raw::ChannelID_t, bool> signalMap;
753 for (
auto const&
channel : (*simChannelHandle)) {
754 signalMap[
channel.Channel()] =
true;
758 std::vector<size_t> randigitmap;
759 for (
size_t i=0; i<rawdigitlist.size(); ++i) randigitmap.push_back(i);
760 std::shuffle ( randigitmap.begin(), randigitmap.end(), std::mt19937(seed) );
762 for (
size_t rdIter = 0; rdIter < rawdigitlist.size(); ++rdIter) {
766 std::vector<float> adcvec(dataSize);
768 size_t ranIdx=randigitmap[rdIter];
770 if (signalMap[digitVec->
Channel()])
continue;
772 std::vector<short> rawadc(dataSize);
775 for (
size_t j = 0; j < rawadc.size(); ++j) {
784 for (
unsigned int i = 0; i < 5; ++i) {
798 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
804 for (
unsigned int itck = start_tick; itck < (start_tick +
fShortWaveformSize); ++itck) {
811 std::cout <<
"Total number of noise channels " << noisechancount <<
std::endl;
def analyze(root, level, gtrees, gbranches, doprint)
double E(const int i=0) const
float GetPedestal() const
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.
int c2numpy_init(c2numpy_writer *writer, const std::string outputFilePrefix, int32_t numRowsPerFile)
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.
int c2numpy_int16(c2numpy_writer *writer, int16_t data)
void add(const int &track_id, const std::string &gname)
ChannelID_t Channel() const
DAQ channel this raw data was read from.
std::string Process() const
int c2numpy_close(c2numpy_writer *writer)
int c2numpy_uint16(c2numpy_writer *writer, uint16_t data)
art framework interface to geometry description
std::pair< int, std::string > track_id_to_string
int c2numpy_addcolumn(c2numpy_writer *writer, const std::string name, c2numpy_type type)
int TrackIdToEveTrackId(int tid) const
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
#define DEFINE_ART_MODULE(klass)
std::string getenv(std::string const &name)
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const
std::vector< track_id_to_string > track_id_map
int c2numpy_uint32(c2numpy_writer *writer, uint32_t data)
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
int c2numpy_string(c2numpy_writer *writer, const char *data)
raw::Compress_t Compression() const
Compression algorithm used to store the ADC counts.
std::set< std::string > generator_names
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)
Interface for experiment-specific channel quality info provider.
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)
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.
second_as<> second
Type of time stored in seconds, in double precision.
std::string get_gen(int tid)
SubRunNumber_t subRun() const
cet::coded_exception< error, detail::translate > exception
QTextStream & endl(QTextStream &s)