22 #include <sys/types.h>
71 static std::string
now(){
76 strftime(buf,
sizeof(buf),
"%F %R %S s", localtime(&t.tv_sec));
77 buf[
sizeof(buf)-1] = 0;
80 buf2 << buf <<
" " << ((t.tv_usec+500)/1000) <<
" ms";
88 outputDir_(pset.getParameter<std::string>(
"outputDir")),
89 fedSubDirs_(pset.getParameter<std::vector<std::string> >(
"fedSubDirs")),
90 timeLogFile_(pset.getUntrackedParameter<std::string>(
"timeLogFile",
"")),
91 disableOutput_(pset.getUntrackedParameter<bool>(
"disableOutput",
false)),
93 outputListFile_(pset.getUntrackedParameter<string>(
"outputListFile",
"")),
95 verbosity_(pset.getUntrackedParameter<int>(
"verbosity", 0)),
96 iNoFullReadoutDccError_(0),
97 maxFullReadoutDccError_(pset.getParameter<int>(
"maxFullReadoutDccError")),
99 maxNoEcalDataMess_(pset.getParameter<int>(
"maxNoEcalDataMess")),
100 lumiBlockSpan_(pset.getParameter<int>(
"lumiBlockSpan")),
107 const unsigned nEcalFeds= 54;
110 <<
"Configuration error: "
111 <<
"fedSubDirs parameter must be a vector "
112 <<
" of " << nEcalFeds <<
" strings"
113 <<
" (subdirectory for unknown triggered FED followed by "
114 "subdirectories for FED ID 601 "
115 "to FED ID 654 in increasing FED ID order)";
123 <<
"' for logging of output file path list.";
131 cout <<
"[LaserSorter " <<
now() <<
"] "
132 <<
"Failed to open file " <<
timeLogFile_ <<
" to log timing.\n";
139 struct stat fileStat;
141 if(!S_ISDIR(fileStat.st_mode)){
143 <<
"File " <<
outputDir_ <<
" exists but is not a directory "
149 <<
"Failed to create directory " <<
outputDir_
150 <<
" for writing data.";
167 logFile_ <<
"Summary. Event count: "
183 << setfill(
'0') << setw(3) << (t.tv_usec+500)/1000 << setfill(
' ')
185 << (t.tv_usec -
timer_.tv_usec)*1.
186 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\t";
201 event.getByType(rawdata);
223 <<
") found in trigger type is out of range.";
228 cout <<
" No fully read-out DCC found\n";
231 }
else if(ids.size()==1){
232 triggeredFedId = ids[0];
237 cout <<
" Several fully read-out Dccs:";
238 for(
unsigned i=0;
i < ids.size(); ++
i)
cout <<
" " << ids[
i];
244 if(
verbosity_>1)
cout <<
"\n----------------------------------------------------------------------\n"
246 <<
" " <<
event.id() <<
"\n"
247 <<
"Lumin block: " <<
event.luminosityBlock() <<
"\n"
250 <<
" FED: " << triggeredFedId
251 <<
" side:" << side <<
"\n"
252 <<
"\n----------------------------------------------------------------------\n";
255 if(
verbosity_>1)
cout <<
"\n----------------------------------------------------------------------\n"
257 <<
" " <<
event.id() <<
"\n"
258 <<
"Lumin block: " <<
event.luminosityBlock() <<
"\n"
260 <<
"\n----------------------------------------------------------------------\n";
263 logFile_ <<
event.id().run() <<
"\t"
264 <<
event.luminosityBlock() <<
"\t"
265 <<
event.id().event() <<
"\t"
267 << triggeredFedId <<
"\t"
270 bool written =
false;
275 const int lb =
event.luminosityBlock();
279 for(
int lb1 = minLumi; lb1 <=
maxLumi; ++lb1){
303 <<
"Writing out event from FED " << triggeredFedId
304 <<
" LB " <<
event.luminosityBlock()
305 <<
" orbit " <<
event.orbitNumber() <<
"\n";
313 <<
"already contains calibration event from FED "
314 << triggeredFedId <<
", LB = "
315 <<
event.luminosityBlock()
317 <<
event.orbitNumber() <<
". Event skipped.\n";
325 logFile_ <<
"\t" << (written?
"Y":
"N") <<
"\n";
332 + (t.tv_sec -
timer_.tv_sec)*1.e6 <<
"\n";
338 int fedid = (dcc%600) + 600;
342 if ( fedid <= 607 ) {
343 lmes.push_back(fedid-601+83);
344 }
else if ( fedid == 608 ) {
347 }
else if ( fedid == 609 ) {
351 else if ( fedid >= 610 && fedid <= 645 ) {
352 lmes.push_back(2*(fedid-610)+1);
353 lmes.push_back(lmes[0]+1);
355 else if ( fedid >= 646 ) {
356 if ( fedid <= 652 ) {
357 lmes.push_back(fedid-646+73);
358 }
else if ( fedid == 653 ) {
361 }
else if ( fedid == 654 ) {
365 return lmes.size()==0?-1:lmes[
min(lmes.size(), (size_t)side)];
371 bool ecalData =
false;
375 const int detailedTrigger32 = 5;
377 <<
"FED " <<
id <<
" data size: "
378 << data.
size() <<
"\n";
379 if(data.
size()>=4*(detailedTrigger32+1)){
381 const uint32_t* pData32 = (
const uint32_t*) data.
data();
382 int tType = pData32[detailedTrigger32] & 0xFFF;
384 <<
"Trigger type " << tType <<
"\n";
388 if(!ecalData)
return -2;
390 int tType = stat.
result(&p);
395 "(This warning will be disabled for the current run after "
402 edm::LogWarning(
"EventCorruption") <<
"Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
405 if(proba) *proba =
p;
421 for(boost::ptr_list<OutStreamRecord>::iterator it =
outStreamList_.begin();
424 if(it->startingLumiBlock() < minLumiBlock
425 || it->startingLumiBlock() > maxLumiBlock){
428 <<
"Closing file for "
431 <<
" LB " << it->startingLumiBlock()
448 <<
"Looking for an opened output file for FED "
449 << fedId <<
" LB " << lumiBlock
456 if(it->fedId()==fedId &&
465 <<
"File not yet opened. Opening it.\n";
467 OutStreamList::iterator streamRecord =
createOutStream(fedId, lumiBlock);
475 ofstream&
out = *outRcd.
out();
477 vector<unsigned> fedIds;
481 uint32_t evtStart = out.tellp();
482 if(out.bad()) evtStart = 0;
486 for(
unsigned iFed = 0; iFed < fedIds.size() && rc; ++iFed){
495 IndexRecord indexRcd = {
static_cast<unsigned int>(
event.orbitNumber()), evtStart};
496 indices.push_back(indexRcd);
505 const uint32_t * pData
506 =
reinterpret_cast<uint32_t*
>(
const_cast<unsigned char*
> ( data.
data()));
508 uint32_t dccLen64 = pData[2] & 0x00FFFFFF;
514 <<
"Mismatch between FED fragment size indicated in header "
515 <<
"(" << dccLen64 <<
"*8 Byte) "
516 <<
"and actual size (" << data.
size() <<
" Byte) "
517 <<
"for FED ID " << ((pData[0] >>8) & 0xFFF) <<
"!\n";
520 if(
verbosity_>3)
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Event fragment size: "
521 << data.
size() <<
" Byte"
522 <<
"\t From Dcc header: " << dccLen64*8 <<
" Byte\n";
524 const size_t nBytes = data.
size();
528 if(out.fail())
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Problem with stream!\n";
529 out.write((
char*)data.
data(), nBytes);
533 << __FILE__ <<
":" << __LINE__ <<
".\n";
539 std::string& newFileName){
542 static int maxTries = 100;
543 stringstream newFileName_;
545 newFileName_.str(
"");
546 newFileName_ << fileName <<
"~";
547 if(i>0) newFileName_ <<
i;
548 err =
link(fileName.c_str(), newFileName_.str().c_str());
550 newFileName = newFileName_.str();
551 err = unlink(fileName.c_str());
554 }
while((err!=0) && (errno == EEXIST) && (i < maxTries));
558 LaserSorter::OutStreamList::iterator
561 if(
verbosity_)
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Creating a stream for FED " << fedId
562 <<
" lumi block " << lumiBlock <<
".\n";
564 std::string finalName;
576 <<
"Failed to rename file " << tmpName
577 <<
" to " << newName <<
"\n";
580 <<
"Already existing File " << tmpName
592 <<
"Failed to create file "
593 << tmpName <<
" for writing event from FED " << fedId
594 <<
" lumi block " << lumiBlock
595 <<
": " << strerror(errno) <<
"\n.";
598 ifstream
in(finalName.c_str());
603 <<
" already exists. It will be updated if needed.\n";
606 streamsize nread = -1;
610 <<
" is not an LMF file despite its extension or "
611 <<
"it is corrupted.\n";
613 edm::LogWarning(
"LaserSorter") <<
"Cannot include events already in file "
615 <<
" because of version "
616 <<
"mismatch (found version "
617 << (int)vers <<
" while "
620 <<
" is supported).\n";
624 const int indexTableOffsetPos8 = 1*
sizeof(uint32_t);
625 uint32_t indexTableOffsetValue = 0;
627 in.seekg(indexTableOffsetPos8, ios::beg);
628 in.read((
char*) &indexTableOffsetValue,
629 sizeof(indexTableOffsetValue));
631 cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Failed to read offset of index table "
632 " in the existing file " << finalName <<
"\n";
634 if(
verbosity_>2)
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Index table offset of "
635 "original file " << finalName <<
": 0x"
636 << hex << setfill(
'0')
637 << setw(8) << indexTableOffsetValue
638 << dec << setfill(
' ') <<
"\n";
641 in.seekg(0, ios::beg);
644 uint32_t toRead = indexTableOffsetValue;
645 cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Copying " << finalName <<
" to " << tmpName << endl;
648 && (nread=
in.readsome(buffer,
min(toRead, (uint32_t)
sizeof(buffer))))!=0){
653 out->write(buffer, nread);
655 throw cms::Exception(
"LaserSorter") <<
"Error while writing to file "
657 <<
". Check if there is enough "
658 <<
"space on the device.\n";
663 indexTableOffsetValue = 0 ;
665 out->seekp(indexTableOffsetPos8, ios::beg);
666 out->write((
char*)&indexTableOffsetValue,
sizeof(uint32_t));
674 cout <<
"Press enter... file name was " << tmpName << endl;
698 uint32_t
id =
'L' | (
'M' <<8) | (
'F' <<16) | (
formatVersion_<<24);
700 out.write((
char*)&
id,
sizeof(uint32_t));
704 out.write((
char*)&zero,
sizeof(uint32_t));
708 <<
"Failed to write file header.\n";
718 data[0] = evt.
time().
value() & 0xFFFFFFFF;
730 cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Write header of event: "
733 <<
", Run: " << evt.
run()
735 <<
", Event ID: " << evt.
id().
event()
736 <<
", Detailed trigger type: 0x" << hex << dtt << dec
738 <<
colorNames[(dtt>>6)&0x3] <<
", DCC " << (dtt&0x3f)
739 <<
", side " << ((dtt>>10) &0x1) <<
")"
740 <<
", number of FEDs: " << nFeds
745 out.write((
char*)data,
sizeof(data));
751 std::string& tmpName, std::string& finalName){
755 }
else if(fedId < 0){
762 <<
"Bug found at " << __FILE__ <<
":" << __LINE__
763 <<
". FED ID is out of index!";
767 struct stat fileStat;
773 string dir = buf.str();
774 if(0==stat(dir.c_str(), &fileStat)){
775 if(!S_ISDIR(fileStat.st_mode)){
777 <<
"File " << dir <<
" exists but is not a directory "
781 if(0!=mkdir(dir.c_str(), 0755)){
783 <<
"Failed to create directory " << dir
784 <<
" for writing data.";
790 <<
"_LB" << setfill(
'0') << setw(4) << lumiBlock
793 string tmpFileName = fileName +
".part";
796 tmpName = dir +
"/" + tmpFileName;
799 << finalName <<
"\n";
802 LaserSorter::OutStreamList::iterator
807 if(
verbosity_)
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Writing Index table of file "
808 << streamRecord->finalFileName() <<
"\n";
809 ofstream& out = *streamRecord->out();
812 cout <<
"Error while writing index table for file "
813 << streamRecord->finalFileName() <<
". "
814 <<
"Resulting file might be corrupted. "
815 <<
"The error can be due to a lack of disk space.";
819 << streamRecord->finalFileName() <<
".\n";
822 const std::string& tmpFileName = streamRecord->tmpFileName();
823 const std::string& finalFileName = streamRecord->finalFileName();
825 if(
verbosity_)
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Renaming " << tmpFileName
826 <<
" to " << finalFileName <<
".\n";
828 if(0!=rename(tmpFileName.c_str(), finalFileName.c_str())){
829 cout <<
"[LaserSorter " <<
now() <<
"] "
830 <<
" Failed to rename output file from "
831 << tmpFileName <<
" to " << finalFileName
832 <<
". " << strerror(errno) <<
"\n";
838 strftime(buf,
sizeof(buf),
"%F %R:%S", localtime(&t));
840 ifstream
f(
".watcherfile");
861 int* nTowerBlocks)
const{
862 if(nTowerBlocks) *nTowerBlocks = 0;
866 if(dccLen) *dccLen = 0;
867 const unsigned nWord32 = data.
size()/
sizeof(uint32_t);
872 for(
unsigned iWord32 = 0; iWord32 < nWord32; iWord32 += 2){
873 uint32_t* data32 = ((uint32_t*)(data.
data())) + iWord32;
874 int dataType = (data32[1] >>28) & 0xF;
879 if(0==(dataType>>2)){
880 const int dccHeaderId = (data32[1] >>24) & 0x3F;
882 if(dccLen) *dccLen=((data32[0] >>0 ) & 0xFFFFFF);
884 }
if((dataType>>2)==3){
905 std::vector<unsigned>& fedIds)
const{
906 fedIds.erase(fedIds.begin(), fedIds.end());
912 fedIds.push_back(
id);
916 <<
"Length error in data of FED " <<
id
917 <<
" in event " <<
event.id()
918 <<
", Data of this FED dropped.";
938 if(nTowers>=68) result.push_back(fed);
945 std::vector<IndexRecord>&
indices){
946 uint32_t indexTablePos = out.tellp();
947 uint32_t
nevts = indices.size();
950 out.write((
char*)&nevts,
sizeof(nevts));
951 const uint32_t reserved = 0;
952 out.write((
char*)&reserved,
sizeof(reserved));
954 if(out.bad())
return false;
956 sort(indices.begin(), indices.end());
958 for(
unsigned i = 0;
i < indices.size(); ++
i){
960 data[0] = indices[
i].orbit;
961 data[1] = indices[
i].filePos;
962 out.write((
char*)data,
sizeof(data));
965 if(out.bad())
return false;
973 if(!out.bad()) out.write((
char*)&indexTablePos,
sizeof(uint32_t));
975 bool rc = !out.bad();
996 uint32_t fileHeader[2];
997 s->read((
char*)&fileHeader[0],
sizeof(fileHeader));
998 uint32_t indexTablePos = fileHeader[1];
1003 errMsg <<
"Failed to read header of file " << inName
1005 if(err) *err = errMsg.str();
1009 s->seekg(indexTablePos);
1012 s->read((
char*)&nevts,
sizeof(nevts));
1015 errMsg <<
"Failed to read index table from file "
1017 if(err) *err = errMsg.str();
1021 errMsg <<
"Number of events indicated in event index of file "
1022 << inName <<
" (" << nevts <<
") "
1023 <<
"is unexpectively large.";
1024 if(err) *err = errMsg.str();
1027 outRcd.
indices()->resize(nevts);
1031 errMsg <<
"Failed to read index table from file "
1033 if(err) *err = errMsg.str();
1037 errMsg <<
"Number of events indicated in event index of file "
1038 << inName <<
" is unexpectively large.";
1039 if(err) *err = errMsg.str();
1044 if(
verbosity_ > 1)
cout <<
"[LaserSorter " <<
now() <<
"] " <<
"Orbit IDs of events "
1045 <<
"already contained in the file "
1047 for(
unsigned i = 0;
i < outRcd.
indices()->size(); ++
i){
1049 cout <<
" " << setw(9) << (*outRcd.
indices())[
i].orbit;
1061 streampos
p = in.tellg();
1065 in.read((
char*)&data,
sizeof(data));
1069 magic[0] = data & 0xFF;
1070 magic[1] = (data >>8) & 0xFF;
1071 magic[2] = (data >>16) & 0xFF;
1075 const string lmf = string(
"LMF");
1077 if(in.good() && lmf.compare(magic)==0){
1078 vers = (data >>24) & 0xFF;
1081 if(lmf.compare(magic)!=0){
1083 <<
"is not an LMF file.\n";
1094 time_t tsec = t>>32;
1096 uint32_t tusec = t & 0xFFFFFFFF;
1097 strftime(buf,
sizeof(buf),
"%F %R %S s", localtime(&tsec));
1098 buf[
sizeof(buf)-1] = 0;
1101 buf2 << (tusec+500)/1000;
1103 return string(buf) +
" " + buf2.str() +
" ms";
1113 else fedId_ = fedId;
1118 if(stat(fileName.c_str(), &
s)==0){
static const char runNumber_[]
EventNumber_t event() const
OutStreamRecord * getStream(int fedId, edm::LuminosityBlockNumber_t lumiBlock)
std::vector< int > getFullyReadoutDccs(const FEDRawDataCollection &data) const
int maxFullReadoutDccError_
double nRestoredDcc
number of events whose DCC ID was restored based on FED block sizes
static const char *const detailedTrigNames[]
static const int tpTrigger
static std::string toString(uint64_t t)
static const int laserTrigger
static const int ecalDccFedIdMin_
static const int indexOffset32_
bool writeIndexTable(std::ofstream &out, std::vector< IndexRecord > &indices)
bool readIndexTable(std::ifstream &in, std::string &inName, OutStreamRecord &outRcd, std::string *err)
bool writeEvent(OutStreamRecord &out, const edm::Event &event, int detailedTriggerType, const FEDRawDataCollection &data)
edm::RunNumber_t runNumber_
bool isDccEventEmpty(const FEDRawData &data, size_t *dccLen=0, int *nTowerBlocks=0) const
void writeFileHeader(std::ofstream &out)
int bunchCrossing() const
std::ofstream * out() const
edm::LuminosityBlockNumber_t luminosityBlock() const
void streamFileName(int fedId, edm::LuminosityBlockNumber_t lumiBlock, std::string &tmpName, std::string &finalName)
unsigned dccId(const DetId &)
edm::LuminosityBlockNumber_t lumiBlock_
std::set< uint32_t > & excludedOrbit()
void getOutputFedList(const edm::Event &event, const FEDRawDataCollection &data, std::vector< unsigned > &fedIds) const
static const int pedTrigger
static const int matacqFedId_
OutStreamList::iterator closeOutStream(OutStreamList::iterator streamRecord)
unsigned int LuminosityBlockNumber_t
std::string finalFileName() const
size_t size() const
Lenght of the data buffer in bytes.
std::vector< std::string > fedSubDirs_
edm::LuminosityBlockNumber_t startingLumiBlock() const
std::string link(std::string &nm, std::string &ns)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
double nRead
number of events read out
static const int ledTrigger
unsigned char formatVersion_
std::vector< IndexRecord > * indices()
int dcc2Lme(int dccNum, int dccSide)
bool writeFedBlock(std::ofstream &out, const FEDRawData &data)
std::string outputListFile_
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
static const unsigned maxEvents_
void closeOldStreams(edm::LuminosityBlockNumber_t lumiBlock)
TimeValue_t value() const
int getDetailedTriggerType(const edm::Handle< FEDRawDataCollection > &rawdata, double *proba=0)
LaserSorter(const edm::ParameterSet &)
unsigned long long uint64_t
static const int ecalDccFedIdMax_
std::ofstream outputList_
int readFormatVersion(std::ifstream &in, const std::string &fileName)
struct LaserSorter::stats_t stats_
static stats_t stats_init
char data[epos_bytes_allocation]
bool writeEventHeader(std::ofstream &out, const edm::Event &evt, int fedId, unsigned nFeds)
static size_t indexReserve_
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
OutStreamList::iterator createOutStream(int fedId, edm::LuminosityBlockNumber_t lumiBlock)
static const char *const colorNames[]
int iNoFullReadoutDccError_
void restoreStreamsOfLumiBlock(int lumiBlock)
virtual void analyze(const edm::Event &, const edm::EventSetup &)
T result(double *proba) const
double nWritten
number of events written out
OutStreamList outStreamList_
edm::Timestamp time() const
bool renameAsBackup(const std::string &fileName, std::string &newFileName)