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::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 std::auto_ptr< StorageFILE_t
typedef IOOffset 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)

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 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_
MatacqRawEvent matacq_
bool mergeRaw_
uint32_t openedFileRunNumber_
std::map< uint32_t, uint32_t > orbitOffset_
std::string orbitOffsetFile_
PosEstimator posEstim_
bool produceDigis_
bool produceRaw_
std::string rawInstanceName_
timeval startTime_
struct MatacqProducer::stats_t stats_
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

EDM producer module providing MATACQ data. The MATACQ is a board sampling the ECAL laser pulse. Data are recording in dedicated files. This module permits to read these files add to each laser type EDM the corresponding MATACQ data: as digis (#see MatacqDigiCollection) or the raw data FED block (#see FEDRawDataCollection). Run and orbit numbers are used to match the MATACQ data with the ECAL DCC data.

Module parameters are listed and documented in EventFilter/EcalRawToDigi/ecalMatacq_cfi.py.

Definition at line 51 of file MatacqProducer.h.


Member Typedef Documentation

typedef std::auto_ptr<Storage> MatacqProducer::FILE_t [private]

Definition at line 63 of file MatacqProducer.h.

Definition at line 62 of file MatacqProducer.h.


Member Enumeration Documentation

Enumerator:
laserType 
ledType 
tpType 
pedType 

Definition at line 54 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 49 of file MatacqProducer.cc.

References gather_cfg::cout, digiInstanceName_, doOrbitOffset_, Exception, loadOrbitOffset(), logFile_, logFileName_, orbitOffsetFile_, dbtoconf::out, posEstim_, produceDigis_, produceRaw_, rawInstanceName_, startTime_, 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")){
  if(verbosity_>=4) cout << "[Matacq] in MatacqProducer ctor"  << endl;
  
  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] registering new "
                       "EcalMatacqDigiCollection product with instance name '"
                          << digiInstanceName_ << "'\n";
    produces<EcalMatacqDigiCollection>(digiInstanceName_);
  }
  
  if(produceRaw_){
    if(verbosity_>0) cout << "[Matacq] 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] exiting MatacqProducer ctor"  << endl;
}
MatacqProducer::~MatacqProducer ( )

Destructor

Definition at line 682 of file MatacqProducer.cc.

References gather_cfg::cout, logFile_, mclose(), MatacqProducer::stats_t::nEvents, MatacqProducer::stats_t::nLaserEventsWithMatacq, MatacqProducer::stats_t::nNonLaserEventsWithMatacq, startTime_, stats_, matplotRender::t, and timing_.

                               {
  mclose();
  timeval t;
  gettimeofday(&t, 0);
  if(timing_ && startTime_.tv_sec!=0){
    //not using logger, to allow timing with different logging options
    cout << "[Matacq] 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";
  }
  logFile_ << "Event count: "
           << "total: " << stats_.nEvents << ", "
           << "Laser event with Matacq data: "
           << stats_.nLaserEventsWithMatacq << ", "
           << "Non laser event (according to DCC header) with Matacq data: "
           << stats_.nNonLaserEventsWithMatacq << "\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 119 of file MatacqProducer.cc.

References bufferSize, filterCSVwithJSON::copy, gather_cfg::cout, data_, digiInstanceName_, doOrbitOffset_, ExpressReco_HICollisions_FallBack::FEDRawDataCollection, formatter_, getCalibTriggerType(), MatacqRawEvent::getDccLen(), getMatacqEvent(), getMatacqFile(), MatacqRawEvent::getOrbitId(), getOrbitId(), MatacqRawEvent::getParsedLen(), getRunNumber(), ignoreTriggerType_, inputRawCollection_, edm::InputTag::instance(), MatacqDataFormatter::interpretRawData(), edm::InputTag::label(), laserType, matacq_, matacqFedId_, mergeRaw_, MatacqProducer::stats_t::nLaserEventsWithMatacq, MatacqProducer::stats_t::nNonLaserEventsWithMatacq, evf::evtn::offset(), orbitOffset_, produceDigis_, produceRaw_, rawInstanceName_, ExpressReco_HICollisions_FallBack::runNumber, stats_, and verbosity_.

Referenced by produce().

                                            {
  edm::Handle<FEDRawDataCollection> sourceColl;
  if(inputRawCollection_.label().size() == 0
     && inputRawCollection_.instance().size() == 0){
    event.getByType(sourceColl);
  } else{
    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(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);
    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;
      uint32_t offset = 0;
      if(doOrbitOffset_){
        map<uint32_t,uint32_t>::iterator it = orbitOffset_.find(runNumber);
        if(it == orbitOffset_.end()){
          LogWarning("IncorrectLaserEvent") << "Orbit offset not found for run "
                               << runNumber
                               << ". No orbit correction will be applied.";
        } else{
          offset = it->second;
        }
      }    
      
      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("IncorrectLaserEvent") << " 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{
          LogWarning("IncorrectLaserEvent") << "No matacq data found for "
                               << "run " << runNumber << " orbit " << orbitId;
        }
      } else{
        LogWarning("IncorrectLaserEvent") << "No matacq file found for event "
                             << event.id();
      }
    }
  }
  
  if(produceRaw_){
    if(verbosity_>1) cout << "[Matacq] "
                          << "Adding FEDRawDataCollection collection "
                          << " to event.\n";
    event.put(rawColl, rawInstanceName_);
  }

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

