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::auto_ptr<FEDRawDataCollection> rawColl;
193 std::auto_ptr<EcalMatacqDigiCollection>
212 LogInfo(
"Matacq") <<
"Run " << runNumber <<
"\t Orbit " << orbitId <<
"\n";
216 map<uint32_t,uint32_t>::iterator it =
orbitOffset_.find(runNumber);
218 LogWarning(
"Matacq") <<
"Orbit offset not found for run "
220 <<
". No orbit correction will be applied.";
226 LogInfo(
"Matacq") <<
"Matacq data file found for "
227 <<
"run " << runNumber <<
" orbit " << orbitId;
235 LogWarning(
"Matacq") <<
" Error in Matacq event fragment length! "
237 <<
"*8 Bytes, Parsed len: "
239 <<
"Matacq data will not be included for this event.\n";
246 LogInfo(
"Matacq") <<
"Associating matacq data with orbit id "
248 <<
" to dcc event with orbit id "
249 << orbitId << std::endl;
257 LogWarning(
"Matacq") <<
"No matacq data found for laser event "
258 <<
"of run " << runNumber <<
" orbit "
263 LogWarning(
"Matacq") <<
"No matacq file found for event "
273 <<
" next events will be skipped, following to an "
274 <<
"error on the last processed event, "
275 <<
"which is expected to be persistant.";
283 <<
"Adding FEDRawDataCollection collection "
290 <<
"Adding EcalMatacqDigiCollection collection "
347 if(!
mtell(startPos))
return false;
349 int32_t startOrb = -1;
350 const size_t headerSize = 8*8;
351 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
353 if(startOrb<0) startOrb = 0;
356 cout <<
"[Matacq " <<
now() <<
"] Failed to read matacq header. Moved to start of "
360 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
364 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Looks like matacq file is empty"
371 <<
" looking for orbit " << orbitId
372 <<
". Current file position: " << startPos
373 <<
" Orbit at current position: " << startOrb <<
"\n";
376 bool didCoarseMove =
false;
392 pos = ((int64_t)fsize/evtSize-1)*evtSize;
394 cout <<
"[Matacq " <<
now() <<
"] Estimated position was beyond end of file. "
395 "Changed to " << pos <<
"\n";
402 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] jumping to estimated position "
404 mseek(pos, SEEK_SET,
"Jumping to estimated event position");
405 if(
mread((
char*)&
data_[0], headerSize,
"Reading matacq header",
true)){
406 didCoarseMove =
true;
410 didCoarseMove =
false;
411 if(!
mread((
char*)&
data_[0], headerSize,
"Reading event header",
true)){
416 if(
verbosity_)
cout <<
"[Matacq " <<
now() <<
"] Event orbit outside of orbit range "
417 "of matacq data file events\n";
427 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Fast retrieval threshold increased from "
429 fastRetrievalThresh_ = 2*
abs(orb-orbitId);
435 && (
abs(orb-orbitId) >
abs(startOrb-orbitId))){
436 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimation (-> orbit " << orb <<
") "
437 "was worst than original position (-> orbit "
439 <<
"). Restoring position (" << startPos <<
").\n";
440 mseek(startPos, SEEK_SET);
441 mread((
char*)&
data_[0], headerSize,
"Reading event header",
true);
446 bool searchBackward = (orb>orbitId)?
true:
false;
452 cout <<
"[Matacq " <<
now() <<
"] read DCC length is null! Cancels matacq event search "
453 <<
" and move matacq file pointer to beginning of the file. "
454 <<
"(" << __FILE__ <<
":" << __LINE__ <<
")."
461 enum state_t { searching,
found, failed } state = searching;
463 while(state == searching){
470 cout <<
"[Matacq " <<
now() <<
"] Header read at file position "
472 <<
": orbit = " << orb
473 <<
" len = " << len <<
"x8 Byte"
474 <<
" run = " << run <<
"\n";
477 && (runNumber==0 || runNumber==run)){
481 if((
int)
data_.size() < len*8){
485 " matacq event." <<
"\n";
486 if(!
mread((
char*)&
data_[0], len*8,
"Reading matacq event")){
493 if((searchBackward && (orb < orbitId))
494 || (!searchBackward && (orb > orbitId))){
498 <<
"] No matacq data found for run " << run
499 <<
", orbit ID " << orbitId <<
"." <<
"\n";
501 off_t
offset = (searchBackward?-len:len)*8;
504 cout <<
"[Matacq " <<
now() <<
"] In matacq file, moving "
505 <<
abs(offset) <<
" byte " << (offset>0?
"forward":
"backward")
509 if(
mseek(offset, SEEK_CUR,
510 (searchBackward?
"Moving to previous event":
511 "Moving to next event"))
512 &&
mread((
char*)&
data_[0], headerSize,
"Reading event header",
515 if(!searchBackward)
mseek(-len*8, SEEK_CUR,
516 "Moving to start of last complete event");
530 cout <<
"[Matacq " <<
now() <<
"] Event found was at the end of the file. Moving "
531 "stream position to beginning of this event."
534 mseek(-(
int)len*8-1, SEEK_CUR,
535 "Moving to beginning of last matacq event");
538 return (state==
found);
547 if(fileChange!=0) *fileChange =
false;
553 const string runNumberFormat =
"%08d";
554 string sRunNumber = str(
boost::format(runNumberFormat) % runNumber);
560 boost::algorithm::replace_all(fname,
"%run_subdir%",
562 boost::algorithm::replace_all(fname,
"%run_number%", sRunNumber);
565 <<
"Looking for a file with path "
569 LogInfo(
"Matacq") <<
"Uses matacq data file: '" << fname <<
"'\n";
575 "for run " << runNumber <<
"\n";
578 if(fileChange!=0) *fileChange =
false;
583 LogWarning(
"Matacq") <<
"Failed to open file " << fname <<
"\n";
586 if(fileChange!=0) *fileChange =
false;
592 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 "
652 <<
"No FED raw data collection found. ECAL raw data are "
653 "required to retrieve the trigger type";
657 for(
int id=601;
id<=654; ++id){
660 const int detailedTrigger32 = 5;
661 if(data.
size()>=4*(detailedTrigger32+1)){
662 const unsigned char* pTType = data.
data() + detailedTrigger32*4;
663 int tType = pTType[1] & 0x7;
668 int tType = stat.
result(&p);
671 LogWarning(
"NotFound") <<
"No ECAL DCC data found\n";
676 LogWarning(
"EventCorruption") <<
"Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
685 const size_t headerSize = 8*8;
686 unsigned char data[headerSize];
687 if(!mp->
mread((
char*)data, headerSize)){
719 <<
" Number of events: " << nEvents << endl;
723 mp->
mseek(last, SEEK_SET,
"Moving to beginning of last complete "
725 if(!mp->
mread((
char*) data, headerSize,
"Reading matacq header",
true)){
726 LogWarning(
"Matacq") <<
"Fast matacq event retrieval failure. "
727 "Falling back to safe retrieval mode.";
734 if(
verbosity_>1)
cout <<
"[Matacq " <<
now() <<
"] Last event orbit: " << lastOrb
735 <<
" last event length: " << lastLen << endl;
741 <<
"Fast matacq event retrieval failure: it looks like "
742 "the matacq file contains events of different sizes.";
758 if(orb<firstOrbit_)
return -1;
760 (((
uint64_t)(orb-firstOrbit_))/orbitStepMean_)*eventLength_*8
762 if(
verbosity_>2)
cout <<
"[Matacq " <<
now() <<
"] Estimated Position for orbit " << orb
763 <<
": " << r << endl;
773 cout <<
"[Matacq " <<
now() <<
"] Time elapsed between first event and "
774 "destruction of MatacqProducer: "
776 + (t.tv_usec-
startTime_.tv_usec)*1.e-6) <<
"s\n";
784 <<
"Failed to open orbit ID correction file '"
788 cout <<
"[Matacq " <<
now() <<
"] "
789 <<
"Offset to substract to Matacq events Orbit ID: \n"
790 <<
"#Run Number\t Offset\n";
811 <<
"Syntax error in Orbit offset file '"
814 cout << run <<
"\t" << orbit <<
"\n";
819 #ifdef USE_STORAGE_MANAGER
821 if(0==
inFile_.get())
return false;
828 << __FILE__ <<
": "<< __LINE__ <<
"\n";
833 cout <<
"[Matacq " <<
now() <<
"] ";
834 if(mess)
cout << mess <<
". ";
835 cout <<
"Random access error on input matacq file. ";
836 if(whence==SEEK_SET)
cout <<
"Failed to seek absolute position " <<
offset;
837 else if(whence==SEEK_CUR)
cout <<
"Failed to move " << offset <<
" bytes forward";
838 else if(whence==SEEK_END)
cout <<
"Failed to seek position at " << offset <<
" bytes before end of file";
839 cout <<
". Reopening file. " << e.
what() <<
"\n";
848 if(0==
inFile_.get())
return false;
854 if(0==
inFile_.get())
return false;
857 if(!
mtell(pos))
return false;
861 rc = (n==
inFile_->xread(buf, n));
864 cout <<
"[Matacq " <<
now() <<
"] ";
865 if(mess)
cout << mess <<
". ";
866 cout <<
"Read failure from input matacq file: "
881 if(
inFile_.get()==0)
return false;
888 if(file==0)
return false;
894 <<
"File will be reopened.";
934 if(
inFile_.get()==0)
return true;
938 #else //USE_STORAGE_MANAGER not defined
941 const int rc = fseeko(
inFile_, offset, whence);
943 cout <<
"[Matacq " <<
now() <<
"] ";
944 if(mess)
cout << mess <<
". ";
945 cout <<
"Random access error on input matacq file. "
962 bool rc = (pos!=-1) && (1==fread(buf, n, 1,
inFile_));
965 cout <<
"[Matacq " <<
now() <<
"] ";
966 if(mess)
cout << mess <<
". ";
967 cout <<
"Read failure from input matacq file.\n";
972 if(0!=fseeko(
inFile_, pos, SEEK_SET)){
974 cout <<
"[Matacq " <<
now() <<
"] ";
975 if(mess)
cout << mess <<
". ";
976 cout <<
"Failed to restore file position of "
977 "before read error. Rewind file.\n";
990 if(0!=fstat(fileno(
inFile_), &buf)){
1002 return fseeko(
inFile_, 0, SEEK_SET)!=0;
1007 return 0==stat(name.c_str(), &dummy);
1020 inFile_ = fopen(name.c_str(),
"r");
1044 #endif //USE_STORAGE_MANAGER defined
1047 int millions = runNumber / (1000*1000);
1048 int thousands = (runNumber-millions*1000*1000) / 1000;
1049 int units = runNumber-millions*1000*1000 - thousands*1000;
1050 return str(
boost::format(
"%03d/%03d/%03d") % millions % thousands % units);
1056 logFile_ <<
"[" <<
now() <<
"] Event count for run "
1059 <<
"Laser event with Matacq data: "
1061 <<
"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_
edm::SortedCollection< EcalMatacqDigi > EcalMatacqDigiCollection
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)
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)