CMS 3D CMS Logo

Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes

MatacqProducer Class Reference

#include <MatacqProducer.h>

Inheritance diagram for MatacqProducer:
edm::EDProducer edm::ProducerBase edm::EDConsumerBase edm::ProductRegistryHelper

List of all members.

Classes

struct  MatacqEventId
class  PosEstimator
struct  stats_t

Public Types

enum  calibTrigType_t { laserType = 4, ledType = 5, tpType = 6, pedType = 7 }

Public Member Functions

 MatacqProducer (const edm::ParameterSet &params)
virtual void produce (edm::Event &event, const edm::EventSetup &eventSetup)
 ~MatacqProducer ()

Private Types

typedef FILE * FILE_t
typedef off_t filepos_t

Private Member Functions

void addMatacqData (edm::Event &event)
int getCalibTriggerType (edm::Event &ev) const
bool getMatacqEvent (uint32_t runNumber, int32_t orbitId, bool fileChange)
bool getMatacqFile (uint32_t runNumber, uint32_t orbitId, bool *fileChange=0)
uint32_t getOrbitId (edm::Event &ev) const
uint32_t getRunNumber (edm::Event &ev) const
void loadOrbitOffset ()
bool mcheck (const std::string &name)
void mclose ()
bool meof ()
bool misOpened ()
bool mopen (const std::string &name)
bool mread (char *buf, size_t n, const char *mess=0, bool peek=false)
bool mrewind ()
bool mseek (filepos_t offset, int whence=SEEK_SET, const char *mess=0)
bool msize (filepos_t &s)
bool mtell (filepos_t &pos)
void newRun (int prevRun, int newRun)

Static Private Member Functions

static std::string runSubDir (uint32_t runNumber)

Private Attributes

std::vector< unsigned char > data_
std::string digiInstanceName_
bool disabled_
bool doOrbitOffset_
int eventSkipCounter_
int fastRetrievalThresh_
std::vector< std::string > fileNames_
MatacqDataFormatter formatter_
bool ignoreTriggerType_
FILE_t inFile_
std::string inFileName_
edm::InputTag inputRawCollection_
int32_t lastOrb_
std::ofstream logFile_
std::string logFileName_
bool logTiming_
MatacqRawEvent matacq_
bool mergeRaw_
int onErrorDisablingEvtCnt_
uint32_t openedFileRunNumber_
std::map< uint32_t, uint32_t > orbitOffset_
std::string orbitOffsetFile_
PosEstimator posEstim_
bool produceDigis_
bool produceRaw_
std::string rawInstanceName_
uint32_t runNumber_
timeval startTime_
struct MatacqProducer::stats_t stats_
std::ofstream timeLog_
std::string timeLogFile_
timeval timer_
bool timing_
int verbosity_

Static Private Attributes

static const int bufferSize = 30000
static const int matacqFedId_ = 655
static int orbitTolerance_ = 80
static stats_t stats_init = {0,0,0}

Detailed Description

Definition at line 43 of file MatacqProducer.h.


Member Typedef Documentation

typedef FILE* MatacqProducer::FILE_t [private]

Definition at line 58 of file MatacqProducer.h.

typedef off_t MatacqProducer::filepos_t [private]

Definition at line 57 of file MatacqProducer.h.


Member Enumeration Documentation

Enumerator:
laserType 
ledType 
tpType 
pedType 

Definition at line 46 of file MatacqProducer.h.

                      {
    laserType = 4,
    ledType   = 5,
    tpType    = 6,
    pedType   = 7
  };

Constructor & Destructor Documentation

MatacqProducer::MatacqProducer ( const edm::ParameterSet params) [explicit]

Constructor

Parameters:
paramsseletive readout parameters

Definition at line 65 of file MatacqProducer.cc.

