CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CaloOnlineTools/EcalTools/plugins/EcalPedHists.cc

Go to the documentation of this file.
00001 
00011 #include "CaloOnlineTools/EcalTools/plugins/EcalPedHists.h"
00012 
00013 EcalPedHists::EcalPedHists(const edm::ParameterSet& ps) :
00014   runNum_(-1),
00015   fileName_ (ps.getUntrackedParameter<std::string>("fileName", std::string("ecalPedDigiDump"))),
00016   barrelDigiCollection_ (ps.getParameter<edm::InputTag> ("EBdigiCollection")),
00017   endcapDigiCollection_ (ps.getParameter<edm::InputTag> ("EEdigiCollection")),
00018   headerProducer_ (ps.getParameter<edm::InputTag> ("headerProducer"))
00019 {
00020   using namespace std;
00021 
00022   fedMap_ = new EcalFedMap();
00023   histsFilled_ = false;
00024   //for(int i=601; i<655; ++i)
00025   //{
00026   //  listDefaults.push_back(i);
00027   //} 
00028   listFEDs_ = ps.getUntrackedParameter<vector<int> >("listFEDs");
00029   listEBs_ = ps.getUntrackedParameter<vector<string> >("listEBs");
00030  
00031   if(listFEDs_.size()==0)
00032   {
00033     allFEDsSelected_ = false;
00034     //if "actual" EB id given, then convert to FEDid and put in listFEDs_
00035     if(listEBs_.size() > 0)
00036     {
00037       listFEDs_.clear();
00038       for(vector<string>::const_iterator itr = listEBs_.begin(); itr != listEBs_.end(); ++itr)
00039       {
00040         listFEDs_.push_back(fedMap_->getFedFromSlice(*itr));
00041       }
00042     }
00043   }
00044   else if(listFEDs_[0]==-1)
00045   {
00046   // Apply no selection if -1 is passed in FED list
00047     allFEDsSelected_ = true;
00048     //debug
00049     //cout << "no selection on FEDs!" << endl;
00050     //inputIsOk_=false;
00051     //return;
00052     //listFEDs_ = listDefaults;
00053   }
00054   else
00055   {
00056     //in this case, listFEDs should be populated
00057     allFEDsSelected_ = false;
00058   }
00059 
00060   if(!allFEDsSelected_)
00061   {
00062     // Verify FED numbers are valid
00063     for (vector<int>::const_iterator intIter = listFEDs_.begin(); intIter != listFEDs_.end(); intIter++)
00064     {  
00065       if ( ((*intIter) < 601)||(654 < (*intIter)) )
00066       {  
00067         cout << "[EcalPedHists] FED value: " << (*intIter) << " found in listFEDs. "
00068           << " Valid range is 601-654. Returning." << endl;
00069         inputIsOk_ = false;
00070         return;
00071       }
00072       else
00073         theRealFedSet_.insert(*intIter);
00074     }
00075   }
00076 
00077   vector<int> listDefaults = vector<int>();
00078   listDefaults.clear();
00079   for(int i=1; i<1701; ++i)
00080   {
00081     listDefaults.push_back(i);
00082   }
00083   listChannels_ = ps.getUntrackedParameter<vector<int> >("listChannels", listDefaults);
00084   listDefaults.clear();
00085   // Get samples to plot (default to 1,2,3)
00086   listDefaults.push_back(0);
00087   listDefaults.push_back(1);
00088   listDefaults.push_back(2);
00089   listSamples_ = ps.getUntrackedParameter<vector<int> >("listSamples", listDefaults);
00090   
00091   inputIsOk_ = true;
00092   vector<int>::iterator intIter;
00093   
00094   // Verify crystal numbers are valid
00095   for (intIter = listChannels_.begin(); intIter != listChannels_.end(); ++intIter)
00096   { 
00097       //TODO: Fix crystal index checking?
00098       //if ( ((*intIter) < 1)||(1700 < (*intIter)) )       
00099       //{  
00100       //  cout << "[EcalPedHists] ic value: " << (*intIter) << " found in listChannels. "
00101       //          << " Valid range is 1-1700. Returning." << endl;
00102       //  inputIsOk_ = false;
00103       //  return;
00104       //}
00105   }
00106   // Verify sample numbers are valid
00107   for (intIter = listSamples_.begin(); intIter != listSamples_.end(); intIter++)
00108   {  
00109       if ( ((*intIter) < 1)||(10 < (*intIter)) )
00110       {  
00111         cout << "[EcalPedHists] sample number: " << (*intIter) << " found in listSamples. "
00112                   << " Valid range is 1-10. Returning." << endl;
00113         inputIsOk_ = false;
00114         return;
00115       }
00116   }
00117 
00118 }
00119 
00120 EcalPedHists::~EcalPedHists() 
00121 {
00122 }
00123 
00124 void EcalPedHists::beginRun(edm::Run const &, edm::EventSetup const & c)
00125 {
00126   edm::ESHandle<EcalElectronicsMapping> elecHandle;
00127   c.get<EcalMappingRcd>().get(elecHandle);
00128   ecalElectronicsMap_ = elecHandle.product();
00129 }
00130 
00131 void EcalPedHists::endJob(void)
00132 {
00133   using namespace std;
00134   if(inputIsOk_)
00135   { 
00136     //debug
00137     //cout << "endJob:creating root file!" << endl;
00138     
00139     fileName_ += "-"+intToString(runNum_)+".graph.root";
00140 
00141     TFile root_file_(fileName_.c_str() , "RECREATE");
00142     //Loop over FEDs first
00143     for(set<int>::const_iterator FEDitr = theRealFedSet_.begin(); FEDitr != theRealFedSet_.end(); ++FEDitr)
00144     {
00145       if(!histsFilled_)
00146         break;
00147       string dir = fedMap_->getSliceFromFed(*FEDitr);
00148       TDirectory* FEDdir = gDirectory->mkdir(dir.c_str());
00149       FEDdir->cd();
00150       //root_file_.mkdir(dir.c_str());
00151       //root_file_.cd(dir.c_str());
00152       map<string,TH1F*> mapHistos = FEDsAndHistMaps_[*FEDitr];
00153       
00154       //Loop over channels; write histos and directory structure
00155       for (vector<int>::const_iterator itr = listChannels_.begin(); itr!=listChannels_.end(); itr++)
00156       {
00157         //debug
00158         //cout << "loop over channels" << endl;
00159         
00160         TH1F* hist = 0;
00161         string chnl = intToString(*itr);
00162         string name1 = "Cry";
00163         name1.append(chnl+"Gain1");
00164         string name2 = "Cry";
00165         name2.append(chnl+"Gain6");
00166         string name3 = "Cry";
00167         name3.append(chnl+"Gain12");
00168         hist = mapHistos[name1];
00169         // This is a sanity check only
00170         if(hist!=0)
00171         {
00172           string cryDirName = "Cry_"+chnl;
00173           TDirectory* cryDir = FEDdir->mkdir(cryDirName.c_str());
00174           cryDir->cd();
00175           hist->SetDirectory(cryDir);
00176           hist->Write();
00177           hist = mapHistos[name2];
00178           hist->SetDirectory(cryDir);
00179           hist->Write();
00180           hist = mapHistos[name3];
00181           hist->SetDirectory(cryDir);
00182           hist->Write();
00183           //root_file_.cd(dir.c_str());
00184           root_file_.cd();
00185         }
00186         else
00187         {
00188           cerr << "EcalPedHists: Error: This shouldn't happen!" << endl;
00189         }
00190       }
00191       root_file_.cd();
00192     }
00193     root_file_.Close();
00194   }
00195 }
00196 
00197 void EcalPedHists::analyze(const edm::Event& e, const edm::EventSetup& c)
00198 {
00199   using namespace std;
00200   using namespace edm;
00201 
00202   if (!inputIsOk_)
00203     return;
00204   
00205   // loop over the headers, this is to detect missing FEDs if all are selected
00206   if(allFEDsSelected_)
00207   {
00208     edm::Handle<EcalRawDataCollection> DCCHeaders;
00209     try {
00210       e.getByLabel (headerProducer_, DCCHeaders);
00211     } catch ( std::exception& ex ) {
00212       edm::LogError ("EcalPedHists") << "Error! can't get the product " 
00213         << headerProducer_;
00214       return;
00215     }
00216 
00217     for (EcalRawDataCollection::const_iterator headerItr= DCCHeaders->begin();
00218         headerItr != DCCHeaders->end (); 
00219         ++headerItr) 
00220     {
00221       int FEDid = 600+headerItr->id();
00222       theRealFedSet_.insert(FEDid);
00223     }
00224   }
00225 
00226   // loop over fed list and make sure that there are histo maps
00227   for(set<int>::const_iterator fedItr = theRealFedSet_.begin(); fedItr != theRealFedSet_.end(); ++fedItr)
00228   {
00229     if(FEDsAndHistMaps_.find(*fedItr)==FEDsAndHistMaps_.end())
00230       initHists(*fedItr);
00231   }
00232   
00233   //debug
00234   //cout << "analyze...input is ok? " << inputIsOk_ << endl;
00235   
00236   bool barrelDigisFound = true;
00237   bool endcapDigisFound = true;
00238   // get the barrel digis
00239   // (one digi for each crystal)
00240   // TODO; SIC: fix this behavior
00241   Handle<EBDigiCollection> barrelDigis;
00242   try {
00243     e.getByLabel (barrelDigiCollection_, barrelDigis);
00244   } catch ( std::exception& ex ) 
00245   {
00246     edm::LogError ("EcalPedOffset") << "Error! can't get the product " 
00247       << barrelDigiCollection_;
00248     barrelDigisFound = false;
00249   }
00250   // get the endcap digis
00251   // (one digi for each crystal)
00252   // TODO; SIC: fix this behavior
00253   Handle<EEDigiCollection> endcapDigis;
00254   try {
00255     e.getByLabel (endcapDigiCollection_, endcapDigis);
00256   } catch ( std::exception& ex ) 
00257   {
00258     edm::LogError ("EcalPedOffset") << "Error! can't get the product " 
00259       << endcapDigiCollection_;
00260     endcapDigisFound = false;
00261   }
00262   
00263   if(barrelDigisFound)
00264     readEBdigis(barrelDigis);
00265   if(endcapDigisFound)
00266     readEEdigis(endcapDigis);
00267   if(!barrelDigisFound && !endcapDigisFound)
00268     edm::LogError ("EcalPedOffset") << "No digis found in the event!";
00269   
00270   if(runNum_==-1)
00271     runNum_ = e.id().run();
00272 }
00273 
00274 // insert the 3-entry hist map into the map keyed by FED number
00275 void EcalPedHists::initHists(int FED)
00276 {
00277   using namespace std;
00278   //using namespace edm;
00279 
00280   std::map<string,TH1F*> histMap;
00281   //debug
00282   //cout << "Initializing map for FED:" << *FEDitr << endl;
00283   for (vector<int>::const_iterator intIter = listChannels_.begin(); intIter != listChannels_.end(); ++intIter)
00284   { 
00285     //Put 3 histos (1 per gain) for the channel into the map
00286     string FEDid = intToString(FED);
00287     string chnl = intToString(*intIter);
00288     string title1 = "Gain1 ADC Counts for channel ";
00289     title1.append(chnl);
00290     string name1 = "Cry";
00291     name1.append(chnl+"Gain1");
00292     string title2 = "Gain6 ADC Counts for channel ";
00293     title2.append(chnl);
00294     string name2 = "Cry";
00295     name2.append(chnl+"Gain6");
00296     string title3 = "Gain12 ADC Counts for channel ";
00297     title3.append(chnl);
00298     string name3 = "Cry";
00299     name3.append(chnl+"Gain12");
00300     histMap.insert(make_pair(name1,new TH1F(name1.c_str(),title1.c_str(),75,175.0,250.0)));
00301     histMap[name1]->SetDirectory(0);
00302     histMap.insert(make_pair(name2,new TH1F(name2.c_str(),title2.c_str(),75,175.0,250.0)));
00303     histMap[name2]->SetDirectory(0);
00304     histMap.insert(make_pair(name3,new TH1F(name3.c_str(),title3.c_str(),75,175.0,250.0)));
00305     histMap[name3]->SetDirectory(0);
00306   }
00307   FEDsAndHistMaps_.insert(make_pair(FED,histMap));
00308 }
00309 
00310 
00311 void EcalPedHists::readEBdigis(edm::Handle<EBDigiCollection> digis)
00312 {
00313   using namespace std;
00314   using namespace edm;
00315   //debug
00316   //cout << "readEBdigis" << endl;
00317   
00318   // Loop over digis
00319   for (EBDigiCollection::const_iterator digiItr= digis->begin();digiItr != digis->end(); ++digiItr )
00320   {
00321     EBDetId detId = EBDetId(digiItr->id());
00322     EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId(detId);
00323     int FEDid = 600+elecId.dccId();
00324     int crystalId = detId.ic();
00325 
00326     //debug
00327     //cout << "FEDid:" << FEDid << " cryId:" << crystalId << endl;
00328     //cout << "FEDid:" << FEDid << endl;
00329     //Select desired supermodules only
00330     set<int>::const_iterator fedIter = find(theRealFedSet_.begin(), theRealFedSet_.end(), FEDid);
00331     if (fedIter == theRealFedSet_.end())
00332       continue;
00333 
00334     // Select desired channels only
00335     vector<int>::iterator icIter;
00336     icIter = find(listChannels_.begin(), listChannels_.end(), crystalId);
00337     if (icIter == listChannels_.end())
00338       continue;
00339 
00340     // Get the adc counts from the selected samples and fill the corresponding histogram
00341     // Must subtract 1 from user-given sample list (e.g., user's sample 1 -> sample 0)
00342     for (vector<int>::iterator itr = listSamples_.begin(); itr!=listSamples_.end(); itr++)
00343     {
00344       histsFilled_ = true;
00345       map<string,TH1F*> mapHistos = FEDsAndHistMaps_[FEDid];
00346       string chnl = intToString(crystalId);
00347       string name1 = "Cry";
00348       name1.append(chnl+"Gain1");
00349       string name2 = "Cry";
00350       name2.append(chnl+"Gain6");
00351       string name3 = "Cry";
00352       name3.append(chnl+"Gain12");
00353       TH1F* hist = 0;
00354       if(((EBDataFrame)(*digiItr)).sample(*itr-1).gainId()==3)
00355         hist = mapHistos[name1];
00356       if(((EBDataFrame)(*digiItr)).sample(*itr-1).gainId()==2)
00357         hist = mapHistos[name2];
00358       if(((EBDataFrame)(*digiItr)).sample(*itr-1).gainId()==1)
00359         hist = mapHistos[name3];
00360       if(hist!=0)
00361         hist->Fill(((EBDataFrame)(*digiItr)).sample(*itr-1).adc());
00362       else
00363         cerr << "EcalPedHistDumper: Error: This shouldn't happen!" << endl;
00364     }
00365   }
00366 }
00367 
00368 
00369 void EcalPedHists::readEEdigis(edm::Handle<EEDigiCollection> digis)
00370 {
00371   using namespace std;
00372   using namespace edm;
00373   //debug
00374   //cout << "readEEdigis" << endl;
00375   
00376   // Loop over digis
00377   for (EEDigiCollection::const_iterator digiItr= digis->begin();digiItr != digis->end(); ++digiItr )
00378   {
00379     EEDetId detId = EEDetId(digiItr->id());
00380     EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId(detId);
00381     int FEDid = 600+elecId.dccId();
00382     int crystalId = 10000*FEDid+100*elecId.towerId()+5*(elecId.stripId()-1)+elecId.xtalId();
00383 
00384     //Select desired FEDs only
00385     set<int>::const_iterator fedIter = find(theRealFedSet_.begin(), theRealFedSet_.end(), FEDid);
00386     if (fedIter == theRealFedSet_.end())
00387       continue;
00388 
00389     // Select desired channels only
00390     vector<int>::iterator icIter;
00391     icIter = find(listChannels_.begin(), listChannels_.end(), crystalId);
00392     if (icIter == listChannels_.end())
00393       continue;
00394 
00395     // Get the adc counts from the selected samples and fill the corresponding histogram
00396     // Must subtract 1 from user-given sample list (e.g., user's sample 1 -> sample 0)
00397     for (vector<int>::iterator itr = listSamples_.begin(); itr!=listSamples_.end(); itr++)
00398     {
00399       histsFilled_ = true;
00400       map<string,TH1F*> mapHistos = FEDsAndHistMaps_[FEDid];
00401       string chnl = intToString(crystalId);
00402       string name1 = "Cry";
00403       name1.append(chnl+"Gain1");
00404       string name2 = "Cry";
00405       name2.append(chnl+"Gain6");
00406       string name3 = "Cry";
00407       name3.append(chnl+"Gain12");
00408       TH1F* hist = 0;
00409       if(((EBDataFrame)(*digiItr)).sample(*itr-1).gainId()==3)
00410         hist = mapHistos[name1];
00411       if(((EBDataFrame)(*digiItr)).sample(*itr-1).gainId()==2)
00412         hist = mapHistos[name2];
00413       if(((EBDataFrame)(*digiItr)).sample(*itr-1).gainId()==1)
00414         hist = mapHistos[name3];
00415       if(hist!=0)
00416         hist->Fill(((EBDataFrame)(*digiItr)).sample(*itr-1).adc());
00417       else
00418         cerr << "EcalPedHistDumper: Error: This shouldn't happen!" << endl;
00419     }
00420   }
00421 }
00422 
00423 
00424 std::string EcalPedHists::intToString(int num)
00425 {
00426   using namespace std;
00427   //
00428   // outputs the number into the string stream and then flushes
00429   // the buffer (makes sure the output is put into the stream)
00430   //
00431   ostringstream myStream; //creates an ostringstream object
00432   myStream << num << flush;
00433   return(myStream.str()); //returns the string form of the stringstream object
00434 }
00435 
00436