CMS 3D CMS Logo

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

List of all members.

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 ()

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 lumiBlockSpan_
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}

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_, Exception, fedSubDirs_, logFile_, now(), dbtoconf::out, outputDir_, outputList_, outputListFile_, timeLog_, timeLogFile_, timer_, and timing_.

  : lumiBlock_(0),
    formatVersion_(5),
    outputDir_(pset.getParameter<std::string>("outputDir")),
    fedSubDirs_(pset.getParameter<std::vector<std::string> >("fedSubDirs")),
    timeLogFile_(pset.getUntrackedParameter<std::string>("timeLogFile", "")),
    disableOutput_(pset.getUntrackedParameter<bool>("disableOutput", false)),
    runNumber_(0),
    outputListFile_(pset.getUntrackedParameter<string>("outputListFile", "")),
    doOutputList_(false),
    verbosity_(pset.getUntrackedParameter<int>("verbosity", 0)),
    iNoFullReadoutDccError_(0),
    maxFullReadoutDccError_(pset.getParameter<int>("maxFullReadoutDccError")),
    iNoEcalDataMess_(0),
    maxNoEcalDataMess_(pset.getParameter<int>("maxNoEcalDataMess")),
    lumiBlockSpan_(pset.getParameter<int>("lumiBlockSpan")),
    stats_(stats_init)
{

  gettimeofday(&timer_, 0);
  logFile_.open("eventSelect.log", ios::app | ios::out); 
  
  const unsigned nEcalFeds= 54;
  if(fedSubDirs_.size()!= nEcalFeds+1){
    throw cms::Exception("LaserSorter") 
      << "Configuration error: "
      << "fedSubDirs parameter must be a vector "
      << " of " << nEcalFeds << " strings"
      << " (subdirectory for unknown triggered FED followed by "
      "subdirectories for FED ID 601 "
      "to FED ID 654 in increasing FED ID order)";
  }
  
  if(outputListFile_.size()!=0){
    outputList_.open(outputListFile_.c_str(), ios::app);
    if(outputList_.bad()){
      throw cms::Exception("FileOpen")
        << "Failed to open file '" << outputListFile_
        << "' for logging of output file path list.";
    }
    doOutputList_ = true;
  }
  
  if(timeLogFile_.size()>0){
    timeLog_.open(timeLogFile_.c_str());
    if(timeLog_.fail()){
      cout << "[LaserSorter " << now() << "] "
           << "Failed to open file " << timeLogFile_ << " to log timing.\n";
      timing_ = false;
    } else{
      timing_ = true;
    }
  }

  struct stat fileStat;
  if(0==stat(outputDir_.c_str(), &fileStat)){
    if(!S_ISDIR(fileStat.st_mode)){
      throw cms::Exception("[LaserSorter]")
        << "File " << outputDir_ << " exists but is not a directory "
        << " as expected.";
    }
  } else {//directory does not exists, let's try to create it
    if(0!=mkdir(outputDir_.c_str(), 0755)){
      throw cms::Exception("[LaserSorter]")
        << "Failed to create directory " << outputDir_
        << " for writing data.";
    }
  }

  logFile_ << "# "
    "run\t"
    "LB\t" 
    "event\t" 
    "trigType\t"
    "FED\t"
    "side\t"
    "LB out\t"
    "Written\t"
    "ECAL data\n";
}
LaserSorter::~LaserSorter ( )

Definition at line 166 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_.

                         {
  logFile_ << "Summary. Event count: "
           << stats_.nRead << " processed, "
           << stats_.nWritten << " written, "
           << stats_.nInvalidDccStrict << " with errors in DCC ID values, "
           << stats_.nInvalidDccWeak << " with unusable DCC ID values, "
           << stats_.nRestoredDcc << " restored DCC ID based on DCC block size\n";
}

Member Function Documentation

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

Implements edm::EDAnalyzer.

Definition at line 178 of file LaserSorter.cc.