References gather_cfg::cout, digiInstanceName_, doOrbitOffset_, Exception, loadOrbitOffset(), logFile_, logFileName_, logTiming_, now(), orbitOffsetFile_, dbtoconf::out, posEstim_, produceDigis_, produceRaw_, rawInstanceName_, startTime_, timeLog_, timeLogFile_, timer_, MatacqProducer::PosEstimator::verbosity(), and verbosity_.

                                                           :
  fileNames_(params.getParameter<std::vector<std::string> >("fileNames")),
  digiInstanceName_(params.getParameter<string>("digiInstanceName")),
  rawInstanceName_(params.getParameter<string>("rawInstanceName")),
  timing_(params.getUntrackedParameter<bool>("timing", false)),
  disabled_(params.getParameter<bool>("disabled")),
  verbosity_(params.getUntrackedParameter<int>("verbosity", 0)),
  produceDigis_(params.getParameter<bool>("produceDigis")),
  produceRaw_(params.getParameter<bool>("produceRaw")),
  inputRawCollection_(params.getParameter<InputTag>("inputRawCollection")),
  mergeRaw_(params.getParameter<bool>("mergeRaw")),
  ignoreTriggerType_(params.getParameter<bool>("ignoreTriggerType")),
  matacq_(0, 0),
  inFile_(0),
  data_(bufferSize),
  openedFileRunNumber_(0),
  lastOrb_(0),
  fastRetrievalThresh_(0),
  orbitOffsetFile_(params.getUntrackedParameter<std::string>("orbitOffsetFile",
                                                             "")),
  inFileName_(""),
  stats_(stats_init),
  logFileName_(params.getUntrackedParameter<std::string>("logFileName",
                                                         "matacqProducer.log")),
  eventSkipCounter_(0),
  onErrorDisablingEvtCnt_(params.getParameter<int>("onErrorDisablingEvtCnt")),
  timeLogFile_(params.getUntrackedParameter<std::string>("timeLogFile", "")),
  runNumber_(0)
{
  if(verbosity_>=4) cout << "[Matacq " << now() << "] in MatacqProducer ctor"  << endl;
  
  gettimeofday(&timer_, 0);

  if(timeLogFile_.size()>0){
    timeLog_.open(timeLogFile_.c_str());
    if(timeLog_.fail()){
      cout << "[LaserSorter " << now() << "] "
           << "Failed to open file " << timeLogFile_ << " to log timing.\n";
      logTiming_ = false;
    } else{
      logTiming_ = true;
    }
  }
  
  posEstim_.verbosity(verbosity_);

  logFile_.open(logFileName_.c_str(), ios::app | ios::out);
  
  if(logFile_.bad()){
    throw cms::Exception("FileOpen") << "Failed to open file "
                                     << logFileName_ << " for logging.\n";
  }

  if(produceDigis_){
    if(verbosity_>0) cout << "[Matacq " << now() << "] registering new "
                       "EcalMatacqDigiCollection product with instance name '"
                          << digiInstanceName_ << "'\n";
    produces<EcalMatacqDigiCollection>(digiInstanceName_);
  }
  
  if(produceRaw_){
    if(verbosity_>0) cout << "[Matacq " << now() << "] registering new FEDRawDataCollection "
                       "product with instance name '"
                          << rawInstanceName_ << "'\n";
    produces<FEDRawDataCollection>(rawInstanceName_);
  }
  
  startTime_.tv_sec = startTime_.tv_usec = 0;
  if(orbitOffsetFile_.size()>0){
    doOrbitOffset_ = true;
    loadOrbitOffset();
  } else{
    doOrbitOffset_ = false;
  }
  if(verbosity_>=4) cout << "[Matacq " << now() << "] exiting MatacqProducer ctor"  << endl;
}
MatacqProducer::~MatacqProducer ( )

Destructor

Definition at line 764 of file MatacqProducer.cc.

References gather_cfg::cout, logTiming_, mclose(), now(), startTime_, and lumiQTWidget::t.

                               {
  mclose();
  timeval t;
  gettimeofday(&t, 0);
  if(logTiming_ && startTime_.tv_sec!=0){
    //not using logger, to allow timing with different logging options
    cout << "[Matacq " << now() << "] Time elapsed between first event and "
      "destruction of MatacqProducer: "
         << ((t.tv_sec-startTime_.tv_sec)*1.
             + (t.tv_usec-startTime_.tv_usec)*1.e-6) << "s\n";
  }
}

Member Function Documentation

void MatacqProducer::addMatacqData ( edm::Event event) [private]

Add matacq digi to the event

Parameters:
eventthe event
digiInstanceName_name to give to the matacq digi instance

Definition at line 176 of file MatacqProducer.cc.

References bufferSize, filterCSVwithJSON::copy, gather_cfg::cout, data_, digiInstanceName_, doOrbitOffset_, eventSkipCounter_, formatter_, getCalibTriggerType(), MatacqRawEvent::getDccLen(), getMatacqEvent(), getMatacqFile(), MatacqRawEvent::getOrbitId(), getOrbitId(), MatacqRawEvent::getParsedLen(), getRunNumber(), ignoreTriggerType_, inputRawCollection_, MatacqDataFormatter::interpretRawData(), laserType, matacq_, matacqFedId_, mergeRaw_, MatacqProducer::stats_t::nLaserEventsWithMatacq, MatacqProducer::stats_t::nNonLaserEventsWithMatacq, now(), orbitOffset_, produceDigis_, produceRaw_, rawInstanceName_, convertSQLiteXML::runNumber, stats_, and verbosity_.

