CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Classes | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
LaserSorter Class Reference

#include <LaserSorter.h>

Inheritance diagram for LaserSorter:
edm::EDAnalyzer

Classes

struct  IndexRecord
 
class  OutStreamRecord
 
struct  stats_t
 

Public Member Functions

virtual void analyze (const edm::Event &, const edm::EventSetup &)
 
virtual void beginJob ()
 
virtual void endJob ()
 
 LaserSorter (const edm::ParameterSet &)
 
 ~LaserSorter ()
 
- Public Member Functions inherited from edm::EDAnalyzer
 EDAnalyzer ()
 
std::string workerType () const
 
virtual ~EDAnalyzer ()
 

Private Types

typedef boost::ptr_list
< OutStreamRecord
OutStreamList
 

Private Member Functions

void closeAllStreams ()
 
void closeOldStreams (edm::LuminosityBlockNumber_t lumiBlock)
 
OutStreamList::iterator closeOutStream (OutStreamList::iterator streamRecord)
 
OutStreamList::iterator createOutStream (int fedId, edm::LuminosityBlockNumber_t lumiBlock)
 
int dcc2Lme (int dccNum, int dccSide)
 
int getDetailedTriggerType (const edm::Handle< FEDRawDataCollection > &rawdata, double *proba=0)
 
std::vector< int > getFullyReadoutDccs (const FEDRawDataCollection &data) const
 
void getOutputFedList (const edm::Event &event, const FEDRawDataCollection &data, std::vector< unsigned > &fedIds) const
 
OutStreamRecordgetStream (int fedId, edm::LuminosityBlockNumber_t lumiBlock)
 
bool isDccEventEmpty (const FEDRawData &data, size_t *dccLen=0, int *nTowerBlocks=0) const
 
int readFormatVersion (std::ifstream &in, const std::string &fileName)
 
bool readIndexTable (std::ifstream &in, std::string &inName, OutStreamRecord &outRcd, std::string *err)
 
bool renameAsBackup (const std::string &fileName, std::string &newFileName)
 
void restoreStreamsOfLumiBlock (int lumiBlock)
 
void streamFileName (int fedId, edm::LuminosityBlockNumber_t lumiBlock, std::string &tmpName, std::string &finalName)
 
bool writeEvent (OutStreamRecord &out, const edm::Event &event, int detailedTriggerType, const FEDRawDataCollection &data)
 
bool writeEventHeader (std::ofstream &out, const edm::Event &evt, int fedId, unsigned nFeds)
 
bool writeFedBlock (std::ofstream &out, const FEDRawData &data)
 
void writeFileHeader (std::ofstream &out)
 
bool writeIndexTable (std::ofstream &out, std::vector< IndexRecord > &indices)
 

Static Private Member Functions

static std::string toString (uint64_t t)
 

Private Attributes

int detailedTrigType_
 
bool disableOutput_
 
bool doOutputList_
 
std::vector< std::string > fedSubDirs_
 
unsigned char formatVersion_
 
int iNoEcalDataMess_
 
int iNoFullReadoutDccError_
 
std::ofstream logFile_
 
edm::LuminosityBlockNumber_t lumiBlock_
 
int maxFullReadoutDccError_
 
int maxNoEcalDataMess_
 
std::string outputDir_
 
std::ofstream outputList_
 
std::string outputListFile_
 
OutStreamList outStreamList_
 
edm::RunNumber_t runNumber_
 
struct LaserSorter::stats_t stats_
 
std::ofstream timeLog_
 
std::string timeLogFile_
 
timeval timer_
 
bool timing_
 
int verbosity_
 

Static Private Attributes

static const int ecalDccFedIdMax_ = 654
 
static const int ecalDccFedIdMin_ = 601
 
static const int indexOffset32_ = 1
 
static const int matacqFedId_ = 655
 
static const unsigned maxEvents_ = 1<<20
 
static stats_t stats_init = {0, 0, 0, 0, 0}
 

Additional Inherited Members

- Public Types inherited from edm::EDAnalyzer
typedef EDAnalyzer ModuleType
 
typedef WorkerT< EDAnalyzerWorkerType
 
- Static Public Member Functions inherited from edm::EDAnalyzer
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
- Protected Member Functions inherited from edm::EDAnalyzer
CurrentProcessingContext const * currentContext () const
 

Detailed Description

This module is used to classify events of laser sequence acquired in a global run. Sorting: Events are grouped by bunch of consecutive events from the same FED. A file of such a bunch of events is identified by the FED or ECAL sector and the luminosity block id of the first event.

Sorting strategy: It is assumes that:

File completion: while being feeding, .part is appended at the end of the each output file.Once a file is completed (see above), it is renamed without the enclosing .part suffix.

Definition at line 44 of file LaserSorter.h.

Member Typedef Documentation

typedef boost::ptr_list<OutStreamRecord> LaserSorter::OutStreamList
private

Definition at line 117 of file LaserSorter.h.

Constructor & Destructor Documentation

LaserSorter::LaserSorter ( const edm::ParameterSet pset)

Definition at line 85 of file LaserSorter.cc.

References gather_cfg::cout, doOutputList_, edm::hlt::Exception, fedSubDirs_, logFile_, dbtoconf::out, outputDir_, outputList_, outputListFile_, timeLog_, timeLogFile_, timer_, and timing_.

