51 #include "cetlib_except/exception.h" 58 #include "CLHEP/Random/RandFlat.h" 91 class RawWaveformClnSigDump;
111 void endJob()
override;
155 bool isSorted =
false;
160 std::sort(this->track_id_map.begin(),
161 this->track_id_map.end(),
162 [](
const auto& a,
const auto& b) {
return (a.first < b.first); });
165 void add(
const int& track_id,
const std::string& gname)
167 this->track_id_map.push_back(std::make_pair(track_id, gname));
168 generator_names.emplace(gname);
171 bool has_gen(std::string gname) {
return static_cast<bool>(generator_names.count(gname)); };
174 if (!isSorted) { this->sort_now(); }
175 return std::lower_bound(track_id_map.begin(),
178 [](
const auto& a,
const auto& b) {
return (a.first < b); })
185 std::string
const instanceName =
"RawWaveformClnSigDump";
197 p.get<std::string>(
"CleanSignalDigitModuleLabel",
"simWire:signal"))
217 -> declareEngine(instanceName, p,
"SeedForRawWaveformDump"),
221 if (std::getenv(
"CLUSTER") && std::getenv(
"PROCESS")) {
223 string(std::getenv(
"CLUSTER")) +
"-" + string(std::getenv(
"PROCESS")) +
"-";
225 string(std::getenv(
"CLUSTER")) +
"-" + string(std::getenv(
"PROCESS")) +
"-";
230 <<
"Both DigitModuleLabel and CleanSignalModuleLabel are empty";
245 for (
unsigned int i = 0; i < 5; i++) {
246 std::ostringstream name;
289 for (
unsigned int i = 0;
292 std::ostringstream name;
300 for (
unsigned int i = 0;
303 std::ostringstream name;
322 std::unique_ptr<genFinder> gf(
new genFinder());
326 std::vector<art::Ptr<raw::RawDigit>> rawdigitlist;
334 std::vector<art::Ptr<raw::RawDigit>> rawdigitlist2;
340 if (rawdigitlist.empty() && rawdigitlist2.empty())
return;
347 unsigned int dataSize;
349 dataSize = digitVec0->
Samples();
350 if (dataSize != detProp.ReadOutWindowSize()) {
351 throw cet::exception(
"RawWaveformClnSigDump") <<
"Bad dataSize: " << dataSize;
354 unsigned int dataSize2 = digitVec20->
Samples();
355 if (dataSize != dataSize2) {
357 <<
"RawDigits from the 2 data products have different dataSizes: " << dataSize <<
"not eq to" 362 std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap;
364 if (rawdigitlist.size()) {
365 for (
size_t rdIter = 0; rdIter < digitVecHandle->size(); ++rdIter) {
369 rawdigitMap[chnum] = digitVec;
372 std::map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> rawdigitMap2;
374 if (rawdigitlist2.size()) {
375 for (
size_t rdIter = 0; rdIter < digitVecHandle2->size(); ++rdIter) {
379 rawdigitMap2[chnum2] = digitVec2;
387 <<
" No simb::MCParticle objects in this event - " 388 <<
" Line " << __LINE__ <<
" in file " << __FILE__ << std::endl;
394 if (!simChannelHandle->size())
return;
400 auto mcHandles = evt.
getMany<std::vector<simb::MCTruth>>();
401 std::vector<std::pair<int, std::string>> track_id_to_label;
403 for (
auto const& mcHandle : mcHandles) {
404 const std::string& sModuleLabel = mcHandle.provenance()->moduleLabel();
406 std::vector<art::Ptr<simb::MCParticle>> mcParts = findMCParts.at(0);
408 int track_id = ptr->TrackId();
409 gf->
add(track_id, sModuleLabel);
413 std::string dummystr6 =
"none ";
414 std::string dummystr7 =
"none ";
418 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>> Ch2TrkWSInfoMap;
421 std::map<int, std::vector<raw::ChannelID_t>> Trk2ChVecMap;
424 for (
auto const& channel : (*simChannelHandle)) {
431 bool selectThisChannel =
false;
434 std::map<int, WireSigInfo> Trk2WSInfoMap;
437 auto const& timeSlices = channel.TDCIDEMap();
438 for (
auto const& timeSlice : timeSlices) {
440 auto const& energyDeposits = timeSlice.second;
441 auto const tpctime = timeSlice.first;
442 unsigned int tdctick =
static_cast<unsigned int>(clockData.TPCTDC2Tick(
double(tpctime)));
443 if (tdctick < 0 || tdctick > (dataSize - 1))
continue;
446 for (
auto const& energyDeposit : energyDeposits) {
448 if (!energyDeposit.trackID)
continue;
449 int trkid = energyDeposit.trackID;
457 if (!eve_id)
continue;
458 std::string genlab = gf->
get_gen(eve_id);
460 if (Trk2WSInfoMap.find(trkid) == Trk2WSInfoMap.end()) {
465 wsinf.
tdcmin = dataSize - 1;
471 Trk2WSInfoMap.insert(std::pair<int, WireSigInfo>(trkid, wsinf));
473 if (tdctick < Trk2WSInfoMap.at(trkid).tdcmin) Trk2WSInfoMap.at(trkid).tdcmin = tdctick;
474 if (tdctick > Trk2WSInfoMap.at(trkid).tdcmax) Trk2WSInfoMap.at(trkid).tdcmax = tdctick;
475 Trk2WSInfoMap.at(trkid).edep += energyDeposit.energy;
476 Trk2WSInfoMap.at(trkid).numel += energyDeposit.numElectrons;
480 auto search2 = rawdigitMap2.find(ch1);
481 if (search2 == rawdigitMap2.end())
continue;
483 std::vector<short> rawadc(dataSize);
485 std::vector<short> adcvec2(dataSize);
486 for (
size_t j = 0; j < rawadc.size(); ++j) {
490 if (!Trk2WSInfoMap.empty()) {
491 for (std::pair<int, WireSigInfo> itmap : Trk2WSInfoMap) {
495 for (
size_t i = itmap.second.tdcmin; i <= itmap.second.tdcmax; i++) {
496 if (
abs(adcvec2[i]) >
abs(pkadc)) {
501 Trk2WSInfoMap.at(itmap.first).tdcpeak = pktdc;
502 Trk2WSInfoMap.at(itmap.first).adcpeak = pkadc;
513 itmap.second.genlab.resize(6,
' ');
514 itmap.second.procid.resize(7,
' ');
521 int trkid = itmap.first;
522 if (Trk2ChVecMap.find(trkid) == Trk2ChVecMap.end()) {
523 std::vector<raw::ChannelID_t> chvec;
524 Trk2ChVecMap.insert(std::pair<
int, std::vector<raw::ChannelID_t>>(trkid, chvec));
526 Trk2ChVecMap.at(trkid).push_back(ch1);
527 selectThisChannel =
true;
531 if (selectThisChannel) {
532 Ch2TrkWSInfoMap.insert(
533 std::pair<
raw::ChannelID_t, std::map<int, WireSigInfo>>(ch1, Trk2WSInfoMap));
539 std::set<raw::ChannelID_t> selected_channels;
542 if (!Trk2ChVecMap.empty()) {
545 using trk2chvecpair = std::pair<const int, std::vector<raw::ChannelID_t>>;
546 std::vector<const trk2chvecpair*> tchpv;
547 for (
auto const& ittrk : Trk2ChVecMap) {
548 tchpv.emplace_back(&ittrk);
550 auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
553 int signalchancount = 0;
554 for (
auto const& itpr : tchpv) {
557 itpr->second.size());
558 chnum = itpr->second[i];
560 if (not selected_channels.insert(chnum).second) {
continue; }
562 std::map<raw::ChannelID_t, std::map<int, WireSigInfo>>
::iterator itchn;
563 itchn = Ch2TrkWSInfoMap.find(chnum);
564 if (itchn != Ch2TrkWSInfoMap.end()) {
566 std::vector<short> adcvec(dataSize);
568 auto search = rawdigitMap.find(chnum);
569 if (search == rawdigitMap.end())
continue;
571 std::vector<short> rawadc(dataSize);
573 for (
size_t j = 0; j < rawadc.size(); ++j) {
577 std::vector<short> adcvec2(
580 auto search2 = rawdigitMap2.find(chnum);
581 if (search2 == rawdigitMap2.end())
continue;
584 for (
size_t j = 0; j < rawadc.size(); ++j) {
600 unsigned int icnt = 0;
601 for (
auto& it : itchn->second) {
615 if (icnt == 5)
break;
619 for (
unsigned int i = icnt; i < 5; ++i) {
632 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
635 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
644 unsigned int TDCMin, TDCMax;
645 bool foundmaxsig =
false;
646 for (
auto& it : itchn->second) {
647 if (it.second.edep > EDep && it.second.adcpeak != 0 && it.second.numel > 0) {
648 EDep = it.second.edep;
649 TDCMin = it.second.tdcmin;
650 TDCMax = it.second.tdcmax;
655 int sigtdc1, sigtdc2, sighwid, sigfwid, sigtdcm;
664 sigfwid = sigtdc2 - sigtdc1;
665 sighwid = sigfwid / 2;
666 sigtdcm = sigtdc1 + sighwid;
673 int dt = fShortWaveformSize - sigfwid;
674 start_tick = sigtdc1 - dt *
fRandFlat.fire(0, 1);
678 int mrgn = fShortWaveformSize / 20;
679 int dt = fShortWaveformSize - 2 * mrgn;
680 start_tick = sigtdcm - mrgn - dt *
fRandFlat.fire(0, 1);
682 if (start_tick < 0) start_tick = 0;
683 end_tick = start_tick + fShortWaveformSize - 1;
684 if (end_tick >
int(dataSize - 1)) {
685 end_tick = dataSize - 1;
686 start_tick = end_tick - fShortWaveformSize + 1;
696 int it_trk[5], it_pdg[5], it_nel[5], pk_tdc[5], pk_adc[5];
697 unsigned int stck_1[5], stck_2[5];
698 std::string it_glb[5], it_prc[5];
701 unsigned int icnt = 0;
703 for (
auto& it : itchn->second) {
705 if ((it.second.tdcmin >= (
unsigned int)start_tick &&
706 it.second.tdcmin < (
unsigned int)end_tick) ||
707 (it.second.tdcmax > (
unsigned int)start_tick &&
708 it.second.tdcmax <= (
unsigned int)end_tick)) {
710 it_trk[icnt] = it.first;
711 it_pdg[icnt] = it.second.pdgcode;
712 it_glb[icnt] = it.second.genlab;
713 it_prc[icnt] = it.second.procid;
714 it_edp[icnt] = it.second.edep;
715 it_nel[icnt] = it.second.numel;
717 unsigned int mintdc = it.second.tdcmin;
718 unsigned int maxtdc = it.second.tdcmax;
719 if (mintdc < (
unsigned int)start_tick) mintdc = start_tick;
720 if (maxtdc > (
unsigned int)end_tick) maxtdc = end_tick;
722 stck_1[icnt] = mintdc - start_tick;
723 stck_2[icnt] = maxtdc - start_tick;
724 pk_tdc[icnt] = it.second.tdcpeak - start_tick;
725 pk_adc[icnt] = it.second.adcpeak;
728 if (icnt == 5)
break;
734 for (
unsigned int i = 0; i < icnt; ++i) {
748 for (
unsigned int i = icnt; i < 5; ++i) {
778 int noisechancount = 0;
779 std::map<raw::ChannelID_t, bool> signalMap;
780 for (
auto const& channel : (*simChannelHandle)) {
781 signalMap[channel.Channel()] =
true;
784 auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
785 std::vector<size_t> randigitmap;
786 for (
size_t i = 0; i < rawdigitlist.size(); ++i)
787 randigitmap.push_back(i);
788 std::shuffle(randigitmap.begin(), randigitmap.end(), std::mt19937(
seed));
790 for (
size_t rdIter = 0; rdIter < rawdigitlist.size(); ++rdIter) {
794 std::vector<float> adcvec(dataSize);
796 size_t ranIdx = randigitmap[rdIter];
798 if (signalMap[digitVec->
Channel()])
continue;
800 std::vector<short> rawadc(dataSize);
804 for (
size_t j = 0; j < rawadc.size(); ++j) {
813 for (
unsigned int i = 0; i < 5; ++i) {
827 for (
unsigned int itck = 0; itck < dataSize; ++itck) {
833 for (
unsigned int itck = start_tick; itck < (start_tick +
fShortWaveformSize); ++itck) {
840 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.
constexpr auto abs(T v)
Returns the absolute value of the argument.
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 end(T &&obj)
ADL-aware version of std::end.
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)
Interface for a class providing readout channel mapping to geometry.
Collect all the RawData header files together.
std::vector< track_id_to_string > track_id_map
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 ...
int c2numpy_float32(c2numpy_writer *writer, float 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
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.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
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