Referenced by produce().

                                            {

  edm::Handle<FEDRawDataCollection> sourceColl;
  event.getByLabel(inputRawCollection_, sourceColl);

  std::auto_ptr<FEDRawDataCollection> rawColl;
  if(produceRaw_){
    if(mergeRaw_){
      rawColl = auto_ptr<FEDRawDataCollection>(new FEDRawDataCollection(*sourceColl));
    } else{
      rawColl = auto_ptr<FEDRawDataCollection>(new FEDRawDataCollection());
    }
  }
  
  std::auto_ptr<EcalMatacqDigiCollection>
    digiColl(new EcalMatacqDigiCollection());
  
  if(eventSkipCounter_==0){
    if(sourceColl->FEDData(matacqFedId_).size()>4 && !produceRaw_){
      //input raw data collection already contains matacqData
      formatter_.interpretRawData(sourceColl->FEDData(matacqFedId_),
                                  *digiColl);             
    } else{
      bool isLaserEvent = (getCalibTriggerType(event) == laserType);


      //      cout << "---> " << (ignoreTriggerType_?"yes":"no") << " " << getCalibTriggerType(event) << endl;
      
      if(isLaserEvent || ignoreTriggerType_){
        
        const uint32_t runNumber = getRunNumber(event);
        const uint32_t orbitId   = getOrbitId(event);
      
        LogInfo("Matacq") << "Run " << runNumber << "\t Orbit " << orbitId << "\n";
      
        bool fileChange;
        if(doOrbitOffset_){
          map<uint32_t,uint32_t>::iterator it = orbitOffset_.find(runNumber);
          if(it == orbitOffset_.end()){
            LogWarning("Matacq") << "Orbit offset not found for run "
                                 << runNumber
                                 << ". No orbit correction will be applied.";
    }
        }    
      
        if(getMatacqFile(runNumber, orbitId, &fileChange)){
          //matacq file retrieval succeeded
          LogInfo("Matacq") << "Matacq data file found for "
                            << "run " << runNumber << " orbit " << orbitId;
          if(getMatacqEvent(runNumber, orbitId, fileChange)){
            if(produceDigis_){
              formatter_.interpretRawData(matacq_, *digiColl);
            }
            if(produceRaw_){
              uint32_t dataLen64 = matacq_.getParsedLen();
              if(dataLen64 > bufferSize*8 || matacq_.getDccLen()!= dataLen64){
                LogWarning("Matacq") << " Error in Matacq event fragment length! "
                                     << "DCC len: " << matacq_.getDccLen()
                                     << "*8 Bytes, Parsed len: "
                                     << matacq_.getParsedLen() << "*8 Bytes.  "
                                     << "Matacq data will not be included for this event.\n";
              } else{
                rawColl->FEDData(matacqFedId_).resize(dataLen64*8);
                copy(data_.begin(), data_.begin() + dataLen64*8,
                     rawColl->FEDData(matacqFedId_).data());
              }
            }
            LogInfo("Matacq") << "Associating matacq data with orbit id "
                              << matacq_.getOrbitId()
                              << " to dcc event with orbit id "
                              << orbitId << std::endl;
            if(isLaserEvent){
              ++stats_.nLaserEventsWithMatacq;
            } else{
              ++stats_.nNonLaserEventsWithMatacq;
            }
          } else{
            if(isLaserEvent){
              LogWarning("Matacq") << "No matacq data found for laser event "
                                   << "of run " << runNumber << " orbit "
                                   << orbitId;
            }
          }
        } else{
          LogWarning("Matacq") << "No matacq file found for event "
                               << event.id();
        }
      }
    }
    if(eventSkipCounter_>0){ //error occured for this events
      //                       and some events will be skipped following
      //                       to this error.
      LogInfo("Matacq") << " [" << now() << "] "
                        << eventSkipCounter_
                        << " next events will be skipped, following to an "
                        << "error on the last processed event, "
                        << "which is expected to be persistant.";
    }
  } else{
    --eventSkipCounter_;
  }
  
  if(produceRaw_){
    if(verbosity_>1) cout << "[Matacq " << now() << "] "
                          << "Adding FEDRawDataCollection collection "
                          << " to event.\n";
    event.put(rawColl, rawInstanceName_);
  }

  if(produceDigis_){
    if(verbosity_>1) cout << "[Matacq " << now() << "] "
                          << "Adding EcalMatacqDigiCollection collection "
                          << " to event.\n";
    event.put(digiColl, digiInstanceName_);
  }
}
int MatacqProducer::getCalibTriggerType ( edm::Event ev) const [private]

Definition at line 644 of file MatacqProducer.cc.

References Majority< T >::add(), FEDRawData::data(), data, Exception, edm::Event::getByLabel(), inputRawCollection_, FEDNumbering::inRange(), edm::HandleBase::isValid(), AlCaHLTBitMon_ParallelJobs::p, lumiPlot::rawdata, Majority< T >::result(), and FEDRawData::size().

Referenced by addMatacqData().

                                                         {  
  edm::Handle<FEDRawDataCollection> rawdata;
  ev.getByLabel(inputRawCollection_, rawdata);
  if(!(rawdata.isValid())){
    throw cms::Exception("NotFound")
      << "No FED raw data collection found. ECAL raw data are "
      "required to retrieve the trigger type";
  }
  
  Majority<int> stat;
  for(int id=601; id<=654; ++id){
    if(!FEDNumbering::inRange(id)) continue;
    const FEDRawData& data = rawdata->FEDData(id);
    const int detailedTrigger32 = 5;
    if(data.size()>=4*(detailedTrigger32+1)){
      const unsigned char* pTType = data.data() + detailedTrigger32*4;
      int tType = pTType[1] & 0x7;
      stat.add(tType);
    }
  }
  double p;
  int tType = stat.result(&p);
  if(p<0){
    //throw cms::Exception("NotFound") << "No ECAL DCC data found\n";
    LogWarning("NotFound")  << "No ECAL DCC data found\n";
    tType = -1;
  }
  if(p<.8){
    //throw cms::Exception("EventCorruption") << "Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
    LogWarning("EventCorruption") << "Inconsitency in detailed trigger type indicated in ECAL DCC data headers\n";
    tType = -1;
  }
  return tType;
}
bool MatacqProducer::getMatacqEvent ( uint32_t  runNumber,
int32_t  orbitId,
bool  fileChange 
) [private]

Definition at line 340 of file MatacqProducer.cc.

References abs, gather_cfg::cout, data_, MatacqProducer::PosEstimator::eventLength(), Exception, fastRetrievalThresh_, newFWLiteAna::found, MatacqRawEvent::getDccLen(), getOrbitId(), MatacqRawEvent::getRunNum(), inFileName_, MatacqProducer::PosEstimator::invalid(), lastOrb_, matacq_, mread(), mrewind(), mseek(), msize(), mtell(), now(), evf::evtn::offset(), orbitTolerance_, pos, MatacqProducer::PosEstimator::pos(), posEstim_, DTTTrigCorrFirst::run, and verbosity_.