References closeAllStreams(), closeOldStreams(), create_public_lumi_plots::color, colorNames, gather_cfg::cout, ecaldqm::dccId(), detailedTrigNames, detailedTrigType_, disableOutput_, ecalDccFedIdMax_, ecalDccFedIdMin_, LaserSorter::OutStreamRecord::excludedOrbit(), LaserSorter::OutStreamRecord::finalFileName(), getDetailedTriggerType(), getFullyReadoutDccs(), getStream(), i, edm::EventBase::id(), iNoEcalDataMess_, iNoFullReadoutDccError_, logFile_, lumiBlock_, lumiBlockSpan_, edm::EventBase::luminosityBlock(), maxFullReadoutDccError_, csvLumiCalc::maxLumi, csvLumiCalc::minLumi, 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, lumiPlot::rawdata, restoreStreamsOfLumiBlock(), edm::EventID::run(), runNumber_, LaserSorter::OutStreamRecord::startingLumiBlock(), stats_, lumiQTWidget::t, timeLog_, timer_, timing_, verbosity_, and writeEvent().

                                                                  {
  if(timing_){
    timeval t;
    gettimeofday(&t, 0);
    timeLog_ << t.tv_sec << "."
             << setfill('0') << setw(3) << (t.tv_usec+500)/1000 << setfill(' ')
             << "\t"
             << (t.tv_usec - timer_.tv_usec)*1. 
      + (t.tv_sec - timer_.tv_sec)*1.e6 << "\t";
    timer_ = t;
  } 

  ++stats_.nRead;
  
  if(event.id().run()!=runNumber_){//run changed or first event
    //for a new run, starts with a new output stream set.
    closeAllStreams();
    runNumber_ = event.id().run();
    iNoFullReadoutDccError_ = 0;
    iNoEcalDataMess_ = 0;
  }
  
  edm::Handle<FEDRawDataCollection> rawdata;
  event.getByType(rawdata);  
  
  //The "detailed trigger type DCC field content:
  double dttProba = 0;
  detailedTrigType_ = getDetailedTriggerType(rawdata, &dttProba);
  
  const int trigType =   (detailedTrigType_ >>8 ) & 0x7;
  const int color    =   (detailedTrigType_ >>6 ) & 0x3;
  const int dccId    =   (detailedTrigType_ >>0 ) & 0x3F;
  int triggeredFedId =   (detailedTrigType_ == -2) ? -1 : (600 + dccId);
  const int side     =   (detailedTrigType_ >>11) & 0x1;
  //monitoring region extended id:
  //  const int lme = dcc2Lme(dccId, side);

  if(detailedTrigType_ > -2){
    if(dttProba < 1. || triggeredFedId < ecalDccFedIdMin_
       || triggeredFedId > ecalDccFedIdMax_){
      ++stats_.nInvalidDccStrict;
    }

    if(triggeredFedId<ecalDccFedIdMin_ || triggeredFedId > ecalDccFedIdMax_){
      if(verbosity_) cout <<  "[LaserSorter " << now() << "] " << "DCC ID (" << dccId
                          << ") found in trigger type is out of range.";
      ++stats_.nInvalidDccWeak;
      vector<int> ids = getFullyReadoutDccs(*rawdata);
      if(ids.size()==0){
        if(verbosity_ && iNoFullReadoutDccError_ < maxFullReadoutDccError_){
          cout << " No fully read-out DCC found\n";
          ++iNoFullReadoutDccError_;
        }
      } else if(ids.size()==1){
        triggeredFedId = ids[0];
        if(verbosity_) cout << " ID guessed from DCC payloads\n";
        ++stats_.nRestoredDcc;
      } else{ //ids.size()>1
        if(verbosity_){
          cout << " Several fully read-out Dccs:";
          for(unsigned i=0; i < ids.size(); ++i) cout << " " << ids[i];
          cout << "\n";
        }
      }
    }
  
    if(verbosity_>1) cout << "\n----------------------------------------------------------------------\n"
                          << "Event id: " 
                          << " " << event.id() << "\n"
                          << "Lumin block: " << event.luminosityBlock() << "\n"
                          << "TrigType: " << detailedTrigNames[trigType&0x7]
                          << " Color: " << colorNames[color&0x3]
                          <<  " FED: " << triggeredFedId
                          << " side:" << side << "\n"
                          << "\n----------------------------------------------------------------------\n";
    
  } else{ //NO ECAL DATA
    if(verbosity_>1) cout << "\n----------------------------------------------------------------------\n"
                          << "Event id: " 
                          << " " << event.id() << "\n"
                          << "Lumin block: " << event.luminosityBlock() << "\n"
                          << "No ECAL data\n"
                          << "\n----------------------------------------------------------------------\n";
  }
  
  logFile_ << event.id().run() << "\t"
           << event.luminosityBlock() << "\t" 
           << event.id().event() << "\t" 
           << trigType << "\t"
           << triggeredFedId << "\t"
           << side;
    
  bool written = false;
  int assignedLB = -1;
  
  if(event.luminosityBlock()!=lumiBlock_){
    //lumi block change => need for stream garbage collection
    const int lb = event.luminosityBlock();
    closeOldStreams(lb);
    int minLumi = event.luminosityBlock() - lumiBlockSpan_;
    int maxLumi = event.luminosityBlock() + lumiBlockSpan_;
    for(int lb1 = minLumi; lb1 <= maxLumi; ++lb1){
      restoreStreamsOfLumiBlock(lb1);
    }
  }
    
//     if(event.luminosityBlock() < lumiBlock_){
//       throw cms::Exception("LaserSorter") 
//         << "Process event has a lumi block (" << event.luminosityBlock() << ")"
//         << "older than previous one (" << lumiBlock_ << "). "
//         << "This can be due by wrong input file ordering or bad luminosity "
//         << "block indication is the event header. "
//         << "Event cannot be processed";
//     }

    if(disableOutput_){
      /* NO OP*/
    } else{
      OutStreamRecord* out = getStream(triggeredFedId, event.luminosityBlock());

      if(out!=0){
        assignedLB = out->startingLumiBlock();
        if(out->excludedOrbit().find(event.orbitNumber())
           ==out->excludedOrbit().end()){
          if(verbosity_ > 1) cout << "[LaserSorter " << now() << "] "
                              << "Writing out event from FED " << triggeredFedId 
                              << " LB " << event.luminosityBlock()
                              << " orbit " << event.orbitNumber() << "\n";
          int dtt = (detailedTrigType_ >=0) ? detailedTrigType_ : -1; //shall we use -1 or 0 for undefined value?
          written = written
            || writeEvent(*out, event, dtt, *rawdata);
          ++stats_.nWritten;
        } else{
          if(verbosity_) cout << "[LaserSorter " << now() << "] "
                              << "File " << out->finalFileName() << " "
                              << "already contains calibration event from FED "
                              << triggeredFedId << ", LB = "
                              << event.luminosityBlock()
                              << " with orbit ID "
                              << event.orbitNumber() << ". Event skipped.\n";
        }
      }
    }
    lumiBlock_ = event.luminosityBlock();
    
    logFile_ << "\t";
    if(assignedLB>=0) logFile_ << assignedLB; else logFile_ << "-";
    logFile_ << "\t" << (written?"Y":"N") << "\n"; 
    logFile_  << "\t" << (detailedTrigType_==-2?"N":"Y") << "\n";
    
    if(timing_){
      timeval t;
      gettimeofday(&t, 0);
      timeLog_ << (t.tv_usec - timer_.tv_usec)*1. 
        + (t.tv_sec - timer_.tv_sec)*1.e6 << "\n";
      timer_ = t;
    }
}
void LaserSorter::beginJob ( void  ) [virtual]