86  : lumiBlock_(0),
87  formatVersion_(5),
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)),
92  runNumber_(0),
93  outputListFile_(pset.getUntrackedParameter<string>("outputListFile", "")),
94  doOutputList_(false),
95  verbosity_(pset.getUntrackedParameter<int>("verbosity", 0)),
97  maxFullReadoutDccError_(pset.getParameter<int>("maxFullReadoutDccError")),
99  maxNoEcalDataMess_(pset.getParameter<int>("maxNoEcalDataMess")),
101 {
102 
103  gettimeofday(&timer_, 0);
104  logFile_.open("eventSelect.log", ios::app | ios::out);
105 
106  const unsigned nEcalFeds= 54;
107  if(fedSubDirs_.size()!= nEcalFeds+1){
108  throw cms::Exception("LaserSorter")
109  << "Configuration error: "
110  << "fedSubDirs parameter must be a vector "
111  << " of " << nEcalFeds << " strings"
112  << " (subdirectory for unknown triggered FED followed by "
113  "subdirectories for FED ID 601 "
114  "to FED ID 654 in increasing FED ID order)";
115  }
116 
117  if(outputListFile_.size()!=0){
118  outputList_.open(outputListFile_.c_str(), ios::app);
119  if(outputList_.bad()){
120  throw cms::Exception("FileOpen")
121  << "Failed to open file '" << outputListFile_
122  << "' for logging of output file path list.";
123  }
124  doOutputList_ = true;
125  }
126 
127  if(timeLogFile_.size()>0){
128  timeLog_.open(timeLogFile_.c_str());
129  if(timeLog_.fail()){
130  cout << "[LaserSorter] "
131  << "Failed to open file " << timeLogFile_ << " to log timing.\n";
132  timing_ = false;
133  } else{
134  timing_ = true;
135  }
136  }
137 
138  struct stat fileStat;
139  if(0==stat(outputDir_.c_str(), &fileStat)){
140  if(!S_ISDIR(fileStat.st_mode)){
141  throw cms::Exception("[LaserSorter]")
142  << "File " << outputDir_ << " exists but is not a directory "
143  << " as expected.";
144  }
145  } else {//directory does not exists, let's try to create it
146  if(0!=mkdir(outputDir_.c_str(), 0755)){
147  throw cms::Exception("[LaserSorter]")
148  << "Failed to create directory " << outputDir_
149  << " for writing data.";
150  }
151  }
152 
153  logFile_ << "# "
154  "run\t"
155  "LB\t"
156  "event\t"
157  "trigType\t"
158  "FED\t"
159  "side\t"
160  "LB out\t"
161  "Written\t"
162  "ECAL data\n";
163 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
bool disableOutput_
Definition: LaserSorter.h:346
int maxFullReadoutDccError_
Definition: LaserSorter.h:406
edm::RunNumber_t runNumber_
Definition: LaserSorter.h:350
edm::LuminosityBlockNumber_t lumiBlock_
Definition: LaserSorter.h:321
std::vector< std::string > fedSubDirs_
Definition: LaserSorter.h:338
unsigned char formatVersion_
Definition: LaserSorter.h:330
std::string outputDir_
Definition: LaserSorter.h:334
timeval timer_
Definition: LaserSorter.h:354
std::string outputListFile_
Definition: LaserSorter.h:366
std::string timeLogFile_
Definition: LaserSorter.h:342
int iNoEcalDataMess_
Definition: LaserSorter.h:411
tuple out
Definition: dbtoconf.py:99
std::ofstream outputList_
Definition: LaserSorter.h:378
std::ofstream logFile_
Definition: LaserSorter.h:317
struct LaserSorter::stats_t stats_
static stats_t stats_init
Definition: LaserSorter.h:448
tuple cout
Definition: gather_cfg.py:41
int maxNoEcalDataMess_
Definition: LaserSorter.h:415
int iNoFullReadoutDccError_
Definition: LaserSorter.h:401
bool doOutputList_
Definition: LaserSorter.h:370
std::ofstream timeLog_
Definition: LaserSorter.h:358
LaserSorter::~LaserSorter ( )

Definition at line 165 of file LaserSorter.cc.

References logFile_, LaserSorter::stats_t::nInvalidDccStrict, LaserSorter::stats_t::nInvalidDccWeak, LaserSorter::stats_t::nRead, LaserSorter::stats_t::nRestoredDcc, LaserSorter::stats_t::nWritten, and stats_.

165  {
166  logFile_ << "Summary. Event count: "
167  << stats_.nRead << " processed, "
168  << stats_.nWritten << " written, "
169  << stats_.nInvalidDccStrict << " with errors in DCC ID values, "
170  << stats_.nInvalidDccWeak << " with unusable DCC ID values, "
171  << stats_.nRestoredDcc << " restored DCC ID based on DCC block size\n";
172 }
double nRestoredDcc
number of events whose DCC ID was restored based on FED block sizes
Definition: LaserSorter.h:446
double nRead
number of events read out
Definition: LaserSorter.h:436
std::ofstream logFile_
Definition: LaserSorter.h:317
struct LaserSorter::stats_t stats_
double nWritten
number of events written out
Definition: LaserSorter.h:438

Member Function Documentation

void LaserSorter::analyze ( const edm::Event event,
const edm::EventSetup es 
)
virtual

Implements edm::EDAnalyzer.

Definition at line 177 of file LaserSorter.cc.

References closeAllStreams(), closeOldStreams(), colorNames, gather_cfg::cout, detailedTrigNames, detailedTrigType_, disableOutput_, ecalDccFedIdMax_, ecalDccFedIdMin_, LaserSorter::OutStreamRecord::excludedOrbit(), LaserSorter::OutStreamRecord::finalFileName(), getDetailedTriggerType(), getFullyReadoutDccs(), getStream(), i, edm::EventBase::id(), iNoEcalDataMess_, iNoFullReadoutDccError_, logFile_, lumiBlock_, edm::EventBase::luminosityBlock(), maxFullReadoutDccError_, LaserSorter::stats_t::nInvalidDccStrict, LaserSorter::stats_t::nInvalidDccWeak, now(), LaserSorter::stats_t::nRead, LaserSorter::stats_t::nRestoredDcc, LaserSorter::stats_t::nWritten, edm::EventBase::orbitNumber(), dbtoconf::out, restoreStreamsOfLumiBlock(), edm::EventID::run(), runNumber_, LaserSorter::OutStreamRecord::startingLumiBlock(), stats_, matplotRender::t, timeLog_, timer_, timing_, verbosity_, and writeEvent().

177  {
178  if(timing_){
179  timeval t;
180  gettimeofday(&t, 0);
181  timeLog_ << (t.tv_usec - timer_.tv_usec)*1.
182  + (t.tv_sec - timer_.tv_sec)*1.e6 << "\n";
183  timer_ = t;
184  }
185 
186  ++stats_.nRead;
187 
188  if(event.id().run()!=runNumber_){//run changed or first event
189  //for a new run, starts with a new output stream set.
190  closeAllStreams();
191  runNumber_ = event.id().run();
193  iNoEcalDataMess_ = 0;
194  }
195 
197  event.getByType(rawdata);
198 
199  //The "detailed trigger type DCC field content:
200  double dttProba = 0;
201  detailedTrigType_ = getDetailedTriggerType(rawdata, &dttProba);
202 
203  const int trigType = (detailedTrigType_ >>8 ) & 0x7;
204  const int color = (detailedTrigType_ >>6 ) & 0x3;
205  const int dccId = (detailedTrigType_ >>0 ) & 0x3F;
206  int triggeredFedId = (detailedTrigType_ == -2) ? -1 : (600 + dccId);
207  const int side = (detailedTrigType_ >>11) & 0x1;
208  //monitoring region extended id:
209  // const int lme = dcc2Lme(dccId, side);
210 
211  if(detailedTrigType_ > -2){
212  if(dttProba < 1. || triggeredFedId < ecalDccFedIdMin_
213  || triggeredFedId > ecalDccFedIdMax_){
215  }
216 
217  if(triggeredFedId<ecalDccFedIdMin_ || triggeredFedId > ecalDccFedIdMax_){
218  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "DCC ID (" << dccId
219  << ") found in trigger type is out of range.";
221  vector<int> ids = getFullyReadoutDccs(*rawdata);
222  if(ids.size()==0){
224  cout << " No fully read-out DCC found\n";
226  }
227  } else if(ids.size()==1){
228  triggeredFedId = ids[0];
229  if(verbosity_) cout << " ID guessed from DCC payloads\n";
231  } else{ //ids.size()>1
232  if(verbosity_){
233  cout << " Several fully read-out Dccs:";
234  for(unsigned i=0; i < ids.size(); ++i) cout << " " << ids[i];
235  cout << "\n";
236  }
237  }
238  }
239 
240  if(verbosity_>1) cout << "\n----------------------------------------------------------------------\n"
241  << "Event id: "
242  << " " << event.id() << "\n"
243  << "Lumin block: " << event.luminosityBlock() << "\n"
244  << "TrigType: " << detailedTrigNames[trigType&0x7]
245  << " Color: " << colorNames[color&0x3]
246  << " FED: " << triggeredFedId
247  << " side:" << side << "\n"
248  << "\n----------------------------------------------------------------------\n";
249 
250  } else{ //NO ECAL DATA
251  if(verbosity_>1) cout << "\n----------------------------------------------------------------------\n"
252  << "Event id: "
253  << " " << event.id() << "\n"
254  << "Lumin block: " << event.luminosityBlock() << "\n"
255  << "No ECAL data\n"
256  << "\n----------------------------------------------------------------------\n";
257  }
258 
259  logFile_ << event.id().run() << "\t"
260  << event.luminosityBlock() << "\t"
261  << event.id().event() << "\t"
262  << trigType << "\t"
263  << triggeredFedId << "\t"
264  << side;
265 
266  bool written = false;
267  int assignedLB = -1;
268 
269  if(event.luminosityBlock()!=lumiBlock_){
270  //lumi block change => need for stream garbage collection
272 // if(event.luminosityBlock()!=lumiBlock_+1){
273 // //ASSUMES lumi block initialized to 0 => will proceed either if
274 // //job started with a lumi block different than 0 or when
275 // //switching to a lumi block other than N+1
276  int prevLumi = event.luminosityBlock()-1;
277  if(prevLumi>=1) restoreStreamsOfLumiBlock(prevLumi);
278  int nextLumi = event.luminosityBlock()+1;
279  restoreStreamsOfLumiBlock(nextLumi);
280  // }
281  }
282 
283 // if(event.luminosityBlock() < lumiBlock_){
284 // throw cms::Exception("LaserSorter")
285 // << "Process event has a lumi block (" << event.luminosityBlock() << ")"
286 // << "older than previous one (" << lumiBlock_ << "). "
287 // << "This can be due by wrong input file ordering or bad luminosity "
288 // << "block indication is the event header. "
289 // << "Event cannot be processed";
290 // }
291 
292  if(disableOutput_){
293  /* NO OP*/
294  } else{
295  OutStreamRecord* out = getStream(triggeredFedId, event.luminosityBlock());
296 
297  if(out!=0){
298  assignedLB = out->startingLumiBlock();
299  if(out->excludedOrbit().find(event.orbitNumber())
300  ==out->excludedOrbit().end()){
301  if(verbosity_ > 1) cout << "[LaserSorter] "
302  << "Writing out event from FED " << triggeredFedId
303  << " LB " << event.luminosityBlock()
304  << " orbit " << event.orbitNumber() << "\n";
305  int dtt = (detailedTrigType_ >=0) ? detailedTrigType_ : -1; //shall we use -1 or 0 for undefined value?
306  written = written
307  || writeEvent(*out, event, dtt, *rawdata);
308  ++stats_.nWritten;
309  } else{
310  if(verbosity_) cout << "[LaserSorter " << now() << "] "
311  << "File " << out->finalFileName() << " "
312  << "already contains calibration event from FED "
313  << triggeredFedId << ", LB = "
314  << event.luminosityBlock()
315  << " with orbit ID "
316  << event.orbitNumber() << ". Event skipped.\n";
317  }
318  }
319  }
320  lumiBlock_ = event.luminosityBlock();
321 
322  logFile_ << "\t";
323  if(assignedLB>=0) logFile_ << assignedLB; else logFile_ << "-";
324  logFile_ << "\t" << (written?"Y":"N") << "\n";
325  logFile_ << "\t" << (detailedTrigType_==-2?"N":"Y") << "\n";
326 
327  if(timing_){
328  timeval t;
329  gettimeofday(&t, 0);
330  timeLog_ << (t.tv_usec - timer_.tv_usec)*1.
331  + (t.tv_sec - timer_.tv_sec)*1.e6 << "\t";
332  timer_ = t;
333  }
334 }
RunNumber_t run() const
Definition: EventID.h:42
OutStreamRecord * getStream(int fedId, edm::LuminosityBlockNumber_t lumiBlock)
Definition: LaserSorter.cc:440
int i
Definition: DBlmapReader.cc:9
bool disableOutput_
Definition: LaserSorter.h:346
std::vector< int > getFullyReadoutDccs(const FEDRawDataCollection &data) const
Definition: LaserSorter.cc:931
int maxFullReadoutDccError_
Definition: LaserSorter.h:406
double nRestoredDcc
number of events whose DCC ID was restored based on FED block sizes
Definition: LaserSorter.h:446
static const char *const detailedTrigNames[]
Definition: LaserSorter.cc:50
static const int ecalDccFedIdMin_
Definition: LaserSorter.h:305
bool writeEvent(OutStreamRecord &out, const edm::Event &event, int detailedTriggerType, const FEDRawDataCollection &data)
Definition: LaserSorter.cc:470
edm::RunNumber_t runNumber_
Definition: LaserSorter.h:350
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:59
edm::LuminosityBlockNumber_t lumiBlock_
Definition: LaserSorter.h:321
void closeAllStreams()
Definition: LaserSorter.cc:408
edm::LuminosityBlockNumber_t startingLumiBlock() const
Definition: LaserSorter.h:70
double nRead
number of events read out
Definition: LaserSorter.h:436
timeval timer_
Definition: LaserSorter.h:354
int orbitNumber() const
Definition: EventBase.h:63
int iNoEcalDataMess_
Definition: LaserSorter.h:411
void closeOldStreams(edm::LuminosityBlockNumber_t lumiBlock)
Definition: LaserSorter.cc:415
tuple out
Definition: dbtoconf.py:99
int getDetailedTriggerType(const edm::Handle< FEDRawDataCollection > &rawdata, double *proba=0)
Definition: LaserSorter.cc:367
int detailedTrigType_
Definition: LaserSorter.h:313
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
static std::string now()
Definition: LaserSorter.cc:71
std::ofstream logFile_
Definition: LaserSorter.h:317
struct LaserSorter::stats_t stats_
edm::EventID id() const
Definition: EventBase.h:56
tuple cout
Definition: gather_cfg.py:41
static const char *const colorNames[]
Definition: LaserSorter.cc:61
int iNoFullReadoutDccError_
Definition: LaserSorter.h:401
void restoreStreamsOfLumiBlock(int lumiBlock)
double nWritten
number of events written out
Definition: LaserSorter.h:438
std::ofstream timeLog_
Definition: LaserSorter.h:358
void LaserSorter::beginJob ( void  )
virtual