Referenced by addMatacqData().

                                               {
  filepos_t startPos;
  if(!mtell(startPos)) return false;
  
  int32_t startOrb = -1;
  const size_t headerSize = 8*8;
  if(mread((char*)&data_[0], headerSize, "Reading matacq header", true)){
    startOrb = MatacqRawEvent::getOrbitId(&data_[0], headerSize);
    if(startOrb<0) startOrb = 0;
  } else{
    if(verbosity_>2){
      cout << "[Matacq " << now() << "] Failed to read matacq header. Moved to start of "
        " the file.\n";
    }
    mrewind();
    if(mread((char*)&data_[0], headerSize, "Reading matacq header", true)){
      startPos = 0;
      startOrb = MatacqRawEvent::getOrbitId(&data_[0], headerSize);
    } else{
      if(verbosity_>2) cout << "[Matacq " << now() << "] Looks like matacq file is empty"
                            << "\n";
      return false;
    }
  }
  
  if(verbosity_>2) cout << "[Matacq " << now() << "] Last read orbit: " << lastOrb_
                        << " looking for orbit " << orbitId
                        << ". Current file position: " << startPos
                        << " Orbit at current position: " << startOrb << "\n";
  
  //  f.clear();
  bool didCoarseMove = false;

  //FIXME: case where posEtim_.invalid() is false
  if(!posEstim_.invalid()
     && (abs(lastOrb_-orbitId) > fastRetrievalThresh_)){
    filepos_t pos = posEstim_.pos(orbitId);

    //    struct stat st;
    filepos_t fsize;
    //    if(0==stat(inFileName_.c_str(), &st)){
    if(msize(fsize)){
      //      const int64_t fsize = st.st_size;
      if(0!=posEstim_.eventLength() && pos > fsize){
        //estimated position is beyong end of file
        //-> move to beginning of last event:
        int64_t evtSize = posEstim_.eventLength()*sizeof(uint64_t);
        pos = ((int64_t)fsize/evtSize-1)*evtSize;
        if(verbosity_>2){
          cout << "[Matacq " << now() << "] Estimated position was beyond end of file. "
            "Changed to " << pos << "\n";
        }
      }
    } else{
      LogWarning("Matacq") << "Failed to access file " << inFileName_ << ".";
    }
    if(pos>=0){
      if(verbosity_>2) cout << "[Matacq " << now() << "] jumping to estimated position "
                            << pos << "\n";
      mseek(pos, SEEK_SET, "Jumping to estimated event position");
      if(mread((char*)&data_[0], headerSize, "Reading matacq header", true)){
        didCoarseMove = true;
      } else{
        //estimated position might have been beyond the end of the file,
        //try, with original position:
        didCoarseMove = false;
        if(!mread((char*)&data_[0], headerSize, "Reading event header", true)){
          return false;
        }
      }
    } else{
      if(verbosity_) cout << "[Matacq " << now() << "] Event orbit outside of orbit range "
                       "of matacq data file events\n";
      return false;
    }
  }
  
  int32_t orb = MatacqRawEvent::getOrbitId(&data_[0], headerSize);

  if(didCoarseMove){
    //autoadjustement of threshold for coarse move:
    if(abs(orb-orbitId) > fastRetrievalThresh_){
      if(verbosity_>2) cout << "[Matacq " << now() << "] Fast retrieval threshold increased from "
                            << fastRetrievalThresh_;
      fastRetrievalThresh_ = 2*abs(orb-orbitId);
      if(verbosity_>2) cout << " to " << fastRetrievalThresh_ << "\n";
    }

    //if coarse move did not improve situation, rolls back:
    if(startOrb > 0
       && (abs(orb-orbitId) > abs(startOrb-orbitId))){
      if(verbosity_>2) cout << "[Matacq " << now() << "] Estimation (-> orbit " << orb << ") "
                         "was worst than original position (-> orbit "
                            << startOrb
                            << "). Restoring position (" << startPos << ").\n";
      mseek(startPos, SEEK_SET);
      mread((char*)&data_[0], headerSize, "Reading event header", true);
      orb = MatacqRawEvent::getOrbitId(&data_[0], headerSize);
    }
  }
  
  bool searchBackward = (orb>orbitId)?true:false;
  //BEWARE: len must be signed, because we are using latter in the code (-len)
  //expression
  int len = (int)MatacqRawEvent::getDccLen(&data_[0], headerSize);

  if(len==0){
    cout << "[Matacq " << now() << "] read DCC length is null! Cancels matacq event search "
         << " and move matacq file pointer to beginning of the file. "
         << "(" << __FILE__ << ":" << __LINE__ << ")."
         << "\n";
    //rewind(f);
    mrewind();
    return false;
  }
  
  enum state_t { searching, found, failed } state = searching;
  
  while(state == searching){
    orb = MatacqRawEvent::getOrbitId(&data_[0], headerSize);
    len = (int)MatacqRawEvent::getDccLen(&data_[0], headerSize);
    uint32_t run = MatacqRawEvent::getRunNum(&data_[0], headerSize);
    if(verbosity_>3){
      filepos_t pos = -1;
      mtell(pos);
      cout << "[Matacq " << now() << "] Header read at file position "
           << pos
           << ":  orbit = " << orb
           << " len = " << len << "x8 Byte"
           << " run = " << run << "\n";
    }
    if((abs(orb-orbitId) < orbitTolerance_)
       && (runNumber==0 || runNumber==run)){
      state = found;
      lastOrb_ = orb;
      //reads the rest of the event:
      if((int)data_.size() < len*8){
        throw cms::Exception("Matacq") << "Buffer overflow";
      }
      if(verbosity_>2) cout << "[Matacq " << now() << "] Event found. Reading "
                         " matacq event." << "\n";
      if(!mread((char*)&data_[0], len*8, "Reading matacq event")){
        if(verbosity_>2) cout << "[Matacq " << now() << "] Failed to read matacq event."
                              << "\n";
        state = failed;
      }
      matacq_ = MatacqRawEvent((unsigned char*)&data_[0], len*8);
    } else {
      if((searchBackward && (orb < orbitId))
         || (!searchBackward && (orb > orbitId))){ //search ended
        lastOrb_ = orb;      
        state = failed;
        if(verbosity_>2) cout << "[Matacq " << now()
                            << "] No matacq data found for run " << run
                            << ", orbit ID " << orbitId << "." << "\n";
      } else{
        off_t offset = (searchBackward?-len:len)*8;
        lastOrb_ = orb; 
        if(verbosity_>3){
          cout << "[Matacq " << now() << "] In matacq file, moving "
               << abs(offset) << " byte " << (offset>0?"forward":"backward")
               << ".\n";
        }       

        if(mseek(offset, SEEK_CUR,
                 (searchBackward?"Moving to previous event":
                  "Moving to next event"))
           && mread((char*)&data_[0], headerSize, "Reading event header",
                    true)){
        } else{
          if(!searchBackward) mseek(-len*8, SEEK_CUR,
                                    "Moving to start of last complete event");
          state = failed;
        }
      }
    }
  }
  
  if(state==found){
    filepos_t pos = -1;
    filepos_t fsize = -1;
    mtell(pos);
    msize(fsize);
    if(pos==fsize-1){ //last byte.
      if(verbosity_>2){
        cout << "[Matacq " << now() << "] Event found was at the end of the file. Moving "
          "stream position to beginning of this event."
             << "\n";
      }
      mseek(-(int)len*8-1, SEEK_CUR,
            "Moving to beginning of last matacq event");
    }
  }
  return (state==found);
}
bool MatacqProducer::getMatacqFile ( uint32_t  runNumber,
uint32_t  orbitId,
bool *  fileChange = 0 
) [private]