Reimplemented from edm::EDAnalyzer.

Definition at line 857 of file LaserSorter.cc.

                          {
}
void LaserSorter::closeAllStreams ( ) [private]

Closes all opened output streams.

Definition at line 409 of file LaserSorter.cc.

References closeOutStream(), and outStreamList_.

Referenced by analyze(), and endJob().

                                 {
  for(OutStreamList::iterator it = outStreamList_.begin();
      it != outStreamList_.end();/*NOOP*/){
    it = closeOutStream(it);
  }
}
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 416 of file LaserSorter.cc.

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

Referenced by analyze().

                                                                     {
  const edm::LuminosityBlockNumber_t minLumiBlock = lumiBlock - lumiBlockSpan_;
  const edm::LuminosityBlockNumber_t maxLumiBlock = lumiBlock + lumiBlockSpan_;
  //If container type is ever changed, beware that
  //closeOutStream call in the loop removes it from outStreamList
  for(boost::ptr_list<OutStreamRecord>::iterator it = outStreamList_.begin();
      it != outStreamList_.end();
      /*NOOP*/){
    if(it->startingLumiBlock() < minLumiBlock
       || it->startingLumiBlock() > maxLumiBlock){
      //event older than 2 lumi block => stream can be closed
      if(verbosity_) cout << "[LaserSorter " << now() << "] "
                       << "Closing file for "
                       << "FED " 
                       << it->fedId()
                       << " LB " << it->startingLumiBlock()
                       << "\n";
      it = closeOutStream(it);
    } else{
      ++it;
    }
  }
}
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 803 of file LaserSorter.cc.

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

Referenced by closeAllStreams(), and closeOldStreams().

                                         {
  if(streamRecord==outStreamList_.end()) return outStreamList_.end();
  
  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Writing Index table of file "
                      << streamRecord->finalFileName() << "\n";
  ofstream& out = *streamRecord->out();
  out.clear();
  if(!writeIndexTable(out, *streamRecord->indices())){
    cout << "Error while writing index table for file "
         << streamRecord->finalFileName() << ". "
         << "Resulting file might be corrupted. "
         << "The error can be due to a lack of disk space.";
  }
  
  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Closing file " 
                      << streamRecord->finalFileName() << ".\n";
  out.close();
  
  const std::string& tmpFileName = streamRecord->tmpFileName();
  const std::string& finalFileName = streamRecord->finalFileName();

  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Renaming " << tmpFileName
                      << " to "    << finalFileName << ".\n";

  if(0!=rename(tmpFileName.c_str(), finalFileName.c_str())){
    cout << "[LaserSorter " << now() << "] "
         << " Failed to rename output file from "
         << tmpFileName << " to "    << finalFileName
         << ". " << strerror(errno) << "\n";
  }

  if(doOutputList_){
    char buf[256];
    time_t t = time(0);
    strftime(buf, sizeof(buf), "%F %R:%S", localtime(&t));

    ifstream f(".watcherfile");
    string inputFile;
    f >> inputFile;
    outputList_ << finalFileName << "\t" << buf
                << "\t" << inputFile
                << endl;
  }
  
  return outStreamList_.erase(streamRecord);
}
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 559 of file LaserSorter.cc.

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

