CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/Validation/EcalHits/src/EcalSimHitsValidation.cc

Go to the documentation of this file.
00001 /*
00002  * \file EcalSimHitsValidation.cc
00003  *
00004  * \author C.Rovelli
00005  *
00006 */
00007 
00008 #include <DataFormats/EcalDetId/interface/EBDetId.h>
00009 #include <DataFormats/EcalDetId/interface/EEDetId.h>
00010 #include <DataFormats/EcalDetId/interface/ESDetId.h>
00011 #include "FWCore/Utilities/interface/Exception.h"
00012 #include "Validation/EcalHits/interface/EcalSimHitsValidation.h"
00013 #include "DQMServices/Core/interface/DQMStore.h"
00014 
00015 using namespace cms;
00016 using namespace edm;
00017 using namespace std;
00018 
00019 EcalSimHitsValidation::EcalSimHitsValidation(const edm::ParameterSet& ps):
00020   HepMCLabel(ps.getParameter<std::string>("moduleLabelMC")),
00021   g4InfoLabel(ps.getParameter<std::string>("moduleLabelG4")),
00022   EBHitsCollection(ps.getParameter<std::string>("EBHitsCollection")),
00023   EEHitsCollection(ps.getParameter<std::string>("EEHitsCollection")),
00024   ESHitsCollection(ps.getParameter<std::string>("ESHitsCollection")){
00025 
00026   // DQM ROOT output
00027   outputFile_ = ps.getUntrackedParameter<std::string>("outputFile", "");
00028  
00029   if ( outputFile_.size() != 0 ) {
00030     edm::LogInfo("OutputInfo") << " Ecal SimHits Task histograms will be saved to " << outputFile_.c_str();
00031   } else {
00032     edm::LogInfo("OutputInfo") << " Ecal SimHits Task histograms will NOT be saved";
00033   }
00034  
00035   // verbosity switch
00036   verbose_ = ps.getUntrackedParameter<bool>("verbose", false);
00037  
00038   // DQMServices                                                        
00039   dbe_ = 0;
00040 
00041   // get hold of back-end interface
00042   dbe_ = edm::Service<DQMStore>().operator->();           
00043   if ( dbe_ ) {
00044     if ( verbose_ ) { dbe_->setVerbose(1); } 
00045     else            { dbe_->setVerbose(0); }
00046   }
00047                                                                                                             
00048   if ( dbe_ ) {
00049     if ( verbose_ ) dbe_->showDirStructure();
00050   }
00051  
00052   meGunEnergy_ = 0;
00053   meGunEta_    = 0;   
00054   meGunPhi_    = 0;   
00055   meEBEnergyFraction_  = 0;
00056   meEEEnergyFraction_  = 0;
00057   meESEnergyFraction_  = 0;
00058 
00059   Char_t histo[200];
00060  
00061   
00062   if ( dbe_ ) {
00063     dbe_->setCurrentFolder("EcalHitsV/EcalSimHitsValidation");
00064   
00065     sprintf (histo, "EcalSimHitsValidation Gun Momentum" ) ;
00066     meGunEnergy_ = dbe_->book1D(histo, histo, 100, 0., 1000.);
00067   
00068     sprintf (histo, "EcalSimHitsValidation Gun Eta" ) ;
00069     meGunEta_ = dbe_->book1D(histo, histo, 700, -3.5, 3.5);
00070   
00071     sprintf (histo, "EcalSimHitsValidation Gun Phi" ) ;
00072     meGunPhi_ = dbe_->book1D(histo, histo, 360, 0., 360.);
00073 
00074     sprintf (histo, "EcalSimHitsValidation Barrel fraction of energy" ) ;
00075     meEBEnergyFraction_ = dbe_->book1D(histo, histo, 100 , 0. , 1.1);
00076   
00077     sprintf (histo, "EcalSimHitsValidation Endcap fraction of energy" ) ;
00078     meEEEnergyFraction_ = dbe_->book1D(histo, histo, 100 , 0. , 1.1);
00079   
00080     sprintf (histo, "EcalSimHitsValidation Preshower fraction of energy" ) ;
00081     meESEnergyFraction_ = dbe_->book1D(histo, histo, 60 , 0. , 0.001);
00082   }
00083  
00084 }
00085 
00086 EcalSimHitsValidation::~EcalSimHitsValidation(){
00087  
00088   if ( outputFile_.size() != 0 && dbe_ ) dbe_->save(outputFile_);
00089 
00090 }
00091 
00092 void EcalSimHitsValidation::beginJob(){
00093 
00094 }
00095 
00096 void EcalSimHitsValidation::endJob(){
00097 
00098 }
00099 
00100 void EcalSimHitsValidation::analyze(const edm::Event& e, const edm::EventSetup& c){
00101 
00102   edm::LogInfo("EventInfo") << " Run = " << e.id().run() << " Event = " << e.id().event();
00103   
00104   std::vector<PCaloHit>  theEBCaloHits;
00105   std::vector<PCaloHit>  theEECaloHits;
00106   std::vector<PCaloHit>  theESCaloHits;
00107 
00108   edm::Handle<edm::HepMCProduct> MCEvt;
00109   edm::Handle<edm::PCaloHitContainer> EcalHitsEB;
00110   edm::Handle<edm::PCaloHitContainer> EcalHitsEE;
00111   edm::Handle<edm::PCaloHitContainer> EcalHitsES;
00112 
00113   e.getByLabel(HepMCLabel, MCEvt);
00114   e.getByLabel(g4InfoLabel,EBHitsCollection,EcalHitsEB);
00115   e.getByLabel(g4InfoLabel,EEHitsCollection,EcalHitsEE);
00116   e.getByLabel(g4InfoLabel,ESHitsCollection,EcalHitsES);
00117 
00118   for ( HepMC::GenEvent::particle_const_iterator p = MCEvt->GetEvent()->particles_begin();
00119         p != MCEvt->GetEvent()->particles_end(); ++p ) {
00120 
00121     double htheta = (*p)->momentum().theta();
00122     double heta = -99999.;
00123     if( tan(htheta * 0.5) > 0 ) {
00124       heta = -log(tan(htheta * 0.5));
00125     }
00126     double hphi = (*p)->momentum().phi();
00127     hphi = (hphi>=0) ? hphi : hphi+2*M_PI;
00128     hphi = hphi / M_PI * 180.;
00129 
00130     LogDebug("EventInfo") << "Particle gun type form MC = " << abs((*p)->pdg_id()) << "\n" << "Energy = "<< (*p)->momentum().e() << " Eta = " << heta << " Phi = " << hphi;
00131 
00132     if (meGunEnergy_) meGunEnergy_->Fill((*p)->momentum().e());
00133     if (meGunEta_)    meGunEta_   ->Fill(heta);
00134     if (meGunPhi_)    meGunPhi_   ->Fill(hphi);
00135 
00136   }
00137   
00138   double EBEnergy_ = 0.;
00139   if ( EcalHitsEB.isValid() ) {
00140     theEBCaloHits.insert(theEBCaloHits.end(), EcalHitsEB->begin(), EcalHitsEB->end());
00141     for (std::vector<PCaloHit>::iterator isim = theEBCaloHits.begin();
00142          isim != theEBCaloHits.end(); ++isim){
00143       EBEnergy_ += isim->energy();
00144     }
00145   }
00146 
00147   double EEEnergy_ = 0.;
00148   if ( EcalHitsEE.isValid() ) {
00149     theEECaloHits.insert(theEECaloHits.end(), EcalHitsEE->begin(), EcalHitsEE->end());
00150     for (std::vector<PCaloHit>::iterator isim = theEECaloHits.begin();
00151          isim != theEECaloHits.end(); ++isim){
00152       EEEnergy_ += isim->energy();
00153     }
00154   }
00155   
00156   double ESEnergy_ = 0.;
00157   if ( EcalHitsES.isValid() ) {
00158     theESCaloHits.insert(theESCaloHits.end(), EcalHitsES->begin(), EcalHitsES->end());
00159     for (std::vector<PCaloHit>::iterator isim = theESCaloHits.begin();
00160          isim != theESCaloHits.end(); ++isim){
00161       ESEnergy_ += isim->energy();
00162     }
00163   }
00164   
00165   double etot = EBEnergy_ + EEEnergy_ + ESEnergy_ ;
00166   double fracEB = 0.0;
00167   double fracEE = 0.0;
00168   double fracES = 0.0;
00169   
00170   if (etot>0.0) { 
00171     fracEB  = EBEnergy_/etot; 
00172     fracEE  = EEEnergy_/etot; 
00173     fracES  = ESEnergy_/etot; 
00174   }
00175   
00176   if (meEBEnergyFraction_) meEBEnergyFraction_->Fill(fracEB);
00177   
00178   if (meEEEnergyFraction_) meEEEnergyFraction_->Fill(fracEE);
00179   
00180   if (meESEnergyFraction_) meESEnergyFraction_->Fill(fracES);
00181 }
00182