12 #include <boost/algorithm/string.hpp>
13 #include <boost/format.hpp>
25 #include <sys/types.h>
31 using namespace boost;
55 strftime(buf,
sizeof(buf),
"%F %R %S s", localtime(&t.tv_sec));
56 buf[
sizeof(buf)-1] = 0;
59 buf2 << buf <<
" " << ((t.tv_usec+500)/1000) <<
" ms";
66 fileNames_(params.getParameter<std::vector<std::
string> >(
"fileNames")),
67 digiInstanceName_(params.getParameter<
string>(
"digiInstanceName")),
68 rawInstanceName_(params.getParameter<
string>(
"rawInstanceName")),
69 timing_(params.getUntrackedParameter<bool>(
"timing",
false)),
70 disabled_(params.getParameter<bool>(
"disabled")),
71 verbosity_(params.getUntrackedParameter<int>(
"verbosity", 0)),
72 produceDigis_(params.getParameter<bool>(
"produceDigis")),
73 produceRaw_(params.getParameter<bool>(
"produceRaw")),
74 inputRawCollection_(params.getParameter<edm::
InputTag>(
"inputRawCollection")),
75 mergeRaw_(params.getParameter<bool>(
"mergeRaw")),
76 ignoreTriggerType_(params.getParameter<bool>(
"ignoreTriggerType")),
80 openedFileRunNumber_(0),
82 fastRetrievalThresh_(0),
83 orbitOffsetFile_(params.getUntrackedParameter<std::
string>(
"orbitOffsetFile",
87 logFileName_(params.getUntrackedParameter<std::
string>(
"logFileName",
88 "matacqProducer.log")),
90 onErrorDisablingEvtCnt_(params.getParameter<int>(
"onErrorDisablingEvtCnt")),
91 timeLogFile_(params.getUntrackedParameter<std::
string>(
"timeLogFile",
"")),
94 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] in MatacqProducer ctor" << endl;
101 cout <<
"[LaserSorter " <<
now() <<
"] "
102 <<
"Failed to open file " <<
timeLogFile_ <<
" to log timing.\n";
123 "EcalMatacqDigiCollection product with instance name '"
129 if(
verbosity_>0)
cout <<
"[Matacq " <<
now() <<
"] registering new FEDRawDataCollection "
130 "product with instance name '"
142 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] exiting MatacqProducer ctor" << endl;
148 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] in MatacqProducer::produce" << endl;
154 << setfill(
'0') << setw(3) << (t.tv_usec+500)/1000 << setfill(
' ')<<
"\t"
155 << (t.tv_usec -
timer_.tv_usec)*1.
156 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\t";
173 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\n";
184 std::unique_ptr<FEDRawDataCollection> rawColl;
187 rawColl = std::make_unique<FEDRawDataCollection>(*sourceColl);
189 rawColl = std::make_unique<FEDRawDataCollection>();
193 auto digiColl = std::make_unique<EcalMatacqDigiCollection>();
211 LogInfo(
"Matacq") <<
"Run " << runNumber <<
"\t Orbit " << orbitId <<
"\n";
215 map<uint32_t,uint32_t>::iterator it =
orbitOffset_.find(runNumber);
217 LogWarning(
"Matacq") <<
"Orbit offset not found for run "
219 <<
". No orbit correction will be applied.";
225 LogInfo(
"Matacq") <<
"Matacq data file found for "
226 <<
"run " << runNumber <<
" orbit " << orbitId;
234 LogWarning(
"Matacq") <<
" Error in Matacq event fragment length! "
236 <<
"*8 Bytes, Parsed len: "
238 <<
"Matacq data will not be included for this event.\n";
245 LogInfo(
"Matacq") <<
"Associating matacq data with orbit id "
247 <<
" to dcc event with orbit id "
248 << orbitId << std::endl;
256 LogWarning(
"Matacq") <<
"No matacq data found for laser event "
257 <<
"of run " << runNumber <<
" orbit "
262 LogWarning(
"Matacq") <<
"No matacq file found for event "
272 <<
" next events will be skipped, following to an "
273 <<
"error on the last processed event, "
274 <<
"which is expected to be persistant.";
282 <<
"Adding FEDRawDataCollection collection "
289 <<
"Adding EcalMatacqDigiCollection collection "
346 if(!
mtell(startPos))
return false;
348 int32_t startOrb = -1;
349 const size_t headerSize = 8*8;
350 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
352 if(startOrb<0) startOrb = 0;
355 cout <<
"[Matacq " <<
now() <<
"] Failed to read matacq header. Moved to start of "
359 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
363 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Looks like matacq file is empty"
370 <<
" looking for orbit " << orbitId
371 <<
". Current file position: " << startPos
372 <<
" Orbit at current position: " << startOrb <<
"\n";
375 bool didCoarseMove =
false;
391 pos = ((int64_t)fsize/evtSize-1)*evtSize;
393 cout <<
"[Matacq " <<
now() <<
"] Estimated position was beyond end of file. "
394 "Changed to " << pos <<
"\n";
401 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] jumping to estimated position "
403 mseek(pos, SEEK_SET,
"Jumping to estimated event position");
404 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
405 didCoarseMove =
true;
409 didCoarseMove =
false;
410 if(!
mread((
char*)&
data_[0], headerSize,
"Reading event header",
true)){
415 if(
verbosity_)
cout <<
"[Matacq " <<
now() <<
"] Event orbit outside of orbit range "
416 "of matacq data file events\n";
426 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Fast retrieval threshold increased from "
428 fastRetrievalThresh_ = 2*
abs(orb-orbitId);
434 && (
abs(orb-orbitId) >
abs(startOrb-orbitId))){
435 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimation (-> orbit " << orb <<
") "
436 "was worst than original position (-> orbit "
438 <<
"). Restoring position (" << startPos <<
").\n";
439 mseek(startPos, SEEK_SET);
440 mread((
char*)&
data_[0], headerSize,
"Reading event header",
true);
445 bool searchBackward = (orb>orbitId)?
true:
false;
451 cout <<
"[Matacq " <<
now() <<
"] read DCC length is null! Cancels matacq event search "
452 <<
" and move matacq file pointer to beginning of the file. "
453 <<
"(" << __FILE__ <<
":" << __LINE__ <<
")."
460 enum state_t { searching,
found, failed } state = searching;
462 while(state == searching){
469 cout <<
"[Matacq " <<
now() <<
"] Header read at file position "
471 <<
": orbit = " << orb
472 <<
" len = " << len <<
"x8 Byte"
473 <<
" run = " << run <<
"\n";
476 && (runNumber==0 || runNumber==run)){
480 if((
int)
data_.size() < len*8){
484 " matacq event." <<
"\n";
485 if(!
mread((
char*)&
data_[0], len*8,
"Reading matacq event")){
492 if((searchBackward && (orb < orbitId))
493 || (!searchBackward && (orb > orbitId))){
497 <<
"] No matacq data found for run " << run
498 <<
", orbit ID " << orbitId <<
"." <<
"\n";
500 off_t
offset = (searchBackward?-len:len)*8;
503 cout <<
"[Matacq " <<
now() <<
"] In matacq file, moving "
504 <<
abs(offset) <<
" byte " << (offset>0?
"forward":
"backward")
508 if(
mseek(offset, SEEK_CUR,
509 (searchBackward?
"Moving to previous event":
510 "Moving to next event"))
511 &&
mread((
char*)&
data_[0], headerSize,
"Reading event header",
514 if(!searchBackward)
mseek(-len*8, SEEK_CUR,
515 "Moving to start of last complete event");
529 cout <<
"[Matacq " <<
now() <<
"] Event found was at the end of the file. Moving "
530 "stream position to beginning of this event."
533 mseek(-(
int)len*8-1, SEEK_CUR,
534 "Moving to beginning of last matacq event");
537 return (state==
found);
546 if(fileChange!=0) *fileChange =
false;
552 const string runNumberFormat =
"%08d";
553 string sRunNumber = str(
boost::format(runNumberFormat) % runNumber);
559 boost::algorithm::replace_all(fname,
"%run_subdir%",
561 boost::algorithm::replace_all(fname,
"%run_number%", sRunNumber);
564 <<
"Looking for a file with path "
568 LogInfo(
"Matacq") <<
"Uses matacq data file: '" << fname <<
"'\n";
574 "for run " << runNumber <<
"\n";
577 if(fileChange!=0) *fileChange =
false;
582 LogWarning(
"Matacq") <<
"Failed to open file " << fname <<
"\n";
585 if(fileChange!=0) *fileChange =
false;
591 if(fileChange!=0) *fileChange =
true;
610 <<
"No FED raw data collection found. ECAL raw data are "
611 "required to retrieve the orbit ID";
615 for(
int id=601;
id<=654; ++id){
618 const int orbitIdOffset64 = 3;
619 if(data.
size()>=8*(orbitIdOffset64+1)){
620 const unsigned char* pOrbit = data.
data() + orbitIdOffset64*8;
621 int thisOrbit = pOrbit[0]
629 <<
"Orbit ID inconsitency in DCC headers";
633 if(thisOrbit!=0) orbit = thisOrbit;
640 LogWarning(
"NotFound") <<
"Failed to retrieve orbit ID of event "
651 <<
"No FED raw data collection found. ECAL raw data are "
652 "required to retrieve the trigger type";
656 for(
int id=601;
id<=654; ++id){
659 const int detailedTrigger32 = 5;
660 if(data.
size()>=4*(detailedTrigger32+1)){
661 const unsigned char* pTType = data.
data() + detailedTrigger32*4;
662 int tType = pTType[1] & 0x7;
667 int tType = stat.
result(&p);
670 LogWarning(
"NotFound") <<
"No ECAL DCC data found\n";
675 LogWarning(
"EventCorruption") <<
"Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
684 const size_t headerSize = 8*8;
685 unsigned char data[headerSize];
686 if(!mp->
mread((
char*)data, headerSize)){
718 <<
" Number of events: " << nEvents << endl;
722 mp->
mseek(last, SEEK_SET,
"Moving to beginning of last complete "
724 if(!mp->
mread((
char*) data, headerSize,
"Reading matacq header",
true)){
725 LogWarning(
"Matacq") <<
"Fast matacq event retrieval failure. "
726 "Falling back to safe retrieval mode.";
733 if(
verbosity_>1)
cout <<
"[Matacq " <<
now() <<
"] Last event orbit: " << lastOrb
734 <<
" last event length: " << lastLen << endl;
740 <<
"Fast matacq event retrieval failure: it looks like "
741 "the matacq file contains events of different sizes.";
757 if(orb<firstOrbit_)
return -1;
759 (((
uint64_t)(orb-firstOrbit_))/orbitStepMean_)*eventLength_*8
761 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimated Position for orbit " << orb
762 <<
": " << r << endl;
772 cout <<
"[Matacq " <<
now() <<
"] Time elapsed between first event and "
773 "destruction of MatacqProducer: "
775 + (t.tv_usec-
startTime_.tv_usec)*1.e-6) <<
"s\n";
783 <<
"Failed to open orbit ID correction file '"
787 cout <<
"[Matacq " <<
now() <<
"] "
788 <<
"Offset to substract to Matacq events Orbit ID: \n"
789 <<
"#Run Number\t Offset\n";
810 <<
"Syntax error in Orbit offset file '"
813 cout << run <<
"\t" << orbit <<
"\n";
818 #ifdef USE_STORAGE_MANAGER
820 if(0==
inFile_.get())
return false;
827 << __FILE__ <<
": "<< __LINE__ <<
"\n";
832 cout <<
"[Matacq " <<
now() <<
"] ";
833 if(mess)
cout << mess <<
". ";
834 cout <<
"Random access error on input matacq file. ";
835 if(whence==SEEK_SET)
cout <<
"Failed to seek absolute position " <<
offset;
836 else if(whence==SEEK_CUR)
cout <<
"Failed to move " << offset <<
" bytes forward";
837 else if(whence==SEEK_END)
cout <<
"Failed to seek position at " << offset <<
" bytes before end of file";
838 cout <<
". Reopening file. " << e.
what() <<
"\n";
847 if(0==
inFile_.get())
return false;
853 if(0==
inFile_.get())
return false;
856 if(!
mtell(pos))
return false;
860 rc = (n==
inFile_->xread(buf, n));
863 cout <<
"[Matacq " <<
now() <<
"] ";
864 if(mess)
cout << mess <<
". ";
865 cout <<
"Read failure from input matacq file: "
880 if(
inFile_.get()==0)
return false;
887 if(file==0)
return false;
893 <<
"File will be reopened.";
933 if(
inFile_.get()==0)
return true;
937 #else //USE_STORAGE_MANAGER not defined
940 const int rc = fseeko(
inFile_, offset, whence);
942 cout <<
"[Matacq " <<
now() <<
"] ";
943 if(mess)
cout << mess <<
". ";
944 cout <<
"Random access error on input matacq file. "
961 bool rc = (pos!=-1) && (1==fread(buf, n, 1,
inFile_));
964 cout <<
"[Matacq " <<
now() <<
"] ";
965 if(mess)
cout << mess <<
". ";
966 cout <<
"Read failure from input matacq file.\n";
971 if(0!=fseeko(
inFile_, pos, SEEK_SET)){
973 cout <<
"[Matacq " <<
now() <<
"] ";
974 if(mess)
cout << mess <<
". ";
975 cout <<
"Failed to restore file position of "
976 "before read error. Rewind file.\n";
989 if(0!=fstat(fileno(
inFile_), &buf)){
1001 return fseeko(
inFile_, 0, SEEK_SET)!=0;
1006 return 0==stat(name.c_str(), &dummy);
1019 inFile_ = fopen(name.c_str(),
"r");
1043 #endif //USE_STORAGE_MANAGER defined
1046 int millions = runNumber / (1000*1000);
1047 int thousands = (runNumber-millions*1000*1000) / 1000;
1048 int units = runNumber-millions*1000*1000 - thousands*1000;
1049 return str(
boost::format(
"%03d/%03d/%03d") % millions % thousands % units);
1055 logFile_ <<
"[" <<
now() <<
"] Event count for run "
1058 <<
"Laser event with Matacq data: "
1060 <<
"Non laser event (according to DCC header) with Matacq data: "
static const char runNumber_[]
virtual char const * what() const
bool check(const std::string &url, IOOffset *size=0) const
T getParameter(std::string const &) const
std::map< uint32_t, uint32_t > orbitOffset_
static const int matacqFedId_
static const int orbitTolerance_
unsigned getRunNum() const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
static unsigned getOrbitId(unsigned char *data, size_t size)
std::vector< std::string > fileNames_
string format
Some error handling for the usage.
std::string digiInstanceName_
size_t size() const
Lenght of the data buffer in bytes.
uint32_t getRunNumber(edm::Event &ev) const
uint32_t getOrbitId() const
unsigned getDccLen() const
uint32_t getOrbitId(edm::Event &ev) const
bool mcheck(const std::string &name)
struct MatacqProducer::stats_t stats_
static const StorageFactory * get(void)
int64_t pos(int orb) const
edm::EDGetTokenT< FEDRawDataCollection > inputRawCollectionToken_
void addMatacqData(edm::Event &event)
double nNonLaserEventsWithMatacq
static std::string runSubDir(uint32_t runNumber)
int onErrorDisablingEvtCnt_
bool mread(char *buf, size_t n, const char *mess=0, bool peek=false)
virtual void produce(edm::Event &event, const edm::EventSetup &eventSetup)
Abs< T >::type abs(const T &t)
double nLaserEventsWithMatacq
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::string orbitOffsetFile_
int getCalibTriggerType(edm::Event &ev) const
unsigned long long uint64_t
MatacqDataFormatter formatter_
uint32_t openedFileRunNumber_
void init(MatacqProducer *mp)
bool mtell(filepos_t &pos)
static const int bufferSize
MatacqProducer(const edm::ParameterSet ¶ms)
TString units(TString variable, Char_t axis)
char data[epos_bytes_allocation]
bool mseek(filepos_t offset, int whence=SEEK_SET, const char *mess=0)
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
std::string rawInstanceName_
volatile std::atomic< bool > shutdown_flag false
T result(double *proba) const
std::vector< unsigned char > data_
void newRun(int prevRun, int newRun)
virtual void rewind(void)
bool getMatacqFile(uint32_t runNumber, uint32_t orbitId, bool *fileChange=0)
static const stats_t stats_init
bool mopen(const std::string &name)
std::unique_ptr< Storage > open(const std::string &url, int mode=IOFlags::OpenRead) const
bool getMatacqEvent(uint32_t runNumber, int32_t orbitId, bool fileChange)