Referenced by getStream().

                                                                  {
  if(verbosity_) cout << "[LaserSorter " << now() << "] " << "Creating a stream for FED " << fedId
                      << " lumi block " << lumiBlock << ".\n";
  std::string tmpName;
  std::string finalName;

  streamFileName(fedId, lumiBlock, tmpName, finalName);

  errno = 0;

  //checks if a file with tmpName name already exists:
  ofstream* out = new ofstream(tmpName.c_str(), ios::out | ios::in);  
  if(out->is_open()){//temporary file already exists. Making a backup:
    string newName;
    if(!renameAsBackup(tmpName, newName)){
      throw cms::Exception("LaserSorter")
        << "Failed to rename file " << tmpName
        << "  to " << newName << "\n";
    }
    if(verbosity_) cout << "[LaserSorter " << now() << "] "
                        << "Already existing File " << tmpName
                        << " renamed to "
                        << newName << "\n";
    out->close();
  }

  out->clear();
  out->open(tmpName.c_str(), ios::out | ios::trunc);
  
  if(out->fail()){//failed to create file
    delete out;
    throw cms::Exception("LaserSorter")
      << "Failed to create file " 
      << tmpName << " for writing event from FED " << fedId
      << " lumi block " << lumiBlock
      << ": "  << strerror(errno) << "\n.";
  }
  
  ifstream in(finalName.c_str());
  bool newFile = true;
  if(in.good()){//file already exists with final name.
    if(verbosity_) cout << "[LaserSorter " << now() << "] " << "File "
                        << finalName
                        << " already exists. It will be updated if needed.\n";
    //Copying its contents:
    char buffer[256];
    streamsize nread = -1;
    int vers = readFormatVersion(in, finalName);
    if(vers==-1){
      edm::LogWarning("LaserSorter") << "File " << tmpName.c_str()
                                     << " is not an LMF file despite its extension or "
                                     << "it is corrupted.\n";
    } else if(vers!=formatVersion_){
      edm::LogWarning("LaserSorter") << "Cannot include events already in file "
                                     << tmpName.c_str()
                                     << " because of version "
                                     << "mismatch (found version "
                                     << (int)vers << " while "
                                     << "only version "
                                     << (int)formatVersion_
                                     << " is supported).\n";
    } else{
      newFile = false;
      //read index table offset value:
      const int indexTableOffsetPos8 = 1*sizeof(uint32_t);
      uint32_t indexTableOffsetValue = 0;
      in.clear();
      in.seekg(indexTableOffsetPos8, ios::beg);
      in.read((char*) &indexTableOffsetValue,
              sizeof(indexTableOffsetValue));
      if(in.fail()){
        cout << "[LaserSorter " << now() << "] " << "Failed to read offset of index table "
          " in the existing file " << finalName << "\n";
      } else{
        if(verbosity_>2) cout << "[LaserSorter " << now() << "] " << "Index table offset of "
                           "original file " << finalName << ": 0x"
                              << hex << setfill('0')
                              << setw(8) << indexTableOffsetValue
                              << dec << setfill(' ') << "\n";
      }
      in.clear();
      in.seekg(0, ios::beg);

      //copy legacy file contents except the index table
      uint32_t toRead = indexTableOffsetValue;
      cout << "[LaserSorter " << now() << "] " << "Copying " << finalName << " to " << tmpName << endl;
      while(!in.eof()
            && (toRead > 0)
            && (nread=in.readsome(buffer, min(toRead, (uint32_t)sizeof(buffer))))!=0){
        //         cout << "Writing " << nread << " bytes to file "
        //              << tmpName.c_str() << "\n";
        toRead -= nread;
        // out->seekp(0, ios::end);
        out->write(buffer, nread);
        if(out->bad()){
          throw cms::Exception("LaserSorter") << "Error while writing to file "
                                              << tmpName
                                              << ". Check if there is enough "
                                              << "space on the device.\n";
        }
      }

      //resets index table offset field:
      indexTableOffsetValue = 0 ;
      out->clear();
      out->seekp(indexTableOffsetPos8, ios::beg);
      out->write((char*)&indexTableOffsetValue, sizeof(uint32_t));
      out->clear();
      out->seekp(0, ios::end);
    }
  }

#if 0
  out->flush();
  cout << "Press enter... file name was " << tmpName << endl;
  char c;
  cin >> c;
#endif
  
  OutStreamRecord* outRcd = new OutStreamRecord(fedId, lumiBlock,
                                                out,
                                                tmpName, finalName);
  
  if(newFile){
    writeFileHeader(*out);
  } else{
    std::string errMsg;
    if(!readIndexTable(in, finalName, *outRcd, &errMsg)){
      throw cms::Exception("LaserSorter") << errMsg << "\n";
    }
  }

  return outStreamList_.insert(outStreamList_.end(), outRcd);
}
int LaserSorter::dcc2Lme ( int  dccNum,
int  dccSide 
) [private]

Definition at line 337 of file LaserSorter.cc.

References min.

                                         {
  int fedid = (dcc%600) + 600; //to handle both FED and DCC id.
  vector<int> lmes;
  // EE -
  if( fedid <= 609 ) {
    if ( fedid <= 607 ) {
      lmes.push_back(fedid-601+83);
    } else if ( fedid == 608 ) {
      lmes.push_back(90);
      lmes.push_back(91);
    } else if ( fedid == 609 ) {
      lmes.push_back(92);
    }
  } //EB
  else if ( fedid >= 610  && fedid <= 645 ) {
    lmes.push_back(2*(fedid-610)+1);
    lmes.push_back(lmes[0]+1);
  } // EE+ 
  else if ( fedid >= 646 ) {
    if ( fedid <= 652 ) {
      lmes.push_back(fedid-646+73);
    } else if ( fedid == 653 ) {
      lmes.push_back(80);
      lmes.push_back(81);
    } else if ( fedid == 654 ) {
      lmes.push_back(82);
    }
  }
  return lmes.size()==0?-1:lmes[min(lmes.size(), (size_t)side)];
}
void LaserSorter::endJob ( void  ) [virtual]

Reimplemented from edm::EDAnalyzer.

Definition at line 851 of file LaserSorter.cc.

References closeAllStreams().

                        {
  //TODO: better treatement of last files:
  //they might be imcomplete...
  closeAllStreams();
}
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 368 of file LaserSorter.cc.

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

