CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/EventFilter/CastorRawToDigi/plugins/CastorRawToDigi.cc

Go to the documentation of this file.
00001 using namespace std;
00002 #include "EventFilter/CastorRawToDigi/plugins/CastorRawToDigi.h"
00003 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00004 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00005 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include "FWCore/Framework/interface/Run.h"
00008 
00009 #include "CalibFormats/CastorObjects/interface/CastorDbService.h"
00010 #include "CalibFormats/CastorObjects/interface/CastorDbRecord.h"
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 #include <iostream>
00013 #include "EventFilter/CastorRawToDigi/interface/CastorRawCollections.h"
00014 
00015 CastorRawToDigi::CastorRawToDigi(edm::ParameterSet const& conf):
00016   dataTag_(conf.getParameter<edm::InputTag>("InputLabel")),
00017   unpacker_(conf.getUntrackedParameter<int>("CastorFirstFED",FEDNumbering::MINCASTORFEDID),conf.getParameter<int>("firstSample"),conf.getParameter<int>("lastSample")),
00018   ctdcunpacker_(conf.getUntrackedParameter<int>("CastorFirstFED",FEDNumbering::MINCASTORFEDID),conf.getParameter<int>("firstSample"),conf.getParameter<int>("lastSample")),
00019   filter_(conf.getParameter<bool>("FilterDataQuality"),conf.getParameter<bool>("FilterDataQuality"),
00020           false,
00021           0, 0, 
00022           -1),
00023   fedUnpackList_(conf.getUntrackedParameter<std::vector<int> >("FEDs", std::vector<int>())),
00024   firstFED_(conf.getUntrackedParameter<int>("CastorFirstFED",FEDNumbering::MINCASTORFEDID)),
00025 //  unpackCalib_(conf.getUntrackedParameter<bool>("UnpackCalib",false)),
00026   complainEmptyData_(conf.getUntrackedParameter<bool>("ComplainEmptyData",false)),
00027   usingctdc_(conf.getUntrackedParameter<bool>("CastorCtdc",false)),
00028   unpackTTP_(conf.getUntrackedParameter<bool>("UnpackTTP",false)),
00029   silent_(conf.getUntrackedParameter<bool>("silent",true)),
00030   usenominalOrbitMessageTime_(conf.getUntrackedParameter<bool>("UseNominalOrbitMessageTime",true)),
00031   expectedOrbitMessageTime_(conf.getUntrackedParameter<int>("ExpectedOrbitMessageTime",-1))
00032 
00033 {
00034   if (fedUnpackList_.empty()) {
00035     for (int i=FEDNumbering::MINCASTORFEDID; i<=FEDNumbering::MAXCASTORFEDID; i++)
00036      fedUnpackList_.push_back(i);
00037   } 
00038 
00039   unpacker_.setExpectedOrbitMessageTime(expectedOrbitMessageTime_);  
00040   std::ostringstream ss;
00041   for (unsigned int i=0; i<fedUnpackList_.size(); i++) 
00042     ss << fedUnpackList_[i] << " ";
00043   edm::LogInfo("CASTOR") << "CastorRawToDigi will unpack FEDs ( " << ss.str() << ")";
00044     
00045   // products produced...
00046   produces<CastorDigiCollection>();
00047   produces<CastorTrigPrimDigiCollection>();
00048   produces<HcalUnpackerReport>();
00049   if (unpackTTP_)
00050     produces<HcalTTPDigiCollection>();
00051 
00052 //  if (unpackCalib_)
00053 //    produces<HcalCalibDigiCollection>();
00054 
00055 }
00056 
00057 // Virtual destructor needed.
00058 CastorRawToDigi::~CastorRawToDigi() { }  
00059 
00060 // Functions that gets called by framework every event
00061 void CastorRawToDigi::produce(edm::Event& e, const edm::EventSetup& es)
00062 {
00063   // Step A: Get Inputs 
00064   edm::Handle<FEDRawDataCollection> rawraw;  
00065   e.getByLabel(dataTag_,rawraw);
00066   // get the mapping
00067   edm::ESHandle<CastorDbService> pSetup;
00068   es.get<CastorDbRecord>().get( pSetup );
00069   const CastorElectronicsMap* readoutMap=pSetup->getCastorMapping();
00070   
00071   // Step B: Create empty output  : three vectors for three classes...
00072   std::vector<CastorDataFrame> castor;
00073   std::vector<HcalTTPDigi> ttp;
00074   std::vector<CastorTriggerPrimitiveDigi> htp;
00075 
00076   std::auto_ptr<HcalUnpackerReport> report(new HcalUnpackerReport);
00077 
00078   CastorRawCollections colls;
00079   colls.castorCont=&castor;
00080   if (unpackTTP_) colls.ttp=&ttp;
00081   colls.tpCont=&htp;
00082   //colls.calibCont=&hc;
00083  
00084   // Step C: unpack all requested FEDs
00085   for (std::vector<int>::const_iterator i=fedUnpackList_.begin(); i!=fedUnpackList_.end(); i++) {
00086     const FEDRawData& fed = rawraw->FEDData(*i);
00087     if (fed.size()==0) {
00088       if (complainEmptyData_) {
00089         edm::LogWarning("EmptyData") << "No data for FED " << *i;
00090         report->addError(*i);
00091       }
00092     } else if (fed.size()<8*3) {
00093       edm::LogWarning("EmptyData") << "Tiny data " << fed.size() << " for FED " << *i;
00094       report->addError(*i);
00095     } else {
00096       try {
00097                   if ( usingctdc_ ) { 
00098                           ctdcunpacker_.unpack(fed,*readoutMap,colls, *report);
00099                   } else {
00100                           unpacker_.unpack(fed,*readoutMap,colls, *report);
00101               }
00102               report->addUnpacked(*i);
00103       } catch (cms::Exception& e) {
00104         edm::LogWarning("Unpacking error") << e.what();
00105         report->addError(*i);
00106       } catch (...) {
00107         edm::LogWarning("Unpacking exception");
00108         report->addError(*i);
00109       }
00110     }
00111   }
00112 
00113   // Step B: encapsulate vectors in actual collections
00114   std::auto_ptr<CastorDigiCollection> castor_prod(new CastorDigiCollection()); 
00115   std::auto_ptr<CastorTrigPrimDigiCollection> htp_prod(new CastorTrigPrimDigiCollection());  
00116 
00117   castor_prod->swap_contents(castor);
00118   htp_prod->swap_contents(htp);
00119 
00120   // Step C2: filter FEDs, if required
00121   if (filter_.active()) {
00122     CastorDigiCollection filtered_castor=filter_.filter(*castor_prod,*report);
00123     
00124     castor_prod->swap(filtered_castor);
00125   }
00126 
00127   // Step D: Put outputs into event
00128   // just until the sorting is proven
00129   castor_prod->sort();
00130   htp_prod->sort();
00131 
00132   e.put(castor_prod);
00133   e.put(htp_prod);
00134 
00136 //  if (unpackCalib_) {
00137 //    std::auto_ptr<CastorCalibDigiCollection> hc_prod(new CastorCalibDigiCollection());
00138 //    hc_prod->swap_contents(hc);
00139 //    hc_prod->sort();
00140 //    e.put(hc_prod);
00141 //  }
00142   if (unpackTTP_) {
00143     std::auto_ptr<HcalTTPDigiCollection> prod(new HcalTTPDigiCollection());
00144     prod->swap_contents(ttp);
00145     
00146     prod->sort();
00147     e.put(prod);
00148   }
00149   e.put(report);
00150 }
00151 void CastorRawToDigi::beginRun(edm::Run const& irun, edm::EventSetup const& es){
00152         if ( usenominalOrbitMessageTime_ ) {
00153                 if ( irun.run() > 132640 ) { 
00154                         expectedOrbitMessageTime_ = 3560;
00155                 } else if ( irun.run() > 132174 ) {
00156                         expectedOrbitMessageTime_ = 3559;
00157                 } else if ( irun.run() > 124371 ) {
00158                         expectedOrbitMessageTime_ = 3557;
00159                 } else if ( irun.run() > 123984 ) {
00160                         expectedOrbitMessageTime_ = 3559;
00161                 } else if ( irun.run() > 123584 ) {
00162                         expectedOrbitMessageTime_ = 2;
00163                 } else {
00164                         expectedOrbitMessageTime_ = 3562;
00165                 }
00166                 unpacker_.setExpectedOrbitMessageTime(expectedOrbitMessageTime_);
00167         }
00168 }
00169