13 #include <boost/algorithm/string.hpp>
14 #include <boost/format.hpp>
25 #include <sys/types.h>
31 using namespace boost;
50 static std::string
now(){
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";
180 event.getByType(sourceColl);
185 std::auto_ptr<FEDRawDataCollection> rawColl;
194 std::auto_ptr<EcalMatacqDigiCollection>
213 LogInfo(
"Matacq") <<
"Run " << runNumber <<
"\t Orbit " << orbitId <<
"\n";
217 map<uint32_t,uint32_t>::iterator it =
orbitOffset_.find(runNumber);
219 LogWarning(
"Matacq") <<
"Orbit offset not found for run "
221 <<
". No orbit correction will be applied.";
227 LogInfo(
"Matacq") <<
"Matacq data file found for "
228 <<
"run " << runNumber <<
" orbit " << orbitId;
236 LogWarning(
"Matacq") <<
" Error in Matacq event fragment length! "
238 <<
"*8 Bytes, Parsed len: "
240 <<
"Matacq data will not be included for this event.\n";
247 LogInfo(
"Matacq") <<
"Associating matacq data with orbit id "
249 <<
" to dcc event with orbit id "
250 << orbitId << std::endl;
258 LogWarning(
"Matacq") <<
"No matacq data found for laser event "
259 <<
"of run " << runNumber <<
" orbit "
264 LogWarning(
"Matacq") <<
"No matacq file found for event "
274 <<
" next events will be skipped, following to an "
275 <<
"error on the last processed event, "
276 <<
"which is expected to be persistant.";
284 <<
"Adding FEDRawDataCollection collection "
291 <<
"Adding EcalMatacqDigiCollection collection "
348 if(!
mtell(startPos))
return false;
350 int32_t startOrb = -1;
351 const size_t headerSize = 8*8;
352 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
354 if(startOrb<0) startOrb = 0;
357 cout <<
"[Matacq " <<
now() <<
"] Failed to read matacq header. Moved to start of "
361 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
365 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Looks like matacq file is empty"
372 <<
" looking for orbit " << orbitId
373 <<
". Current file position: " << startPos
374 <<
" Orbit at current position: " << startOrb <<
"\n";
377 bool didCoarseMove =
false;
393 pos = ((int64_t)fsize/evtSize-1)*evtSize;
395 cout <<
"[Matacq " <<
now() <<
"] Estimated position was beyond end of file. "
396 "Changed to " << pos <<
"\n";
403 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] jumping to estimated position "
405 mseek(pos, SEEK_SET,
"Jumping to estimated event position");
406 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
407 didCoarseMove =
true;
411 didCoarseMove =
false;
412 if(!
mread((
char*)&
data_[0], headerSize,
"Reading event header",
true)){
417 if(
verbosity_)
cout <<
"[Matacq " <<
now() <<
"] Event orbit outside of orbit range "
418 "of matacq data file events\n";
428 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Fast retrieval threshold increased from "
430 fastRetrievalThresh_ = 2*
abs(orb-orbitId);
436 && (
abs(orb-orbitId) >
abs(startOrb-orbitId))){
437 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimation (-> orbit " << orb <<
") "
438 "was worst than original position (-> orbit "
440 <<
"). Restoring position (" << startPos <<
").\n";
441 mseek(startPos, SEEK_SET);
442 mread((
char*)&
data_[0], headerSize,
"Reading event header",
true);
447 bool searchBackward = (orb>orbitId)?
true:
false;
453 cout <<
"[Matacq " <<
now() <<
"] read DCC length is null! Cancels matacq event search "
454 <<
" and move matacq file pointer to beginning of the file. "
455 <<
"(" << __FILE__ <<
":" << __LINE__ <<
")."
462 enum state_t { searching,
found, failed }
state = searching;
464 while(
state == searching){
471 cout <<
"[Matacq " <<
now() <<
"] Header read at file position "
473 <<
": orbit = " << orb
474 <<
" len = " << len <<
"x8 Byte"
475 <<
" run = " << run <<
"\n";
478 && (runNumber==0 || runNumber==run)){
482 if((
int)
data_.size() < len*8){
486 " matacq event." <<
"\n";
487 if(!
mread((
char*)&
data_[0], len*8,
"Reading matacq event")){
494 if((searchBackward && (orb < orbitId))
495 || (!searchBackward && (orb > orbitId))){
499 <<
"] No matacq data found for run " << run
500 <<
", orbit ID " << orbitId <<
"." <<
"\n";
502 off_t
offset = (searchBackward?-len:len)*8;
505 cout <<
"[Matacq " <<
now() <<
"] In matacq file, moving "
506 <<
abs(offset) <<
" byte " << (offset>0?
"forward":
"backward")
510 if(
mseek(offset, SEEK_CUR,
511 (searchBackward?
"Moving to previous event":
512 "Moving to next event"))
513 &&
mread((
char*)&
data_[0], headerSize,
"Reading event header",
516 if(!searchBackward)
mseek(-len*8, SEEK_CUR,
517 "Moving to start of last complete event");
531 cout <<
"[Matacq " <<
now() <<
"] Event found was at the end of the file. Moving "
532 "stream position to beginning of this event."
535 mseek(-(
int)len*8-1, SEEK_CUR,
536 "Moving to beginning of last matacq event");
548 if(fileChange!=0) *fileChange =
false;
554 const string runNumberFormat =
"%08d";
555 string sRunNumber = str(
boost::format(runNumberFormat) % runNumber);
561 boost::algorithm::replace_all(fname,
"%run_subdir%",
563 boost::algorithm::replace_all(fname,
"%run_number%", sRunNumber);
566 <<
"Looking for a file with path "
570 LogInfo(
"Matacq") <<
"Uses matacq data file: '" << fname <<
"'\n";
576 "for run " << runNumber <<
"\n";
579 if(fileChange!=0) *fileChange =
false;
584 LogWarning(
"Matacq") <<
"Failed to open file " << fname <<
"\n";
587 if(fileChange!=0) *fileChange =
false;
593 if(fileChange!=0) *fileChange =
true;
611 <<
"No FED raw data collection found. ECAL raw data are "
612 "required to retrieve the orbit ID";
616 for(
int id=601;
id<=654; ++
id){
619 const int orbitIdOffset64 = 3;
620 if(data.
size()>=8*(orbitIdOffset64+1)){
621 const unsigned char* pOrbit = data.
data() + orbitIdOffset64*8;
622 int thisOrbit = pOrbit[0]
630 <<
"Orbit ID inconsitency in DCC headers";
634 if(thisOrbit!=0) orbit = thisOrbit;
641 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
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_
bool getByType(Handle< PROD > &result) const
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 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_
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_
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)