Retrieve the file containing a given matacq event

Parameters:
runNumberNumber of the run the matacq event is looking from
orbitIdId of the orbit of the matacq event
fileChangeif not null pointer, set to true if the file changed.
Returns:
true if file retrieval succeeded, false otherwise. found.

Definition at line 540 of file MatacqProducer.cc.

References gather_cfg::cout, eventSkipCounter_, fileNames_, alignmentValidation::fname, cfg-viewer::format(), newFWLiteAna::found, i, MatacqProducer::PosEstimator::init(), lastOrb_, mcheck(), misOpened(), mopen(), now(), onErrorDisablingEvtCnt_, openedFileRunNumber_, posEstim_, convertSQLiteXML::runNumber, runSubDir(), and verbosity_.

Referenced by addMatacqData().

                                               {
  if(openedFileRunNumber_!=0
     && openedFileRunNumber_==runNumber){
    if(fileChange!=0) *fileChange = false;
    return misOpened();
  }

  if(fileNames_.size()==0) return 0;

  const string runNumberFormat = "%08d";
  string sRunNumber = str(boost::format(runNumberFormat) % runNumber);
  //cout << "Run number string: " << sRunNumber << "\n";
  bool found = false;
  string fname;
  for(unsigned i=0; i < fileNames_.size() && !found; ++i){
    fname = fileNames_[i];
    boost::algorithm::replace_all(fname, "%run_subdir%",
                                  runSubDir(runNumber));
    boost::algorithm::replace_all(fname, "%run_number%", sRunNumber);

    if(verbosity_>0) cout << "[Matacq " << now() << "] "
                          << "Looking for a file with path "
                          << fname << "\n";
    
    if(mcheck(fname)){
      LogInfo("Matacq") << "Uses matacq data file: '" << fname << "'\n";
      found = true;
    }
  }
  if(!found){
    if(verbosity_>=0) cout << "[Matacq " << now() << "] no matacq file found "
                        "for run " << runNumber << "\n";
    eventSkipCounter_ = onErrorDisablingEvtCnt_;
    openedFileRunNumber_ = 0;
    if(fileChange!=0) *fileChange = false;
    return 0;
  }
  
  if(!mopen(fname)){
    LogWarning("Matacq") << "Failed to open file " << fname << "\n";
    eventSkipCounter_ = onErrorDisablingEvtCnt_;
    openedFileRunNumber_ = 0;
    if(fileChange!=0) *fileChange = false;
    return false;
  } else{
    openedFileRunNumber_ = runNumber;
    lastOrb_ = 0;
    posEstim_.init(this);
    if(fileChange!=0) *fileChange = true;
    return true;
  }
}
uint32_t MatacqProducer::getOrbitId ( edm::Event ev) const [private]

Definition at line 599 of file MatacqProducer.cc.

References abs, data, FEDRawData::data(), Exception, edm::Event::getByLabel(), edm::EventBase::id(), inputRawCollection_, FEDNumbering::inRange(), edm::HandleBase::isValid(), orbitTolerance_, lumiPlot::rawdata, and FEDRawData::size().

