16 #include "dam/HeaderFragmentUnpack.hh" 17 #include "dam/DataFragmentUnpack.hh" 18 #include "dam/TpcFragmentUnpack.hh" 19 #include "dam/TpcStreamUnpack.hh" 20 #include "dam/access/WibFrame.hh" 21 #include "dam/access/Headers.hh" 22 #include "dam/access/TpcStream.hh" 23 #include "dam/access/TpcRanges.hh" 24 #include "dam/access/TpcToc.hh" 25 #include "dam/access/TpcPacket.hh" 26 #include "dam/RceFragmentUnpack.hh" 32 _input_labels_by_apa[3] = p.
get< std::vector<std::string> >(
"APA3InputLabels");
33 _input_labels_by_apa[4] = p.
get< std::vector<std::string> >(
"APA4InputLabels");
34 _input_labels_by_apa[5] = p.
get< std::vector<std::string> >(
"APA5InputLabels");
35 _input_labels_by_apa[6] = p.
get< std::vector<std::string> >(
"APA6InputLabels");
36 _input_labels_by_apa[7] = p.
get< std::vector<std::string> >(
"APA7InputLabels");
37 _input_labels_by_apa[8] = p.
get< std::vector<std::string> >(
"APA8InputLabels");
38 _input_labels_by_apa[-1] = p.
get< std::vector<std::string> >(
"MISCAPAInputLabels");
79 std::vector<raw::RawDigit> &raw_digits,
80 std::vector<raw::RDTimeStamp> &rd_timestamps,
81 std::vector<raw::RDStatus> &rdstatuses)
83 std::vector<int> apalist;
84 apalist.push_back(-1);
94 std::vector<raw::RawDigit> &raw_digits,
95 std::vector<raw::RDTimeStamp> &rd_timestamps,
96 std::vector<raw::RDStatus> &rdstatuses,
97 std::vector<int> &apalist)
101 for (
size_t i=0; i<apalist.size(); ++i)
106 MF_LOG_WARNING(
"PDSPTPCDataInterface:") <<
" No list of input labels known for APA " << apalist.at(i) <<
" Returning no data.";
108 for (
size_t j=0; j<lli->second.size(); ++j)
111 if (retcode > totretcode) totretcode = retcode;
122 std::vector<raw::RawDigit> &raw_digits,
123 std::vector<raw::RDTimeStamp> &rd_timestamps,
124 std::vector<raw::RDStatus> &rdstatuses,
125 std::vector<int> &apalist)
134 if (inputLabel.find(
"TPC") != std::string::npos)
136 _processRCE(evt, inputLabel, raw_digits, rd_timestamps, apalist);
138 else if (inputLabel.find(
"FELIX") != std::string::npos)
140 _processFELIX(evt, inputLabel, raw_digits, rd_timestamps, apalist);
144 throw cet::exception(
"PDSPTPCInterface_tool") <<
"ununderstood fragment branch label: \"" << inputLabel <<
"\"";
147 bool flagged_duplicate =
false;
151 std::set<unsigned int> channels_seen;
153 for (
const auto& rd : raw_digits)
155 unsigned int ichan = rd.Channel();
156 if (channels_seen.find(ichan) == channels_seen.end())
158 channels_seen.insert(ichan);
162 MF_LOG_WARNING(
"PDSPTPCDataInterface:") <<
" Duplicate channel detected: " << ichan <<
" Discarding TPC data for this chunk: " << inputLabel;
164 rd_timestamps.clear();
165 flagged_duplicate =
true;
177 std::vector<size_t> ticklist;
178 for (
size_t i=0; i<raw_digits.size(); ++i)
180 ticklist.push_back(raw_digits.at(i).Samples());
182 size_t tls = ticklist.size();
186 tickmed = TMath::Median(tls,ticklist.data());
188 size_t tickexample=0;
189 std::vector<size_t> dlist;
190 for (
size_t i=0; i<raw_digits.size(); ++i)
192 if (ticklist.at(i) != tickmed)
194 unsigned int channel = raw_digits.at(i).Channel();
199 if ( (crate == 3) && (slot == 3) && (fiber == 2) && ( ticklist.at(i) > 0.9*tickmed && ticklist.at(i) < tickmed ) )
continue;
201 tickexample = ticklist.at(i);
205 if (dlist.size() != 0)
208 MF_LOG_WARNING(
"PDSPTPCDataInterface_tool:") <<
" Discarding data with n_ticks not at the median. Example invalid ticks: " << tickexample;
209 for (
size_t i=dlist.size(); i>0; --i)
211 raw_digits.erase(raw_digits.begin() + dlist.at(i-1));
212 rd_timestamps.erase(rd_timestamps.begin() + dlist.at(i-1));
217 unsigned int statword=0;
221 if (flagged_duplicate) statword = 4;
229 if (rdstatuses.size() < 2)
return;
230 unsigned int statword=0;
233 for (
size_t i=0; i<rdstatuses.size(); ++i)
235 statword |= rdstatuses.at(i).GetStatWord();
236 dcflag |= rdstatuses.at(i).GetCorruptDataDroppedFlag();
237 kcflag |= rdstatuses.at(i).GetCorruptDataKeptFlag();
240 rdstatuses.emplace_back(dcflag,kcflag,statword);
248 std::vector<int> &apalist)
250 size_t n_rce_frags = 0;
251 bool have_data=
false;
252 bool have_data_nc=
false;
254 if (inputLabel.find(
"Container") != std::string::npos)
260 if (!
_rceProcContNCFrags(cont_frags, n_rce_frags,
true, evt, raw_digits, timestamps, apalist))
272 if (!
_rceProcContNCFrags(frags, n_rce_frags,
false, evt, raw_digits, timestamps, apalist))
281 return have_data || have_data_nc;
290 std::vector<int> &apalist)
293 for (
auto const& frag : *frags)
297 bool process_flag =
true;
302 MF_LOG_WARNING(
"_process_RCE:") <<
" Small RCE fragment size: " << frag.sizeBytes() <<
" Discarding just this fragment on request.";
304 process_flag =
false;
315 artdaq::ContainerFragment cont_frag(frag);
316 for (
size_t ii = 0; ii < cont_frag.block_count(); ++ii)
318 if (
_process_RCE_AUX(evt,*cont_frag[ii], raw_digits, timestamps, apalist)) ++n_rce_frags;
323 if (
_process_RCE_AUX(evt,frag, raw_digits, timestamps, apalist)) ++n_rce_frags;
334 const artdaq::Fragment& frag,
337 std::vector<int> &apalist
343 std::ios oldState(
nullptr);
344 oldState.copyfmt(std::cout);
346 std::cout <<
"RCE Fragment: all numbers in hex " <<
std::hex 347 <<
" SequenceID = " << frag.sequenceID()
348 <<
" fragmentID = " << frag.fragmentID()
349 <<
" fragmentType = " << (unsigned)frag.type()
350 <<
" Timestamp = " << frag.timestamp() <<
std::endl;
351 std::cout <<
"Offset Data";
352 artdaq::Fragment fragloc(frag);
353 unsigned char *dbegin =
reinterpret_cast<unsigned char *
>(fragloc.dataAddress());
354 size_t dsize = fragloc.dataSizeBytes();
356 for (
size_t bcounter=0; bcounter<dsize;++bcounter)
358 if ( (offcounter % 8) == 0 )
367 std::cout.copyfmt(oldState);
380 TString outfilename=
"rce_";
381 outfilename += evt.
run();
383 outfilename += frag.sequenceID();
385 outfilename += frag.fragmentID();
386 outfilename+=
".fragment";
387 rce.
save(outfilename.Data());
388 std::cout <<
"Saved an RCE fragment with " << rce.
size() <<
" streams: " << outfilename <<
std::endl;
392 artdaq::Fragment cfragloc(frag);
393 size_t cdsize = cfragloc.dataSizeBytes();
394 const uint64_t* cdptr = (uint64_t
const*) (cfragloc.dataBeginBytes() + 12);
395 HeaderFragmentUnpack
const cdheader(cdptr);
397 if (cdsize>16) cdsize -= 16;
398 bool isOkay = RceFragmentUnpack::isOkay(cdptr,cdsize);
401 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"RCE Fragment isOkay failed: " << cdsize <<
" Discarding this fragment";
410 uint32_t ch_counter = 0;
411 for (
int i = 0; i < rce.
size(); ++i)
414 size_t n_ch = rce_stream->getNChannels();
415 size_t n_ticks = rce_stream->getNTicks();
416 if (n_ticks == 0)
continue;
417 auto const identifier = rce_stream->getIdentifier();
418 uint32_t crateNumber = identifier.getCrate();
419 uint32_t slotNumber = identifier.getSlot();
420 uint32_t fiberNumber = identifier.getFiber();
423 bool foundapainlist =
false;
424 for (
size_t ialist=0; ialist < apalist.size(); ++ ialist)
427 (apalist[ialist] == (
int) crateNumber) ||
428 (apalist[ialist] == 7 && (crateNumber == 0 || crateNumber > 6)) )
430 foundapainlist =
true;
434 if (!foundapainlist)
continue;
438 if (slotNumber > 4 || fiberNumber == 0 || fiberNumber > 4)
442 MF_LOG_WARNING(
"_process_RCE:") <<
"Bad crate, slot, fiber number, discarding fragment on request: " 443 << crateNumber <<
" " << slotNumber <<
" " << fiberNumber;
454 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"Nticks not the required value: " << n_ticks <<
" " 473 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"Nticks different for two channel streams: " << n_ticks
490 size_t buffer_size = n_ch * n_ticks;
496 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"n_ch*nticks too large: " << n_ch <<
" * " << n_ticks <<
" = " <<
507 std::vector<int16_t> _buffer(buffer_size);
509 int16_t* adcs = _buffer.data();
510 bool sgmcdretcode = rce_stream->getMultiChannelData(adcs);
515 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"getMutliChannelData returns error flag: " 516 <<
" c:s:f:ich: " << crateNumber <<
" " << slotNumber <<
" " << fiberNumber <<
" Discarding Data";
526 unsigned int crateloc = crateNumber;
530 for (
size_t i_ch = 0; i_ch < n_ch; i_ch++)
543 v_adc.push_back(adcs[i_tick+_rce_fix110_nticks]);
547 v_adc.push_back(v_adc.back());
553 for (
size_t i_tick = 0; i_tick < n_ticks; i_tick++)
555 v_adc.push_back(adcs[i_tick]);
568 auto uncompressed_nticks = v_adc.size();
572 raw::RawDigit raw_digit(offlineChannel, uncompressed_nticks, v_adc, cflag);
574 raw_digits.push_back(raw_digit);
577 timestamps.push_back(rdtimestamp);
591 std::vector<int> &apalist)
593 size_t n_felix_frags = 0;
594 bool have_data=
false;
595 bool have_data_nc=
false;
597 if (inputLabel.find(
"Container") != std::string::npos)
624 return have_data || have_data_nc;
628 size_t &n_felix_frags,
633 std::vector<int> &apalist)
635 for (
auto const& frag : *frags)
639 bool process_flag =
true;
644 MF_LOG_WARNING(
"_process_FELIX:") <<
" Small FELIX fragment size: " << frag.sizeBytes() <<
" Discarding just this fragment on request.";
646 process_flag =
false;
657 artdaq::ContainerFragment cont_frag(frag);
658 for (
size_t ii = 0; ii < cont_frag.block_count(); ++ii)
660 if (
_process_FELIX_AUX(evt,*cont_frag[ii], raw_digits, timestamps, apalist)) ++n_felix_frags;
675 const artdaq::Fragment& frag,
RawDigits& raw_digits,
677 std::vector<int> &apalist)
688 std::ios oldState(
nullptr);
689 oldState.copyfmt(std::cout);
691 std::cout <<
"FELIX Fragment: all numbers in hex " <<
std::hex 692 <<
" SequenceID = " << frag.sequenceID()
693 <<
" fragmentID = " << frag.fragmentID()
694 <<
" fragmentType = " << (unsigned)frag.type()
695 <<
" Timestamp = " << frag.timestamp() <<
std::endl;
696 std::cout <<
"Offset Data";
697 artdaq::Fragment fragloc(frag);
698 unsigned char *dbegin =
reinterpret_cast<unsigned char *
>(fragloc.dataAddress());
699 size_t dsize = fragloc.dataSizeBytes();
701 for (
size_t bcounter=0; bcounter<dsize;++bcounter)
703 if ( (offcounter % 8) == 0 )
712 std::cout.copyfmt(oldState);
723 uint8_t slot = felix.
slot_no(0);
731 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"Invalid crate or slot: c=" << (
int) crate <<
" s=" << (
int) slot <<
" discarding FELIX data.";
738 bool foundapainlist =
false;
739 for (
size_t ialist=0; ialist < apalist.size(); ++ ialist)
742 (apalist[ialist] == (
int) crate) ||
743 (apalist[ialist] == 7 && (crate == 0 || crate > 6)) )
745 foundapainlist =
true;
749 if (!foundapainlist)
return true;
756 if (n_frames ==0)
return true;
765 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"n_channels*n_frames too large: " << n_channels <<
" * " << n_frames <<
" = " <<
776 for (
unsigned int iframe=0; iframe<n_frames; ++iframe)
784 <<
" Discarding Data";
798 for(
unsigned ch = 0; ch < n_channels; ++ch) {
802 unsigned int fiberloc = 0;
813 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
" Fiber number " << (
int) fiber <<
" is expected to be 1 or 2 -- revisit logic";
822 unsigned int chloc = ch;
828 unsigned int crateloc =
crate;
841 for(
unsigned int nframe=0;nframe<waveform.size();nframe++){
842 v_adc.push_back(waveform.at(nframe));
849 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"Nticks not the required value: " << v_adc.size() <<
" " 868 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"Nticks different for two channel streams: " << v_adc.size()
881 auto n_ticks = v_adc.size();
883 raw::RawDigit raw_digit(offlineChannel, n_ticks, v_adc, cflag);
885 raw_digits.push_back(raw_digit);
888 timestamps.push_back(rdtimestamp);
899 size_t asiz = v_adc.size();
910 imed = TMath::Median(asiz,v_adc.data()) + 0.01;
912 sigma = TMath::RMS(asiz,v_adc.data());
918 for (
size_t i=0; i<asiz; ++i)
920 if (v_adc[i] < imed) s1++;
921 if (v_adc[i] == imed) sm++;
925 float mcorr = (-0.5 + (0.5*(
float) asiz - (
float) s1)/ ((
float) sm) );
size_t _felix_buffer_size_checklimit
bool _processFELIX(art::Event &evt, std::string inputLabel, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
int retrieveData(art::Event &evt, std::string inputlabel, std::vector< raw::RawDigit > &raw_digits, std::vector< raw::RDTimeStamp > &rd_timestamps, std::vector< raw::RDStatus > &rdstatuses)
static constexpr size_t num_ch_per_frame
int retrieveDataForSpecifiedAPAs(art::Event &evt, std::vector< raw::RawDigit > &raw_digits, std::vector< raw::RDTimeStamp > &rd_timestamps, std::vector< raw::RDStatus > &rdstatuses, std::vector< int > &apalist)
size_t total_frames() const
enum raw::_compress Compress_t
unsigned int InstalledAPAFromOfflineChannel(unsigned int offlineChannel) const
Returns APA/crate in installation notation.
Collection of charge vs time digitized from a single readout channel.
bool _felix_drop_frags_with_badsf
TpcStreamUnpack const * get_stream(int i) const
long int _max_offline_channel
uint8_t slot_no(const unsigned &frame_ID=0) const
void computeMedianSigma(raw::RawDigit::ADCvector_t &v_adc, float &median, float &sigma)
Handle< PROD > getHandle(SelectorBase const &) const
uint8_t crate_no(const unsigned &frame_ID=0) const
unsigned int _default_crate_if_unexpected
bool _drop_small_felix_frags
unsigned int _tick_count_this_event
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
bool _processRCE(art::Event &evt, std::string inputLabel, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
size_t _rce_frag_small_size
QTextStream & hex(QTextStream &s)
bool _process_FELIX_AUX(art::Event &evt, const artdaq::Fragment &frag, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
unsigned int GetOfflineNumberFromDetectorElements(unsigned int crate, unsigned int slot, unsigned int fiber, unsigned int fembchannel, FelixOrRCE frswitch)
std::vector< raw::RawDigit > RawDigits
bool _rce_drop_frags_with_badsf
bool _DiscardedCorruptData
bool _enforce_no_duplicate_channels
unsigned int _full_tick_count
bool _felix_check_buffer_size
struct dune::tde::crate crate
bool _enforce_same_tick_count
T get(std::string const &key) const
bool _felixProcContNCFrags(art::Handle< artdaq::Fragments > frags, size_t &n_felix_frags, bool is_container, art::Event &evt, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
void _collectRDStatus(std::vector< raw::RDStatus > &rdstatuses)
bool _enforce_median_tick_count
bool _drop_small_rce_frags
bool _initialized_tick_count_this_event
bool removeCachedProduct(Handle< PROD > &) const
Q_EXPORT QTSManip setw(int w)
void save(const std::string &filepath) const
uint8_t fiber_no(const unsigned &frame_ID=0) const
adc_v get_ADCs_by_channel(const uint8_t channel_ID) const
bool _enforce_full_tick_count
unsigned int WIBFromOfflineChannel(unsigned int offlineChannel) const
Returns WIB/slot.
uint16_t wib_errors(const unsigned &frame_ID=0) const
std::vector< raw::RDTimeStamp > RDTimeStamps
int retrieveDataAPAListWithLabels(art::Event &evt, std::string inputlabel, std::vector< raw::RawDigit > &raw_digits, std::vector< raw::RDTimeStamp > &rd_timestamps, std::vector< raw::RDStatus > &rdstatuses, std::vector< int > &apalist)
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
size_t _felix_frag_small_size
bool _rceProcContNCFrags(art::Handle< artdaq::Fragments > frags, size_t &n_rce_frags, bool is_container, art::Event &evt, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
std::map< int, std::vector< std::string > > _input_labels_by_apa
unsigned int FEMBFromOfflineChannel(unsigned int offlineChannel) const
Returns FEMB/fiber.
bool _rce_save_frags_to_files
bool _process_RCE_AUX(art::Event &evt, const artdaq::Fragment &frag, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
bool _rce_check_buffer_size
bool _felix_drop_frags_with_badc
#define MF_LOG_WARNING(category)
PDSPTPCDataInterface(fhicl::ParameterSet const &ps)
Q_EXPORT QTSManip setfill(int f)
unsigned int _rce_fix110_nticks
std::vector< Fragment > Fragments
unsigned int ChipFromOfflineChannel(unsigned int offlineChannel) const
Returns chip number.
cet::coded_exception< error, detail::translate > exception
double median(sqlite3 *db, std::string const &table_name, std::string const &column_name)
size_t _rce_buffer_size_checklimit
QTextStream & endl(QTextStream &s)
bool _rce_drop_frags_with_badc
long int _min_offline_channel
uint64_t timestamp(const unsigned &frame_ID=0) const