Referenced by analyze().

                                                      {  
  Majority<int> stat;
  bool ecalData = false;
  for(int id=ecalDccFedIdMin_; id<=ecalDccFedIdMax_; ++id){
    if(!FEDNumbering::inRange(id)) continue;
    const FEDRawData& data = rawdata->FEDData(id);
    const int detailedTrigger32 = 5;
    if(verbosity_>3) cout << "[LaserSorter " << now() << "] " 
                          << "FED " << id << " data size: "  
                          << data.size() << "\n"; 
    if(data.size()>=4*(detailedTrigger32+1)){
      ecalData = true;
      const uint32_t* pData32 = (const uint32_t*) data.data();
      int tType = pData32[detailedTrigger32] & 0xFFF;
      if(verbosity_>3) cout << "[LaserSorter " << now() << "] "
                            << "Trigger type " << tType << "\n";
      stat.add(tType);
    }
  }
  if(!ecalData) return -2;
  double p;
  int tType = stat.result(&p);
  if(p<0){
    //throw cms::Exception("NotFound") << "No ECAL DCC data found\n";
    if(iNoEcalDataMess_ < maxNoEcalDataMess_){
      edm::LogWarning("NotFound")  << "No ECAL DCC data found. "
        "(This warning will be disabled for the current run after "
                                   << maxNoEcalDataMess_ << " occurences.)";
      ++iNoEcalDataMess_;
    }
    tType = -1;
  } else if(p<.8){
    //throw cms::Exception("EventCorruption") << "Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
    edm::LogWarning("EventCorruption") << "Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
    tType = -1;
  }
  if(proba) *proba = p;
  return tType;
}
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 932 of file LaserSorter.cc.

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

Referenced by analyze().

                                                                      {
  int nTowers;
  vector<int> result;
  for(int fed = ecalDccFedIdMin_; fed < ecalDccFedIdMax_; ++fed){
    const FEDRawData& fedData = data.FEDData(fed);
    isDccEventEmpty(fedData, 0, &nTowers);
    if(nTowers>=68) result.push_back(fed);
  }
  return result;
}
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 903 of file LaserSorter.cc.

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

Referenced by writeEvent().

                                                                     {
  fedIds.erase(fedIds.begin(), fedIds.end());
  for(int id=ecalDccFedIdMin_; id<=ecalDccFedIdMax_; ++id){
    size_t dccLen;
    const FEDRawData& dccEvent = data.FEDData(id);
    if(id==matacqFedId_
       || !isDccEventEmpty(dccEvent, &dccLen)){
      fedIds.push_back(id);
    }
    if(dccLen*sizeof(uint64_t)!=dccEvent.size()){
      edm::LogWarning("LaserSorter")
        << "Length error in data of FED " << id
        << " in event " << event.id() 
        << ", Data of this FED dropped.";
    }
  }
  //   cout << __FILE__ << ":" << __LINE__ << ": "
  //        <<  "data.FEDData(" << matacqFedId_ << ").size() = "
  //        <<  data.FEDData(matacqFedId_).size() << "\n";
  if(data.FEDData(matacqFedId_).size()>4){//matacq block present
    //    cout << __FILE__ << ":" << __LINE__ << ": "
    //     <<  "Adding matacq to list of FEDs\n";
    fedIds.push_back(matacqFedId_);
  }
}
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 441 of file LaserSorter.cc.

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

Referenced by analyze(), and restoreStreamsOfLumiBlock().

                                                            {

  if((fedId != -1) &&
     (fedId < ecalDccFedIdMin_ || fedId > ecalDccFedIdMax_)) fedId = -1;
  
  if(verbosity_>1) cout << "[LaserSorter " << now() << "] "
       << "Looking for an opened output file for FED " 
       << fedId << " LB " << lumiBlock
       << "\n";

  //first look if stream is already open:
  for(OutStreamList::iterator it = outStreamList_.begin();
      it != outStreamList_.end();
      ++it){
    if(it->fedId()==fedId && 
       (abs((int)it->startingLumiBlock()-(int)lumiBlock)<=lumiBlockSpan_)){
      //stream found!
      return &(*it);
    }
  }
  //stream was not found. Let's create one

  if(verbosity_) cout << "[LaserSorter " << now() << "] "
                      << "File not yet opened. Opening it.\n";

  OutStreamList::iterator streamRecord = createOutStream(fedId, lumiBlock);
  return streamRecord!=outStreamList_.end()?&(*streamRecord):0;
}
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 860 of file LaserSorter.cc.

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

Referenced by getFullyReadoutDccs(), and getOutputFedList().

                                                          {
  if(nTowerBlocks) *nTowerBlocks = 0;
  //DCC event is considered empty if it does not contains any Tower block
  //( = FE data)
  bool rc = true;
  if(dccLen) *dccLen = 0;
  const unsigned nWord32 = data.size()/sizeof(uint32_t);
  if(nWord32==0){
    //cout << "[LaserSorter " << now() << "] " << "FED block completly empty\n";
    return true;
  }
  for(unsigned iWord32 = 0; iWord32 < nWord32; iWord32 += 2){
    uint32_t* data32 = ((uint32_t*)(data.data())) + iWord32;
    int dataType = (data32[1] >>28) & 0xF;
    //     cout << hex << "0x" << setfill('0')
    //          << setw(8) << data32[1] << "'" << setw(8) << data32[0]
    //          << " dataType: 0x" << dataType 
    //          << dec << setfill(' ') << "\n";
    if(0==(dataType>>2)){//in DCC header
      const int dccHeaderId = (data32[1] >>24) & 0x3F;
      if(dccHeaderId==1){
        if(dccLen) *dccLen=((data32[0] >>0 ) & 0xFFFFFF);
      }
    } if((dataType>>2)==3){//Tower block
      rc = false;
      if(nTowerBlocks){//number of tower block must be counted
        ++(*nTowerBlocks);
      } else{
        break;
      }
    }
  }
  //   cout << "[LaserSorter " << now() << "] " << "DCC Len: ";

  //   if(dccLen){
  //     cout << (*dccLen) << " event ";
  //   }
  //   cout << (rc?"":"non") << " empty"
  //        << endl;
  return rc;
}
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 1058 of file LaserSorter.cc.

