CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/EventFilter/EcalRawToDigi/plugins/EcalRawToDigi.cc

Go to the documentation of this file.
00001 #include "EventFilter/EcalRawToDigi/plugins/EcalRawToDigi.h"
00002 #include "EventFilter/EcalRawToDigi/interface/EcalElectronicsMapper.h"
00003 #include "EventFilter/EcalRawToDigi/interface/DCCDataUnpacker.h"
00004 
00005 
00006 #include "FWCore/Framework/interface/EventSetup.h"
00007 #include "FWCore/Framework/interface/ESHandle.h"
00008 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
00009 #include "DataFormats/EcalRawData/interface/EcalListOfFEDS.h"
00010 #include "CondFormats/EcalObjects/interface/EcalChannelStatus.h"
00011 #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
00012 
00013 EcalRawToDigi::EcalRawToDigi(edm::ParameterSet const& conf):
00014   
00015   //define the list of FED to be unpacked
00016   fedUnpackList_(conf.getParameter<std::vector<int> >("FEDs")),
00017 
00018   //define the ordered FED list
00019   orderedFedUnpackList_(conf.getParameter<std::vector<int> >("orderedFedList")),
00020 
00021   //define the ordered DCCId list
00022   orderedDCCIdList_(conf.getParameter<std::vector<int> >("orderedDCCIdList")),
00023 
00024   //get number of Xtal Time Samples
00025   numbXtalTSamples_(conf.getParameter<int>("numbXtalTSamples")),
00026 
00027   //Get number of Trigger Time Samples
00028   numbTriggerTSamples_(conf.getParameter<int>("numbTriggerTSamples")),
00029   
00030   //See if header unpacking is enabled
00031   headerUnpacking_(conf.getParameter<bool>("headerUnpacking")),
00032  
00033   //See if srp unpacking is enabled
00034   srpUnpacking_(conf.getParameter<bool>("srpUnpacking")),
00035   
00036   //See if tcc unpacking is enabled
00037   tccUnpacking_(conf.getParameter<bool>("tccUnpacking")),
00038   
00039   //See if fe unpacking is enabled
00040   feUnpacking_(conf.getParameter<bool>("feUnpacking")),
00041   
00042   //See if fe unpacking is enabled for mem box
00043   memUnpacking_(conf.getParameter<bool>("memUnpacking")), 
00044 
00045   //See if syncCheck is enabled
00046   syncCheck_(conf.getParameter<bool>("syncCheck")), 
00047 
00048   //See if feIdCheck is enabled
00049   feIdCheck_(conf.getParameter<bool>("feIdCheck")),
00050 
00051   // See if we want to keep data even if we have a mismatch between SR decision and block length
00052   forceToKeepFRdata_(conf.getParameter<bool>("forceToKeepFRData")),
00053 
00054   
00055   put_(conf.getParameter<bool>("eventPut")),
00056   
00057   dataLabel_(conf.getParameter<edm::InputTag>("InputLabel")),
00058 
00059   REGIONAL_(conf.getParameter<bool>("DoRegional")),
00060 
00061   fedsLabel_(conf.getParameter<edm::InputTag>("FedLabel")),
00062 
00063   myMap_(0),
00064   
00065   theUnpacker_(0)
00066 
00067 {
00068   
00069   first_ = true;
00070   DCCDataUnpacker::silentMode_ = conf.getUntrackedParameter<bool>("silentMode",false) ;
00071   
00072   if( numbXtalTSamples_ <6 || numbXtalTSamples_>64 || (numbXtalTSamples_-2)%4 ){
00073     std::ostringstream output;
00074     output      <<"\n Unsuported number of xtal time samples : "<<numbXtalTSamples_
00075                 <<"\n Valid Number of xtal time samples are : 6,10,14,18,...,62"; 
00076     edm::LogError("IncorrectConfiguration")<< output.str();
00077     // todo : throw an execption
00078   }
00079   
00080   if( numbTriggerTSamples_ !=1 && numbTriggerTSamples_ !=4 && numbTriggerTSamples_ !=8  ){
00081     std::ostringstream output;
00082     output      <<"\n Unsuported number of trigger time samples : "<<numbTriggerTSamples_
00083                 <<"\n Valid number of trigger time samples are :  1, 4 or 8"; 
00084     edm::LogError("IncorrectConfiguration")<< output.str();
00085     // todo : throw an execption
00086   }
00087   
00088   //NA : testing
00089   //nevts_=0;
00090   //RUNNING_TIME_=0;
00091 
00092   // if there are FEDs specified to unpack fill the vector of the fedUnpackList_
00093   // else fill with the entire ECAL fed range (600-670)
00094   if (fedUnpackList_.empty()) 
00095     for (int i=FEDNumbering::MINECALFEDID; i<=FEDNumbering::MAXECALFEDID; i++)
00096       fedUnpackList_.push_back(i);
00097 
00098   //print the FEDs to unpack to the logger
00099   std::ostringstream loggerOutput_;
00100   if(fedUnpackList_.size()!=0){
00101     for (unsigned int i=0; i<fedUnpackList_.size(); i++) 
00102       loggerOutput_ << fedUnpackList_[i] << " ";
00103     edm::LogInfo("EcalRawToDigi") << "EcalRawToDigi will unpack FEDs ( " << loggerOutput_.str() << ")";
00104   }
00105   
00106   edm::LogInfo("EcalRawToDigi")
00107     <<"\n ECAL RawToDigi configuration:"
00108     <<"\n Header  unpacking is "<<headerUnpacking_
00109     <<"\n SRP Bl. unpacking is "<<srpUnpacking_
00110     <<"\n TCC Bl. unpacking is "<<tccUnpacking_  
00111     <<"\n FE  Bl. unpacking is "<<feUnpacking_
00112     <<"\n MEM Bl. unpacking is "<<memUnpacking_<<"\n";
00113   
00114   // Producer products :
00115   produces<EBDigiCollection>("ebDigis"); 
00116   produces<EEDigiCollection>("eeDigis");
00117   produces<EBSrFlagCollection>();
00118   produces<EESrFlagCollection>();
00119   produces<EcalRawDataCollection>();
00120   produces<EcalPnDiodeDigiCollection>();
00121   produces<EcalTrigPrimDigiCollection>("EcalTriggerPrimitives");
00122   produces<EcalPSInputDigiCollection>("EcalPseudoStripInputs");
00123   
00124   // Integrity for xtal data
00125   produces<EBDetIdCollection>("EcalIntegrityGainErrors");
00126   produces<EBDetIdCollection>("EcalIntegrityGainSwitchErrors");
00127   produces<EBDetIdCollection>("EcalIntegrityChIdErrors");
00128 
00129   // Integrity for xtal data - EE specific (to be rivisited towards EB+EE common collection)
00130   produces<EEDetIdCollection>("EcalIntegrityGainErrors");
00131   produces<EEDetIdCollection>("EcalIntegrityGainSwitchErrors");
00132   produces<EEDetIdCollection>("EcalIntegrityChIdErrors");
00133 
00134   // Integrity Errors
00135   produces<EcalElectronicsIdCollection>("EcalIntegrityTTIdErrors");
00136   produces<EcalElectronicsIdCollection>("EcalIntegrityZSXtalIdErrors");
00137   produces<EcalElectronicsIdCollection>("EcalIntegrityBlockSizeErrors");
00138  
00139   // Mem channels' integrity
00140   produces<EcalElectronicsIdCollection>("EcalIntegrityMemTtIdErrors");
00141   produces<EcalElectronicsIdCollection>("EcalIntegrityMemBlockSizeErrors");
00142   produces<EcalElectronicsIdCollection>("EcalIntegrityMemChIdErrors");
00143   produces<EcalElectronicsIdCollection>("EcalIntegrityMemGainErrors");
00144 
00145 
00146  
00147   // Build a new Electronics mapper and parse default map file
00148   myMap_ = new EcalElectronicsMapper(numbXtalTSamples_,numbTriggerTSamples_);
00149 
00150   // in case of external  text file (deprecated by HLT environment) 
00151   //  bool readResult = myMap_->readDCCMapFile(conf.getParameter<std::string>("DCCMapFile",""));
00152 
00153   // use two arrays from cfg to establish DCCId:FedId. If they are empy, than use hard coded correspondence 
00154   bool readResult = myMap_->makeMapFromVectors(orderedFedUnpackList_, orderedDCCIdList_);
00155   // myMap::makeMapFromVectors() returns "true" always
00156   // need to be fixed?
00157 
00158   if(!readResult){
00159     edm::LogWarning("IncorrectConfiguration")
00160       << "Arrays orderedFedList and orderedDCCIdList are emply. "
00161          "Hard coded correspondence for DCCId:FedId will be used.";
00162     // edm::LogError("EcalRawToDigi")<<"\n unable to read file : "
00163     //   <<conf.getParameter<std::string>("DCCMapFile");
00164   }
00165   
00166   // Build a new ECAL DCC data unpacker
00167   theUnpacker_ = new DCCDataUnpacker(myMap_,headerUnpacking_,srpUnpacking_,tccUnpacking_,feUnpacking_,memUnpacking_,syncCheck_,feIdCheck_,forceToKeepFRdata_);
00168    
00169 }
00170 
00171 
00172 // print list of crystals with non-zero statuses
00173 // this functions is only for debug purposes
00174 void printStatusRecords(const DCCDataUnpacker* unpacker,
00175                         const EcalElectronicsMapping* mapping)
00176 {
00177   // Endcap
00178   std::cout << "===> ENDCAP" << std::endl;
00179   for (int i = 0; i < EEDetId::kSizeForDenseIndexing; ++i) {
00180     const EEDetId id = EEDetId::unhashIndex(i);
00181     if (!id.null()) {
00182       // channel status
00183       const uint16_t code = unpacker->getChannelValue(id);
00184       
00185       if (code) {
00186         const EcalElectronicsId ei = mapping->getElectronicsId(id);
00187         
00188         // convert DCC ID (1 - 54) to FED ID (601 - 654)
00189         const int fed_id = unpacker->electronicsMapper()->getDCCId(ei.dccId());
00190         
00191         std::cout
00192           << " id " << id.rawId()
00193           << " -> (" << id.ix() << ", " << id.iy() << ", " << id.zside() << ") "
00194           << "(" << ei.dccId() << " : " << fed_id << ", " << ei.towerId() << ", " << ei.stripId() << ", " << ei.xtalId() << ") "
00195           << "status = " << code
00196           << std::endl;
00197       }
00198     }
00199   }
00200   std::cout << "<=== ENDCAP" << std::endl;
00201   
00202   std::cout << "===> BARREL" << std::endl;
00203   for (int i = 0; i < EBDetId::kSizeForDenseIndexing; ++i) {
00204     const EBDetId id = EBDetId::unhashIndex(i);
00205     if (!id.null()) {
00206       // channel status
00207       const uint16_t code = unpacker->getChannelValue(id);
00208       
00209       if (code) {
00210         const EcalElectronicsId ei = mapping->getElectronicsId(id);
00211         
00212         // convert DCC ID (1 - 54) to FED ID (601 - 654)
00213         const int fed_id = unpacker->electronicsMapper()->getDCCId(ei.dccId());
00214         
00215         std::cout
00216           << " id " << id.rawId()
00217           << " -> (" << id.ieta() << ", " << id.iphi() << ", " << id.zside() << ") "
00218           << "(" << ei.dccId() << " : " << fed_id << ", " << ei.towerId() << ", " << ei.stripId() << ", " << ei.xtalId() << ") "
00219           << "status = " << code
00220           << std::endl;
00221       }
00222     }
00223   }
00224   std::cout << "<=== BARREL" << std::endl;
00225 }
00226 
00227 void EcalRawToDigi::beginRun(edm::Run&, const edm::EventSetup& es)
00228 {
00229   // channel status database
00230   edm::ESHandle<EcalChannelStatusMap> pChStatus;
00231   es.get<EcalChannelStatusRcd>().get(pChStatus);
00232   theUnpacker_->setChannelStatusDB(pChStatus.product());
00233   
00234   // uncomment following line to print list of crystals with bad status
00235   //edm::ESHandle<EcalElectronicsMapping> pEcalMapping;
00236   //es.get<EcalMappingRcd>().get(pEcalMapping);
00237   //const EcalElectronicsMapping* mapping = pEcalMapping.product();
00238   //printStatusRecords(theUnpacker_, mapping);
00239 }
00240 
00241 
00242 void EcalRawToDigi::produce(edm::Event& e, const edm::EventSetup& es)
00243 {
00244   
00245   //double TIME_START = clock();
00246   //nevts_++; //NUNO
00247 
00248 
00249   if (first_) {
00250    watcher_.check(es);
00251    edm::ESHandle< EcalElectronicsMapping > ecalmapping;
00252    es.get< EcalMappingRcd >().get(ecalmapping);
00253    myMap_ -> setEcalElectronicsMapping(ecalmapping.product());
00254    
00255    first_ = false;
00256 
00257   }else{
00258 
00259     if ( watcher_.check(es) ) {    
00260       edm::ESHandle< EcalElectronicsMapping > ecalmapping;
00261       es.get< EcalMappingRcd >().get(ecalmapping);
00262       myMap_ -> deletePointers();
00263       myMap_ -> resetPointers();
00264       myMap_ -> setEcalElectronicsMapping(ecalmapping.product());
00265     }
00266 
00267   }
00268 
00269   // Get list of FEDS :
00270   std::vector<int> FEDS_to_unpack;
00271   if (REGIONAL_) {
00272         edm::Handle<EcalListOfFEDS> listoffeds;
00273         e.getByLabel(fedsLabel_, listoffeds);
00274         FEDS_to_unpack = listoffeds -> GetList();
00275   }
00276 
00277 
00278 
00279   // Step A: Get Inputs    
00280 
00281   edm::Handle<FEDRawDataCollection> rawdata;  
00282   e.getByLabel(dataLabel_,rawdata);
00283 
00284 
00285   // Step B: encapsulate vectors in actual collections and set unpacker pointers
00286 
00287   // create the collection of Ecal Digis
00288   std::auto_ptr<EBDigiCollection> productDigisEB(new EBDigiCollection);
00289   productDigisEB->reserve(1700);
00290   theUnpacker_->setEBDigisCollection(&productDigisEB);
00291   
00292   // create the collection of Ecal Digis
00293   std::auto_ptr<EEDigiCollection> productDigisEE(new EEDigiCollection);
00294   theUnpacker_->setEEDigisCollection(&productDigisEE);
00295   
00296   // create the collection for headers
00297   std::auto_ptr<EcalRawDataCollection> productDccHeaders(new EcalRawDataCollection);
00298   theUnpacker_->setDccHeadersCollection(&productDccHeaders); 
00299 
00300   // create the collection for invalid gains
00301   std::auto_ptr< EBDetIdCollection> productInvalidGains(new EBDetIdCollection);
00302   theUnpacker_->setInvalidGainsCollection(&productInvalidGains); 
00303 
00304   // create the collection for invalid gain Switch
00305   std::auto_ptr< EBDetIdCollection> productInvalidGainsSwitch(new EBDetIdCollection);
00306   theUnpacker_->setInvalidGainsSwitchCollection(&productInvalidGainsSwitch);
00307    
00308   // create the collection for invalid chids
00309   std::auto_ptr< EBDetIdCollection> productInvalidChIds(new EBDetIdCollection);
00310   theUnpacker_->setInvalidChIdsCollection(&productInvalidChIds);
00311   
00313     
00314   // create the collection for invalid gains
00315   std::auto_ptr<EEDetIdCollection> productInvalidEEGains(new EEDetIdCollection);
00316   theUnpacker_->setInvalidEEGainsCollection(&productInvalidEEGains); 
00317     
00318   // create the collection for invalid gain Switch
00319   std::auto_ptr<EEDetIdCollection> productInvalidEEGainsSwitch(new EEDetIdCollection);
00320   theUnpacker_->setInvalidEEGainsSwitchCollection(&productInvalidEEGainsSwitch);
00321     
00322   // create the collection for invalid chids
00323   std::auto_ptr<EEDetIdCollection> productInvalidEEChIds(new EEDetIdCollection);
00324   theUnpacker_->setInvalidEEChIdsCollection(&productInvalidEEChIds);
00325 
00327 
00328   // create the collection for EB srflags       
00329   std::auto_ptr<EBSrFlagCollection> productEBSrFlags(new EBSrFlagCollection);
00330   theUnpacker_->setEBSrFlagsCollection(&productEBSrFlags);
00331   
00332   // create the collection for EB srflags       
00333   std::auto_ptr<EESrFlagCollection> productEESrFlags(new EESrFlagCollection);
00334   theUnpacker_->setEESrFlagsCollection(&productEESrFlags);
00335 
00336   // create the collection for ecal trigger primitives
00337   std::auto_ptr<EcalTrigPrimDigiCollection> productEcalTps(new EcalTrigPrimDigiCollection);
00338   theUnpacker_->setEcalTpsCollection(&productEcalTps);
00340 
00341   // create the collection for ecal trigger primitives
00342   std::auto_ptr<EcalPSInputDigiCollection> productEcalPSs(new EcalPSInputDigiCollection);
00343   theUnpacker_->setEcalPSsCollection(&productEcalPSs);
00345 
00346   // create the collection for invalid TTIds
00347   std::auto_ptr<EcalElectronicsIdCollection> productInvalidTTIds(new EcalElectronicsIdCollection);
00348   theUnpacker_->setInvalidTTIdsCollection(&productInvalidTTIds);
00349  
00350    // create the collection for invalid TTIds
00351   std::auto_ptr<EcalElectronicsIdCollection> productInvalidZSXtalIds(new EcalElectronicsIdCollection);
00352   theUnpacker_->setInvalidZSXtalIdsCollection(&productInvalidZSXtalIds);
00353 
00354 
00355  
00356   // create the collection for invalid BlockLengths
00357   std::auto_ptr<EcalElectronicsIdCollection> productInvalidBlockLengths(new EcalElectronicsIdCollection);
00358   theUnpacker_->setInvalidBlockLengthsCollection(&productInvalidBlockLengths);
00359 
00360   // MEMs Collections
00361   // create the collection for the Pn Diode Digis
00362   std::auto_ptr<EcalPnDiodeDigiCollection> productPnDiodeDigis(new EcalPnDiodeDigiCollection);
00363   theUnpacker_->setPnDiodeDigisCollection(&productPnDiodeDigis);
00364 
00365   // create the collection for invalid Mem Tt id 
00366   std::auto_ptr<EcalElectronicsIdCollection> productInvalidMemTtIds(new EcalElectronicsIdCollection);
00367   theUnpacker_->setInvalidMemTtIdsCollection(& productInvalidMemTtIds);
00368   
00369   // create the collection for invalid Mem Block Size 
00370   std::auto_ptr<EcalElectronicsIdCollection> productInvalidMemBlockSizes(new EcalElectronicsIdCollection);
00371   theUnpacker_->setInvalidMemBlockSizesCollection(& productInvalidMemBlockSizes);
00372   
00373   // create the collection for invalid Mem Block Size 
00374   std::auto_ptr<EcalElectronicsIdCollection> productInvalidMemChIds(new EcalElectronicsIdCollection);
00375   theUnpacker_->setInvalidMemChIdsCollection(& productInvalidMemChIds);
00376  
00377   // create the collection for invalid Mem Gain Errors 
00378   std::auto_ptr<EcalElectronicsIdCollection> productInvalidMemGains(new EcalElectronicsIdCollection);
00379   theUnpacker_->setInvalidMemGainsCollection(& productInvalidMemGains); 
00380   //  double TIME_START = clock(); 
00381   
00382 
00383   // Step C: unpack all requested FEDs    
00384   for (std::vector<int>::const_iterator i=fedUnpackList_.begin(); i!=fedUnpackList_.end(); i++) {
00385 
00386     if (REGIONAL_) {
00387       std::vector<int>::const_iterator fed_it = find(FEDS_to_unpack.begin(), FEDS_to_unpack.end(), *i);
00388       if (fed_it == FEDS_to_unpack.end()) continue;
00389     }
00390 
00391   
00392     // get fed raw data and SM id
00393     const FEDRawData & fedData = rawdata->FEDData(*i);
00394     int length = fedData.size();
00395 
00396     LogDebug("EcalRawToDigi") << "raw data length: " << length ;
00397     //if data size is not null interpret data
00398     if ( length >= EMPTYEVENTSIZE ){
00399       
00400       if(myMap_->setActiveDCC(*i)){
00401 
00402         int smId = myMap_->getActiveSM();
00403         LogDebug("EcalRawToDigi") << "Getting FED = " << *i <<"(SM = "<<smId<<")"<<" data size is: " << length;
00404 
00405         uint64_t * pData = (uint64_t *)(fedData.data());
00406         theUnpacker_->unpack( pData, static_cast<unsigned int>(length),smId,*i);
00407       }
00408     }
00409     
00410   }// loop on FEDs
00411   
00412   //if(nevts_>1){   //NUNO
00413   //  double TIME_END = clock(); //NUNO
00414   //  RUNNING_TIME_ += TIME_END-TIME_START; //NUNO
00415   // }
00416   
00417   
00418   // Add collections to the event 
00419   
00420   if(put_){
00421     
00422     if( headerUnpacking_){ 
00423       e.put(productDccHeaders); 
00424     }
00425     
00426     if(feUnpacking_){
00427       productDigisEB->sort();
00428       e.put(productDigisEB,"ebDigis");
00429       productDigisEE->sort();
00430       e.put(productDigisEE,"eeDigis");
00431       e.put(productInvalidGains,"EcalIntegrityGainErrors");
00432       e.put(productInvalidGainsSwitch, "EcalIntegrityGainSwitchErrors");
00433       e.put(productInvalidChIds, "EcalIntegrityChIdErrors");
00434       // EE (leaving for now the same names as in EB)
00435       e.put(productInvalidEEGains,"EcalIntegrityGainErrors");
00436       e.put(productInvalidEEGainsSwitch, "EcalIntegrityGainSwitchErrors");
00437       e.put(productInvalidEEChIds, "EcalIntegrityChIdErrors");
00438       // EE
00439       e.put(productInvalidTTIds,"EcalIntegrityTTIdErrors");
00440       e.put(productInvalidZSXtalIds,"EcalIntegrityZSXtalIdErrors");
00441       e.put(productInvalidBlockLengths,"EcalIntegrityBlockSizeErrors");
00442       e.put(productPnDiodeDigis);
00443     }
00444     if(memUnpacking_){
00445       e.put(productInvalidMemTtIds,"EcalIntegrityMemTtIdErrors");
00446       e.put(productInvalidMemBlockSizes,"EcalIntegrityMemBlockSizeErrors");
00447       e.put(productInvalidMemChIds,"EcalIntegrityMemChIdErrors");
00448       e.put(productInvalidMemGains,"EcalIntegrityMemGainErrors");
00449     }
00450     if(srpUnpacking_){
00451       e.put(productEBSrFlags);
00452       e.put(productEESrFlags);
00453     }
00454     if(tccUnpacking_){
00455       e.put(productEcalTps,"EcalTriggerPrimitives");
00456       e.put(productEcalPSs,"EcalPseudoStripInputs");
00457     }
00458   }
00459   
00460 //if(nevts_>1){   //NUNO
00461 //  double TIME_END = clock(); //NUNO 
00462 //  RUNNING_TIME_ += TIME_END-TIME_START; //NUNO
00463 //}
00464   
00465 }
00466 
00467 EcalRawToDigi::~EcalRawToDigi()
00468 {
00469   
00470   //cout << "EcalDCCUnpackingModule  " << "N events        " << (nevts_-1)<<endl;
00471   //cout << "EcalDCCUnpackingModule  " << " --- SETUP time " << endl;
00472   //cout << "EcalDCCUnpackingModule  " << "Time (sys)      " << SETUP_TIME_ << endl;
00473   //cout << "EcalDCCUnpackingModule  " << "Time in sec.    " << SETUP_TIME_/ CLOCKS_PER_SEC  << endl;
00474   //cout << "EcalDCCUnpackingModule  " << " --- Per event  " << endl;
00475   
00476   //RUNNING_TIME_ = RUNNING_TIME_ / (nevts_-1);
00477   
00478   //cout << "EcalDCCUnpackingModule  "<< "Time (sys)      " << RUNNING_TIME_  << endl;
00479   //cout << "EcalDCCUnpackingModule  "<< "Time in sec.    " << RUNNING_TIME_ / CLOCKS_PER_SEC  << endl;
00480   
00481   
00482   
00483   if(myMap_      ) delete myMap_;
00484   if(theUnpacker_) delete theUnpacker_;
00485   
00486 }