Reimplemented from edm::EDAnalyzer.

Definition at line 856 of file LaserSorter.cc.

856  {
857 }
void LaserSorter::closeAllStreams ( )
private

Closes all opened output streams.

Definition at line 408 of file LaserSorter.cc.

References closeOutStream(), and outStreamList_.

Referenced by analyze(), and endJob().

408  {
409  for(OutStreamList::iterator it = outStreamList_.begin();
410  it != outStreamList_.end();/*NOOP*/){
411  it = closeOutStream(it);
412  }
413 }
OutStreamList::iterator closeOutStream(OutStreamList::iterator streamRecord)
Definition: LaserSorter.cc:802
OutStreamList outStreamList_
Definition: LaserSorter.h:326
void LaserSorter::closeOldStreams ( edm::LuminosityBlockNumber_t  lumiBlock)
private

Closes output stream 2 lumi block older than the input 'lumiBlock' ID.

Parameters
lumiBlockID of the reference luminosity block.

Definition at line 415 of file LaserSorter.cc.

References closeOutStream(), gather_cfg::cout, outStreamList_, and verbosity_.

Referenced by analyze().

415  {
416  const edm::LuminosityBlockNumber_t minLumiBlock = lumiBlock - 1;
417  const edm::LuminosityBlockNumber_t maxLumiBlock = lumiBlock + 1;
418  //If container type is ever changed, beware that
419  //closeOutStream call in the loop removes it from outStreamList
420  for(boost::ptr_list<OutStreamRecord>::iterator it = outStreamList_.begin();
421  it != outStreamList_.end();
422  /*NOOP*/){
423  if(it->startingLumiBlock() < minLumiBlock
424  || it->startingLumiBlock() > maxLumiBlock){
425  //event older than 2 lumi block => stream can be closed
426  if(verbosity_) cout << "[LaserSorter] "
427  << "Closing file for "
428  << "FED "
429  << it->fedId()
430  << " LB " << it->startingLumiBlock()
431  << "\n";
432  it = closeOutStream(it);
433  } else{
434  ++it;
435  }
436  }
437 }
OutStreamList::iterator closeOutStream(OutStreamList::iterator streamRecord)
Definition: LaserSorter.cc:802
unsigned int LuminosityBlockNumber_t
Definition: EventID.h:31
tuple cout
Definition: gather_cfg.py:41
OutStreamList outStreamList_
Definition: LaserSorter.h:326
LaserSorter::OutStreamList::iterator LaserSorter::closeOutStream ( OutStreamList::iterator  streamRecord)
private

Closes an output stream and removed it from opened stream records. Beware: this methode modifies outStreamList_.

Parameters
streamRecordrecord of the output stream to close.
Returns
iterator to element next to the deleted one.

Definition at line 802 of file LaserSorter.cc.

References gather_cfg::cout, doOutputList_, f, analyzePatCleaning_cfg::inputFile, now(), dbtoconf::out, outputList_, outStreamList_, matplotRender::t, cond::rpcobgas::time, verbosity_, and writeIndexTable().

Referenced by closeAllStreams(), and closeOldStreams().

803  {
804  if(streamRecord==outStreamList_.end()) return outStreamList_.end();
805 
806  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Writing Index table of file "
807  << streamRecord->finalFileName() << "\n";
808  ofstream& out = *streamRecord->out();
809  out.clear();
810  if(!writeIndexTable(out, *streamRecord->indices())){
811  cout << "Error while writing index table for file "
812  << streamRecord->finalFileName() << ". "
813  << "Resulting file might be corrupted. "
814  << "The error can be due to a lack of disk space.";
815  }
816 
817  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Closing file "
818  << streamRecord->finalFileName() << ".\n";
819  out.close();
820 
821  const std::string& tmpFileName = streamRecord->tmpFileName();
822  const std::string& finalFileName = streamRecord->finalFileName();
823 
824  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Renaming " << tmpFileName
825  << " to " << finalFileName << ".\n";
826 
827  if(0!=rename(tmpFileName.c_str(), finalFileName.c_str())){
828  cout << "[LaserSorter " << now() << "] "
829  << " Failed to rename output file from "
830  << tmpFileName << " to " << finalFileName
831  << ". " << strerror(errno) << "\n";
832  }
833 
834  if(doOutputList_){
835  char buf[256];
836  time_t t = time(0);
837  strftime(buf, sizeof(buf), "%F %R:%S", localtime(&t));
838 
839  ifstream f(".watcherfile");
840  string inputFile;
841  f >> inputFile;
842  outputList_ << finalFileName << "\t" << buf
843  << "\t" << inputFile
844  << endl;
845  }
846 
847  return outStreamList_.erase(streamRecord);
848 }
bool writeIndexTable(std::ofstream &out, std::vector< IndexRecord > &indices)
Definition: LaserSorter.cc:943
double f[11][100]
tuple out
Definition: dbtoconf.py:99
static std::string now()
Definition: LaserSorter.cc:71
std::ofstream outputList_
Definition: LaserSorter.h:378
tuple cout
Definition: gather_cfg.py:41
bool doOutputList_
Definition: LaserSorter.h:370
OutStreamList outStreamList_
Definition: LaserSorter.h:326
LaserSorter::OutStreamList::iterator LaserSorter::createOutStream ( int  fedId,
edm::LuminosityBlockNumber_t  lumiBlock 
)
private

Creates an output stream. It must be ensured before calling this method that the output stream is not already opened (See outStreamList_).

Parameters
fedIdFED ID of the event to stream out.
lumiBlockstarting lumi block of the event group to write to the stream.
Returns
iterator to the new stream record. outStreamList_.end() in case of failure.

Definition at line 558 of file LaserSorter.cc.

References trackerHits::c, gather_cfg::cout, end, edm::hlt::Exception, formatVersion_, recoMuon::in, min, now(), dbtoconf::out, outStreamList_, readFormatVersion(), readIndexTable(), renameAsBackup(), streamFileName(), verbosity_, and writeFileHeader().

Referenced by getStream().