References data, and AlCaHLTBitMon_ParallelJobs::p.

Referenced by createOutStream().

                                                             {
  int vers = -1;
  streampos p = in.tellg();

  uint32_t data;

  in.read((char*)&data, sizeof(data));
  
  char magic[4];
  
  magic[0] = data & 0xFF;
  magic[1] = (data >>8) & 0xFF;
  magic[2] = (data >>16) & 0xFF;
  magic[3] = 0;


  const string lmf = string("LMF");
  
  if(in.good() && lmf.compare(magic)==0){
    vers = (data >>24) & 0xFF;
  }

  if(lmf.compare(magic)!=0){
    edm::LogWarning("LaserSorter") << "File " << fileName
                                   << "is not an LMF file.\n";
  }
  
  in.clear();
  in.seekg(p);
  return vers;
}
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 984 of file LaserSorter.cc.

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

Referenced by createOutStream().

                                                {
  stringstream errMsg;

  ifstream* s = &in;
  
  //streampos pos = s->tellg();
  s->clear();
  s->seekg(0);
  
  uint32_t fileHeader[2];
  s->read((char*)&fileHeader[0], sizeof(fileHeader));
  uint32_t indexTablePos = fileHeader[1]; 
  
  if(s->eof()){
    s->clear();
    s->seekg(0);
    errMsg << "Failed to read header of file " << inName
           << ".";
    if(err) *err = errMsg.str();
    return false;
  }
  
  s->seekg(indexTablePos);

  uint32_t nevts = 0;
  s->read((char*)&nevts, sizeof(nevts));
  s->ignore(4);  
  if(s->bad()){
    errMsg << "Failed to read index table from file "
           << inName << ".";
    if(err) *err = errMsg.str();
    return false;
  }
  if(nevts>maxEvents_){
    errMsg << "Number of events indicated in event index of file "
           << inName << " (" << nevts << ") "
           << "is unexpectively large.";
    if(err) *err = errMsg.str();
    return false;
  }
  outRcd.indices()->resize(nevts);
  s->read((char*)&(*outRcd.indices())[0], nevts*sizeof(IndexRecord));
  if(s->bad()){
    outRcd.indices()->clear();
    errMsg << "Failed to read index table from file "
           << inName << ".";
    if(err) *err = errMsg.str();
    return false;
  }
  if(nevts>maxEvents_){
    errMsg << "Number of events indicated in event index of file "
           << inName << " is unexpectively large.";
    if(err) *err = errMsg.str();
    outRcd.indices()->clear();
    return false;
  }

  if(verbosity_ > 1) cout << "[LaserSorter " << now() << "] " << "Orbit IDs of events "
                          << "already contained in the file "
                          << inName << ":";
  for(unsigned i = 0; i < outRcd.indices()->size(); ++i){
    if(verbosity_>1){
      cout << " "  << setw(9) << (*outRcd.indices())[i].orbit;
    }
    outRcd.excludedOrbit().insert((*outRcd.indices())[i].orbit);
  }
  if(verbosity_>1) cout << "\n";

  return true;
}
bool LaserSorter::renameAsBackup ( const std::string &  fileName,
std::string &  newFileName 
) [private]

Definition at line 538 of file LaserSorter.cc.

References i, and link().

Referenced by createOutStream().

                                                        {
  int i = 0;
  int err;
  static int maxTries = 100;
  stringstream newFileName_;
  do{
    newFileName_.str("");
    newFileName_ << fileName << "~";
    if(i>0) newFileName_ << i;
    err = link(fileName.c_str(), newFileName_.str().c_str());
    if(err==0){
      newFileName = newFileName_.str();
      err = unlink(fileName.c_str());
    }
    ++i;
  } while((err!=0) && (errno == EEXIST) && (i < maxTries));
  return err==0;
}
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 1106 of file LaserSorter.cc.

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

Referenced by analyze().

                                                        {
  string dummy;
  string fileName;
  
  for(int fedId = ecalDccFedIdMin_-2; fedId <= ecalDccFedIdMax_; ++fedId){
    int fedId_;
    if(fedId == ecalDccFedIdMin_-2) fedId_ = -1; //stream for event w/o ECAL data
    else fedId_ = fedId;
    streamFileName(fedId_, lumiBlock, dummy, fileName);
    struct stat s;
    //TODO: could be optimized by adding an option to get stream
    //to open only existing file: would avoid double call to streamFileName.
    if(stat(fileName.c_str(), &s)==0){//file exists
      getStream(fedId_, lumiBlock);
    }
  }
}
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 749 of file LaserSorter.cc.

References gather_cfg::cout, dir, ecalDccFedIdMax_, ecalDccFedIdMin_, Exception, fedSubDirs_, convertXMLtoSQLite_cfg::fileName, now(), outputDir_, runNumber_, and verbosity_.

