CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CaloOnlineTools/EcalTools/plugins/EcalMipGraphs.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:   EcalMipGraphs 
00004 // Class:     EcalMipGraphs 
00005 // 
00013 //
00014 // Original Author:  Seth COOPER
00015 //         Created:  Th Nov 22 5:46:22 CEST 2007
00016 // $Id: EcalMipGraphs.cc,v 1.12 2010/10/20 10:02:00 elmer Exp $
00017 //
00018 //
00019 
00020 #include "CaloOnlineTools/EcalTools/plugins/EcalMipGraphs.h"
00021 #include "RecoCaloTools/Navigation/interface/CaloNavigator.h"
00022 #include "TCanvas.h"
00023 #include <utility>
00024 #include <string>
00025 #include <vector>
00026 
00027 using namespace edm;
00028 using namespace std;
00029 
00030 //
00031 // constants, enums and typedefs
00032 //
00033 
00034 //
00035 // static data member definitions
00036 //
00037 float EcalMipGraphs::gainRatio[3] = { 1., 2. , 12. }; 
00038 edm::Service<TFileService> EcalMipGraphs::fileService;
00039 
00040 //
00041 // constructors and destructor
00042 //
00043 EcalMipGraphs::EcalMipGraphs(const edm::ParameterSet& iConfig) :
00044   EBRecHitCollection_ (iConfig.getParameter<edm::InputTag>("EcalRecHitCollectionEB")),
00045   EERecHitCollection_ (iConfig.getParameter<edm::InputTag>("EcalRecHitCollectionEE")),
00046   EBDigis_ (iConfig.getParameter<edm::InputTag>("EBDigiCollection")),
00047   EEDigis_ (iConfig.getParameter<edm::InputTag>("EEDigiCollection")),
00048   headerProducer_ (iConfig.getParameter<edm::InputTag> ("headerProducer")),
00049   runNum_(-1),
00050   side_ (iConfig.getUntrackedParameter<int>("side", 3)),
00051   threshold_ (iConfig.getUntrackedParameter<double>("amplitudeThreshold", 12.0)),
00052   minTimingAmp_ (iConfig.getUntrackedParameter<double>("minimumTimingAmplitude", 0.100))
00053 {
00054   vector<int> listDefaults;
00055   listDefaults.push_back(-1);
00056   
00057   maskedChannels_ = iConfig.getUntrackedParameter<vector<int> >("maskedChannels", listDefaults);
00058   maskedFEDs_ = iConfig.getUntrackedParameter<vector<int> >("maskedFEDs", listDefaults);
00059   seedCrys_ = iConfig.getUntrackedParameter<vector<int> >("seedCrys",vector<int>());
00060 
00061   vector<string> defaultMaskedEBs;
00062   defaultMaskedEBs.push_back("none");
00063   maskedEBs_ =  iConfig.getUntrackedParameter<vector<string> >("maskedEBs",defaultMaskedEBs);
00064   
00065   fedMap_ = new EcalFedMap();
00066 
00067   string title1 = "Jitter for all FEDs";
00068   string name1 = "JitterAllFEDs";
00069   allFedsTimingHist_ = fileService->make<TH1F>(name1.c_str(),title1.c_str(),150,-7,7);
00070   
00071   // load up the maskedFED list with the proper FEDids
00072   if(maskedFEDs_[0]==-1)
00073   {
00074     //if "actual" EB id given, then convert to FEDid and put in listFEDs_
00075     if(maskedEBs_[0] != "none")
00076     {
00077       maskedFEDs_.clear();
00078       for(vector<string>::const_iterator ebItr = maskedEBs_.begin(); ebItr != maskedEBs_.end(); ++ebItr)
00079       {
00080         maskedFEDs_.push_back(fedMap_->getFedFromSlice(*ebItr));
00081       }
00082     }
00083   }
00084   
00085   for (int i=0; i<10; i++)        
00086     abscissa[i] = i;
00087   
00088   naiveEvtNum_ = 0;
00089 
00090 }
00091 
00092 
00093 EcalMipGraphs::~EcalMipGraphs()
00094 {
00095 }
00096 
00097 
00098 //
00099 // member functions
00100 //
00101 
00102 // ------------ method called to for each event  ------------
00103 void
00104 EcalMipGraphs::analyze(edm::Event const & iEvent, edm::EventSetup const & iSetup)
00105 {
00106 
00107   // get the headers
00108   // (one header for each supermodule)
00109   edm::Handle<EcalRawDataCollection> DCCHeaders;
00110   iEvent.getByLabel(headerProducer_, DCCHeaders);
00111 
00112   for (EcalRawDataCollection::const_iterator headerItr= DCCHeaders->begin();
00113                   headerItr != DCCHeaders->end (); 
00114                   ++headerItr) 
00115   {
00116     FEDsAndDCCHeaders_[headerItr->id()+600] = *headerItr;
00117   }
00118 
00119   int ievt = iEvent.id().event();
00120   naiveEvtNum_++;
00121 
00122   if(runNum_==-1)
00123   {
00124     runNum_ = iEvent.id().run();
00125     canvasNames_ = fileService->make<TTree>("canvasNames","Names of written canvases");
00126     names = new std::vector<string>();
00127     canvasNames_->Branch("names","vector<string>",&names);
00128   }
00129 
00130   //We only want the 3x3's for this event...
00131   listEBChannels.clear();
00132   listEEChannels.clear();
00133   Handle<EcalRecHitCollection> EBhits;
00134   Handle<EcalRecHitCollection> EEhits;
00135   ESHandle<CaloTopology> caloTopo;
00136   iSetup.get<CaloTopologyRecord>().get(caloTopo);
00137   iEvent.getByLabel(EBRecHitCollection_, EBhits);
00138   iEvent.getByLabel(EERecHitCollection_, EEhits);
00139   // Now, retrieve the crystal digi from the event
00140   iEvent.getByLabel(EBDigis_, EBdigisHandle);
00141   iEvent.getByLabel(EEDigis_, EEdigisHandle);
00142   //debug
00143   //LogWarning("EcalMipGraphs") << "event " << ievt << " EBhits collection size " << EBhits->size();
00144   //LogWarning("EcalMipGraphs") << "event " << ievt << " EEhits collection size " << EEhits->size();
00145 
00146   selectHits(EBhits, ievt, caloTopo);
00147   selectHits(EEhits, ievt, caloTopo);
00148   
00149 }
00150 
00151 
00152 TGraph* EcalMipGraphs::selectDigi(DetId thisDet, int ievt)
00153 {
00154   int emptyY[10];
00155   for (int i=0; i<10; i++)
00156     emptyY[i] = 0;
00157   TGraph* emptyGraph = fileService->make<TGraph>(10, abscissa, emptyY);
00158   emptyGraph->SetTitle("NOT ECAL");
00159   
00160   //If the DetId is not from Ecal, return
00161   if(thisDet.det() != DetId::Ecal)
00162     return emptyGraph;
00163   
00164   emptyGraph->SetTitle("NO DIGIS");
00165   //find digi we need  -- can't get find() to work; need DataFrame(DetId det) to work? 
00166   EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId(thisDet);
00167   int FEDid = 600+elecId.dccId();
00168   bool isBarrel = true;
00169   if(FEDid < 610 || FEDid > 645)
00170     isBarrel = false;
00171   int cryIndex = isBarrel ? ((EBDetId)thisDet).hashedIndex() : getEEIndex(elecId);
00172   int ic = isBarrel ? ((EBDetId)thisDet).ic() : cryIndex;
00173 
00174   string sliceName = fedMap_->getSliceFromFed(FEDid);
00175   EcalDataFrame df;
00176   if(isBarrel)
00177   {
00178     EBDigiCollection::const_iterator digiItr = EBdigisHandle->begin();
00179     while(digiItr != EBdigisHandle->end() && ((*digiItr).id() != (EBDetId)thisDet))
00180     {
00181       ++digiItr;
00182     }
00183     if(digiItr==EBdigisHandle->end())
00184     {
00185       //LogWarning("EcalMipGraphs") << "Cannot find digi for ic:" << ic
00186       //  << " FED:" << FEDid << " evt:" << naiveEvtNum_;
00187       return emptyGraph;
00188     }
00189     else
00190       df = *digiItr;
00191   }
00192   else
00193   {
00194     EEDigiCollection::const_iterator digiItr = EEdigisHandle->begin();
00195     while(digiItr != EEdigisHandle->end() && ((*digiItr).id() != (EEDetId)thisDet))
00196     {
00197       ++digiItr;
00198     }
00199     if(digiItr==EEdigisHandle->end())
00200     {
00201       //LogWarning("EcalMipGraphs") << "Cannot find digi for ic:" << ic
00202       //  << " FED:" << FEDid << " evt:" << naiveEvtNum_;
00203       return emptyGraph;
00204     }
00205     else df = *digiItr;
00206   }
00207 
00208   int gainId = FEDsAndDCCHeaders_[FEDid].getMgpaGain();
00209   int gainHuman;
00210   if      (gainId ==1) gainHuman =12;
00211   else if (gainId ==2) gainHuman =6;
00212   else if (gainId ==3) gainHuman =1;
00213   else                 gainHuman =-1; 
00214 
00215   double pedestal = 200;
00216 
00217   emptyGraph->SetTitle("FIRST TWO SAMPLES NOT GAIN12");
00218   if(df.sample(0).gainId()!=1 || df.sample(1).gainId()!=1) return emptyGraph; //goes to the next digi
00219   else {
00220     ordinate[0] = df.sample(0).adc();
00221     ordinate[1] = df.sample(1).adc();
00222     pedestal = (double)(ordinate[0]+ordinate[1])/(double)2;
00223   } 
00224 
00225 
00226   for (int i=0; i < df.size(); ++i ) {
00227     if (df.sample(i).gainId() != 0)
00228       ordinate[i] = (int)(pedestal+(df.sample(i).adc()-pedestal)*gainRatio[df.sample(i).gainId()-1]);
00229     else
00230       ordinate[i] = 49152; //Saturation of gain1 
00231   }
00232 
00233   TGraph* oneGraph = fileService->make<TGraph>(10, abscissa, ordinate);
00234   string name = "Graph_ev" + intToString(naiveEvtNum_) + "_ic" + intToString(ic)
00235     + "_FED" + intToString(FEDid);
00236   string gainString = (gainId==1) ? "Free" : intToString(gainHuman);
00237   string title = "Event" + intToString(naiveEvtNum_) + "_lv1a" + intToString(ievt) +
00238     "_ic" + intToString(ic) + "_" + sliceName + "_gain" + gainString;
00239     
00240   float energy = 0;
00241   map<int,float>::const_iterator itr;
00242   itr = crysAndAmplitudesMap_.find(cryIndex);
00243   if(itr!=crysAndAmplitudesMap_.end())
00244   {
00245     //edm::LogWarning("EcalMipGraphs")<< "itr->second(ampli)="<< itr->second;
00246     energy = (float) itr->second;
00247   }
00248   //else
00249   //edm::LogWarning("EcalMipGraphs") << "cry " << ic << "not found in ampMap";
00250 
00251   title+="_Energy"+floatToString(round(energy*1000));
00252 
00253   oneGraph->SetTitle(title.c_str());
00254   oneGraph->SetName(name.c_str());
00255   oneGraph->GetXaxis()->SetTitle("sample");
00256   oneGraph->GetYaxis()->SetTitle("ADC");
00257   return oneGraph;
00258 }
00259 
00260 void EcalMipGraphs::selectHits(Handle<EcalRecHitCollection> hits,
00261     int ievt, ESHandle<CaloTopology> caloTopo)
00262 {
00263   for (EcalRecHitCollection::const_iterator hitItr = hits->begin(); hitItr != hits->end(); ++hitItr)
00264   {
00265     EcalRecHit hit = (*hitItr);
00266     DetId det = hit.id();
00267     EcalElectronicsId elecId = ecalElectronicsMap_->getElectronicsId(det);
00268     int FEDid = 600+elecId.dccId();
00269     bool isBarrel = true;
00270     if(FEDid < 610 || FEDid > 645)
00271       isBarrel = false;
00272     int cryIndex = isBarrel ? ((EBDetId)det).hashedIndex() : ((EEDetId)det).hashedIndex();
00273     int ic = isBarrel ? ((EBDetId)det).ic() : getEEIndex(elecId);
00274     
00275     float ampli = hit.energy();
00276 
00277     vector<int>::iterator result;
00278     result = find(maskedFEDs_.begin(), maskedFEDs_.end(), FEDid);
00279     if(result != maskedFEDs_.end())
00280     {
00281       //LogWarning("EcalMipGraphs") << "skipping uncalRecHit for FED " << FEDid << " ; amplitude " << ampli;
00282       continue;
00283     }      
00284     result = find(maskedChannels_.begin(), maskedChannels_.end(), cryIndex);
00285     if  (result != maskedChannels_.end())
00286     {
00287       //LogWarning("EcalMipGraphs") << "skipping uncalRecHit for channel: " << cryIndex << " in fed: " << FEDid << " with amplitude " << ampli ;
00288       continue;
00289     } 
00290     bool cryIsInList = false;
00291     result = find(seedCrys_.begin(), seedCrys_.end(), cryIndex);
00292     if  (result != seedCrys_.end())
00293       cryIsInList = true;
00294 
00295     // Either we must have a user-requested cry (in which case there is no amplitude selection)
00296     // Or we pick all crys that pass the amplitude cut (in which case there is no fixed crystal selection)
00297     if(cryIsInList || (seedCrys_.empty() && ampli > threshold_))
00298     {
00299       // We have a winner!
00300       crysAndAmplitudesMap_[cryIndex] = ampli;
00301       string name = "Event" + intToString(naiveEvtNum_) + "_ic" + intToString(ic)
00302         + "_FED" + intToString(FEDid);
00303       string title = "Digis";
00304       string seed = "ic" + intToString(ic) + "_FED" + intToString(FEDid);
00305       int freq=1;
00306       pair<map<string,int>::iterator,bool> pair = seedFrequencyMap_.insert(make_pair(seed,freq));
00307       if(!pair.second)
00308       {
00309         ++(pair.first->second);
00310       }
00311       
00312       TCanvas can(name.c_str(),title.c_str(),200,50,900,900);
00313       can.Divide(side_,side_);
00314       TGraph* myGraph;
00315       int canvasNum = 1;
00316 
00317       CaloNavigator<DetId> cursor = CaloNavigator<DetId>(det,caloTopo->getSubdetectorTopology(det));
00318       //Now put each graph in one by one
00319       for(int j=side_/2; j>=-side_/2; --j)
00320       {
00321         for(int i=-side_/2; i<=side_/2; ++i)
00322         {
00323           cursor.home();
00324           cursor.offsetBy(i,j);
00325           can.cd(canvasNum);
00326           myGraph = selectDigi(*cursor,ievt);
00327           myGraph->Draw("A*");
00328           canvasNum++;
00329         }
00330       }
00331       can.Write();
00332       names->push_back(name);
00333     }
00334     
00335     TH1F* timingHist = FEDsAndTimingHists_[FEDid];
00336     if(timingHist==0)
00337     {
00338       initHists(FEDid);
00339       timingHist = FEDsAndTimingHists_[FEDid];
00340     }
00341     if(ampli > minTimingAmp_)
00342     {
00343       timingHist->Fill(hit.time());
00344       allFedsTimingHist_->Fill(hit.time());
00345     }
00346   }
00347 }
00348 
00349 int EcalMipGraphs::getEEIndex(EcalElectronicsId elecId)
00350 {
00351   int FEDid = 600+elecId.dccId();
00352   return 10000*FEDid+100*elecId.towerId()+5*(elecId.stripId()-1)+elecId.xtalId();
00353 }
00354 
00355 // insert the hist map into the map keyed by FED number
00356 void EcalMipGraphs::initHists(int FED)
00357 {
00358   using namespace std;
00359   
00360   string title1 = "Jitter for ";
00361   title1.append(fedMap_->getSliceFromFed(FED));
00362   string name1 = "JitterFED";
00363   name1.append(intToString(FED));
00364   TH1F* timingHist = fileService->make<TH1F>(name1.c_str(),title1.c_str(),150,-7,7);
00365   FEDsAndTimingHists_[FED] = timingHist;
00366 }
00367 
00368 // ------------ method called once each job just before starting event loop  ------------
00369 void 
00370 EcalMipGraphs::beginRun(edm::Run const &, edm::EventSetup const & c)
00371 {
00372   edm::ESHandle< EcalElectronicsMapping > handle;
00373   c.get< EcalMappingRcd >().get(handle);
00374   ecalElectronicsMap_ = handle.product();
00375 }
00376 
00377 // ------------ method called once each job just after ending the event loop  ------------
00378 void 
00379 EcalMipGraphs::endJob()
00380 {
00381   canvasNames_->Fill();
00382 
00383   string frequencies = "";
00384   for(std::map<std::string,int>::const_iterator itr = seedFrequencyMap_.begin();
00385       itr != seedFrequencyMap_.end(); ++itr)
00386   {
00387     if(itr->second > 1)
00388     {
00389       frequencies+=itr->first;
00390       frequencies+=" Frequency: ";
00391       frequencies+=intToString(itr->second);
00392       frequencies+="\n";
00393     }
00394   }
00395   LogWarning("EcalMipGraphs") << "Found seeds with frequency > 1: " << "\n\n" << frequencies;
00396   
00397   std::string channels;
00398   for(std::vector<int>::const_iterator itr = maskedChannels_.begin();
00399       itr != maskedChannels_.end(); ++itr)
00400   {
00401     channels+=intToString(*itr);
00402     channels+=",";
00403   }
00404   
00405   std::string feds;
00406   for(std::vector<int>::const_iterator itr = maskedFEDs_.begin();
00407       itr != maskedFEDs_.end(); ++itr)
00408   {
00409     feds+=intToString(*itr);
00410     feds+=",";
00411   }
00412 
00413   LogWarning("EcalMipGraphs") << "Masked channels are: " << channels;
00414   LogWarning("EcalMipGraphs") << "Masked FEDs are: " << feds << " and that is all!";
00415 }
00416 
00417 
00418 std::string EcalMipGraphs::intToString(int num)
00419 {
00420     using namespace std;
00421     ostringstream myStream;
00422     myStream << num << flush;
00423     return(myStream.str()); //returns the string form of the stringstream object
00424 }
00425 
00426 std::string EcalMipGraphs::floatToString(float num)
00427 {
00428     using namespace std;
00429     ostringstream myStream;
00430     myStream << num << flush;
00431     return(myStream.str()); //returns the string form of the stringstream object
00432 }