13 #include <boost/algorithm/string.hpp>
14 #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<
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";
120 "EcalMatacqDigiCollection product with instance name '"
126 if(
verbosity_>0)
cout <<
"[Matacq " <<
now() <<
"] registering new FEDRawDataCollection "
127 "product with instance name '"
139 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] exiting MatacqProducer ctor" << endl;
145 if(
verbosity_>=4)
cout <<
"[Matacq " <<
now() <<
"] in MatacqProducer::produce" << endl;
151 << setfill(
'0') << setw(3) << (t.tv_usec+500)/1000 << setfill(
' ')<<
"\t"
152 << (t.tv_usec -
timer_.tv_usec)*1.
153 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\t";
170 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\n";
181 std::auto_ptr<FEDRawDataCollection> rawColl;
190 std::auto_ptr<EcalMatacqDigiCollection>
209 LogInfo(
"Matacq") <<
"Run " << runNumber <<
"\t Orbit " << orbitId <<
"\n";
213 map<uint32_t,uint32_t>::iterator it =
orbitOffset_.find(runNumber);
215 LogWarning(
"Matacq") <<
"Orbit offset not found for run "
217 <<
". No orbit correction will be applied.";
223 LogInfo(
"Matacq") <<
"Matacq data file found for "
224 <<
"run " << runNumber <<
" orbit " << orbitId;
232 LogWarning(
"Matacq") <<
" Error in Matacq event fragment length! "
234 <<
"*8 Bytes, Parsed len: "
236 <<
"Matacq data will not be included for this event.\n";
243 LogInfo(
"Matacq") <<
"Associating matacq data with orbit id "
245 <<
" to dcc event with orbit id "
246 << orbitId << std::endl;
254 LogWarning(
"Matacq") <<
"No matacq data found for laser event "
255 <<
"of run " << runNumber <<
" orbit "
260 LogWarning(
"Matacq") <<
"No matacq file found for event "
270 <<
" next events will be skipped, following to an "
271 <<
"error on the last processed event, "
272 <<
"which is expected to be persistant.";
280 <<
"Adding FEDRawDataCollection collection "
287 <<
"Adding EcalMatacqDigiCollection collection "
344 if(!
mtell(startPos))
return false;
346 int32_t startOrb = -1;
347 const size_t headerSize = 8*8;
348 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
350 if(startOrb<0) startOrb = 0;
353 cout <<
"[Matacq " <<
now() <<
"] Failed to read matacq header. Moved to start of "
357 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
361 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Looks like matacq file is empty"
368 <<
" looking for orbit " << orbitId
369 <<
". Current file position: " << startPos
370 <<
" Orbit at current position: " << startOrb <<
"\n";
373 bool didCoarseMove =
false;
389 pos = ((int64_t)fsize/evtSize-1)*evtSize;
391 cout <<
"[Matacq " <<
now() <<
"] Estimated position was beyond end of file. "
392 "Changed to " << pos <<
"\n";
399 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] jumping to estimated position "
401 mseek(pos, SEEK_SET,
"Jumping to estimated event position");
402 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
403 didCoarseMove =
true;
407 didCoarseMove =
false;
408 if(!
mread((
char*)&
data_[0], headerSize,
"Reading event header",
true)){
413 if(
verbosity_)
cout <<
"[Matacq " <<
now() <<
"] Event orbit outside of orbit range "
414 "of matacq data file events\n";
424 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Fast retrieval threshold increased from "
426 fastRetrievalThresh_ = 2*
abs(orb-orbitId);
432 && (
abs(orb-orbitId) >
abs(startOrb-orbitId))){
433 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimation (-> orbit " << orb <<
") "
434 "was worst than original position (-> orbit "
436 <<
"). Restoring position (" << startPos <<
").\n";
437 mseek(startPos, SEEK_SET);
438 mread((
char*)&
data_[0], headerSize,
"Reading event header",
true);
443 bool searchBackward = (orb>orbitId)?
true:
false;
449 cout <<
"[Matacq " <<
now() <<
"] read DCC length is null! Cancels matacq event search "
450 <<
" and move matacq file pointer to beginning of the file. "
451 <<
"(" << __FILE__ <<
":" << __LINE__ <<
")."
458 enum state_t { searching,
found, failed }
state = searching;
460 while(
state == searching){
467 cout <<
"[Matacq " <<
now() <<
"] Header read at file position "
469 <<
": orbit = " << orb
470 <<
" len = " << len <<
"x8 Byte"
471 <<
" run = " << run <<
"\n";
474 && (runNumber==0 || runNumber==run)){
478 if((
int)
data_.size() < len*8){
482 " matacq event." <<
"\n";
483 if(!
mread((
char*)&
data_[0], len*8,
"Reading matacq event")){
490 if((searchBackward && (orb < orbitId))
491 || (!searchBackward && (orb > orbitId))){
495 <<
"] No matacq data found for run " << run
496 <<
", orbit ID " << orbitId <<
"." <<
"\n";
498 off_t
offset = (searchBackward?-len:len)*8;
501 cout <<
"[Matacq " <<
now() <<
"] In matacq file, moving "
502 <<
abs(offset) <<
" byte " << (offset>0?
"forward":
"backward")
506 if(
mseek(offset, SEEK_CUR,
507 (searchBackward?
"Moving to previous event":
508 "Moving to next event"))
509 &&
mread((
char*)&
data_[0], headerSize,
"Reading event header",
512 if(!searchBackward)
mseek(-len*8, SEEK_CUR,
513 "Moving to start of last complete event");
527 cout <<
"[Matacq " <<
now() <<
"] Event found was at the end of the file. Moving "
528 "stream position to beginning of this event."
531 mseek(-(
int)len*8-1, SEEK_CUR,
532 "Moving to beginning of last matacq event");
544 if(fileChange!=0) *fileChange =
false;
550 const string runNumberFormat =
"%08d";
551 string sRunNumber = str(
boost::format(runNumberFormat) % runNumber);
557 boost::algorithm::replace_all(fname,
"%run_subdir%",
559 boost::algorithm::replace_all(fname,
"%run_number%", sRunNumber);
562 <<
"Looking for a file with path "
566 LogInfo(
"Matacq") <<
"Uses matacq data file: '" << fname <<
"'\n";
572 "for run " << runNumber <<
"\n";
575 if(fileChange!=0) *fileChange =
false;
580 LogWarning(
"Matacq") <<
"Failed to open file " << fname <<
"\n";
583 if(fileChange!=0) *fileChange =
false;
589 if(fileChange!=0) *fileChange =
true;
608 <<
"No FED raw data collection found. ECAL raw data are "
609 "required to retrieve the orbit ID";
613 for(
int id=601;
id<=654; ++id){
616 const int orbitIdOffset64 = 3;
617 if(data.
size()>=8*(orbitIdOffset64+1)){
618 const unsigned char* pOrbit = data.
data() + orbitIdOffset64*8;
619 int thisOrbit = pOrbit[0]
627 <<
"Orbit ID inconsitency in DCC headers";
631 if(thisOrbit!=0) orbit = thisOrbit;
638 LogWarning(
"NotFound") <<
"Failed to retrieve orbit ID of event "
649 <<
"No FED raw data collection found. ECAL raw data are "
650 "required to retrieve the trigger type";
654 for(
int id=601;
id<=654; ++id){
657 const int detailedTrigger32 = 5;
658 if(data.
size()>=4*(detailedTrigger32+1)){
659 const unsigned char* pTType = data.
data() + detailedTrigger32*4;
660 int tType = pTType[1] & 0x7;
665 int tType = stat.
result(&p);
668 LogWarning(
"NotFound") <<
"No ECAL DCC data found\n";
673 LogWarning(
"EventCorruption") <<
"Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
682 const size_t headerSize = 8*8;
683 unsigned char data[headerSize];
684 if(!mp->
mread((
char*)data, headerSize)){
716 <<
" Number of events: " << nEvents << endl;
720 mp->
mseek(last, SEEK_SET,
"Moving to beginning of last complete "
722 if(!mp->
mread((
char*) data, headerSize,
"Reading matacq header",
true)){
723 LogWarning(
"Matacq") <<
"Fast matacq event retrieval failure. "
724 "Falling back to safe retrieval mode.";
731 if(
verbosity_>1)
cout <<
"[Matacq " <<
now() <<
"] Last event orbit: " << lastOrb
732 <<
" last event length: " << lastLen << endl;
738 <<
"Fast matacq event retrieval failure: it looks like "
739 "the matacq file contains events of different sizes.";
755 if(orb<firstOrbit_)
return -1;
757 (((
uint64_t)(orb-firstOrbit_))/orbitStepMean_)*eventLength_*8
759 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimated Position for orbit " << orb
760 <<
": " << r << endl;
770 cout <<
"[Matacq " <<
now() <<
"] Time elapsed between first event and "
771 "destruction of MatacqProducer: "
773 + (t.tv_usec-
startTime_.tv_usec)*1.e-6) <<
"s\n";
781 <<
"Failed to open orbit ID correction file '"
785 cout <<
"[Matacq " <<
now() <<
"] "
786 <<
"Offset to substract to Matacq events Orbit ID: \n"
787 <<
"#Run Number\t Offset\n";
808 <<
"Syntax error in Orbit offset file '"
811 cout << run <<
"\t" << orbit <<
"\n";
816 #ifdef USE_STORAGE_MANAGER
818 if(0==
inFile_.get())
return false;
825 << __FILE__ <<
": "<< __LINE__ <<
"\n";
830 cout <<
"[Matacq " <<
now() <<
"] ";
831 if(mess)
cout << mess <<
". ";
832 cout <<
"Random access error on input matacq file. ";
833 if(whence==SEEK_SET)
cout <<
"Failed to seek absolute position " <<
offset;
834 else if(whence==SEEK_CUR)
cout <<
"Failed to move " << offset <<
" bytes forward";
835 else if(whence==SEEK_END)
cout <<
"Failed to seek position at " << offset <<
" bytes before end of file";
836 cout <<
". Reopening file. " << e.
what() <<
"\n";
845 if(0==
inFile_.get())
return false;
851 if(0==
inFile_.get())
return false;
854 if(!
mtell(pos))
return false;
858 rc = (n==
inFile_->xread(buf, n));
861 cout <<
"[Matacq " <<
now() <<
"] ";
862 if(mess)
cout << mess <<
". ";
863 cout <<
"Read failure from input matacq file: "
878 if(
inFile_.get()==0)
return false;
885 if(file==0)
return false;
891 <<
"File will be reopened.";
931 if(
inFile_.get()==0)
return true;
935 #else //USE_STORAGE_MANAGER not defined
938 const int rc = fseeko(
inFile_, offset, whence);
940 cout <<
"[Matacq " <<
now() <<
"] ";
941 if(mess)
cout << mess <<
". ";
942 cout <<
"Random access error on input matacq file. "
959 bool rc = (pos!=-1) && (1==fread(buf, n, 1,
inFile_));
962 cout <<
"[Matacq " <<
now() <<
"] ";
963 if(mess)
cout << mess <<
". ";
964 cout <<
"Read failure from input matacq file.\n";
969 if(0!=fseeko(
inFile_, pos, SEEK_SET)){
971 cout <<
"[Matacq " <<
now() <<
"] ";
972 if(mess)
cout << mess <<
". ";
973 cout <<
"Failed to restore file position of "
974 "before read error. Rewind file.\n";
987 if(0!=fstat(fileno(
inFile_), &buf)){
999 return fseeko(
inFile_, 0, SEEK_SET)!=0;
1004 return 0==stat(name.c_str(), &dummy);
1017 inFile_ = fopen(name.c_str(),
"r");
1041 #endif //USE_STORAGE_MANAGER defined
1044 int millions = runNumber / (1000*1000);
1045 int thousands = (runNumber-millions*1000*1000) / 1000;
1046 int units = runNumber-millions*1000*1000 - thousands*1000;
1047 return str(
boost::format(
"%03d/%03d/%03d") % millions % thousands % units);
1053 logFile_ <<
"[" <<
now() <<
"] Event count for run "
1056 <<
"Laser event with Matacq data: "
1058 <<
"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_
edm::SortedCollection< EcalMatacqDigi > EcalMatacqDigiCollection
static int orbitTolerance_
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_
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 stats_t stats_init
static StorageFactory * get(void)
bool mcheck(const std::string &name)
struct MatacqProducer::stats_t stats_
const T & max(const T &a, const T &b)
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)
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)
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_
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)
bool mopen(const std::string &name)
bool getMatacqEvent(uint32_t runNumber, int32_t orbitId, bool fileChange)