Referenced by createOutStream(), and restoreStreamsOfLumiBlock().

                                                                          {
  int iFed;
  if(fedId >= ecalDccFedIdMin_ && fedId <= ecalDccFedIdMax_){
    iFed = fedId - ecalDccFedIdMin_ + 1;
  } else if(fedId < 0){
    iFed = -1; //event w/o ECAL data
  } else {
    iFed = 0;
  }
  if(iFed < -1 || iFed >= (int)fedSubDirs_.size()){
    throw cms::Exception("LaserSorter")
      << "Bug found at " << __FILE__ << ":" << __LINE__ 
      << ". FED ID is out of index!";
  }

  
  struct stat fileStat;

  stringstream buf;
  buf << outputDir_ << "/"
      << (iFed<0 ? "Empty" : fedSubDirs_[iFed]);

  string dir = buf.str();
  if(0==stat(dir.c_str(), &fileStat)){
    if(!S_ISDIR(fileStat.st_mode)){
      throw cms::Exception("[LaserSorter]")
        << "File " << dir << " exists but is not a directory "
        << " as expected.";
    }
  } else {//directory does not exists, let's try to create it
    if(0!=mkdir(dir.c_str(), 0755)){
      throw cms::Exception("[LaserSorter]")
        << "Failed to create directory " << dir
        << " for writing data.";
    }
  }

  buf.str("");
  buf << "Run" << runNumber_
      << "_LB" << setfill('0') << setw(4) << lumiBlock
      << ".lmf";
  string fileName = buf.str();
  string tmpFileName = fileName + ".part";

  finalName = dir + "/" + fileName;
  tmpName = dir + "/" + tmpFileName;

  if(verbosity_>3) cout << "[LaserSorter " << now() << "] " << "File path: "
                        << finalName << "\n";
}
std::string LaserSorter::toString ( uint64_t  t) [static, private]

Help function to format a date

Definition at line 1091 of file LaserSorter.cc.

Referenced by writeEventHeader().

                                         {
  char buf[256];
  
  time_t tsec = t>>32;
  
  uint32_t tusec = t & 0xFFFFFFFF;
  strftime(buf, sizeof(buf), "%F %R %S s", localtime(&tsec));
  buf[sizeof(buf)-1] = 0;
  
  stringstream buf2;
  buf2 << (tusec+500)/1000;

  return string(buf) + " " + buf2.str() + " ms";
}
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 471 of file LaserSorter.cc.

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

Referenced by analyze().

                                                              {

  ofstream& out = *outRcd.out();
  bool rc = true;
  vector<unsigned> fedIds;
  getOutputFedList(event, data, fedIds);
  
  out.clear();
  uint32_t evtStart = out.tellp();
  if(out.bad()) evtStart = 0;
  //  cout << "------> fedIds.size() = " << fedIds.size() << endl;
  rc &= writeEventHeader(out, event, dtt, fedIds.size());
  
  for(unsigned iFed = 0; iFed < fedIds.size() && rc; ++iFed){
//     cout << "------> data.FEDData(" << fedIds[iFed] << ").size = "
//          << data.FEDData(fedIds[iFed]).size() << "\n";
    rc  &= writeFedBlock(out, data.FEDData(fedIds[iFed]));
  }

  if(rc){
    //update index table for this file:
    vector<IndexRecord>& indices = *outRcd.indices();
    IndexRecord indexRcd = {event.orbitNumber(), evtStart};
    indices.push_back(indexRcd);
  }
  return rc;
}
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 712 of file LaserSorter.cc.

References edm::EventBase::bunchCrossing(), colorNames, gather_cfg::cout, 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().

                                                  {
  uint32_t data[10];
  
  data[0] = evt.time().value() & 0xFFFFFFFF;
  data[1] = evt.time().value() >>32;
  data[2] = evt.luminosityBlock();
  data[3] = evt.run();     
  data[4] = evt.orbitNumber();
  data[5] = evt.bunchCrossing();
  data[6] = evt.id().event();
  data[7] = dtt;
  data[8] = nFeds;
  data[9] = 0; //reserved (to be aligned on 64-bits)

  if(verbosity_>1){
    cout << "[LaserSorter " << now() << "] " << "Write header of event: "
         << "Time: " << toString(evt.time().value())
         << ", LB: " << evt.luminosityBlock()
         << ", Run: " << evt.run()
         << ", Bx: " << evt.bunchCrossing()
         << ", Event ID: " << evt.id().event()
         << ", Detailed trigger type: 0x" << hex << dtt << dec
         << " (" << detailedTrigNames[(dtt>>8)&0x7]  << ", "
         << colorNames[(dtt>>6)&0x3] << ", DCC " << (dtt&0x3f)
         << ", side " << ((dtt>>10) &0x1) << ")"
         << ", number of FEDs: " << nFeds
         << "\n";
  }
    
  out.clear();
  out.write((char*)data, sizeof(data));
  return !out.bad();
}
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 501 of file LaserSorter.cc.

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