559  {
560  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Creating a stream for FED " << fedId
561  << " lumi block " << lumiBlock << ".\n";
562  std::string tmpName;
563  std::string finalName;
564 
565  streamFileName(fedId, lumiBlock, tmpName, finalName);
566 
567  errno = 0;
568 
569  //checks if a file with tmpName name already exists:
570  ofstream* out = new ofstream(tmpName.c_str(), ios::out | ios::in);
571  if(out->is_open()){//temporary file already exists. Making a backup:
572  string newName;
573  if(!renameAsBackup(tmpName, newName)){
574  throw cms::Exception("LaserSorter")
575  << "Failed to rename file " << tmpName
576  << " to " << newName << "\n";
577  }
578  if(verbosity_) cout << "[LaserSorter " << now() << "] "
579  << "Already existing File " << tmpName
580  << " renamed to "
581  << newName << "\n";
582  out->close();
583  }
584 
585  out->clear();
586  out->open(tmpName.c_str(), ios::out | ios::trunc);
587 
588  if(out->fail()){//failed to create file
589  delete out;
590  throw cms::Exception("LaserSorter")
591  << "Failed to create file "
592  << tmpName << " for writing event from FED " << fedId
593  << " lumi block " << lumiBlock
594  << ": " << strerror(errno) << "\n.";
595  }
596 
597  ifstream in(finalName.c_str());
598  bool newFile = true;
599  if(in.good()){//file already exists with final name.
600  if(verbosity_) cout << "[LaserSorter] " << "File "
601  << finalName
602  << " already exists. It will be updated if needed.\n";
603  //Copying its contents:
604  char buffer[256];
605  streamsize nread = -1;
606  int vers = readFormatVersion(in, finalName);
607  if(vers==-1){
608  edm::LogWarning("LaserSorter") << "File " << tmpName.c_str()
609  << " is not an LMF file despite its extension or "
610  << "it is corrupted.\n";
611  } else if(vers!=formatVersion_){
612  edm::LogWarning("LaserSorter") << "Cannot include events already in file "
613  << tmpName.c_str()
614  << " because of version "
615  << "mismatch (found version "
616  << (int)vers << " while "
617  << "only version "
618  << (int)formatVersion_
619  << " is supported).\n";
620  } else{
621  newFile = false;
622  //read index table offset value:
623  const int indexTableOffsetPos8 = 1*sizeof(uint32_t);
624  uint32_t indexTableOffsetValue = 0;
625  in.clear();
626  in.seekg(indexTableOffsetPos8, ios::beg);
627  in.read((char*) &indexTableOffsetValue,
628  sizeof(indexTableOffsetValue));
629  if(in.fail()){
630  cout << "[LaserSorter] " << "Failed to read offset of index table "
631  " in the existing file " << finalName << "\n";
632  } else{
633  if(verbosity_>2) cout << "[LaserSorter] " << "Index table offset of "
634  "original file " << finalName << ": 0x"
635  << hex << setfill('0')
636  << setw(8) << indexTableOffsetValue
637  << dec << setfill(' ') << "\n";
638  }
639  in.clear();
640  in.seekg(0, ios::beg);
641 
642  //copy legacy file contents except the index table
643  uint32_t toRead = indexTableOffsetValue;
644  cout << "[LaserSorter " << now() << "] " << "Copying " << finalName << " to " << tmpName << endl;
645  while(!in.eof()
646  && (toRead > 0)
647  && (nread=in.readsome(buffer, min(toRead, (uint32_t)sizeof(buffer))))!=0){
648  // cout << "Writing " << nread << " bytes to file "
649  // << tmpName.c_str() << "\n";
650  toRead -= nread;
651  // out->seekp(0, ios::end);
652  out->write(buffer, nread);
653  if(out->bad()){
654  throw cms::Exception("LaserSorter") << "Error while writing to file "
655  << tmpName
656  << ". Check if there is enough "
657  << "space on the device.\n";
658  }
659  }
660 
661  //resets index table offset field:
662  indexTableOffsetValue = 0 ;
663  out->clear();
664  out->seekp(indexTableOffsetPos8, ios::beg);
665  out->write((char*)&indexTableOffsetValue, sizeof(uint32_t));
666  out->clear();
667  out->seekp(0, ios::end);
668  }
669  }
670 
671 #if 0
672  out->flush();
673  cout << "Press enter... file name was " << tmpName << endl;
674  char c;
675  cin >> c;
676 #endif
677 
678  OutStreamRecord* outRcd = new OutStreamRecord(fedId, lumiBlock,
679  out,
680  tmpName, finalName);
681 
682  if(newFile){
683  writeFileHeader(*out);
684  } else{
685  std::string errMsg;
686  if(!readIndexTable(in, finalName, *outRcd, &errMsg)){
687  throw cms::Exception("LaserSorter") << errMsg << "\n";
688  }
689  }
690 
691  return outStreamList_.insert(outStreamList_.end(), outRcd);
692 }
bool readIndexTable(std::ifstream &in, std::string &inName, OutStreamRecord &outRcd, std::string *err)
Definition: LaserSorter.cc:982
void writeFileHeader(std::ofstream &out)
Definition: LaserSorter.cc:694
void streamFileName(int fedId, edm::LuminosityBlockNumber_t lumiBlock, std::string &tmpName, std::string &finalName)
Definition: LaserSorter.cc:748
#define min(a, b)
Definition: mlp_lapack.h:161
unsigned char formatVersion_
Definition: LaserSorter.h:330
#define end
Definition: vmac.h:38
tuple out
Definition: dbtoconf.py:99
static std::string now()
Definition: LaserSorter.cc:71
int readFormatVersion(std::ifstream &in, const std::string &fileName)
tuple cout
Definition: gather_cfg.py:41
OutStreamList outStreamList_
Definition: LaserSorter.h:326
bool renameAsBackup(const std::string &fileName, std::string &newFileName)
Definition: LaserSorter.cc:537
int LaserSorter::dcc2Lme ( int  dccNum,
int  dccSide 
)
private

Definition at line 336 of file LaserSorter.cc.

References min.

336  {
337  int fedid = (dcc%600) + 600; //to handle both FED and DCC id.
338  vector<int> lmes;
339  // EE -
340  if( fedid <= 609 ) {
341  if ( fedid <= 607 ) {
342  lmes.push_back(fedid-601+83);
343  } else if ( fedid == 608 ) {
344  lmes.push_back(90);
345  lmes.push_back(91);
346  } else if ( fedid == 609 ) {
347  lmes.push_back(92);
348  }
349  } //EB
350  else if ( fedid >= 610 && fedid <= 645 ) {
351  lmes.push_back(2*(fedid-610)+1);
352  lmes.push_back(lmes[0]+1);
353  } // EE+
354  else if ( fedid >= 646 ) {
355  if ( fedid <= 652 ) {
356  lmes.push_back(fedid-646+73);
357  } else if ( fedid == 653 ) {
358  lmes.push_back(80);
359  lmes.push_back(81);
360  } else if ( fedid == 654 ) {
361  lmes.push_back(82);
362  }
363  }
364  return lmes.size()==0?-1:lmes[min(lmes.size(), (size_t)side)];
365 }
#define min(a, b)
Definition: mlp_lapack.h:161
void LaserSorter::endJob ( void  )
virtual

Reimplemented from edm::EDAnalyzer.

Definition at line 850 of file LaserSorter.cc.

References closeAllStreams().

850  {
851  //TODO: better treatement of last files:
852  //they might be imcomplete...
853  closeAllStreams();
854 }
void closeAllStreams()
Definition: LaserSorter.cc:408
int LaserSorter::getDetailedTriggerType ( const edm::Handle< FEDRawDataCollection > &  rawdata,
double *  proba = 0 
)
private

Retrieve detailed trigger type (trigger type, DCC, side) from raw event

Parameters
rawdataFED data collection
probaif not null used to store the maximum of occurence frequency of the detailed trigger types (DTT) appearing in each DCC block. In normal condition every DCC indicated the same DTT and this value is 1.
Returns
detailed trigger type. In case of descripancy between the DCCs, the most frequent value is returned if it covers more than 80% of the present DCC blocks, -1 is returned otherwise. If event does not contain any ECAL data -2 is returned.

Definition at line 367 of file LaserSorter.cc.

References Majority< T >::add(), gather_cfg::cout, FEDRawData::data(), runTheMatrix::data, ecalDccFedIdMax_, ecalDccFedIdMin_, iNoEcalDataMess_, FEDNumbering::inRange(), maxNoEcalDataMess_, L1TEmulatorMonitor_cff::p, Majority< T >::result(), FEDRawData::size(), and verbosity_.

Referenced by analyze().

