13 #include <boost/algorithm/string.hpp>
14 #include <boost/format.hpp>
26 #include <sys/types.h>
32 using namespace boost;
56 strftime(buf,
sizeof(buf),
"%F %R %S s", localtime(&t.tv_sec));
57 buf[
sizeof(buf)-1] = 0;
60 buf2 << buf <<
" " << ((t.tv_usec+500)/1000) <<
" ms";
67 fileNames_(params.getParameter<std::vector<std::
string> >(
"fileNames")),
68 digiInstanceName_(params.getParameter<
string>(
"digiInstanceName")),
69 rawInstanceName_(params.getParameter<
string>(
"rawInstanceName")),
70 timing_(params.getUntrackedParameter<bool>(
"timing",
false)),
71 disabled_(params.getParameter<bool>(
"disabled")),
72 verbosity_(params.getUntrackedParameter<int>(
"verbosity", 0)),
73 produceDigis_(params.getParameter<bool>(
"produceDigis")),
74 produceRaw_(params.getParameter<bool>(
"produceRaw")),
75 inputRawCollection_(params.getParameter<
InputTag>(
"inputRawCollection")),
76 mergeRaw_(params.getParameter<bool>(
"mergeRaw")),
77 ignoreTriggerType_(params.getParameter<bool>(
"ignoreTriggerType")),
81 openedFileRunNumber_(0),
83 fastRetrievalThresh_(0),
84 orbitOffsetFile_(params.getUntrackedParameter<std::
string>(
"orbitOffsetFile",
88 logFileName_(params.getUntrackedParameter<std::
string>(
"logFileName",
89 "matacqProducer.log")),
91 onErrorDisablingEvtCnt_(params.getParameter<int>(
"onErrorDisablingEvtCnt")),
92 timeLogFile_(params.getUntrackedParameter<std::
string>(
"timeLogFile",
"")),
95 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] in MatacqProducer ctor" << endl;
102 cout <<
"[LaserSorter " <<
now() <<
"] "
103 <<
"Failed to open file " <<
timeLogFile_ <<
" to log timing.\n";
121 "EcalMatacqDigiCollection product with instance name '"
127 if(
verbosity_>0)
cout <<
"[Matacq " <<
now() <<
"] registering new FEDRawDataCollection "
128 "product with instance name '"
140 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] exiting MatacqProducer ctor" << endl;
146 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] in MatacqProducer::produce" << endl;
152 << setfill(
'0') << setw(3) << (t.tv_usec+500)/1000 << setfill(
' ')<<
"\t"
153 << (t.tv_usec -
timer_.tv_usec)*1.
154 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\t";
171 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\n";
182 std::auto_ptr<FEDRawDataCollection> rawColl;
191 std::auto_ptr<EcalMatacqDigiCollection>
210 LogInfo(
"Matacq") <<
"Run " << runNumber <<
"\t Orbit " << orbitId <<
"\n";
214 map<uint32_t,uint32_t>::iterator it =
orbitOffset_.find(runNumber);
216 LogWarning(
"Matacq") <<
"Orbit offset not found for run "
218 <<
". No orbit correction will be applied.";
224 LogInfo(
"Matacq") <<
"Matacq data file found for "
225 <<
"run " << runNumber <<
" orbit " << orbitId;
233 LogWarning(
"Matacq") <<
" Error in Matacq event fragment length! "
235 <<
"*8 Bytes, Parsed len: "
237 <<
"Matacq data will not be included for this event.\n";
244 LogInfo(
"Matacq") <<
"Associating matacq data with orbit id "
246 <<
" to dcc event with orbit id "
247 << orbitId << std::endl;
255 LogWarning(
"Matacq") <<
"No matacq data found for laser event "
256 <<
"of run " << runNumber <<
" orbit "
261 LogWarning(
"Matacq") <<
"No matacq file found for event "
271 <<
" next events will be skipped, following to an "
272 <<
"error on the last processed event, "
273 <<
"which is expected to be persistant.";
281 <<
"Adding FEDRawDataCollection collection "
288 <<
"Adding EcalMatacqDigiCollection collection "
345 if(!
mtell(startPos))
return false;
347 int32_t startOrb = -1;
348 const size_t headerSize = 8*8;
349 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
351 if(startOrb<0) startOrb = 0;
354 cout <<
"[Matacq " <<
now() <<
"] Failed to read matacq header. Moved to start of "
358 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
362 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Looks like matacq file is empty"
369 <<
" looking for orbit " << orbitId
370 <<
". Current file position: " << startPos
371 <<
" Orbit at current position: " << startOrb <<
"\n";
374 bool didCoarseMove =
false;
390 pos = ((int64_t)fsize/evtSize-1)*evtSize;
392 cout <<
"[Matacq " <<
now() <<
"] Estimated position was beyond end of file. "
393 "Changed to " << pos <<
"\n";
400 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] jumping to estimated position "
402 mseek(pos, SEEK_SET,
"Jumping to estimated event position");
403 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
404 didCoarseMove =
true;
408 didCoarseMove =
false;
409 if(!
mread((
char*)&
data_[0], headerSize,
"Reading event header",
true)){
414 if(
verbosity_)
cout <<
"[Matacq " <<
now() <<
"] Event orbit outside of orbit range "
415 "of matacq data file events\n";
425 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Fast retrieval threshold increased from "
427 fastRetrievalThresh_ = 2*
abs(orb-orbitId);
433 && (
abs(orb-orbitId) >
abs(startOrb-orbitId))){
434 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimation (-> orbit " << orb <<
") "
435 "was worst than original position (-> orbit "
437 <<
"). Restoring position (" << startPos <<
").\n";
438 mseek(startPos, SEEK_SET);
439 mread((
char*)&
data_[0], headerSize,
"Reading event header",
true);
444 bool searchBackward = (orb>orbitId)?
true:
false;
450 cout <<
"[Matacq " <<
now() <<
"] read DCC length is null! Cancels matacq event search "
451 <<
" and move matacq file pointer to beginning of the file. "
452 <<
"(" << __FILE__ <<
":" << __LINE__ <<
")."
459 enum state_t { searching,
found, failed } state = searching;
461 while(state == searching){
468 cout <<
"[Matacq " <<
now() <<
"] Header read at file position "
470 <<
": orbit = " << orb
471 <<
" len = " << len <<
"x8 Byte"
472 <<
" run = " << run <<
"\n";
475 && (runNumber==0 || runNumber==run)){
479 if((
int)
data_.size() < len*8){
483 " matacq event." <<
"\n";
484 if(!
mread((
char*)&
data_[0], len*8,
"Reading matacq event")){
491 if((searchBackward && (orb < orbitId))
492 || (!searchBackward && (orb > orbitId))){
496 <<
"] No matacq data found for run " << run
497 <<
", orbit ID " << orbitId <<
"." <<
"\n";
499 off_t
offset = (searchBackward?-len:len)*8;
502 cout <<
"[Matacq " <<
now() <<
"] In matacq file, moving "
503 <<
abs(offset) <<
" byte " << (offset>0?
"forward":
"backward")
507 if(
mseek(offset, SEEK_CUR,
508 (searchBackward?
"Moving to previous event":
509 "Moving to next event"))
510 &&
mread((
char*)&
data_[0], headerSize,
"Reading event header",
513 if(!searchBackward)
mseek(-len*8, SEEK_CUR,
514 "Moving to start of last complete event");
528 cout <<
"[Matacq " <<
now() <<
"] Event found was at the end of the file. Moving "
529 "stream position to beginning of this event."
532 mseek(-(
int)len*8-1, SEEK_CUR,
533 "Moving to beginning of last matacq event");
536 return (state==
found);
545 if(fileChange!=0) *fileChange =
false;
551 const string runNumberFormat =
"%08d";
552 string sRunNumber = str(
boost::format(runNumberFormat) % runNumber);
558 boost::algorithm::replace_all(fname,
"%run_subdir%",
560 boost::algorithm::replace_all(fname,
"%run_number%", sRunNumber);
563 <<
"Looking for a file with path "
567 LogInfo(
"Matacq") <<
"Uses matacq data file: '" << fname <<
"'\n";
573 "for run " << runNumber <<
"\n";
576 if(fileChange!=0) *fileChange =
false;
581 LogWarning(
"Matacq") <<
"Failed to open file " << fname <<
"\n";
584 if(fileChange!=0) *fileChange =
false;
590 if(fileChange!=0) *fileChange =
true;
609 <<
"No FED raw data collection found. ECAL raw data are "
610 "required to retrieve the orbit ID";
614 for(
int id=601;
id<=654; ++id){
617 const int orbitIdOffset64 = 3;
618 if(data.
size()>=8*(orbitIdOffset64+1)){
619 const unsigned char* pOrbit = data.
data() + orbitIdOffset64*8;
620 int thisOrbit = pOrbit[0]
628 <<
"Orbit ID inconsitency in DCC headers";
632 if(thisOrbit!=0) orbit = thisOrbit;
639 LogWarning(
"NotFound") <<
"Failed to retrieve orbit ID of event "
650 <<
"No FED raw data collection found. ECAL raw data are "
651 "required to retrieve the trigger type";
655 for(
int id=601;
id<=654; ++id){
658 const int detailedTrigger32 = 5;
659 if(data.
size()>=4*(detailedTrigger32+1)){
660 const unsigned char* pTType = data.
data() + detailedTrigger32*4;
661 int tType = pTType[1] & 0x7;
666 int tType = stat.
result(&p);
669 LogWarning(
"NotFound") <<
"No ECAL DCC data found\n";
674 LogWarning(
"EventCorruption") <<
"Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
683 const size_t headerSize = 8*8;
684 unsigned char data[headerSize];
685 if(!mp->
mread((
char*)data, headerSize)){
717 <<
" Number of events: " << nEvents << endl;
721 mp->
mseek(last, SEEK_SET,
"Moving to beginning of last complete "
723 if(!mp->
mread((
char*) data, headerSize,
"Reading matacq header",
true)){
724 LogWarning(
"Matacq") <<
"Fast matacq event retrieval failure. "
725 "Falling back to safe retrieval mode.";
732 if(
verbosity_>1)
cout <<
"[Matacq " <<
now() <<
"] Last event orbit: " << lastOrb
733 <<
" last event length: " << lastLen << endl;
739 <<
"Fast matacq event retrieval failure: it looks like "
740 "the matacq file contains events of different sizes.";
756 if(orb<firstOrbit_)
return -1;
758 (((
uint64_t)(orb-firstOrbit_))/orbitStepMean_)*eventLength_*8
760 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimated Position for orbit " << orb
761 <<
": " << r << endl;
771 cout <<
"[Matacq " <<
now() <<
"] Time elapsed between first event and "
772 "destruction of MatacqProducer: "
774 + (t.tv_usec-
startTime_.tv_usec)*1.e-6) <<
"s\n";
782 <<
"Failed to open orbit ID correction file '"
786 cout <<
"[Matacq " <<
now() <<
"] "
787 <<
"Offset to substract to Matacq events Orbit ID: \n"
788 <<
"#Run Number\t Offset\n";
809 <<
"Syntax error in Orbit offset file '"
812 cout << run <<
"\t" << orbit <<
"\n";
817 #ifdef USE_STORAGE_MANAGER
819 if(0==
inFile_.get())
return false;
826 << __FILE__ <<
": "<< __LINE__ <<
"\n";
831 cout <<
"[Matacq " <<
now() <<
"] ";
832 if(mess)
cout << mess <<
". ";
833 cout <<
"Random access error on input matacq file. ";
834 if(whence==SEEK_SET)
cout <<
"Failed to seek absolute position " <<
offset;
835 else if(whence==SEEK_CUR)
cout <<
"Failed to move " << offset <<
" bytes forward";
836 else if(whence==SEEK_END)
cout <<
"Failed to seek position at " << offset <<
" bytes before end of file";
837 cout <<
". Reopening file. " << e.
what() <<
"\n";
846 if(0==
inFile_.get())
return false;
852 if(0==
inFile_.get())
return false;
855 if(!
mtell(pos))
return false;
859 rc = (n==
inFile_->xread(buf, n));
862 cout <<
"[Matacq " <<
now() <<
"] ";
863 if(mess)
cout << mess <<
". ";
864 cout <<
"Read failure from input matacq file: "
879 if(
inFile_.get()==0)
return false;
886 if(file==0)
return false;
892 <<
"File will be reopened.";
932 if(
inFile_.get()==0)
return true;
936 #else //USE_STORAGE_MANAGER not defined
939 const int rc = fseeko(
inFile_, offset, whence);
941 cout <<
"[Matacq " <<
now() <<
"] ";
942 if(mess)
cout << mess <<
". ";
943 cout <<
"Random access error on input matacq file. "
960 bool rc = (pos!=-1) && (1==fread(buf, n, 1,
inFile_));
963 cout <<
"[Matacq " <<
now() <<
"] ";
964 if(mess)
cout << mess <<
". ";
965 cout <<
"Read failure from input matacq file.\n";
970 if(0!=fseeko(
inFile_, pos, SEEK_SET)){
972 cout <<
"[Matacq " <<
now() <<
"] ";
973 if(mess)
cout << mess <<
". ";
974 cout <<
"Failed to restore file position of "
975 "before read error. Rewind file.\n";
988 if(0!=fstat(fileno(
inFile_), &buf)){
1000 return fseeko(
inFile_, 0, SEEK_SET)!=0;
1005 return 0==stat(name.c_str(), &dummy);
1018 inFile_ = fopen(name.c_str(),
"r");
1042 #endif //USE_STORAGE_MANAGER defined
1045 int millions = runNumber / (1000*1000);
1046 int thousands = (runNumber-millions*1000*1000) / 1000;
1047 int units = runNumber-millions*1000*1000 - thousands*1000;
1048 return str(
boost::format(
"%03d/%03d/%03d") % millions % thousands % units);
1054 logFile_ <<
"[" <<
now() <<
"] Event count for run "
1057 <<
"Laser event with Matacq data: "
1059 <<
"Non laser event (according to DCC header) with Matacq data: "
static const char runNumber_[]
virtual char const * what() const
std::map< uint32_t, uint32_t > orbitOffset_
static const int matacqFedId_
static const int orbitTolerance_
edm::SortedCollection< EcalMatacqDigi > EcalMatacqDigiCollection
unsigned getRunNum() const
Storage * open(const std::string &url, int mode=IOFlags::OpenRead)
static unsigned getOrbitId(unsigned char *data, size_t size)
bool check(const std::string &url, IOOffset *size=0)
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
static StorageFactory * get(void)
bool mcheck(const std::string &name)
struct MatacqProducer::stats_t stats_
int64_t pos(int orb) const
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)
edm::InputTag inputRawCollection_
double nLaserEventsWithMatacq
unsigned int offset(bool)
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_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
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)
bool getMatacqEvent(uint32_t runNumber, int32_t orbitId, bool fileChange)