Referenced by addMatacqData(), getMatacqEvent(), and MatacqProducer::PosEstimator::init().

                                                     {
  //on CVS HEAD (June 4, 08), class Event has a method orbitNumber()
  //we could use here. The code would be shorten to:
  //return ev.orbitNumber();
  //we have to deal with what we have in current CMSSW releases:
  edm::Handle<FEDRawDataCollection> rawdata;
  ev.getByLabel(inputRawCollection_, rawdata);
  if(!(rawdata.isValid())){
    throw cms::Exception("NotFound")
      << "No FED raw data collection found. ECAL raw data are "
      "required to retrieve the orbit ID";
  }
  
  int orbit = 0;
  for(int id=601; id<=654; ++id){
    if(!FEDNumbering::inRange(id)) continue;
    const FEDRawData& data = rawdata->FEDData(id);
    const int orbitIdOffset64 = 3;
    if(data.size()>=8*(orbitIdOffset64+1)){//orbit id is in 4th 64-bit word
      const unsigned char* pOrbit = data.data() + orbitIdOffset64*8;
      int thisOrbit = pOrbit[0]
        | (pOrbit[1] <<8)
        | (pOrbit[2] <<16)
        | (pOrbit[3] <<24);
      if(orbit!=0 && thisOrbit!=0 && abs(orbit-thisOrbit)>orbitTolerance_){
        //throw cms::Exception("EventCorruption")
        //  << "Orbit ID inconsitency in DCC headers";
        LogWarning("EventCorruption")
          << "Orbit ID inconsitency in DCC headers";
        orbit = 0;
        break;
      }
      if(thisOrbit!=0) orbit = thisOrbit;
    }
  }
  
  if(orbit==0){
    //    throw cms::Exception("NotFound")
    //  << "Failed to retrieve orbit ID of event "<< ev.id();
    LogWarning("NotFound") << "Failed to retrieve orbit ID of event "
                                << ev.id();
  }
  return orbit;
}
uint32_t MatacqProducer::getRunNumber ( edm::Event ev) const [private]

Definition at line 595 of file MatacqProducer.cc.

References edm::Event::run().

Referenced by addMatacqData(), and produce().

                                                       {
  return ev.run();
}
void MatacqProducer::loadOrbitOffset ( ) [private]

Loading orbit correction table from file.

See also:
orbitOffsetFile_

Definition at line 777 of file MatacqProducer.cc.

References gather_cfg::cout, Exception, f, ntuplemaker::iline, max(), now(), orbitOffset_, orbitOffsetFile_, DTTTrigCorrFirst::run, and alignCSCRings::s.

Referenced by MatacqProducer().

                                    {
  ifstream f(orbitOffsetFile_.c_str());
  if(f.bad()){
    throw cms::Exception("Matacq")
      << "Failed to open orbit ID correction file '"
      << orbitOffsetFile_ << "'\n";
  }

  cout << "[Matacq " << now() << "] "
       << "Offset to substract to Matacq events Orbit ID: \n"
       << "#Run Number\t Offset\n";

  int iline = 0;
  string s;
  stringstream buf;
  while(f.eof()){
    getline(f, s);
    ++iline;
    if(s[0]=='#'){//comment
      //skip line:
      f.ignore(numeric_limits<streamsize>::max(), '\n');
      continue;
    }
    buf.str("");
    buf << s;
    int run;
    int orbit;
    buf >> run;
    buf >> orbit;
    if(buf.bad()){
      throw cms::Exception("Matacq")
        << "Syntax error in Orbit offset file '"
        << orbitOffsetFile_ << "'";
    }
    cout << run << "\t" << orbit << "\n";
    orbitOffset_.insert(pair<int, int>(run, orbit));
  }
}
bool MatacqProducer::mcheck ( const std::string &  name) [private]

Definition at line 1002 of file MatacqProducer.cc.

Referenced by getMatacqFile().

                                                {
  struct stat dummy;
  return 0==stat(name.c_str(), &dummy);
//   if(stat(name.c_str(), &dummy)==0){
//     return true;
//   } else{
//     cout << "[Matacq " << now() << "] Failed to stat file '"
//       << name.c_str() << "'. " 
//       << "Error " << errno << ": " << strerror(errno) << "\n";
//     return false;
//   }
}
void MatacqProducer::mclose ( ) [private]

Definition at line 1027 of file MatacqProducer.cc.

References inFile_.

Referenced by mopen(), and ~MatacqProducer().

                           {
  if(inFile_!=0) fclose(inFile_);
  inFile_ = 0;
}
bool MatacqProducer::meof ( ) [private]

Definition at line 1036 of file MatacqProducer.cc.

References inFile_.

                         {
  if(0==inFile_) return true;
  return feof(inFile_)==0;
}
bool MatacqProducer::misOpened ( ) [private]

Definition at line 1032 of file MatacqProducer.cc.

References inFile_.

Referenced by getMatacqFile().

                              {
  return inFile_!=0;
}
bool MatacqProducer::mopen ( const std::string &  name) [private]

Definition at line 1015 of file MatacqProducer.cc.

References inFile_, inFileName_, mclose(), and mergeVDriftHistosByStation::name.

Referenced by getMatacqFile().

                                               {
  if(inFile_!=0) mclose();
  inFile_ = fopen(name.c_str(), "r");
  if(inFile_!=0){
    inFileName_ = name;
    return true;
  } else{
    inFileName_ = "";
    return false;
  }
}
bool MatacqProducer::mread ( char *  buf,
size_t  n,
const char *  mess = 0,
bool  peek = false 
) [private]

Read a data block from input file. On failure file position is restored and if position restoring fails, file is rewind.

Parameters:
bufbuffer to store read data
nsize of data block
messtext to insert in the eventual error message.
peekif true file position is restored after the data read
Returns:
true on success, false on failure

Definition at line 956 of file MatacqProducer.cc.

References gather_cfg::cout, inFile_, lastOrb_, mrewind(), now(), and verbosity_.