Definition at line 564 of file MatacqProducer.cc.

References Majority< T >::add(), FEDRawData::data(), runTheMatrix::data, Exception, edm::Event::getByType(), ExpressReco_HICollisions_FallBack::id, FEDNumbering::inRange(), edm::HandleBase::isValid(), L1TEmulatorMonitor_cff::p, Majority< T >::result(), and FEDRawData::size().

Referenced by addMatacqData().

                                                         {  
  edm::Handle<FEDRawDataCollection> rawdata;
  if(!(ev.getByType(rawdata) && 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("IncorrectEvent")  << "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("IncorrectEvent") << "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 270 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(), evf::evtn::offset(), orbitTolerance_, pos, MatacqProducer::PosEstimator::pos(), posEstim_, CrabTask::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] 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] Looks like matacq file is empty"
                            << "\n";
      return false;
    }
  }
  
  if(verbosity_>2) cout << "[Matacq] Last read orbit: " << lastOrb_
                        << " looking for orbit " << orbitId
                        << ". Current file position: " << startPos
                        << " Orbit at current position: " << startOrb << "\n";
  
  //  f.clear();
  bool didCoarseMove = 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] Estimated position was beyond end of file. "
            "Changed to " << pos << "\n";
        }
      }
    } else{
      LogWarning("IncorrectConfiguration") << "Failed to access file " << inFileName_ << ".";
    }
    if(pos>=0){
      if(verbosity_>2) cout << "[Matacq] 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] 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] 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] 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] 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;
      mtell(pos);
      cout << "[Matacq] 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] Event found. Reading "
                         " matacq event." << "\n";
      if(!mread((char*)&data_[0], len*8, "Reading matacq event")){
        if(verbosity_>2) cout << "[Matacq] 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_) cout << "[Matacq] Event search failed." << "\n";
      } else{
        off_t offset = (searchBackward?-len:len)*8;
        lastOrb_ = orb; 
        if(verbosity_>3){
          cout << "[Matacq] 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;
    filepos_t fsize;
    mtell(pos);
    msize(fsize);
    if(pos==fsize-1){ //last byte.
      if(verbosity_>2){
        cout << "[Matacq] 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 466 of file MatacqProducer.cc.

References gather_cfg::cout, fileNames_, alignmentValidation::fname, diffTreeTool::format(), newFWLiteAna::found, i, MatacqProducer::PosEstimator::init(), lastOrb_, mcheck(), misOpened(), mopen(), openedFileRunNumber_, posEstim_, ExpressReco_HICollisions_FallBack::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_) cout << "[Matacq] Looking for a file with path "
                        << fname << "\n";
    
    if(mcheck(fname)){
      LogInfo("Matacq") << "Uses matacq data file: '" << fname << "'\n";
      found = true;
    }
  }
  if(!found){
    openedFileRunNumber_ = 0;
    if(fileChange!=0) *fileChange = false;
    return 0;
  }
  
  if(!mopen(fname)){
    LogWarning("IncorrectConfiguration") << "Failed to open file " << fname << "\n";
    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 520 of file MatacqProducer.cc.

References abs, FEDRawData::data(), runTheMatrix::data, Exception, edm::Event::getByType(), edm::EventBase::id(), ExpressReco_HICollisions_FallBack::id, FEDNumbering::inRange(), edm::HandleBase::isValid(), orbitTolerance_, 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;
  if(!(ev.getByType(rawdata) && 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("IncorrectEvent")
          << "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("IncorrectEvent") << "Failed to retrieve orbit ID of event "
                                << ev.id();
  }
  return orbit;
}
uint32_t MatacqProducer::getRunNumber ( edm::Event ev) const [private]

Definition at line 516 of file MatacqProducer.cc.

References edm::Event::run().

Referenced by addMatacqData().

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

Loading orbit correction table from file.

See also:
orbitOffsetFile_

Definition at line 701 of file MatacqProducer.cc.

References gather_cfg::cout, Exception, f, ntuplemaker::iline, max(), orbitOffset_, orbitOffsetFile_, CrabTask::run, and asciidump::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] "
       << "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 923 of file MatacqProducer.cc.

Referenced by getMatacqFile().

                                                {
  struct stat dummy;
  return 0==stat(name.c_str(), &dummy);
}
void MatacqProducer::mclose ( ) [private]

Definition at line 940 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 949 of file MatacqProducer.cc.

References inFile_.

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

Definition at line 945 of file MatacqProducer.cc.

References inFile_.

Referenced by getMatacqFile().

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

Definition at line 928 of file MatacqProducer.cc.

References inFile_, inFileName_, mclose(), and AlCaRecoCosmics_cfg::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 877 of file MatacqProducer.cc.

References gather_cfg::cout, inFile_, lastOrb_, mrewind(), 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] ";
      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] ";
        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 917 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 857 of file MatacqProducer.cc.

References gather_cfg::cout, inFile_, mrewind(), 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] ";
    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 905 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 870 of file MatacqProducer.cc.

