17 #include "dam/HeaderFragmentUnpack.hh" 18 #include "dam/DataFragmentUnpack.hh" 19 #include "dam/TpcFragmentUnpack.hh" 20 #include "dam/TpcStreamUnpack.hh" 21 #include "dam/access/WibFrame.hh" 22 #include "dam/access/Headers.hh" 23 #include "dam/access/TpcStream.hh" 24 #include "dam/access/TpcRanges.hh" 25 #include "dam/access/TpcToc.hh" 26 #include "dam/access/TpcPacket.hh" 27 #include "dam/RceFragmentUnpack.hh" 33 _input_labels_by_apa[3] = p.
get< std::vector<std::string> >(
"APA3InputLabels");
34 _input_labels_by_apa[4] = p.
get< std::vector<std::string> >(
"APA4InputLabels");
35 _input_labels_by_apa[5] = p.
get< std::vector<std::string> >(
"APA5InputLabels");
36 _input_labels_by_apa[6] = p.
get< std::vector<std::string> >(
"APA6InputLabels");
37 _input_labels_by_apa[7] = p.
get< std::vector<std::string> >(
"APA7InputLabels");
38 _input_labels_by_apa[8] = p.
get< std::vector<std::string> >(
"APA8InputLabels");
39 _input_labels_by_apa[-1] = p.
get< std::vector<std::string> >(
"MISCAPAInputLabels");
75 std::vector<raw::RawDigit> &raw_digits,
76 std::vector<raw::RDTimeStamp> &rd_timestamps,
77 std::vector<raw::RDStatus> &rdstatuses)
79 std::vector<int> apalist;
80 apalist.push_back(-1);
90 std::vector<raw::RawDigit> &raw_digits,
91 std::vector<raw::RDTimeStamp> &rd_timestamps,
92 std::vector<raw::RDStatus> &rdstatuses,
93 std::vector<int> &apalist)
97 for (
size_t i=0; i<apalist.size(); ++i)
102 MF_LOG_WARNING(
"IcebergDataInterface:") <<
" No list of input labels known for APA " << apalist.at(i) <<
" Returning no data.";
104 for (
size_t j=0; j<lli->second.size(); ++j)
107 if (retcode > totretcode) totretcode = retcode;
118 std::vector<raw::RawDigit> &raw_digits,
119 std::vector<raw::RDTimeStamp> &rd_timestamps,
120 std::vector<raw::RDStatus> &rdstatuses,
121 std::vector<int> &apalist)
130 if (inputLabel.find(
"TPC") != std::string::npos)
132 _processRCE(evt, inputLabel, raw_digits, rd_timestamps, apalist);
134 else if ( (inputLabel.find(
"FELIX") != std::string::npos) ||
135 (inputLabel.find(
"FRAME14") != std::string::npos) )
137 _processFELIX(evt, inputLabel, raw_digits, rd_timestamps, apalist);
141 throw cet::exception(
"IcebergInterface_tool") <<
"ununderstood fragment branch label: \"" << inputLabel <<
"\"";
144 bool flagged_duplicate =
false;
148 std::set<unsigned int> channels_seen;
150 for (
const auto& rd : raw_digits)
152 unsigned int ichan = rd.Channel();
153 if (channels_seen.find(ichan) == channels_seen.end())
155 channels_seen.insert(ichan);
159 MF_LOG_WARNING(
"IcebergDataInterface:") <<
" Duplicate channel detected: " << ichan <<
" Discarding TPC data for this chunk: " << inputLabel;
161 rd_timestamps.clear();
162 flagged_duplicate =
true;
174 std::vector<size_t> ticklist;
175 for (
size_t i=0; i<raw_digits.size(); ++i)
177 ticklist.push_back(raw_digits.at(i).Samples());
179 size_t tls = ticklist.size();
183 tickmed = TMath::Median(tls,ticklist.data());
185 size_t tickexample=0;
186 std::vector<size_t> dlist;
187 for (
size_t i=0; i<raw_digits.size(); ++i)
189 if (ticklist.at(i) != tickmed)
191 unsigned int channel = raw_digits.at(i).Channel();
196 if ( (crate == 3) && (slot == 3) && (fiber == 2) && ( ticklist.at(i) > 0.9*tickmed && ticklist.at(i) < tickmed ) )
continue;
198 tickexample = ticklist.at(i);
202 if (dlist.size() != 0)
205 MF_LOG_WARNING(
"IcebergDataInterface_tool:") <<
" Discarding data with n_ticks not at the median. Example invalid ticks: " << tickexample;
206 for (
size_t i=dlist.size(); i>0; --i)
208 raw_digits.erase(raw_digits.begin() + dlist.at(i-1));
209 rd_timestamps.erase(rd_timestamps.begin() + dlist.at(i-1));
214 unsigned int statword=0;
218 if (flagged_duplicate) statword = 4;
226 if (rdstatuses.size() < 2)
return;
227 unsigned int statword=0;
230 for (
size_t i=0; i<rdstatuses.size(); ++i)
232 statword |= rdstatuses.at(i).GetStatWord();
233 dcflag |= rdstatuses.at(i).GetCorruptDataDroppedFlag();
234 kcflag |= rdstatuses.at(i).GetCorruptDataKeptFlag();
237 rdstatuses.emplace_back(dcflag,kcflag,statword);
245 std::vector<int> &apalist)
247 size_t n_rce_frags = 0;
248 bool have_data=
false;
249 bool have_data_nc=
false;
251 if (inputLabel.find(
"Container") != std::string::npos)
257 if (!
_rceProcContNCFrags(cont_frags, n_rce_frags,
true, evt, raw_digits, timestamps, apalist))
269 if (!
_rceProcContNCFrags(frags, n_rce_frags,
false, evt, raw_digits, timestamps, apalist))
278 return have_data || have_data_nc;
287 std::vector<int> &apalist)
290 for (
auto const& frag : *frags)
294 bool process_flag =
true;
299 MF_LOG_WARNING(
"_process_RCE:") <<
" Small RCE fragment size: " << frag.sizeBytes() <<
" Discarding just this fragment on request.";
301 process_flag =
false;
312 artdaq::ContainerFragment cont_frag(frag);
313 for (
size_t ii = 0; ii < cont_frag.block_count(); ++ii)
315 if (
_process_RCE_AUX(evt,*cont_frag[ii], raw_digits, timestamps, apalist)) ++n_rce_frags;
320 if (
_process_RCE_AUX(evt,frag, raw_digits, timestamps, apalist)) ++n_rce_frags;
331 const artdaq::Fragment& frag,
334 std::vector<int> &apalist
340 std::ios oldState(
nullptr);
341 oldState.copyfmt(std::cout);
343 std::cout <<
"RCE Fragment: all numbers in hex " <<
std::hex 344 <<
" SequenceID = " << frag.sequenceID()
345 <<
" fragmentID = " << frag.fragmentID()
346 <<
" fragmentType = " << (unsigned)frag.type()
347 <<
" Timestamp = " << frag.timestamp() <<
std::endl;
348 std::cout <<
"Offset Data";
349 artdaq::Fragment fragloc(frag);
350 unsigned char *dbegin =
reinterpret_cast<unsigned char *
>(fragloc.dataAddress());
351 size_t dsize = fragloc.dataSizeBytes();
353 for (
size_t bcounter=0; bcounter<dsize;++bcounter)
355 if ( (offcounter % 8) == 0 )
364 std::cout.copyfmt(oldState);
374 artdaq::Fragment cfragloc(frag);
375 size_t cdsize = cfragloc.dataSizeBytes();
376 const uint64_t* cdptr = (uint64_t
const*) (cfragloc.dataBeginBytes() + 12);
377 HeaderFragmentUnpack
const cdheader(cdptr);
379 if (cdsize>16) cdsize -= 16;
380 bool isOkay = RceFragmentUnpack::isOkay(cdptr,cdsize);
383 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"RCE Fragment isOkay failed: " << cdsize <<
" Discarding this fragment";
394 TString outfilename=
"rce_";
395 outfilename += evt.
run();
397 outfilename += frag.sequenceID();
399 outfilename += frag.fragmentID();
400 outfilename+=
".fragment";
401 rce.
save(outfilename.Data());
402 std::cout <<
"Saved an RCE fragment with " << rce.
size() <<
" streams: " << outfilename <<
std::endl;
405 uint32_t ch_counter = 0;
406 for (
int i = 0; i < rce.
size(); ++i)
409 size_t n_ch = rce_stream->getNChannels();
410 size_t n_ticks = rce_stream->getNTicks();
411 if (n_ticks == 0)
continue;
412 auto const identifier = rce_stream->getIdentifier();
413 uint32_t crateNumber = identifier.getCrate();
417 uint32_t slotNumber = identifier.getSlot();
418 uint32_t fiberNumber = identifier.getFiber();
421 bool foundapainlist =
false;
422 for (
size_t ialist=0; ialist < apalist.size(); ++ ialist)
425 (apalist[ialist] == (
int) crateNumber) ||
426 (apalist[ialist] == 7 && (crateNumber == 0 || crateNumber > 6)) )
428 foundapainlist =
true;
432 if (!foundapainlist)
continue;
440 uint32_t runNumber = evt.
run();
445 if (runNumber > 2572)
447 auto oldfiber = fiberNumber;
448 auto oldslot = slotNumber;
450 if (oldslot == 0 && oldfiber == 4)
455 if (oldslot == 1 && oldfiber == 4)
460 if (oldslot == 1 && oldfiber == 3)
465 if (oldslot == 0 && oldfiber == 3)
474 if (runNumber > 1530 && runNumber < 2572)
476 auto oldfiber = fiberNumber;
477 auto oldslot = slotNumber;
481 if (oldslot == 2 && oldfiber == 1)
486 if (oldslot == 1 && oldfiber == 1)
491 if (oldslot == 2 && oldfiber == 3)
496 if (oldslot == 1 && oldfiber == 3)
502 oldfiber = fiberNumber;
503 oldslot = slotNumber;
505 if (oldslot == 0 && oldfiber == 4)
510 if (oldslot == 1 && oldfiber == 4)
515 if (oldslot == 0 && oldfiber == 3)
520 if (oldslot == 1 && oldfiber == 3)
529 if ( slotNumber == 1 && fiberNumber == 1 )
534 if ( slotNumber == 2 && fiberNumber == 1 )
545 if (slotNumber > 4 || fiberNumber == 0 || fiberNumber > 4)
549 MF_LOG_WARNING(
"_process_RCE:") <<
"Bad crate, slot, fiber number, discarding fragment on request: " 550 << crateNumber <<
" " << slotNumber <<
" " << fiberNumber;
561 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"Nticks not the required value: " << n_ticks <<
" " 580 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"Nticks different for two channel streams: " << n_ticks
597 size_t buffer_size = n_ch * n_ticks;
603 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"n_ch*nticks too large: " << n_ch <<
" * " << n_ticks <<
" = " <<
614 std::vector<int16_t> _buffer(buffer_size);
616 int16_t* adcs = _buffer.data();
617 bool sgmcdretcode = rce_stream->getMultiChannelData(adcs);
622 MF_LOG_WARNING(
"_process_RCE_AUX:") <<
"getMutliChannelData returns error flag: " 623 <<
" c:s:f:ich: " << crateNumber <<
" " << slotNumber <<
" " << fiberNumber <<
" Discarding Data";
633 unsigned int crateloc = crateNumber;
638 for (
size_t i_ch = 0; i_ch < n_ch; i_ch++)
647 for (
size_t i_tick = 0; i_tick < n_ticks; i_tick++)
649 v_adc.push_back(adcs[i_tick]);
662 auto uncompressed_nticks = v_adc.size();
666 raw::RawDigit raw_digit(offlineChannel, uncompressed_nticks, v_adc, cflag);
668 raw_digits.push_back(raw_digit);
671 timestamps.push_back(rdtimestamp);
685 std::vector<int> &apalist)
687 size_t n_felix_frags = 0;
688 bool have_data=
false;
689 bool have_data_nc=
false;
691 if (inputLabel.find(
"Container") != std::string::npos)
697 if (!
_felixProcContNCFrags(cont_frags, n_felix_frags,
true, evt, raw_digits, timestamps, apalist, inputLabel))
710 if (!
_felixProcContNCFrags(frags, n_felix_frags,
false, evt, raw_digits, timestamps, apalist, inputLabel))
719 return have_data || have_data_nc;
723 size_t &n_felix_frags,
728 std::vector<int> &apalist,
731 uint32_t runNumber = evt.
run();
733 for (
auto const& frag : *frags)
737 bool process_flag =
true;
742 MF_LOG_WARNING(
"_process_FELIX:") <<
" Small FELIX fragment size: " << frag.sizeBytes() <<
" Discarding just this fragment on request.";
744 process_flag =
false;
755 artdaq::ContainerFragment cont_frag(frag);
756 for (
size_t ii = 0; ii < cont_frag.block_count(); ++ii)
758 if (
_process_FELIX_AUX(evt,*cont_frag[ii], raw_digits, timestamps, apalist, runNumber, inputLabel)) ++n_felix_frags;
763 if (
_process_FELIX_AUX(evt,frag, raw_digits, timestamps, apalist, runNumber, inputLabel)) ++n_felix_frags;
773 const artdaq::Fragment& frag,
RawDigits& raw_digits,
775 std::vector<int> &apalist,
788 std::ios oldState(
nullptr);
789 oldState.copyfmt(std::cout);
791 std::cout <<
"FELIX Fragment: all numbers in hex " <<
std::hex 792 <<
" SequenceID = " << frag.sequenceID()
793 <<
" fragmentID = " << frag.fragmentID()
794 <<
" fragmentType = " << (unsigned)frag.type()
795 <<
" Timestamp = " << frag.timestamp() <<
std::endl;
796 std::cout <<
"Offset Data";
797 artdaq::Fragment fragloc(frag);
798 unsigned char *dbegin =
reinterpret_cast<unsigned char *
>(fragloc.dataAddress());
799 size_t dsize = fragloc.dataSizeBytes();
801 for (
size_t bcounter=0; bcounter<dsize;++bcounter)
803 if ( (offcounter % 8) == 0 )
812 std::cout.copyfmt(oldState);
820 bool is14 = (inputLabel.find(
"FRAME14") != std::string::npos);
821 std::unique_ptr<dune::FelixFragment> felixptr;
822 std::unique_ptr<dune::Frame14FragmentUnordered> frame14ptr;
848 if (frame_version == 0)
850 std::cout <<
"ICEBERG frame_version = 0; skipping this fragment" <<
std::endl;
868 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"Invalid crate or slot: c=" << (
int) crate <<
" s=" << (
int) slot <<
" discarding FELIX data.";
875 bool foundapainlist =
false;
876 for (
size_t ialist=0; ialist < apalist.size(); ++ ialist)
879 (apalist[ialist] == (
int) crate) ||
880 (apalist[ialist] == 7 && (crate == 0 || crate > 6)) )
882 foundapainlist =
true;
886 if (!foundapainlist)
return true;
891 unsigned n_frames = 0;
904 if (n_frames ==0)
return true;
913 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"n_channels*n_frames too large: " << n_channels <<
" * " << n_frames <<
" = " <<
928 for (
unsigned int iframe=0; iframe<n_frames; ++iframe)
936 <<
" Discarding Data";
952 for(
unsigned ch = 0; ch < n_channels; ++ch) {
956 unsigned int fiberloc = 0;
967 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
" Fiber number " << (
int) fiber <<
" is expected to be 1 or 2 -- revisit logic";
976 unsigned int chloc = ch;
982 unsigned int crateloc =
crate;
987 if (runNumber > 9745)
989 auto slotloc3 = slot;
990 auto fiberloc3 = fiberloc;
994 unsigned int sfmap[10][4] =
1008 for (
size_t imap = 0; imap<10; ++imap)
1010 if (slot == sfmap[imap][0] && fiberloc == sfmap[imap][1])
1012 slotloc3 = sfmap[imap][2];
1013 fiberloc3 = sfmap[imap][3];
1019 std::cout <<
"Slot, fiber not understood in mapping from Run 7 to Run 5: " << (
int) slot <<
" " << (
int) fiberloc <<
std::endl;
1025 fiberloc = fiberloc3;
1032 auto slotloc2 = slot;
1033 auto fiberloc2 = fiberloc;
1035 if (runNumber > 2572)
1038 if (slot == 0 && fiberloc == 4)
1043 if (slot == 1 && fiberloc == 4)
1048 if (slot == 1 && fiberloc == 3)
1053 if (slot == 0 && fiberloc == 3)
1062 if ( slotloc2 == 1 && fiberloc2 == 1 )
1067 if ( slotloc2 == 2 && fiberloc2 == 1 )
1081 std::vector<dune::adc_t> waveform( is14 ?
1085 for(
unsigned int nframe=0;nframe<waveform.size();nframe++){
1086 v_adc.push_back(waveform.at(nframe));
1093 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"Nticks not the required value: " << v_adc.size() <<
" " 1112 MF_LOG_WARNING(
"_process_FELIX_AUX:") <<
"Nticks different for two channel streams: " << v_adc.size()
1125 auto n_ticks = v_adc.size();
1127 raw::RawDigit raw_digit(offlineChannel, n_ticks, v_adc, cflag);
1129 raw_digits.push_back(raw_digit);
1132 timestamps.push_back(rdtimestamp);
1143 size_t asiz = v_adc.size();
1154 imed = TMath::Median(asiz,v_adc.data()) + 0.01;
1156 sigma = TMath::RMS(asiz,v_adc.data());
1162 for (
size_t i=0; i<asiz; ++i)
1164 if (v_adc[i] < imed) s1++;
1165 if (v_adc[i] == imed) sm++;
1169 float mcorr = (-0.5 + (0.5*(
float) asiz - (
float) s1)/ ((
float) sm) );
bool _DiscardedCorruptData
static constexpr size_t num_ch_per_frame
bool _rce_check_buffer_size
size_t total_frames() const
size_t _rce_buffer_size_checklimit
enum raw::_compress Compress_t
Collection of charge vs time digitized from a single readout channel.
TpcStreamUnpack const * get_stream(int i) const
bool _felix_drop_frags_with_badsf
uint8_t slot_no(const unsigned &frame_ID=0) const
uint8_t slot_no(const unsigned &frame_ID=0) const
uint8_t fiber_no(const unsigned &frame_ID=0) const
Handle< PROD > getHandle(SelectorBase const &) const
uint8_t crate_no(const unsigned &frame_ID=0) const
bool _process_RCE_AUX(art::Event &evt, const artdaq::Fragment &frag, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
long int _max_offline_channel
unsigned int _tick_count_this_event
bool _rce_drop_frags_with_badc
bool _enforce_median_tick_count
unsigned int _default_crate_if_unexpected
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
size_t total_frames() const
QTextStream & hex(QTextStream &s)
bool _felix_check_buffer_size
bool _processRCE(art::Event &evt, std::string inputLabel, 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
unsigned int _full_tick_count
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, std::string inputLabel)
struct dune::tde::crate crate
IcebergDataInterface(fhicl::ParameterSet const &ps)
T get(std::string const &key) const
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)
bool _rce_save_frags_to_files
uint64_t timestamp(const unsigned &frame_ID=0) const
bool _enforce_no_duplicate_channels
bool _enforce_full_tick_count
size_t _felix_buffer_size_checklimit
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 APAFromOfflineChannel(unsigned int offlineChannel) const
Returns APA/crate.
size_t _felix_frag_small_size
bool removeCachedProduct(Handle< PROD > &) const
bool _processFELIX(art::Event &evt, std::string inputLabel, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist)
Q_EXPORT QTSManip setw(int w)
void save(const std::string &filepath) const
bool _initialized_tick_count_this_event
uint8_t fiber_no(const unsigned &frame_ID=0) const
bool _drop_small_felix_frags
adc_v get_ADCs_by_channel(const uint8_t channel_ID) const
uint8_t frame_version(const unsigned &frame_ID=0) const
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)
bool _drop_small_rce_frags
uint16_t wib_errors(const unsigned &frame_ID=0) const
uint8_t crate_no(const unsigned &frame_ID=0) const
bool _process_FELIX_AUX(art::Event &evt, const artdaq::Fragment &frag, RawDigits &raw_digits, RDTimeStamps ×tamps, std::vector< int > &apalist, uint32_t runNumber, std::string inputLabel)
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
bool _felix_drop_frags_with_badc
adc_v get_ADCs_by_channel(const uint8_t block_ID, const uint8_t channel_ID) const
void computeMedianSigma(raw::RawDigit::ADCvector_t &v_adc, float &median, float &sigma)
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)
bool _rce_drop_frags_with_badsf
long int _min_offline_channel
#define MF_LOG_WARNING(category)
size_t _rce_frag_small_size
bool _enforce_same_tick_count
std::vector< raw::RDTimeStamp > RDTimeStamps
unsigned int WIBFromOfflineChannel(unsigned int offlineChannel) const
Returns WIB/slot.
Q_EXPORT QTSManip setfill(int f)
unsigned int FEMBFromOfflineChannel(unsigned int offlineChannel) const
Returns FEMB/fiber.
std::vector< Fragment > Fragments
cet::coded_exception< error, detail::translate > exception
double median(sqlite3 *db, std::string const &table_name, std::string const &column_name)
void _collectRDStatus(std::vector< raw::RDStatus > &rdstatuses)
QTextStream & endl(QTextStream &s)
uint64_t timestamp(const unsigned &frame_ID=0) const