Referenced by writeEvent().

                                                       {
  bool rc = false;
  if (data.size()>4){
    const uint32_t * pData
      = reinterpret_cast<uint32_t*>(const_cast<unsigned char*> ( data.data()));
    
    uint32_t dccLen64 = pData[2] & 0x00FFFFFF; //in 32-byte unit

    if(data.size() != dccLen64*sizeof(uint64_t)){
//       throw cms::Exception("Bug") << "Bug found in "
//                                   << __FILE__ << ":" << __LINE__ << ".";
      throw cms::Exception("LaserSorter")
        << "Mismatch between FED fragment size indicated in header "
        << "(" << dccLen64 << "*8 Byte) "
        << "and actual size (" <<  data.size() << " Byte) "
        << "for FED ID " <<  ((pData[0] >>8) & 0xFFF) << "!\n";
    }
    
    if(verbosity_>3) cout << "[LaserSorter " << now() << "] " << "Event fragment size: "
                          << data.size() << " Byte"
                          << "\t From Dcc header: " << dccLen64*8 << " Byte\n";
    
    const size_t nBytes = data.size();
    //       cout << "[LaserSorter " << now() << "] " 
    //            << "Writing " << nBytes << " byte from adress " 
    //            << (void*) data.data() << " to file.\n";
    if(out.fail()) cout << "[LaserSorter " << now() << "] " << "Problem with stream!\n";
    out.write((char*)data.data(), nBytes);
    rc = true;
  } else{
    throw cms::Exception("Bug") << "Bug found in "
                                << __FILE__ << ":" << __LINE__ << ".\n";
  }
  return rc;
}
void LaserSorter::writeFileHeader ( std::ofstream &  out) [private]

Writing file header for an LMF binary file

Parameters:
outstream of the output file

Definition at line 695 of file LaserSorter.cc.

References Exception, formatVersion_, and zero.

Referenced by createOutStream().

                                                 {
  out.clear();
  
  uint32_t id = 'L' | ('M' <<8) | ('F' <<16) | (formatVersion_<<24);

  out.write((char*)&id, sizeof(uint32_t));

  //index position (to be filled at end of writing)
  uint32_t zero = 0;
  out.write((char*)&zero, sizeof(uint32_t));

  if(out.fail()){
    throw cms::Exception("LaserSorter")
      << "Failed to write file header.\n";
  }
}
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 944 of file LaserSorter.cc.

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

Referenced by closeOutStream().

                                                                  {
  uint32_t indexTablePos = out.tellp();
  uint32_t nevts = indices.size();
  
  out.clear();
  out.write((char*)&nevts, sizeof(nevts));
  const uint32_t reserved = 0;
  out.write((char*)&reserved, sizeof(reserved));
  
  if(out.bad()) return false;

  sort(indices.begin(), indices.end());

  for(unsigned i = 0; i < indices.size(); ++i){
    uint32_t data[2];
    data[0] = indices[i].orbit;
    data[1] = indices[i].filePos;
    out.write((char*)data, sizeof(data));
  }

  if(out.bad()) return false; //intial 0 valur for index table position
  //                            is left to indicate corrupted table.
  
  //writes index table position:x
  out.clear();
  out.seekp(indexOffset32_*sizeof(uint32_t));
  //   cout << "[LaserSorter] Index table position: 0x" << hex << indexTablePos
  //        << dec << "\n"; 
  if(!out.bad()) out.write((char*)&indexTablePos, sizeof(uint32_t));

  bool rc = !out.bad();
  
  //reposition pointer to eof:
  out.seekp(0, ios::end);

  return rc;
}

Member Data Documentation

Trigger type of calibration event. -1 if unkown.

Definition at line 313 of file LaserSorter.h.

Referenced by analyze().

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

Definition at line 346 of file LaserSorter.h.

Referenced by analyze().

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 [static, private]
const int LaserSorter::ecalDccFedIdMin_ = 601 [static, private]

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 [static, private]

Definition at line 436 of file LaserSorter.h.

Referenced by writeIndexTable().

number of "ECAL DCC data" message in a run

Definition at line 411 of file LaserSorter.h.

Referenced by analyze(), and getDetailedTriggerType().

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().

Luminosity block of event under processing

Definition at line 321 of file LaserSorter.h.

Referenced by analyze().

Tolerance on lumi block spanning of a FED sequence. Subsequent events of a same FED must span at most on 2*lumiBlockSpan_+1 luminosity blocks.

  • It is important that the laser sequence scane does not pass twice on the same FED within the 2*lumiBlockSpan_+1. Failing this requirement will result mixing event of different passes in the same output file.
  • The number of input files opened simultinuously is proportional to 2*lumiBlockSpan_+1. So increasing lumiBlockSpan_ will also increase the number of opened files and may have some impact of sorting time performances.

Definition at line 428 of file LaserSorter.h.

Referenced by analyze(), closeOldStreams(), and getStream().

const int LaserSorter::matacqFedId_ = 655 [static, private]

FED ID associated to Matacq data

Definition at line 432 of file LaserSorter.h.

Referenced by getOutputFedList().

const unsigned LaserSorter::maxEvents_ = 1<<20 [static, private]

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

Definition at line 441 of file LaserSorter.h.

Referenced by readIndexTable().

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

Definition at line 406 of file LaserSorter.h.

Referenced by analyze().

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(), 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().

List of output stream to write sorted data

Definition at line 326 of file LaserSorter.h.

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

Run number of event under process

Definition at line 350 of file LaserSorter.h.

Referenced by analyze(), and streamFileName().

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

LaserSorter::stats_t LaserSorter::stats_init = {0, 0, 0, 0, 0} [static, private]

Definition at line 459 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]