References inFile_.

Referenced by getMatacqEvent().

                                        {
  if(0==inFile_) return false;
  pos = ftello(inFile_);
  return pos != -1;
    
}
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 110 of file MatacqProducer.cc.

References addMatacqData(), gather_cfg::cout, disabled_, MatacqProducer::stats_t::nEvents, startTime_, stats_, and verbosity_.

                                                                       {
  if(verbosity_>=4) cout << "[Matacq] in MatacqProducer::produce"  << endl;
  if(startTime_.tv_sec==0) gettimeofday(&startTime_, 0);
  ++stats_.nEvents;  
  if(disabled_) return;
  addMatacqData(event);
}
std::string MatacqProducer::runSubDir ( uint32_t  runNumber) [static, private]

Definition at line 956 of file MatacqProducer.cc.

References diffTreeTool::format().

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 262 of file MatacqProducer.h.

Referenced by addMatacqData().

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

Definition at line 264 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 216 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 228 of file MatacqProducer.h.

Referenced by produce().

Switch for orbit ID correction.

See also:
orbitOffsetFile_

Definition at line 287 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

Definition at line 269 of file MatacqProducer.h.

Referenced by getMatacqEvent().

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

Definition at line 212 of file MatacqProducer.h.

Referenced by getMatacqFile().

Definition at line 265 of file MatacqProducer.h.

Referenced by addMatacqData().

When true look for matacq data independently of trigger type.

Definition at line 254 of file MatacqProducer.h.

Referenced by addMatacqData().

Stream of currently opened matacq file

Definition at line 260 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 291 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 245 of file MatacqProducer.h.

Referenced by addMatacqData().

int32_t MatacqProducer::lastOrb_ [private]

Definition at line 268 of file MatacqProducer.h.

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

std::ofstream MatacqProducer::logFile_ [private]

Log file

Definition at line 308 of file MatacqProducer.h.

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

std::string MatacqProducer::logFileName_ [private]

Log file name

Definition at line 304 of file MatacqProducer.h.

Referenced by MatacqProducer().

Definition at line 256 of file MatacqProducer.h.

Referenced by addMatacqData(), and getMatacqEvent().

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

Definition at line 293 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 250 of file MatacqProducer.h.

Referenced by addMatacqData().

Definition at line 267 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 283 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 279 of file MatacqProducer.h.

Referenced by loadOrbitOffset(), and MatacqProducer().

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

Definition at line 266 of file MatacqProducer.h.

Referenced by getMatacqEvent(), and getOrbitId().

Definition at line 271 of file MatacqProducer.h.

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

Swictch for Matacq digi producion

Definition at line 236 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

Switch for Matacq FED raw data production

Definition at line 240 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 220 of file MatacqProducer.h.

Referenced by addMatacqData(), and MatacqProducer().

timeval MatacqProducer::startTime_ [private]

Definition at line 273 of file MatacqProducer.h.

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

Definition at line 301 of file MatacqProducer.h.

bool MatacqProducer::timing_ [private]

Parameter to switch module timing.

Definition at line 224 of file MatacqProducer.h.

Referenced by ~MatacqProducer().