368  {
369  Majority<int> stat;
370  bool ecalData = false;
371  for(int id=ecalDccFedIdMin_; id<=ecalDccFedIdMax_; ++id){
372  if(!FEDNumbering::inRange(id)) continue;
373  const FEDRawData& data = rawdata->FEDData(id);
374  const int detailedTrigger32 = 5;
375  if(verbosity_>3) cout << "[LaserSorter] "
376  << "FED " << id << " data size: "
377  << data.size() << "\n";
378  if(data.size()>=4*(detailedTrigger32+1)){
379  ecalData = true;
380  const uint32_t* pData32 = (const uint32_t*) data.data();
381  int tType = pData32[detailedTrigger32] & 0xFFF;
382  if(verbosity_>3) cout << "[LaserSorter] "
383  << "Trigger type " << tType << "\n";
384  stat.add(tType);
385  }
386  }
387  if(!ecalData) return -2;
388  double p;
389  int tType = stat.result(&p);
390  if(p<0){
391  //throw cms::Exception("NotFound") << "No ECAL DCC data found\n";
393  edm::LogWarning("NotFound") << "No ECAL DCC data found. "
394  "(This warning will be disabled for the current run after "
395  << maxNoEcalDataMess_ << " occurences.)";
397  }
398  tType = -1;
399  } else if(p<.8){
400  //throw cms::Exception("EventCorruption") << "Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
401  edm::LogWarning("EventCorruption") << "Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
402  tType = -1;
403  }
404  if(proba) *proba = p;
405  return tType;
406 }
static const int ecalDccFedIdMin_
Definition: LaserSorter.h:305
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
void add(const T &value)
Definition: Majority.h:25
int iNoEcalDataMess_
Definition: LaserSorter.h:411
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
static bool inRange(int)
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
tuple cout
Definition: gather_cfg.py:41
int maxNoEcalDataMess_
Definition: LaserSorter.h:415
T result(double *proba) const
Definition: Majority.h:30
std::vector< int > LaserSorter::getFullyReadoutDccs ( const FEDRawDataCollection data) const
private

Retrieves DCCs which were fully read out (>=68 readout channels).

Parameters
dataDCC data collection
Returns
FED ids.

Definition at line 931 of file LaserSorter.cc.

References ecalDccFedIdMax_, ecalDccFedIdMin_, FEDRawDataCollection::FEDData(), isDccEventEmpty(), and query::result.

Referenced by analyze().

931  {
932  int nTowers;
933  vector<int> result;
934  for(int fed = ecalDccFedIdMin_; fed < ecalDccFedIdMax_; ++fed){
935  const FEDRawData& fedData = data.FEDData(fed);
936  isDccEventEmpty(fedData, 0, &nTowers);
937  if(nTowers>=68) result.push_back(fed);
938  }
939  return result;
940 }
static const int ecalDccFedIdMin_
Definition: LaserSorter.h:305
bool isDccEventEmpty(const FEDRawData &data, size_t *dccLen=0, int *nTowerBlocks=0) const
Definition: LaserSorter.cc:859
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
tuple result
Definition: query.py:137
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
void LaserSorter::getOutputFedList ( const edm::Event event,
const FEDRawDataCollection data,
std::vector< unsigned > &  fedIds 
) const
private

Computes the list of FEDs which data must be written out.

Parameters
dataCMS raw event
fedIds[out] list of FEDs to keep

Definition at line 902 of file LaserSorter.cc.

References ecalDccFedIdMax_, ecalDccFedIdMin_, FEDRawDataCollection::FEDData(), isDccEventEmpty(), matacqFedId_, and FEDRawData::size().

Referenced by writeEvent().

904  {
905  fedIds.erase(fedIds.begin(), fedIds.end());
906  for(int id=ecalDccFedIdMin_; id<=ecalDccFedIdMax_; ++id){
907  size_t dccLen;
908  const FEDRawData& dccEvent = data.FEDData(id);
909  if(id==matacqFedId_
910  || !isDccEventEmpty(dccEvent, &dccLen)){
911  fedIds.push_back(id);
912  }
913  if(dccLen*sizeof(uint64_t)!=dccEvent.size()){
914  edm::LogWarning("LaserSorter")
915  << "Length error in data of FED " << id
916  << " in event " << event.id()
917  << ", Data of this FED dropped.";
918  }
919  }
920  // cout << __FILE__ << ":" << __LINE__ << ": "
921  // << "data.FEDData(" << matacqFedId_ << ").size() = "
922  // << data.FEDData(matacqFedId_).size() << "\n";
923  if(data.FEDData(matacqFedId_).size()>4){//matacq block present
924  // cout << __FILE__ << ":" << __LINE__ << ": "
925  // << "Adding matacq to list of FEDs\n";
926  fedIds.push_back(matacqFedId_);
927  }
928 }
static const int ecalDccFedIdMin_
Definition: LaserSorter.h:305
bool isDccEventEmpty(const FEDRawData &data, size_t *dccLen=0, int *nTowerBlocks=0) const
Definition: LaserSorter.cc:859
static const int matacqFedId_
Definition: LaserSorter.h:420
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
unsigned long long uint64_t
Definition: Time.h:15
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
LaserSorter::OutStreamRecord * LaserSorter::getStream ( int  fedId,
edm::LuminosityBlockNumber_t  lumiBlock 
)
private

Gets and eventually creates the output stream for writing the events of a given FED and luminosity block.

Parameters
fedIdID of the FED the event is issued from
lumiBlockluminositu block of the event
Returns
pointer of the output stream record or null if not found.

Definition at line 440 of file LaserSorter.cc.

References abs, gather_cfg::cout, createOutStream(), ecalDccFedIdMax_, outStreamList_, and verbosity_.

Referenced by analyze(), and restoreStreamsOfLumiBlock().

441  {
442 
443  if((fedId != -1) &&
444  (fedId < ecalDccFedIdMin_ || fedId > ecalDccFedIdMax_)) fedId = -1;
445 
446  if(verbosity_) cout << "[LaserSorter] "
447  << "Looking for an opened output file for FED "
448  << fedId << " LB " << lumiBlock
449  << "\n";
450 
451  //first look if stream is already open:
452  for(OutStreamList::iterator it = outStreamList_.begin();
453  it != outStreamList_.end();
454  ++it){
455  if(it->fedId()==fedId &&
456  (abs((int)it->startingLumiBlock()-(int)lumiBlock)<=1)){
457  //stream found!
458  return &(*it);
459  }
460  }
461  //stream was not found. Let's create one
462 
463  if(verbosity_) cout << "[LaserSorter] "
464  << "File not yet opened. Opening it.\n";
465 
466  OutStreamList::iterator streamRecord = createOutStream(fedId, lumiBlock);
467  return streamRecord!=outStreamList_.end()?&(*streamRecord):0;
468 }
#define abs(x)
Definition: mlp_lapack.h:159
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
OutStreamList::iterator createOutStream(int fedId, edm::LuminosityBlockNumber_t lumiBlock)
Definition: LaserSorter.cc:558
tuple cout
Definition: gather_cfg.py:41
OutStreamList outStreamList_
Definition: LaserSorter.h:326
bool LaserSorter::isDccEventEmpty ( const FEDRawData data,
size_t *  dccLen = 0,
int *  nTowerBlocks = 0 
) const
private

Checks if an ECAL DCC event is empty. It is considered as empty if it does not contains FE data ("tower" block). So an event containing SRP or TCC data can be tagged as empty by this method. , if not null filled with the event length read from the DCC header. if not null, filled with number of tower blocks

Returns
true if event is empty, false otherwise

Definition at line 859 of file LaserSorter.cc.

References FEDRawData::data(), dtT0WireCalibration_cfg::dataType, and FEDRawData::size().

Referenced by getFullyReadoutDccs(), and getOutputFedList().

860  {
861  if(nTowerBlocks) *nTowerBlocks = 0;
862  //DCC event is considered empty if it does not contains any Tower block
863  //( = FE data)
864  bool rc = true;
865  if(dccLen) *dccLen = 0;
866  const unsigned nWord32 = data.size()/sizeof(uint32_t);
867  if(nWord32==0){
868  //cout << "[LaserSorter " << now() << "] " << "FED block completly empty\n";
869  return true;
870  }
871  for(unsigned iWord32 = 0; iWord32 < nWord32; iWord32 += 2){
872  uint32_t* data32 = ((uint32_t*)(data.data())) + iWord32;
873  int dataType = (data32[1] >>28) & 0xF;
874  // cout << hex << "0x" << setfill('0')
875  // << setw(8) << data32[1] << "'" << setw(8) << data32[0]
876  // << " dataType: 0x" << dataType
877  // << dec << setfill(' ') << "\n";
878  if(0==(dataType>>2)){//in DCC header
879  const int dccHeaderId = (data32[1] >>24) & 0x3F;
880  if(dccHeaderId==1){
881  if(dccLen) *dccLen=((data32[0] >>0 ) & 0xFFFFFF);
882  }
883  } if((dataType>>2)==3){//Tower block
884  rc = false;
885  if(nTowerBlocks){//number of tower block must be counted
886  ++(*nTowerBlocks);
887  } else{
888  break;
889  }
890  }
891  }
892  // cout << "[LaserSorter " << now() << "] " << "DCC Len: ";
893 
894  // if(dccLen){
895  // cout << (*dccLen) << " event ";
896  // }
897  // cout << (rc?"":"non") << " empty"
898  // << endl;
899  return rc;
900 }
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
int LaserSorter::readFormatVersion ( std::ifstream &  in,
const std::string &  fileName 
)
private