Referenced by getMatacqEvent(), and MatacqProducer::PosEstimator::init().

                                                                          {
  if(0==inFile_) return false;
  off_t pos = ftello(inFile_);
  bool rc = (pos!=-1) && (1==fread(buf, n, 1, inFile_));
  if(!rc){
    if(verbosity_){
      cout << "[Matacq " << now() << "] ";
      if(mess) cout << mess << ". ";
      cout << "Read failure from input matacq file.\n";
    }
    clearerr(inFile_);
  }
  if(peek || !rc){//need to restore file position
    if(0!=fseeko(inFile_, pos, SEEK_SET)){
      if(verbosity_){
        cout << "[Matacq " << now() << "] ";
        if(mess) cout << mess << ". ";
        cout << "Failed to restore file position of "
          "before read error. Rewind file.\n";
      }
      //rewind(inFile_.get());
      mrewind();
      lastOrb_ = 0;
    }
  }
  return rc;
}
bool MatacqProducer::mrewind ( ) [private]

Definition at line 996 of file MatacqProducer.cc.

References inFile_.

Referenced by getMatacqEvent(), MatacqProducer::PosEstimator::init(), mread(), and mseek().

                            {
  if(0==inFile_) return false;
  clearerr(inFile_);
  return fseeko(inFile_, 0, SEEK_SET)!=0; 
}
bool MatacqProducer::mseek ( filepos_t  offset,
int  whence = SEEK_SET,
const char *  mess = 0 
) [private]

Move input file read pointer. On failure file is rewind.

Parameters:
bufbuffer to store read data
nsize of data block
messtext to insert in the eventual error message.
Returns:
true on success, false on failure

Definition at line 936 of file MatacqProducer.cc.

References gather_cfg::cout, inFile_, mrewind(), now(), and verbosity_.

Referenced by getMatacqEvent(), and MatacqProducer::PosEstimator::init().

                                                                    {
  if(0==inFile_) return false;
  const int rc = fseeko(inFile_, offset, whence);
  if(rc!=0 && verbosity_){
    cout << "[Matacq " << now() << "] ";
    if(mess) cout << mess << ". ";
    cout << "Random access error on input matacq file. "
      "Rewind file.\n";
    mrewind();
  }
  return rc==0;
}
bool MatacqProducer::msize ( filepos_t s) [private]

Definition at line 984 of file MatacqProducer.cc.

References inFile_.

Referenced by getMatacqEvent(), and MatacqProducer::PosEstimator::init().

                                      {
  if(0==inFile_) return false;
  struct stat buf;
  if(0!=fstat(fileno(inFile_), &buf)){
    s = 0;
    return false;
  } else{
    s = buf.st_size;
    return true;
  }
}
bool MatacqProducer::mtell ( filepos_t pos) [private]

Definition at line 949 of file MatacqProducer.cc.

References inFile_.

Referenced by getMatacqEvent().

                                        {
  if(0==inFile_) return false;
  pos = ftello(inFile_);
  return pos != -1;
    
}
void MatacqProducer::newRun ( int  prevRun,
int  newRun 
) [private]

Definition at line 1050 of file MatacqProducer.cc.

References eventSkipCounter_, logFile_, MatacqProducer::stats_t::nEvents, MatacqProducer::stats_t::nLaserEventsWithMatacq, MatacqProducer::stats_t::nNonLaserEventsWithMatacq, now(), runNumber_, and stats_.

Referenced by produce().

                                                  {
    runNumber_ = newRun;
    eventSkipCounter_ = 0;
    logFile_ << "[" << now() << "] Event count for run "
             << runNumber_ << ": "
             << "total: " << stats_.nEvents << ", "
             << "Laser event with Matacq data: "
             << stats_.nLaserEventsWithMatacq << ", "
             << "Non laser event (according to DCC header) with Matacq data: "
             << stats_.nNonLaserEventsWithMatacq << "\n" << flush;

    stats_.nEvents = 0;
    stats_.nLaserEventsWithMatacq = 0;
    stats_.nNonLaserEventsWithMatacq = 0;

    
}
void MatacqProducer::produce ( edm::Event event,
const edm::EventSetup eventSetup 
) [virtual]

Produces the EDM products

Parameters:
CMSevent
eventSetupevent conditions

Implements edm::EDProducer.

Definition at line 144 of file MatacqProducer.cc.

References addMatacqData(), gather_cfg::cout, disabled_, getRunNumber(), logTiming_, MatacqProducer::stats_t::nEvents, newRun(), now(), convertSQLiteXML::runNumber, runNumber_, startTime_, stats_, lumiQTWidget::t, timeLog_, timer_, and verbosity_.

                                                                       {
  if(verbosity_>=4) cout << "[Matacq " << now() << "] in MatacqProducer::produce"  << endl;
  if(logTiming_){
    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;
  } 
 
  if(startTime_.tv_sec==0) gettimeofday(&startTime_, 0);
  ++stats_.nEvents;  
  if(disabled_) return;
  const uint32_t runNumber = getRunNumber(event);
  if(runNumber!=runNumber_){
    newRun(runNumber_, runNumber);
  }
  addMatacqData(event);

  if(logTiming_){
    timeval t;
      gettimeofday(&t, 0);
      timeLog_ << (t.tv_usec - timer_.tv_usec)*1. 
        + (t.tv_sec - timer_.tv_sec)*1.e6 << "\n";
      timer_ = t;
  }
}
std::string MatacqProducer::runSubDir ( uint32_t  runNumber) [static, private]

Definition at line 1043 of file MatacqProducer.cc.

References cfg-viewer::format(), and create_public_lumi_plots::units.

Referenced by getMatacqFile().

                                                     {
  int millions = runNumber / (1000*1000);
  int thousands = (runNumber-millions*1000*1000) / 1000;
  int units = runNumber-millions*1000*1000 - thousands*1000;
  return str(boost::format("%03d/%03d/%03d") % millions % thousands % units);
}

