30 #define CHECKBYTEBIT(var, pos) ( (var) & (1<<pos) ) 31 #define DCBITFLAG 0x0 // 0x0 LSB -> 0x7 MSB 32 #define GETDCFLAG(info) (CHECKBYTEBIT(info, DCBITFLAG)>0) 38 #define EVDQFLAG(info) ( (info & 0x3F ) == EVCARD0 ) 51 size_t asiz = v_adc.size();
60 imed = TMath::Median(asiz,v_adc.data()) + 0.01;
62 sigma = TMath::RMS(asiz,v_adc.data());
68 for (
size_t i = 0; i < asiz; ++i) {
69 if (v_adc.at(i) < imed) s1++;
70 if (v_adc.at(i) == imed) sm++;
73 float mcorr = (-0.5 + (0.5*(
float) asiz - (
float) s1)/ ((
float) sm) );
80 void unpackData(
const char *buf,
size_t nb,
bool cflag,
81 unsigned nsa, adcbuf_t &
data )
88 const BYTE* start = buf;
89 const BYTE* stop = start +
nb;
96 uint16_t tmp1 = ((v1 << 4) + ((v2 >> 4) & 0xf)) & 0xfff;
97 uint16_t tmp2 = (((v2 & 0xf) << 8 ) + (v3 & 0xff)) & 0xfff;
101 data.back()[sz++] = (short)tmp1;
104 data.back()[sz++] = (short)tmp2;
109 mf::LogError(__FUNCTION__)<<
"The format for the compressed data is not defined";
117 const T *ptr =
static_cast<const T*
>(in);
125 virtual const char* what()
const throw()
127 return "Bad file format";
130 static formatexception
fex;
143 __sourceHelper( pm ),
148 const std::string myname =
"VDColdboxTDERawInput::ctor: ";
151 __nsacro = pset.
get<
size_t>(
"SamplesPerChannel", 10000);
157 auto vecped_crps = pset.
get<std::vector<UIntVec>>(
"InvertBaseline", std::vector<UIntVec>());
158 auto select_crps = pset.
get<std::vector<unsigned>>(
"SelectCRPs", std::vector<unsigned>());
160 std::map<unsigned, unsigned> invped_crps;
161 if( !vecped_crps.empty() ){
162 for(
auto &v : vecped_crps ){
165 mf::LogError(__FUNCTION__)<<
"Bad vector size for pedestal inversion parameters";
169 invped_crps[ v[0] ] = v[1];
175 std::cout << myname <<
" Configuration : " <<
std::endl;
177 std::cout << myname <<
" SamplesPerChannel : " << __nsacro <<
std::endl;
178 std::cout << myname <<
" maxEvents : " << __maxEvents <<
std::endl;
179 std::cout << myname <<
" StartTDEChCRU : " << __start_tde_cru <<
std::endl;
180 std::cout << myname <<
" OutputLabelRawDigits : " << __outlbl_digits <<
std::endl;
181 std::cout << myname <<
" OutputLabelRDStatus : " << __outlbl_status <<
std::endl;
182 std::cout << myname <<
" OutputLabelRDtime : " << __outlbl_rdtime <<
std::endl;
183 std::cout << myname <<
" SelectCRPs : ";
184 if( select_crps.empty() ) std::cout<<
"all"<<std::endl;
187 std::ostringstream vstr;
188 std::copy(select_crps.begin(), select_crps.end()-1,
189 std::ostream_iterator<unsigned>(vstr,
", "));
190 vstr << select_crps.back();
193 std::cout << myname <<
" InvertBaseline : ";
194 if( invped_crps.empty() ) std::cout<<
"None"<<std::endl;
196 for(
auto const &
m : invped_crps )
197 std::cout<<
"[ "<<
m.first<<
", "<<
m.second<<
" ] ";
221 auto crpidx = channelMap->get_crpidx();
222 for(
auto c: crpidx )
225 std::vector<dune::tde::ChannelId> chidx = channelMap->find_by_crp(
c,
true );
229 if( !select_crps.empty() )
231 if( std::find( select_crps.begin(), select_crps.end(),
c ) == select_crps.end() )
237 std::cout<<myname<<
" CRP "<<
c<<
" selected "<<keep<<
std::endl;
241 if( !invped_crps.empty() ){
242 auto it = invped_crps.find(
c );
243 if( it != invped_crps.end() ){
248 std::cout<<myname<<
" CRP "<<
c<<
" baseline "<<invped<<
std::endl;
252 for(
auto id: chidx )
255 if( !
id.
exists() )
continue;
256 __daqch.push_back(
id.seqn() );
263 std::cout << myname <<
" Readout info : " <<
std::endl;
264 std::cout<<myname <<
" Number of CRPs from chmap : " << channelMap->ncrps() <<
std::endl;
265 std::cout<<myname <<
" Total channels expected : " <<
__daqch.size() <<
std::endl;
287 __file.open( name.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
292 <<
"Error opening binary file " << name <<
std::endl;
299 __file.seekg(0, std::ios::beg);
306 <<
"File " << name <<
" does not have any events"<<
std::endl;
337 std::vector<BYTE> buf;
351 uint64_t sec =
event.trigstamp.tv_sec;
352 uint64_t nsec =
event.trigstamp.tv_nsec;
355 uint64_t tval = (sec << 32) + nsec;
379 event.evnum, tstamp );
382 std::unique_ptr< std::vector<raw::RawDigit> >
data (
new std::vector<raw::RawDigit> );
383 std::unique_ptr< std::vector<raw::RDTimeStamp> > rdtm (
new std::vector<raw::RDTimeStamp> );
384 std::unique_ptr< std::vector<raw::RDStatus> >
stat (
new std::vector<raw::RDStatus> );
387 data->reserve( event.crodata.size() );
392 for(
size_t i=0;i<
event.crodata.size();i++ )
406 for(
auto &
e : event.crodata[daqch] ){
407 float v = (
float)invped -
e;
412 float median = 0., sigma = 0.;
413 getMedianSigma(event.crodata[daqch], median, sigma);
416 event.compression) );
417 data->back().SetPedestal( median, sigma );
420 data->push_back(
raw::RawDigit(ch, 0, dummy, event.compression) );
423 unsigned int statword = 0;
424 bool discarded =
false;
431 if( discarded ) statword |= 1;
432 if( kept ) statword |= 2;
433 stat->emplace_back( discarded, kept, statword );
436 uint16_t rdtsflags = 0xd;
451 size_t ipos = lbl.find(
":");
452 if ( ipos != std::string::npos ) {
453 res = lbl.substr(ipos + 1);
454 lbl = lbl.substr(0, ipos);
477 __file.read( &bytes[0], sz );
496 std::vector<BYTE> buf;
497 size_t msz = 2*
sizeof(uint32_t);
499 if( buf.size() != msz )
501 mf::LogError(__FUNCTION__)<<
"Could not read number of events";
507 __eventNum = ConvertToValue<uint32_t>(&buf[4]);
513 mf::LogError(__FUNCTION__)<<
"File does not contain any events";
519 size_t evtsz =
__eventNum * 4 *
sizeof(uint32_t);
533 for(
unsigned i=0;i<buf.size();i+=16 )
539 uint32_t sz = ConvertToValue<uint32_t>(&buf[i+o]);
548 for(
size_t i=0; i <
__evsz.size()-1; i++)
553 mf::LogError(__FUNCTION__)<<
"Event table does not match file size";
580 static const unsigned evskey = 0xFF;
581 if( !( ((k0 & 0xFF) == evskey) && ((k1 & 0xFF) == evskey) ) )
583 mf::LogError(__FUNCTION__)<<
"Event delimiting word could not be detected";
589 ei.
runnum = ConvertToValue<uint32_t>( buf+rval );
590 rval +=
sizeof( ei.
runnum );
596 ei.
ti = ConvertToValue<triginfo_t>( buf+rval );
597 rval +=
sizeof(ei.
ti);
600 ei.
evflag = (uint8_t)buf[rval++];
603 ei.
evnum = ConvertToValue<uint32_t>( buf+rval );
604 rval +=
sizeof( ei.
evnum );
607 ei.
evszlro = ConvertToValue<uint32_t>( buf+rval );
611 ei.
evszcro = ConvertToValue<uint32_t>( buf+rval );
627 std::vector<fragment_t> frags;
633 if( idx >= buf.size() )
break;
635 if( rval == 0 )
return false;
638 afrag.
bytes = &buf[idx];
644 if( frags.size() >= 1 )
646 if( frags[0].ei.runnum != afrag.
ei.
runnum )
648 mf::LogError(__FUNCTION__)<<
"run numbers do not match among event fragments";
651 if( frags[0].ei.evnum != afrag.
ei.
evnum )
653 mf::LogError(__FUNCTION__)<<
"event numbers do not match among event fragments";
654 mf::LogDebug(__FUNCTION__)<<
"event number "<< frags[0].ei.evnum;
661 frags.push_back( afrag );
667 std::vector<std::thread>
threads(frags.size() - 1);
670 for (
unsigned i = 1; i<frags.size(); ++i)
672 auto afrag = frags.begin() + i;
673 threads[i-1] = std::thread([&iomutex, i, nsa, afrag] {
675 std::lock_guard<std::mutex> iolock(iomutex);
678 if (i==100000) std::cout <<
"Unpack thread #" << i <<
" is running\n";
680 unpackData( afrag->bytes + afrag->ei.evszlro, afrag->ei.evszcro,
681 GETDCFLAG(afrag->ei.runflags), nsa, afrag->crodata);
686 auto f0 = frags.begin();
687 event.good =
EVDQFLAG( f0->ei.evflag );
688 event.runnum = f0->ei.runnum;
689 event.runflags = f0->ei.runflags;
691 event.evnum = f0->ei.evnum;
692 event.evflags.push_back( f0->ei.evflag );
694 event.trigtype = f0->ei.ti.type;
695 event.trignum = f0->ei.ti.num;
696 event.trigstamp = f0->ei.ti.ts;
698 unpackData( f0->bytes + f0->ei.evszlro, f0->ei.evszcro,
GETDCFLAG(f0->ei.runflags),
699 nsa, event.crodata );
711 for (
auto it = frags.begin() + 1; it != frags.end(); ++it )
713 event.good = (
event.good &&
EVDQFLAG( it->ei.evflag ) );
714 event.evflags.push_back( it->ei.evflag );
717 event.crodata.reserve(event.crodata.size() + it->crodata.size() );
719 std::back_inserter( event.crodata ));
734 std::regex
r(
"[[:digit:]]+_([[:digit:]]+)_[[:alnum:]]+\\.[[:alnum:]]+");
737 if( !regex_search( s, m, r ) )
739 mf::LogWarning(__FUNCTION__) <<
"Unable to extract seqno from file name "<<
s;
747 rval = std::stoi( m.str(1) );
749 catch (
const std::invalid_argument& ia)
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
static constexpr double nb
Collection of charge vs time digitized from a single readout channel.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
SubRunPrincipal * makeSubRunPrincipal(SubRunAuxiliary const &subRunAux) const
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
RunPrincipal * makeRunPrincipal(RunAuxiliary const &runAux) const
bool exists(std::string path)
TypeLabel const & reconstitutes(std::string const &modLabel, std::string const &instanceName={})
T get(std::string const &key) const
IDNumber_t< Level::SubRun > SubRunNumber_t
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
T ConvertToValue(const void *in)
std::enable_if_t<!detail::range_sets_supported(P::branch_type)> put_product_in_principal(std::unique_ptr< T > &&product, P &principal, std::string const &module_label, std::string const &instance_name={})
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
cet::LibraryManager dummy("noplugin")
EventPrincipal * makeEventPrincipal(EventAuxiliary const &eventAux, std::unique_ptr< History > &&history) const
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
SubRunNumber_t subRun() const
unsigned int ChannelID_t
Type representing the ID of a readout channel.
byte bytes
Alias for common language habits.
static formatexception fex
cet::coded_exception< error, detail::translate > exception
double median(sqlite3 *db, std::string const &table_name, std::string const &column_name)
QTextStream & endl(QTextStream &s)
Event finding and building.
IDNumber_t< Level::Run > RunNumber_t