CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoTBCalo/HcalTBObjectUnpacker/plugins/HcalTBObjectUnpacker.cc

Go to the documentation of this file.
00001 using namespace std;
00002 
00003 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00004 #include "TBDataFormats/HcalTBObjects/interface/HcalTBTriggerData.h"
00005 #include "TBDataFormats/HcalTBObjects/interface/HcalTBRunData.h"
00006 #include "TBDataFormats/HcalTBObjects/interface/HcalTBEventPosition.h"
00007 #include "TBDataFormats/HcalTBObjects/interface/HcalTBTiming.h"
00008 #include "TBDataFormats/HcalTBObjects/interface/HcalTBBeamCounters.h"
00009 #include "RecoTBCalo/HcalTBObjectUnpacker/plugins/HcalTBObjectUnpacker.h"
00010 #include "DataFormats/Common/interface/EDCollection.h"
00011 #include "DataFormats/Common/interface/Handle.h"
00012 #include "FWCore/Framework/interface/Selector.h"
00013 #include <iostream>
00014 #include <fstream>
00015 
00016 
00017   HcalTBObjectUnpacker::HcalTBObjectUnpacker(edm::ParameterSet const& conf):
00018     triggerFed_(conf.getUntrackedParameter<int>("HcalTriggerFED",-1)),
00019     sdFed_(conf.getUntrackedParameter<int>("HcalSlowDataFED",-1)),
00020     spdFed_(conf.getUntrackedParameter<int>("HcalSourcePositionFED",-1)),
00021     tdcFed_(conf.getUntrackedParameter<int>("HcalTDCFED",-1)),
00022     qadcFed_(conf.getUntrackedParameter<int>("HcalQADCFED",-1)),
00023     calibFile_(conf.getUntrackedParameter<string>("ConfigurationFile","")),
00024     tdcUnpacker_(conf.getUntrackedParameter<bool>("IncludeUnmatchedHits",false)),
00025     doRunData_(false),doTriggerData_(false),doEventPosition_(false),doTiming_(false),doSourcePos_(false),doBeamADC_(false)
00026   {
00027 
00028     if (triggerFed_ >=0) {
00029       std::cout << "HcalTBObjectUnpacker will unpack Trigger FED ";
00030       std::cout << triggerFed_ << endl;
00031       doTriggerData_=true;
00032     }
00033 
00034     if (sdFed_ >=0) {
00035       std::cout << "HcalTBObjectUnpacker will unpack SlowData FED ";
00036       std::cout << sdFed_ << endl;
00037       doRunData_=true;
00038       doEventPosition_=true; // at least the table
00039     }
00040 
00041     if (tdcFed_ >=0) {
00042       std::cout << "HcalTBObjectUnpacker will unpack TDC FED ";
00043       std::cout << tdcFed_ << endl;
00044       doTiming_=true;
00045       doEventPosition_=true; // at least the WC
00046     }
00047 
00048     if (qadcFed_ >=0) {
00049       std::cout << "HcalTBObjectUnpacker will unpack QADC FED ";
00050       std::cout << qadcFed_ << endl;
00051       doBeamADC_=true;
00052     }
00053 
00054     if (spdFed_ >=0) {
00055       std::cout << "HcalTBObjectUnpacker will unpack Source Position Data FED ";
00056       std::cout << spdFed_ << endl;
00057       doSourcePos_=true;
00058     }
00059 
00060 
00061     if (tdcFed_ >= 0 || qadcFed_ >=0 ) {
00062       calibLines_.clear();
00063       if(calibFile_.size()>0){
00064         
00065         parseCalib();
00066         //  printf("I got %d lines!\n",calibLines_.size());
00067         if(calibLines_.size()==0)
00068           throw cms::Exception("Incomplete configuration") << 
00069             "HcalTBObjectUnpacker: TDC/QADC/WC configuration file not found or is empty: "<<calibFile_<<endl;
00070       }
00071       else{
00072         throw cms::Exception("Incomplete configuration") << 
00073           "HcalTBObjectUnpacker: TDC/QADC/WC configuration file not found: "<<calibFile_<<endl;
00074       }
00075     }
00076 
00077 
00078     if (doTriggerData_) produces<HcalTBTriggerData>();
00079     if (doRunData_) produces<HcalTBRunData>();
00080     if (doEventPosition_) produces<HcalTBEventPosition>();
00081     if (doTiming_) produces<HcalTBTiming>();
00082 //    if (doBeamADC_) produces<HcalTBBeamCounters>();
00083     if (doBeamADC_) {produces<HcalTBBeamCounters>();qadcUnpacker_.setCalib(calibLines_);}
00084 //    if (doSourcePos_) produces<HcalSourcePositionData>();
00085     if(doTiming_||doEventPosition_)tdcUnpacker_.setCalib(calibLines_);
00086   }
00087 
00088   // Virtual destructor needed.
00089   HcalTBObjectUnpacker::~HcalTBObjectUnpacker() { }  
00090 
00091   // Functions that gets called by framework every event
00092   void HcalTBObjectUnpacker::produce(edm::Event& e, const edm::EventSetup&)
00093   {
00094     // Step A: Get Inputs 
00095     edm::Handle<FEDRawDataCollection> rawraw;  
00096     //    edm::ProcessNameSelector s("PROD"); // HACK!
00097     e.getByType(rawraw);           
00098 
00099     // Step B: Create empty output    
00100     std::auto_ptr<HcalTBTriggerData>
00101       trigd(new HcalTBTriggerData);
00102 
00103     std::auto_ptr<HcalTBRunData>
00104       rund(new HcalTBRunData);
00105 
00106     std::auto_ptr<HcalTBEventPosition>
00107       epd(new HcalTBEventPosition);
00108 
00109     std::auto_ptr<HcalTBTiming>
00110       tmgd(new HcalTBTiming);
00111 
00112     std::auto_ptr<HcalTBBeamCounters>
00113       bcntd(new HcalTBBeamCounters);
00114 
00115     std::auto_ptr<HcalSourcePositionData>
00116       spd(new HcalSourcePositionData);
00117     
00118     if (triggerFed_ >=0) {
00119       // Step C: unpack all requested FEDs
00120       const FEDRawData& fed = rawraw->FEDData(triggerFed_);
00121       tdUnpacker_.unpack(fed,*trigd);
00122     }
00123 
00124     if (sdFed_ >=0) {
00125       // Step C: unpack all requested FEDs
00126       const FEDRawData& fed = rawraw->FEDData(sdFed_);
00127       sdUnpacker_.unpack(fed, *rund, *epd);
00128     }
00129 
00130     if (tdcFed_ >=0) {
00131       // Step C: unpack all requested FEDs
00132       const FEDRawData& fed = rawraw->FEDData(tdcFed_);
00133       tdcUnpacker_.unpack(fed, *epd, *tmgd);
00134     }
00135 
00136     if (qadcFed_ >=0) {
00137       // Step C: unpack all requested FEDs
00138       const FEDRawData& fed = rawraw->FEDData(qadcFed_);
00139       bool is04 = true;
00140       if(qadcFed_==8) is04=false;
00141       qadcUnpacker_.unpack(fed, *bcntd,is04);
00142     }
00143 
00144     if (spdFed_ >=0) {      
00145       // Step C: unpack all requested FEDs
00146       const FEDRawData& fed = rawraw->FEDData(spdFed_);
00147       spdUnpacker_.unpack(fed, *spd);
00148     }
00149 
00150     // Step D: Put outputs into event
00151     if (doTriggerData_) e.put(trigd);
00152     if (doRunData_) e.put(rund);
00153     if (doEventPosition_) e.put(epd);
00154     if (doTiming_) e.put(tmgd);
00155     if (doBeamADC_) e.put(bcntd);
00156     if (doSourcePos_) e.put(spd);
00157   }
00158 
00159 
00160 void HcalTBObjectUnpacker::parseCalib(){
00161   
00162   if(calibFile_.size()==0){
00163     printf("HcalTBObjectUnpacker cowardly refuses to parse a NULL file...\n"); 
00164     return;
00165   }
00166 
00167   edm::FileInPath fip(calibFile_);
00168   
00169  
00170   ifstream infile(fip.fullPath().c_str());
00171 
00172   char buffer [1024];
00173   string tmpStr;
00174   
00175   while (infile.getline(buffer, 1024)) {
00176     if (buffer [0] == '#') continue; //ignore comment
00177     if (buffer [0] == '/' && buffer [1] == '/') continue; //ignore comment
00178     tmpStr = string(buffer);
00179     vector<string> lineVect;
00180     
00181     int start = 0; bool empty = true;
00182     for (unsigned i=0; i<=tmpStr.size(); i++) {
00183       if (tmpStr[i] == ' ' || i==tmpStr.size()) {
00184         if (!empty) {
00185           std::string item(tmpStr, start, i-start);
00186           lineVect.push_back(item);
00187           empty = true;
00188 //        printf("Got: %s\n",item.c_str());
00189         }
00190         start = i+1;
00191       }
00192       else {
00193         if (empty) empty = false;       
00194       }
00195     }  
00196     
00197     if(lineVect.size()>0) calibLines_.push_back(lineVect);    
00198   }
00199 
00200   infile.close();
00201   return;
00202 }
00203 
00204 #include "FWCore/PluginManager/interface/ModuleDef.h"
00205 #include "FWCore/Framework/interface/MakerMacros.h"
00206 
00207 DEFINE_FWK_MODULE(HcalTBObjectUnpacker);