Member Data Documentation

const int MatacqProducer::bufferSize = 30000 [static, private]

Definition at line 256 of file MatacqProducer.h.

Referenced by addMatacqData().

std::vector<unsigned char> MatacqProducer::data_ [private]

Definition at line 258 of file MatacqProducer.h.

Referenced by addMatacqData(), and getMatacqEvent().

std::string MatacqProducer::digiInstanceName_ [private]

Instance name to use for the produced Matacq digi collection

Definition at line 210 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

bool MatacqProducer::disabled_ [private]

Parameter to disable matacq data production. For timing purpose.

Definition at line 222 of file MatacqProducer.h.

Referenced by produce().

Switch for orbit ID correction.

See also:
orbitOffsetFile_

Definition at line 281 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

counter for event skipping

Definition at line 307 of file MatacqProducer.h.

Referenced by addMatacqData(), getMatacqFile(), and newRun().

Definition at line 263 of file MatacqProducer.h.

Referenced by getMatacqEvent().

std::vector<std::string> MatacqProducer::fileNames_ [private]

Definition at line 206 of file MatacqProducer.h.

Referenced by getMatacqFile().

Definition at line 259 of file MatacqProducer.h.

Referenced by addMatacqData().

When true look for matacq data independently of trigger type.

Definition at line 248 of file MatacqProducer.h.

Referenced by addMatacqData().

Stream of currently opened matacq file

Definition at line 254 of file MatacqProducer.h.

Referenced by mclose(), meof(), misOpened(), mopen(), mread(), mrewind(), mseek(), msize(), and mtell().

std::string MatacqProducer::inFileName_ [private]

Name of currently opened matacq file

Definition at line 285 of file MatacqProducer.h.

Referenced by getMatacqEvent(), and mopen().

Name of raw data collection the Matacq data must be merge to if merging is enabled.

Definition at line 239 of file MatacqProducer.h.

Referenced by addMatacqData(), getCalibTriggerType(), and getOrbitId().

int32_t MatacqProducer::lastOrb_ [private]

Definition at line 262 of file MatacqProducer.h.

Referenced by getMatacqEvent(), getMatacqFile(), and mread().

std::ofstream MatacqProducer::logFile_ [private]

Log file

Definition at line 302 of file MatacqProducer.h.

Referenced by MatacqProducer(), and newRun().

std::string MatacqProducer::logFileName_ [private]

Log file name

Definition at line 298 of file MatacqProducer.h.

Referenced by MatacqProducer().

Switch for code timing.

Definition at line 326 of file MatacqProducer.h.

Referenced by MatacqProducer(), produce(), and ~MatacqProducer().

Definition at line 250 of file MatacqProducer.h.

Referenced by addMatacqData(), and getMatacqEvent().

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

Definition at line 287 of file MatacqProducer.h.

Referenced by addMatacqData().

bool MatacqProducer::mergeRaw_ [private]

Switch for merging Matacq raw data with existing raw data collection.

Definition at line 244 of file MatacqProducer.h.

Referenced by addMatacqData().

Number of events to skip in case of error

Definition at line 311 of file MatacqProducer.h.

Referenced by getMatacqFile().

Definition at line 261 of file MatacqProducer.h.

Referenced by getMatacqFile().

std::map<uint32_t,uint32_t> MatacqProducer::orbitOffset_ [private]

Orbit offset table.

See also:
orbitOffsetFile_

Definition at line 277 of file MatacqProducer.h.

Referenced by addMatacqData(), and loadOrbitOffset().

std::string MatacqProducer::orbitOffsetFile_ [private]

File name of table with orbit offset between matacq event and DCC. Used to recover data suffering from orbit miss-synchonization

Definition at line 273 of file MatacqProducer.h.

Referenced by loadOrbitOffset(), and MatacqProducer().

int MatacqProducer::orbitTolerance_ = 80 [static, private]

Definition at line 260 of file MatacqProducer.h.

Referenced by getMatacqEvent(), and getOrbitId().

Definition at line 265 of file MatacqProducer.h.

Referenced by getMatacqEvent(), getMatacqFile(), and MatacqProducer().

Swictch for Matacq digi producion

Definition at line 230 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

Switch for Matacq FED raw data production

Definition at line 234 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

std::string MatacqProducer::rawInstanceName_ [private]

Instance name to use for the produced Matacq raw data collection

Definition at line 214 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

uint32_t MatacqProducer::runNumber_ [private]

Number of the currently processed run

Definition at line 330 of file MatacqProducer.h.

Referenced by newRun(), and produce().

timeval MatacqProducer::startTime_ [private]

Definition at line 267 of file MatacqProducer.h.

Referenced by MatacqProducer(), produce(), and ~MatacqProducer().

Referenced by addMatacqData(), newRun(), and produce().

Definition at line 295 of file MatacqProducer.h.

std::ofstream MatacqProducer::timeLog_ [private]

Output stream to log code timing

Definition at line 322 of file MatacqProducer.h.

Referenced by MatacqProducer(), and produce().

std::string MatacqProducer::timeLogFile_ [private]

Name of file to log timing

Definition at line 315 of file MatacqProducer.h.

Referenced by MatacqProducer().

timeval MatacqProducer::timer_ [private]

Buffer for timing

Definition at line 318 of file MatacqProducer.h.

Referenced by MatacqProducer(), and produce().

bool MatacqProducer::timing_ [private]

Parameter to switch module timing.

Definition at line 218 of file MatacqProducer.h.