CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/SimG4CMS/HcalTestBeam/plugins/HcalTB02Analysis.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     HcalTestBeam
00004 // Class  :     HcalTB02Analysis
00005 //
00006 // Implementation:
00007 //     Main analysis class for Hcal Test Beam 2002 Analysis
00008 //
00009 // Original Author:
00010 //         Created:  Sun May 21 10:14:34 CEST 2006
00011 // $Id: HcalTB02Analysis.cc,v 1.1 2012/07/02 04:44:40 sunanda Exp $
00012 //
00013   
00014 // system include files
00015 #include <cmath>
00016 #include <iostream>
00017 #include <iomanip>
00018   
00019 // user include files
00020 #include "SimG4CMS/HcalTestBeam/interface/HcalTB02Analysis.h"
00021 
00022 // to retreive hits
00023 #include "SimG4CMS/Calo/interface/CaloG4Hit.h"
00024 #include "SimG4CMS/Calo/interface/CaloG4HitCollection.h"
00025 #include "SimG4CMS/HcalTestBeam/interface/HcalTB02HcalNumberingScheme.h"
00026 #include "SimG4CMS/HcalTestBeam/interface/HcalTB02XtalNumberingScheme.h"
00027 #include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
00028 
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00031 #include "FWCore/ServiceRegistry/interface/Service.h"
00032 #include "FWCore/Framework/interface/MakerMacros.h"
00033 #include "FWCore/PluginManager/interface/ModuleDef.h"
00034 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00035 #include "CLHEP/Random/RandGaussQ.h"
00036 #include "FWCore/Utilities/interface/Exception.h"
00037 
00038 #include "G4SDManager.hh"
00039 #include "G4VProcess.hh"
00040 #include "G4HCofThisEvent.hh"
00041 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00042 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00043 
00044 //
00045 // constructors and destructor
00046 //
00047 
00048 HcalTB02Analysis::HcalTB02Analysis(const edm::ParameterSet &p): histo(0) {
00049 
00050   edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("HcalTB02Analysis");
00051   hcalOnly      = m_Anal.getUntrackedParameter<bool>("HcalClusterOnly",true);
00052   names         = m_Anal.getParameter<std::vector<std::string> >("Names");
00053   
00054   produces<HcalTB02HistoClass>();
00055 
00056   edm::LogInfo("HcalTBSim") << "HcalTB02Analysis:: Initialised as observer of "
00057                             << "BeginOfJob/BeginOfEvent/EndOfEvent with "
00058                             << "Parameter values:\n \thcalOnly = " << hcalOnly;
00059 
00060   histo  = new HcalTB02Histo(m_Anal);
00061 } 
00062    
00063 HcalTB02Analysis::~HcalTB02Analysis() {
00064 
00065   finish();
00066 
00067   if (histo)   {
00068     delete histo;
00069     histo  = 0;
00070   }
00071   edm::LogInfo("HcalTBSim") << "HcalTB02Analysis is deleting";
00072 }
00073 
00074 //
00075 // member functions
00076 //
00077 
00078 void HcalTB02Analysis::produce(edm::Event& e, const edm::EventSetup&) {
00079 
00080   std::auto_ptr<HcalTB02HistoClass> product(new HcalTB02HistoClass);
00081   fillEvent(*product);
00082   e.put(product);
00083 }
00084 
00085 void HcalTB02Analysis::update(const BeginOfEvent * evt) {
00086  
00087   edm::LogInfo("HcalTBSim") << "HcalTB02Analysis: =====> Begin of event = "
00088                             << (*evt) ()->GetEventID();
00089   clear();
00090 }
00091 
00092 void HcalTB02Analysis::update(const EndOfEvent * evt) {
00093 
00094   edm::Service<edm::RandomNumberGenerator> rng;
00095   if ( ! rng.isAvailable()) {
00096     throw cms::Exception("Configuration")
00097       << "HcalTB02Analysis requires the RandomNumberGeneratorService\n"
00098       << "which is not present in the configuration file. "
00099       << "You must add the service\n in the configuration file or "
00100       << "remove the modules that require it.";
00101   }
00102   CLHEP::RandGaussQ  randGauss(rng->getEngine());
00103 
00104   // Look for the Hit Collection
00105   LogDebug("HcalTBSim") << "HcalTB02Analysis::Fill event " 
00106                         << (*evt)()->GetEventID();
00107 
00108   // access to the G4 hit collections 
00109   G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
00110   int ihit = 0;    
00111  
00112   // HCAL
00113   std::string sd = names[0];
00114   int HCHCid = G4SDManager::GetSDMpointer()->GetCollectionID(sd);
00115   CaloG4HitCollection* theHCHC = (CaloG4HitCollection*) allHC->GetHC(HCHCid);
00116   HcalTB02HcalNumberingScheme *org = new HcalTB02HcalNumberingScheme();   
00117   LogDebug("HcalTBSim") << "HcalTB02Analysis :: Hit Collection for " << sd 
00118                         << " of ID " << HCHCid << " is obtained at " <<theHCHC;
00119 
00120   int nentries = 0;
00121   nentries = theHCHC->entries();
00122   if (nentries==0) return;
00123 
00124   int xentries = 0;  
00125   int XTALSid=0;
00126   CaloG4HitCollection* theXTHC=0;
00127 
00128   if (!hcalOnly) {
00129     // XTALS
00130     sd      = names[1];
00131     XTALSid = G4SDManager::GetSDMpointer()->GetCollectionID(sd);
00132     //    assert (XTALSid != 0);
00133     theXTHC = (CaloG4HitCollection*) allHC->GetHC(XTALSid);
00134     //    assert (theXTHC != 0);
00135     //HcalTB02XtalNumberingScheme *xorg = new HcalTB02XtalNumberingScheme();
00136     LogDebug("HcalTBSim") << "HcalTB02Analysis :: Hit Collection for " << sd
00137                           << " of ID " << XTALSid << " is obtained at " 
00138                           << theXTHC;
00139     xentries = theXTHC->entries();
00140     if (xentries==0) return;
00141   }
00142 
00143   LogDebug("HcalTBSim") << "HcalTB02Analysis :: There are " << nentries 
00144                         << " HCal hits, and" << xentries  << " xtal hits";
00145 
00146   float ETot=0., xETot=0.;
00147   //float maxE = 0.; 
00148   //int maxI=0, 
00149   int scintID=0, xtalID=0;
00150 
00151   // HCAL
00152 
00153   if (HCHCid >= 0 && theHCHC > 0) {
00154     for ( ihit = 0; ihit < nentries; ihit++) {
00155 
00156       CaloG4Hit* aHit = (*theHCHC)[ihit]; 
00157       scintID     = aHit->getUnitID();
00158       int layer   = org->getlayerID(scintID);
00159       float enEm  = aHit->getEM();
00160       float enhad = aHit->getHadr();
00161       
00162       if (layer==0) {
00163         enEm =enEm/2.;
00164         enhad=enhad/2.;
00165       }
00166 
00167       energyInScints[scintID]+= enEm + enhad;
00168       primaries[aHit->getTrackID()]+= enEm + enhad;
00169       float time = aHit->getTimeSliceID();
00170       if (time > maxTime) maxTime=(int)time;
00171       histo->fillAllTime(time);   
00172       
00173     }           
00174  
00175     //
00176     // Profile
00177     //
00178 
00179     float TowerEne[8][18], TowerEneCF[8][18], LayerEne[19], EnRing[100];
00180     for (int iphi=0 ; iphi<8; iphi++) {
00181       for (int jeta=0 ; jeta<18; jeta++) {
00182         TowerEne[iphi][jeta]=0.;
00183         TowerEneCF[iphi][jeta]=0.;
00184       }
00185     }
00186     
00187     for (int ilayer=0; ilayer<19; ilayer++) LayerEne[ilayer]=0.;
00188     for (int iring=0; iring<100; iring++) EnRing[iring]=0.;
00189     
00190     for (std::map<int,float>::iterator is = energyInScints.begin();
00191          is!= energyInScints.end(); is++) {
00192 
00193       ETot = (*is).second;
00194 
00195       int layer = org->getlayerID((*is).first);
00196 
00197       if ( (layer!=17) && (layer!=18) )  {
00198             
00199         float eta = org->getetaID((*is).first);
00200         float phi = org->getphiID((*is).first);
00201             
00202         SEnergy += ETot;
00203         TowerEne[(int)phi][(int)eta] += ETot;
00204 
00205         TowerEneCF[(int)phi][(int)eta] += ETot*(1.+ 0.1*randGauss.fire() );
00206         double dR=0.08727*std::sqrt( (eta-8.)*(eta-8.) + (phi-3.)*(phi-3.) );
00207         EnRing[(int)(dR/0.01)] += ETot;
00208       }
00209 
00210       LayerEne[layer] += ETot;
00211         
00212     }
00213     
00214     for (int ilayer=0 ; ilayer<19 ; ilayer++) {
00215       histo->fillProfile(ilayer,LayerEne[ilayer]/GeV);
00216     }
00217     
00218     for (int iring=0; iring<100; iring++)
00219       histo->fillTransProf(0.01*iring+0.005,EnRing[iring]/GeV);
00220     
00221     for (int iphi=0 ; iphi<8; iphi++) {
00222       for (int jeta=0 ; jeta<18; jeta++) {
00223         
00224         //SEnergyN += TowerEneCF[iphi][jeta] + 3.2*randGauss.fire(); // LHEP
00225         SEnergyN += TowerEneCF[iphi][jeta] + 3.*randGauss.fire(); // QGSP
00226 
00227         //double dR=0.08727*sqrt( (jeta-8.)*(jeta-8.)+(iphi-3.)*(iphi-3.) );
00228         //cout.testOut << " phi= " << iphi << " eta= " << jeta 
00229         //           << " TowerEne[iphi,jeta]= " << TowerEne[iphi][jeta] 
00230         //           << "dR= "  << dR << endl;
00231         
00232         //double Rand = 3.2*randGauss.fire(); // LHEP
00233         double Rand = 3.*randGauss.fire(); // QGSP
00234         
00235         if ( (iphi>=0) && (iphi<7) ) {
00236           if ( (jeta>=5) && (jeta<12) ) {
00237                 
00238             E7x7Matrix += TowerEne[iphi][jeta];
00239             E7x7MatrixN += TowerEneCF[iphi][jeta] + Rand;
00240                 
00241             if ( (iphi>=1) && (iphi<6) ) {
00242               if ( (jeta>=6) && (jeta<11) ) {
00243                         
00244                 E5x5Matrix += TowerEne[iphi][jeta];
00245                 E5x5MatrixN += TowerEneCF[iphi][jeta] + Rand;
00246                         
00247               }
00248             }
00249                 
00250           }
00251         }
00252         
00253       }
00254     }
00255     
00256     //
00257     // Find Primary info:
00258     //  
00259     int trackID = 0;
00260     G4PrimaryParticle* thePrim=0;
00261     G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
00262     LogDebug("HcalTBSim") << "HcalTB02Analysis :: Event has " << nvertex 
00263                           << " vertex";
00264     if (nvertex==0)
00265       edm::LogWarning("HcalTBSim") << "HcalTB02Analysis:: End Of Event  "
00266                                    << "ERROR: no vertex";
00267 
00268     for (int i = 0 ; i<nvertex; i++) {
00269         
00270       G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
00271       if (avertex == 0) {
00272         edm::LogWarning("HcalTBSim") << "HcalTB02Analysis:: End Of Event "
00273                                      << "ERROR: pointer to vertex = 0";
00274       } else {
00275         int npart = avertex->GetNumberOfParticle();
00276         LogDebug("HcalTBSim") << "HcalTB02Analysis::Vertex number :" << i 
00277                               << " with " << npart << " particles";
00278         if (thePrim==0) thePrim=avertex->GetPrimary(trackID);
00279       }
00280     }
00281     
00282     double px=0.,py=0.,pz=0.;
00283     
00284     if (thePrim != 0) {
00285       px = thePrim->GetPx();
00286       py = thePrim->GetPy();
00287       pz = thePrim->GetPz();
00288       pInit = std::sqrt(pow(px,2.)+pow(py,2.)+pow(pz,2.));
00289       if (pInit==0) {
00290         edm::LogWarning("HcalTBSim") << "HcalTB02Analysis:: End Of Event "
00291                                      << " ERROR: primary has p=0 ";
00292       } else {   
00293         float costheta = pz/pInit;
00294         float theta = acos(std::min(std::max(costheta,float(-1.)),float(1.)));
00295         eta = -log(tan(theta/2));
00296         if (px != 0) phi = atan(py/px);  
00297       }
00298       particleType      = thePrim->GetPDGcode();
00299     } else {
00300       LogDebug("HcalTBSim") << "HcalTB02Analysis:: End Of Event ERROR: could"
00301                             << " not find primary ";
00302     }
00303     
00304     CaloG4Hit* firstHit =(*theHCHC)[0];
00305     incidentEnergy = (firstHit->getIncidentEnergy()/GeV);
00306     
00307   }// number of Hits > 0
00308 
00309   if (!hcalOnly) {
00310 
00311     // XTALS
00312 
00313     if (XTALSid >= 0 && theXTHC > 0) {
00314       for (int xihit = 0; xihit < xentries; xihit++) {
00315 
00316         CaloG4Hit* xaHit = (*theXTHC)[xihit]; 
00317 
00318         float xenEm = xaHit->getEM();
00319         float xenhad = xaHit->getHadr();
00320         xtalID = xaHit->getUnitID();
00321           
00322         energyInCrystals[xtalID]+= xenEm + xenhad;
00323       }
00324       
00325       float xCrysEne[7][7];
00326       for (int irow=0 ; irow<7; irow++) {
00327         for (int jcol=0 ; jcol<7; jcol++) {
00328           xCrysEne[irow][jcol]=0.;
00329         }
00330       }
00331       
00332       for (std::map<int,float>::iterator is = energyInCrystals.begin();
00333            is!= energyInCrystals.end(); is++) {
00334         int xtalID = (*is).first;
00335         xETot = (*is).second;
00336             
00337         int irow = (int)(xtalID/100.);
00338         int jcol = (int)(xtalID-100.*irow);
00339         
00340         xSEnergy += xETot;
00341         xCrysEne[irow][jcol] = xETot;
00342             
00343         float dR=std::sqrt( 0.01619*0.01619*(jcol-3)*(jcol-3) + 
00344                             0.01606*0.01606*(irow-3)*(irow-3) );
00345         histo->fillTransProf(dR,xETot*1.05);
00346             
00347         if ( (irow>0) && (irow<6) ) {
00348           if ( (jcol>0) && (jcol<6) ) {
00349                     
00350             xE5x5Matrix += xCrysEne[irow][jcol];
00351             xE5x5MatrixN += xCrysEne[irow][jcol] + 108.5*randGauss.fire();
00352                     
00353             if ( (irow>1) && (irow<5) ) {
00354               if ( (jcol>1) && (jcol<5) ) {             
00355                 xE3x3Matrix += xCrysEne[irow][jcol];
00356                 xE3x3MatrixN += xCrysEne[irow][jcol] +108.5*randGauss.fire();
00357               }    
00358             }
00359           }
00360         }  
00361         
00362       }      
00363 
00364       if (!hcalOnly) {
00365         //      assert(theXTHC);
00366         if ( theXTHC != 0 ) {
00367           CaloG4Hit* xfirstHit =(*theXTHC)[0];
00368           xIncidentEnergy = xfirstHit->getIncidentEnergy()/GeV;
00369         }
00370       }
00371       
00372     }// number of Hits > 0
00373 
00374   }
00375 
00376   int iEvt = (*evt)()->GetEventID();
00377   if (iEvt < 10) 
00378     std::cout << " Event " << iEvt << std::endl;
00379   else if ((iEvt < 100) && (iEvt%10 == 0)) 
00380     std::cout << " Event " << iEvt << std::endl;
00381   else if ((iEvt < 1000) && (iEvt%100 == 0)) 
00382     std::cout << " Event " << iEvt << std::endl;
00383   else if ((iEvt < 10000) && (iEvt%1000 == 0)) 
00384     std::cout << " Event " << iEvt << std::endl;
00385 }
00386 
00387 void HcalTB02Analysis::fillEvent(HcalTB02HistoClass& product) {
00388 
00389   //Beam information
00390   product.set_Nprim(float(primaries.size()));
00391   product.set_partType(particleType);
00392   product.set_Einit(pInit/GeV);
00393   product.set_eta(eta);
00394   product.set_phi(phi);
00395   product.set_Eentry(incidentEnergy);
00396 
00397   //Calorimeter energy
00398   product.set_ETot(SEnergy/GeV );
00399   product.set_E7x7(E7x7Matrix/GeV );
00400   product.set_E5x5(E5x5Matrix/GeV );
00401   product.set_ETotN(SEnergyN/GeV);
00402   product.set_E7x7N(E7x7MatrixN/GeV );
00403   product.set_E5x5N(E5x5MatrixN/GeV );
00404   product.set_NUnit(float(energyInScints.size()));
00405   product.set_Ntimesli(float(maxTime));
00406 
00407   //crystal information
00408   product.set_xEentry(xIncidentEnergy);
00409   product.set_xNUnit(float(energyInCrystals.size()));
00410   product.set_xETot(xSEnergy/GeV);
00411   product.set_xETotN(xSEnergyN/GeV);
00412   product.set_xE5x5(xE5x5Matrix/GeV);
00413   product.set_xE3x3(xE3x3Matrix/GeV);
00414   product.set_xE5x5N(xE5x5MatrixN/GeV);
00415   product.set_xE3x3N(xE3x3MatrixN/GeV);
00416 }
00417 
00418 void HcalTB02Analysis::clear() {
00419 
00420   primaries.clear();
00421   particleType = 0;
00422   pInit = eta = phi = incidentEnergy = 0;
00423 
00424   SEnergy = E7x7Matrix = E5x5Matrix = SEnergyN = 0;
00425   E7x7MatrixN = E5x5MatrixN = 0;
00426   energyInScints.clear();
00427   maxTime = 0;
00428 
00429   xIncidentEnergy = 0;
00430   energyInCrystals.clear();
00431   xSEnergy = xSEnergyN = xE5x5Matrix = xE3x3Matrix = 0;
00432   xE5x5MatrixN = xE3x3MatrixN = 0;
00433 }
00434 
00435 void HcalTB02Analysis::finish() {
00436 
00437   /*
00438   //Profile 
00439   std::ofstream   oFile;
00440   oFile.open("profile.dat");
00441   float st[19] = {0.8,
00442                   0.4,  0.4,  0.4,  0.4,  0.4,   
00443                   0.4,  0.4,  0.4,  0.4,  0.4,
00444                   0.4,  0.4,  0.4,  0.4,  0.4, 
00445                   0.8,  1.0,  1.0};
00446                  
00447   //cm of material (brass) in front of scintillator layer i:
00448  
00449   float w[19] = {7.45,                         //iron !
00450                  6.00, 6.00, 6.00, 6.00, 6.00, //brass
00451                  6.00, 6.00, 6.00, 6.00, 6.60, //brass
00452                  6.60, 6.60, 6.60, 6.60, 6.60, //brass
00453                  8.90, 20.65, 19.5};            //brass,iron !
00454 
00455   for (int ilayer = 0; ilayer<19; ilayer++) {
00456 
00457     // Histogram mean and sigma calculated from the ROOT histos
00458     edm::LogInfo("HcalTBSim") << "Layer number: " << ilayer << " Mean = " 
00459                               << histo->getMean(ilayer) << " sigma = "   
00460                               << histo->getRMS(ilayer) << " LThick= "   
00461                               << w[ilayer] << " SThick= "   << st[ilayer];
00462       
00463     oFile << ilayer << " "  << histo->getMean(ilayer) << " " 
00464           << histo->getRMS(ilayer)  << " " << w[ilayer] << " " << st[ilayer] 
00465           << std::endl;
00466 
00467   } 
00468   oFile.close(); 
00469   */
00470 }
00471  
00472 DEFINE_SIMWATCHER (HcalTB02Analysis);