Gets format version of an LMF file. Position of file is preserved.

Parameters
instream to read the file
fileNamename of the file. Used in error message.
Returns
version or -1 in case of error.

Definition at line 1055 of file LaserSorter.cc.

References runTheMatrix::data, and L1TEmulatorMonitor_cff::p.

Referenced by createOutStream().

1056  {
1057  int vers = -1;
1058  streampos p = in.tellg();
1059 
1060  uint32_t data;
1061 
1062  in.read((char*)&data, sizeof(data));
1063 
1064  char magic[4];
1065 
1066  magic[0] = data & 0xFF;
1067  magic[1] = (data >>8) & 0xFF;
1068  magic[2] = (data >>16) & 0xFF;
1069  magic[3] = 0;
1070 
1071 
1072  const string lmf = string("LMF");
1073 
1074  if(in.good() && lmf.compare(magic)==0){
1075  vers = (data >>24) & 0xFF;
1076  }
1077 
1078  if(lmf.compare(magic)!=0){
1079  edm::LogWarning("LaserSorter") << "File " << fileName
1080  << "is not an LMF file.\n";
1081  }
1082 
1083  in.clear();
1084  in.seekg(p);
1085  return vers;
1086 }
bool LaserSorter::readIndexTable ( std::ifstream &  in,
std::string &  inName,
OutStreamRecord outRcd,
std::string *  err 
)
private

Read index table of an LMF file.

Parameters
inLMF file whose index table must be read.
inNamename of the in file
outRcdrecord of the output file. whose the index table must be copied to.
errif not nul, in case of failure filled with the error message.
Returns
true in case of success, false otherwise

Definition at line 982 of file LaserSorter.cc.

References gather_cfg::cout, LaserSorter::OutStreamRecord::excludedOrbit(), i, recoMuon::in, LaserSorter::OutStreamRecord::indices(), maxEvents_, launcher::nevts, pos, asciidump::s, and verbosity_.

Referenced by createOutStream().

985  {
986  stringstream errMsg;
987 
988  ifstream* s = &in;
989 
990  streampos pos = s->tellg();
991  s->clear();
992  s->seekg(0);
993 
994  uint32_t fileHeader[2];
995  s->read((char*)&fileHeader[0], sizeof(fileHeader));
996  uint32_t indexTablePos = fileHeader[1];
997 
998  if(s->eof()){
999  s->clear();
1000  s->seekg(0);
1001  errMsg << "Failed to read header of file " << inName
1002  << ".";
1003  if(err) *err = errMsg.str();
1004  return false;
1005  }
1006 
1007  s->seekg(indexTablePos);
1008 
1009  uint32_t nevts = 0;
1010  s->read((char*)&nevts, sizeof(nevts));
1011  s->ignore(4);
1012  if(s->bad()){
1013  errMsg << "Failed to read index table from file "
1014  << inName << ".";
1015  if(err) *err = errMsg.str();
1016  return false;
1017  }
1018  if(nevts>maxEvents_){
1019  errMsg << "Number of events indicated in event index of file "
1020  << inName << " (" << nevts << ") "
1021  << "is unexpectively large.";
1022  if(err) *err = errMsg.str();
1023  return false;
1024  }
1025  outRcd.indices()->resize(nevts);
1026  s->read((char*)&(*outRcd.indices())[0], nevts*sizeof(IndexRecord));
1027  if(s->bad()){
1028  outRcd.indices()->clear();
1029  errMsg << "Failed to read index table from file "
1030  << inName << ".";
1031  if(err) *err = errMsg.str();
1032  return false;
1033  }
1034  if(nevts>maxEvents_){
1035  errMsg << "Number of events indicated in event index of file "
1036  << inName << " is unexpectively large.";
1037  if(err) *err = errMsg.str();
1038  outRcd.indices()->clear();
1039  return false;
1040  }
1041 
1042  if(verbosity_ > 1) cout << "[LaserSorter] " << "Orbit IDs of events "
1043  << "already contained in the file "
1044  << inName << ":";
1045  for(unsigned i = 0; i < outRcd.indices()->size(); ++i){
1046  if(verbosity_>1){
1047  cout << " " << setw(9) << (*outRcd.indices())[i].orbit;
1048  }
1049  outRcd.excludedOrbit().insert((*outRcd.indices())[i].orbit);
1050  }
1051  if(verbosity_>1) cout << "\n";
1052  return true;
1053 }
int i
Definition: DBlmapReader.cc:9
static const unsigned maxEvents_
Definition: LaserSorter.h:430
list nevts
Definition: launcher.py:16
tuple cout
Definition: gather_cfg.py:41
string s
Definition: asciidump.py:422
bool LaserSorter::renameAsBackup ( const std::string &  fileName,
std::string &  newFileName 
)
private

Definition at line 537 of file LaserSorter.cc.

References i, and link().

Referenced by createOutStream().

538  {
539  int i = 0;
540  int err;
541  static int maxTries = 100;
542  stringstream newFileName_;
543  do{
544  newFileName_.str("");
545  newFileName_ << fileName << "~";
546  if(i>0) newFileName_ << i;
547  err = link(fileName.c_str(), newFileName_.str().c_str());
548  if(err==0){
549  newFileName = newFileName_.str();
550  err = unlink(fileName.c_str());
551  }
552  ++i;
553  } while((err!=0) && (errno == EEXIST) && (i < maxTries));
554  return err==0;
555 }
int i
Definition: DBlmapReader.cc:9
std::string link(std::string &nm, std::string &ns)
Definition: hierarchy.cc:47
void LaserSorter::restoreStreamsOfLumiBlock ( int  lumiBlock)
private

Opens output streams associated to a lumi block according to already existing files. To be used when previously processed luminosity block is not

Parameters
lumiBlockID of the luminosity block whose output streams must be reopened.

Definition at line 1103 of file LaserSorter.cc.

References ecalDccFedIdMax_, ecalDccFedIdMin_, convertXMLtoSQLite_cfg::fileName, getStream(), asciidump::s, and streamFileName().

Referenced by analyze().

1103  {
1104  string dummy;
1105  string fileName;
1106 
1107  for(int fedId = ecalDccFedIdMin_-2; fedId <= ecalDccFedIdMax_; ++fedId){
1108  int fedId_;
1109  if(fedId == ecalDccFedIdMin_-2) fedId_ = -1; //stream for event w/o ECAL data
1110  else fedId_ = fedId;
1111  streamFileName(fedId_, lumiBlock, dummy, fileName);
1112  struct stat s;
1113  //TODO: could be optimized by adding an option to get stream
1114  //to open only existing file: would avoid double call to streamFileName.
1115  if(stat(fileName.c_str(), &s)==0){//file exists
1116  getStream(fedId_, lumiBlock);
1117  }
1118  }
1119 }
OutStreamRecord * getStream(int fedId, edm::LuminosityBlockNumber_t lumiBlock)
Definition: LaserSorter.cc:440
static const int ecalDccFedIdMin_
Definition: LaserSorter.h:305
void streamFileName(int fedId, edm::LuminosityBlockNumber_t lumiBlock, std::string &tmpName, std::string &finalName)
Definition: LaserSorter.cc:748
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
string s
Definition: asciidump.py:422
void LaserSorter::streamFileName ( int  fedId,
edm::LuminosityBlockNumber_t  lumiBlock,
std::string &  tmpName,
std::string &  finalName 
)
private

Builds the file names for the group of event corresponding to a FED and a starting lumi block.

Parameters
fedIdFED ID of the event set
lumiBlockstarting luminoisty block of the event set
[out]tmpNamename of the file to use when filling it.
[out]finalNamename of the file once completed.

Definition at line 748 of file LaserSorter.cc.

References gather_cfg::cout, dir, ecalDccFedIdMax_, ecalDccFedIdMin_, edm::hlt::Exception, fedSubDirs_, convertXMLtoSQLite_cfg::fileName, outputDir_, runNumber_, and verbosity_.

Referenced by createOutStream(), and restoreStreamsOfLumiBlock().

750  {
751  int iFed;
752  if(fedId >= ecalDccFedIdMin_ && fedId <= ecalDccFedIdMax_){
753  iFed = fedId - ecalDccFedIdMin_ + 1;
754  } else if(fedId < 0){
755  iFed = -1; //event w/o ECAL data
756  } else {
757  iFed = 0;
758  }
759  if(iFed < -1 || iFed >= (int)fedSubDirs_.size()){
760  throw cms::Exception("LaserSorter")
761  << "Bug found at " << __FILE__ << ":" << __LINE__
762  << ". FED ID is out of index!";
763  }
764 
765 
766  struct stat fileStat;
767 
768  stringstream buf;
769  buf << outputDir_ << "/"
770  << (iFed<0 ? "Empty" : fedSubDirs_[iFed]);
771 
772  string dir = buf.str();
773  if(0==stat(dir.c_str(), &fileStat)){
774  if(!S_ISDIR(fileStat.st_mode)){
775  throw cms::Exception("[LaserSorter]")
776  << "File " << dir << " exists but is not a directory "
777  << " as expected.";
778  }
779  } else {//directory does not exists, let's try to create it
780  if(0!=mkdir(dir.c_str(), 0755)){
781  throw cms::Exception("[LaserSorter]")
782  << "Failed to create directory " << dir
783  << " for writing data.";
784  }
785  }
786 
787  buf.str("");
788  buf << "Run" << runNumber_
789  << "_LB" << setfill('0') << setw(4) << lumiBlock
790  << ".lmf";
791  string fileName = buf.str();
792  string tmpFileName = fileName + ".part";
793 
794  finalName = dir + "/" + fileName;
795  tmpName = dir + "/" + tmpFileName;
796 
797  if(verbosity_>3) cout << "[LaserSorter] " << "File path: "
798  << finalName << "\n";
799 }
static const int ecalDccFedIdMin_
Definition: LaserSorter.h:305
edm::RunNumber_t runNumber_
Definition: LaserSorter.h:350
std::vector< std::string > fedSubDirs_
Definition: LaserSorter.h:338
std::string outputDir_
Definition: LaserSorter.h:334
static const int ecalDccFedIdMax_
Definition: LaserSorter.h:309
tuple cout
Definition: gather_cfg.py:41
dbl *** dir
Definition: mlp_gen.cc:35
std::string LaserSorter::toString ( uint64_t  t)
staticprivate

Help function to format a date

Definition at line 1088 of file LaserSorter.cc.

Referenced by writeEventHeader().

1088  {
1089  char buf[256];
1090 
1091  time_t tsec = t>>32;
1092 
1093  uint32_t tusec = t & 0xFFFFFFFF;
1094  strftime(buf, sizeof(buf), "%F %R %S s", localtime(&tsec));
1095  buf[sizeof(buf)-1] = 0;
1096 
1097  stringstream buf2;
1098  buf2 << (tusec+500)/1000;
1099 
1100  return string(buf) + " " + buf2.str() + " ms";
1101 }
bool LaserSorter::writeEvent ( OutStreamRecord out,
const edm::Event event,
int  detailedTriggerType,
const FEDRawDataCollection data 
)
private

Writes a monitoring events to an output stream.

Parameters
outstream to write the event out
eventEDM event, used to retrieve meta information like timestamp and ID.
IDof the unique FED block of the event
dataDCC data
Returns
true on success, false on failure
See Also
getStream(int, edm::LuminosityBlockNumber_t)

Definition at line 470 of file LaserSorter.cc.

References FEDRawDataCollection::FEDData(), getOutputFedList(), LaserSorter::OutStreamRecord::indices(), LaserSorter::OutStreamRecord::out(), dbtoconf::out, edm::Event::size(), writeEventHeader(), and writeFedBlock().

Referenced by analyze().

472  {
473 
474  ofstream& out = *outRcd.out();
475  bool rc = true;
476  vector<unsigned> fedIds;
477  getOutputFedList(event, data, fedIds);
478 
479  out.clear();
480  uint32_t evtStart = out.tellp();
481  if(out.bad()) evtStart = 0;
482  // cout << "------> fedIds.size() = " << fedIds.size() << endl;
483  rc &= writeEventHeader(out, event, dtt, fedIds.size());
484 
485  for(unsigned iFed = 0; iFed < fedIds.size() && rc; ++iFed){
486 // cout << "------> data.FEDData(" << fedIds[iFed] << ").size = "
487 // << data.FEDData(fedIds[iFed]).size() << "\n";
488  rc &= writeFedBlock(out, data.FEDData(fedIds[iFed]));
489  }
490 
491  if(rc){
492  //update index table for this file:
493  vector<IndexRecord>& indices = *outRcd.indices();
494  IndexRecord indexRcd = {event.orbitNumber(), evtStart};
495  indices.push_back(indexRcd);
496  }
497  return rc;
498 }
size_t size() const
Definition: Event.cc:194
void getOutputFedList(const edm::Event &event, const FEDRawDataCollection &data, std::vector< unsigned > &fedIds) const
Definition: LaserSorter.cc:902
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
bool writeFedBlock(std::ofstream &out, const FEDRawData &data)
Definition: LaserSorter.cc:500
tuple out
Definition: dbtoconf.py:99
bool writeEventHeader(std::ofstream &out, const edm::Event &evt, int fedId, unsigned nFeds)
Definition: LaserSorter.cc:711
bool LaserSorter::writeEventHeader ( std::ofstream &  out,
const edm::Event evt,
int  fedId,
unsigned  nFeds 
)
private

Write event header with event identification and timestamp.

Parameters
outoutput stream to write to
evtevent
Returns
false in case of write failure

Definition at line 711 of file LaserSorter.cc.

References edm::EventBase::bunchCrossing(), colorNames, gather_cfg::cout, runTheMatrix::data, detailedTrigNames, edm::EventID::event(), edm::EventBase::id(), edm::EventBase::luminosityBlock(), now(), edm::EventBase::orbitNumber(), edm::Event::run(), edm::EventBase::time(), toString(), edm::Timestamp::value(), and verbosity_.

Referenced by writeEvent().

714  {
715  uint32_t data[10];
716 
717  data[0] = evt.time().value() & 0xFFFFFFFF;
718  data[1] = evt.time().value() >>32;
719  data[2] = evt.luminosityBlock();
720  data[3] = evt.run();
721  data[4] = evt.orbitNumber();
722  data[5] = evt.bunchCrossing();
723  data[6] = evt.id().event();
724  data[7] = dtt;
725  data[8] = nFeds;
726  data[9] = 0; //reserved (to be aligned on 64-bits)
727 
728  if(verbosity_>1){
729  cout << "[LaserSorter " << now() << "] " << "Write header of event: "
730  << "Time: " << toString(evt.time().value())
731  << ", LB: " << evt.luminosityBlock()
732  << ", Run: " << evt.run()
733  << ", Bx: " << evt.bunchCrossing()
734  << ", Event ID: " << evt.id().event()
735  << ", Detailed trigger type: 0x" << hex << dtt << dec
736  << " (" << detailedTrigNames[(dtt>>8)&0x7] << ", "
737  << colorNames[(dtt>>6)&0x3] << ", DCC " << (dtt&0x3f)
738  << ", side " << ((dtt>>10) &0x1) << ")"
739  << ", number of FEDs: " << nFeds
740  << "\n";
741  }
742 
743  out.clear();
744  out.write((char*)data, sizeof(data));
745  return !out.bad();
746 }
EventNumber_t event() const
Definition: EventID.h:44
static const char *const detailedTrigNames[]
Definition: LaserSorter.cc:50
static std::string toString(uint64_t t)
int bunchCrossing() const
Definition: EventBase.h:62
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:59
RunNumber_t run() const
Definition: Event.h:66
int orbitNumber() const
Definition: EventBase.h:63
tuple out
Definition: dbtoconf.py:99
TimeValue_t value() const
Definition: Timestamp.cc:72
static std::string now()
Definition: LaserSorter.cc:71
edm::EventID id() const
Definition: EventBase.h:56
tuple cout
Definition: gather_cfg.py:41
static const char *const colorNames[]
Definition: LaserSorter.cc:61
edm::Timestamp time() const
Definition: EventBase.h:57
bool LaserSorter::writeFedBlock ( std::ofstream &  out,
const FEDRawData data 
)
private

Writes out data of a FED

Parameters
outstream to write the event out
dataFED data
Returns
true on success, false on failure

Definition at line 500 of file LaserSorter.cc.

References gather_cfg::cout, FEDRawData::data(), edm::hlt::Exception, now(), FEDRawData::size(), and verbosity_.

Referenced by writeEvent().

501  {
502  bool rc = false;
503  if (data.size()>4){
504  const uint32_t * pData
505  = reinterpret_cast<uint32_t*>(const_cast<unsigned char*> ( data.data()));
506 
507  uint32_t dccLen64 = pData[2] & 0x00FFFFFF; //in 32-byte unit
508 
509  if(data.size() != dccLen64*sizeof(uint64_t)){
510 // throw cms::Exception("Bug") << "Bug found in "
511 // << __FILE__ << ":" << __LINE__ << ".";
512  throw cms::Exception("LaserSorter")
513  << "Mismatch between FED fragment size indicated in header "
514  << "(" << dccLen64 << "*8 Byte) "
515  << "and actual size (" << data.size() << " Byte) "
516  << "for FED ID " << ((pData[0] >>8) & 0xFFF) << "!\n";
517  }
518 
519  if(verbosity_>3) cout << "[LaserSorter " << now() << "] " << "Event fragment size: "
520  << data.size() << " Byte"
521  << "\t From Dcc header: " << dccLen64*8 << " Byte\n";
522 
523  const size_t nBytes = data.size();
524  // cout << "[LaserSorter] "
525  // << "Writing " << nBytes << " byte from adress "
526  // << (void*) data.data() << " to file.\n";
527  if(out.fail()) cout << "[LaserSorter " << now() << "] " << "Problem with stream!\n";
528  out.write((char*)data.data(), nBytes);
529  rc = true;
530  } else{
531  throw cms::Exception("Bug") << "Bug found in "
532  << __FILE__ << ":" << __LINE__ << ".\n";
533  }
534  return rc;
535 }
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
tuple out
Definition: dbtoconf.py:99
unsigned long long uint64_t
Definition: Time.h:15
static std::string now()
Definition: LaserSorter.cc:71
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
tuple cout
Definition: gather_cfg.py:41
void LaserSorter::writeFileHeader ( std::ofstream &  out)
private

Writing file header for an LMF binary file

Parameters
outstream of the output file

Definition at line 694 of file LaserSorter.cc.

References edm::hlt::Exception, formatVersion_, and zero.

Referenced by createOutStream().

694  {
695  out.clear();
696 
697  uint32_t id = 'L' | ('M' <<8) | ('F' <<16) | (formatVersion_<<24);
698 
699  out.write((char*)&id, sizeof(uint32_t));
700 
701  //index position (to be filled at end of writing)
702  uint32_t zero = 0;
703  out.write((char*)&zero, sizeof(uint32_t));
704 
705  if(out.fail()){
706  throw cms::Exception("LaserSorter")
707  << "Failed to write file header.\n";
708  }
709 }
unsigned char formatVersion_
Definition: LaserSorter.h:330
tuple out
Definition: dbtoconf.py:99
bool LaserSorter::writeIndexTable ( std::ofstream &  out,
std::vector< IndexRecord > &  indices 
)
private

Writes index table in LMF output file. stream must be positionned to the place for the index table (end of file).

Parameters
outstream of output file.
indicesindex table

Definition at line 943 of file LaserSorter.cc.

References runTheMatrix::data, end, i, indexOffset32_, launcher::nevts, and python.multivaluedict::sort().

Referenced by closeOutStream().

944  {
945  uint32_t indexTablePos = out.tellp();
946  uint32_t nevts = indices.size();
947 
948  out.clear();
949  out.write((char*)&nevts, sizeof(nevts));
950  const uint32_t reserved = 0;
951  out.write((char*)&reserved, sizeof(reserved));
952 
953  if(out.bad()) return false;
954 
955  sort(indices.begin(), indices.end());
956 
957  for(unsigned i = 0; i < indices.size(); ++i){
958  uint32_t data[2];
959  data[0] = indices[i].orbit;
960  data[1] = indices[i].filePos;
961  out.write((char*)data, sizeof(data));
962  }
963 
964  if(out.bad()) return false; //intial 0 valur for index table position
965  // is left to indicate corrupted table.
966 
967  //writes index table position:x
968  out.clear();
969  out.seekp(indexOffset32_*sizeof(uint32_t));
970  // cout << "[LaserSorter] Index table position: 0x" << hex << indexTablePos
971  // << dec << "\n";
972  if(!out.bad()) out.write((char*)&indexTablePos, sizeof(uint32_t));
973 
974  bool rc = !out.bad();
975 
976  //reposition pointer to eof:
977  out.seekp(0, ios::end);
978 
979  return rc;
980 }
int i
Definition: DBlmapReader.cc:9
static const int indexOffset32_
Definition: LaserSorter.h:425
#define end
Definition: vmac.h:38
tuple out
Definition: dbtoconf.py:99
list nevts
Definition: launcher.py:16

Member Data Documentation

int LaserSorter::detailedTrigType_
private

Trigger type of calibration event. -1 if unkown.

Definition at line 313 of file LaserSorter.h.

Referenced by analyze().

bool LaserSorter::disableOutput_
private

Switch to disable writing output file (for test purpose).

Definition at line 346 of file LaserSorter.h.

Referenced by analyze().

bool LaserSorter::doOutputList_
private

Switch for logging paths of the output files

Definition at line 370 of file LaserSorter.h.

Referenced by closeOutStream(), and LaserSorter().

const int LaserSorter::ecalDccFedIdMax_ = 654
staticprivate
const int LaserSorter::ecalDccFedIdMin_ = 601
staticprivate

Lower bound of ECAL DCC FED ID

Definition at line 305 of file LaserSorter.h.

Referenced by analyze(), getDetailedTriggerType(), getFullyReadoutDccs(), getOutputFedList(), restoreStreamsOfLumiBlock(), and streamFileName().

std::vector<std::string> LaserSorter::fedSubDirs_
private

Subdirectories for output file of each FED

Definition at line 338 of file LaserSorter.h.

Referenced by LaserSorter(), and streamFileName().

unsigned char LaserSorter::formatVersion_
private

Data format version of lmf output file

Definition at line 330 of file LaserSorter.h.

Referenced by createOutStream(), and writeFileHeader().

const int LaserSorter::indexOffset32_ = 1
staticprivate

Definition at line 425 of file LaserSorter.h.

Referenced by writeIndexTable().

int LaserSorter::iNoEcalDataMess_
private

number of "ECAL DCC data" message in a run

Definition at line 411 of file LaserSorter.h.

Referenced by analyze(), and getDetailedTriggerType().

int LaserSorter::iNoFullReadoutDccError_
private

Number of "No fully readout DCC error"

Definition at line 401 of file LaserSorter.h.

Referenced by analyze().

std::ofstream LaserSorter::logFile_
private

File for logging

Definition at line 317 of file LaserSorter.h.

Referenced by analyze(), LaserSorter(), and ~LaserSorter().

edm::LuminosityBlockNumber_t LaserSorter::lumiBlock_
private

Luminosity block of event under processing

Definition at line 321 of file LaserSorter.h.

Referenced by analyze().

const int LaserSorter::matacqFedId_ = 655
staticprivate

FED ID associated to Matacq data

Definition at line 420 of file LaserSorter.h.

Referenced by getOutputFedList().

const unsigned LaserSorter::maxEvents_ = 1<<20
staticprivate

Limit of number of events to prevent exhausting memory with indexTable_ in case of file corruption.

Definition at line 430 of file LaserSorter.h.

Referenced by readIndexTable().

int LaserSorter::maxFullReadoutDccError_
private

Maximum number of "No fully readout DCC error" message in a run

Definition at line 406 of file LaserSorter.h.

Referenced by analyze().

int LaserSorter::maxNoEcalDataMess_
private

Maximum number of "ECAL DCC data" message in a run

Definition at line 415 of file LaserSorter.h.

Referenced by getDetailedTriggerType().

std::string LaserSorter::outputDir_
private

Top directory for output files

Definition at line 334 of file LaserSorter.h.

Referenced by LaserSorter(), valtools.webpage::readCaptions(), and streamFileName().

std::ofstream LaserSorter::outputList_
private

stream where list of output file is listed to

Definition at line 378 of file LaserSorter.h.

Referenced by closeOutStream(), and LaserSorter().

std::string LaserSorter::outputListFile_
private

name of file where list of output file is listed to

Definition at line 366 of file LaserSorter.h.

Referenced by LaserSorter().

OutStreamList LaserSorter::outStreamList_
private

List of output stream to write sorted data

Definition at line 326 of file LaserSorter.h.

Referenced by closeAllStreams(), closeOldStreams(), closeOutStream(), createOutStream(), and getStream().

edm::RunNumber_t LaserSorter::runNumber_
private

Run number of event under process

Definition at line 350 of file LaserSorter.h.

Referenced by analyze(), and streamFileName().

struct LaserSorter::stats_t LaserSorter::stats_
private

Referenced by analyze(), and ~LaserSorter().

LaserSorter::stats_t LaserSorter::stats_init = {0, 0, 0, 0, 0}
staticprivate

Definition at line 448 of file LaserSorter.h.

std::ofstream LaserSorter::timeLog_
private

Output stream to log code timing

Definition at line 358 of file LaserSorter.h.

Referenced by analyze(), and LaserSorter().

std::string LaserSorter::timeLogFile_
private

Name of the file to log the processing time

Definition at line 342 of file LaserSorter.h.

Referenced by LaserSorter().

timeval LaserSorter::timer_
private

Buffer for timing

Definition at line 354 of file LaserSorter.h.

Referenced by analyze(), and LaserSorter().

bool LaserSorter::timing_
private

Switch for code timing.

Definition at line 362 of file LaserSorter.h.

Referenced by analyze(), and LaserSorter().

int LaserSorter::verbosity_
private