CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/SimG4CMS/Forward/src/CastorShowerLibrary.cc

Go to the documentation of this file.
00001 
00002 // File: CastorShowerLibrary.cc
00003 // Description: Shower library for CASTOR calorimeter
00004 //              Adapted from HFShowerLibrary
00005 //
00006 //  Wagner Carvalho
00007 //
00009 
00010 #include "SimG4CMS/Forward/interface/CastorShowerLibrary.h"
00011 #include "FWCore/Utilities/interface/Exception.h"
00012 
00013 #include "G4VPhysicalVolume.hh"
00014 #include "G4Step.hh"
00015 #include "G4Track.hh"
00016 #include "G4ParticleTable.hh"
00017 #include "Randomize.hh"
00018 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00019 
00020 //#define DebugLog
00021 
00022 CastorShowerLibrary::CastorShowerLibrary(std::string & name, edm::ParameterSet const & p) 
00023                                           : hf(0), evtInfo(0), emBranch(0), hadBranch(0),
00024                                             nMomBin(0), totEvents(0), evtPerBin(0),
00025                                             nBinsE(0),nBinsEta(0),nBinsPhi(0),
00026                                             nEvtPerBinE(0),nEvtPerBinEta(0),nEvtPerBinPhi(0),
00027                                             SLenergies(),SLetas(),SLphis() {
00028   
00029   initFile(p);
00030   
00031 }
00032 
00033 //=============================================================================================
00034 
00035 
00036 CastorShowerLibrary::~CastorShowerLibrary() {
00037   if (hf)     hf->Close();
00038 }
00039 
00040 
00041 //=============================================================================================
00042 
00043 void CastorShowerLibrary::initFile(edm::ParameterSet const & p) {
00045 //
00046 //  Init TFile and associated TBranch's of CASTOR Root file 
00047 //  holding library events 
00048 //
00050 
00051   //
00052   //  Read PSet for Castor shower library
00053   //
00054 
00055   edm::ParameterSet m_CS   = p.getParameter<edm::ParameterSet>("CastorShowerLibrary");
00056   edm::FileInPath fp       = m_CS.getParameter<edm::FileInPath>("FileName");
00057   std::string pTreeName    = fp.fullPath();
00058   std::string branchEvInfo = m_CS.getUntrackedParameter<std::string>("BranchEvt");
00059   std::string branchEM     = m_CS.getUntrackedParameter<std::string>("BranchEM");
00060   std::string branchHAD    = m_CS.getUntrackedParameter<std::string>("BranchHAD");
00061   verbose                  = m_CS.getUntrackedParameter<bool>("Verbosity",false);
00062 
00063   // Open TFile 
00064   if (pTreeName.find(".") == 0) pTreeName.erase(0,2);
00065   const char* nTree = pTreeName.c_str();
00066   hf                = TFile::Open(nTree);
00067 
00068   // Check that TFile has been successfully opened
00069   if (!hf->IsOpen()) { 
00070      edm::LogError("CastorShower") << "CastorShowerLibrary: opening " << nTree << " failed";
00071      throw cms::Exception("Unknown", "CastorShowerLibrary") 
00072                                    << "Opening of " << pTreeName << " fails\n";
00073   } else {
00074      edm::LogInfo("CastorShower")  << "CastorShowerLibrary: opening " << nTree << " successfully"; 
00075   }
00076 
00077   // Check for the TBranch holding EventInfo in "Events" TTree
00078   TTree* event = (TTree *) hf ->Get("CastorCherenkovPhotons");
00079   if (event) {
00080      evtInfo = (TBranchObject *) event->GetBranch(branchEvInfo.c_str());
00081      if (evtInfo) {
00082         loadEventInfo(evtInfo);
00083      } else {
00084         edm::LogError("CastorShower") << "CastorShowerLibrary: " << branchEvInfo.c_str()
00085                                       << " Branch does not exit in Event";
00086         throw cms::Exception("Unknown", "CastorShowerLibrary") << "Event information absent\n";
00087      }
00088   } else {
00089      edm::LogError("CastorShower") << "CastorShowerLibrary: Events Tree does not exist";
00090      throw cms::Exception("Unknown", "CastorShowerLibrary") << "Events tree absent\n";
00091   }
00092   
00093   // Get EM and HAD Branchs
00094   emBranch         = (TBranchObject *) event->GetBranch(branchEM.c_str());
00095   if (verbose) emBranch->Print();
00096   hadBranch        = (TBranchObject *) event->GetBranch(branchHAD.c_str());
00097   if (verbose) hadBranch->Print();
00098   edm::LogInfo("CastorShower") << "CastorShowerLibrary: Branch " << branchEM 
00099                                << " has " << emBranch->GetEntries() 
00100                                << " entries and Branch " << branchHAD 
00101                                << " has " << hadBranch->GetEntries() 
00102                                << " entries";
00103 
00104 }
00105 
00106 //=============================================================================================
00107 
00108 void CastorShowerLibrary::loadEventInfo(TBranchObject* branch) {
00110 //
00111 //  Get EventInfo from the "TBranch* branch" of Root file 
00112 //  holding library events 
00113 //
00114 //  Based on HFShowerLibrary::loadEventInfo
00115 //
00117 
00118   eventInfo = new CastorShowerLibraryInfo();
00119   branch->SetAddress(&eventInfo);
00120   branch->GetEntry(0);
00121   // Initialize shower library general parameters
00122 
00123   totEvents   = eventInfo->Energy.getNEvts();
00124 //  nMomBin     = eventInfo->Energy.getNBins();
00125 //  evtPerBin   = eventInfo->Energy.getNEvtPerBin();
00126 //  pmom        = eventInfo->Energy.getBin();
00127   nBinsE        = eventInfo->Energy.getNBins();
00128   nEvtPerBinE   = eventInfo->Energy.getNEvtPerBin();
00129   SLenergies    = eventInfo->Energy.getBin();
00130   nBinsEta      = eventInfo->Eta.getNBins();
00131   nEvtPerBinEta = eventInfo->Eta.getNEvtPerBin();
00132   SLetas        = eventInfo->Eta.getBin();
00133   nBinsPhi      = eventInfo->Phi.getNBins();
00134   nEvtPerBinPhi = eventInfo->Phi.getNEvtPerBin();
00135   SLphis        = eventInfo->Phi.getBin();
00136   
00137   // Convert from GeV to MeV
00138   for (unsigned int i=0; i<SLenergies.size(); i++) SLenergies[i] *= GeV;
00139   
00140   edm::LogInfo("CastorShower") << " CastorShowerLibrary::loadEventInfo : " 
00141                                << "\n \n Total number of events     :  " << totEvents 
00142                                <<    "\n   Number of bins  (E)       :  " << nBinsE
00143                                <<    "\n   Number of events/bin (E)  :  " << nEvtPerBinE
00144                                <<    "\n   Number of bins  (Eta)       :  " << nBinsEta
00145                                <<    "\n   Number of events/bin (Eta)  :  " << nEvtPerBinEta 
00146                                <<    "\n   Number of bins  (Phi)       :  " << nBinsPhi
00147                                <<    "\n   Number of events/bin (Phi)  :  " << nEvtPerBinPhi << "\n";
00148   
00149   for (unsigned int i=0; i<nBinsE; i++)
00150      edm::LogInfo("CastorShower") << "CastorShowerLibrary: SLenergies[" << i << "] = "
00151                                   << SLenergies[i]/GeV << " GeV";
00152   for (unsigned int i=0; i<nBinsEta; i++)
00153      edm::LogInfo("CastorShower") << "CastorShowerLibrary: SLetas[" << i << "] = "
00154                                   << SLetas[i];
00155   for (unsigned int i=0; i<nBinsPhi; i++)
00156      edm::LogInfo("CastorShower") << "CastorShowerLibrary: SLphis[" << i << "] = "
00157                                   << SLphis[i] << " rad";
00158 }
00159 
00160 //=============================================================================================
00161 
00162 void CastorShowerLibrary::initParticleTable(G4ParticleTable * theParticleTable) {
00164 //
00165 //  Set particle codes according to PDG encoding 
00166 //
00167 //  Based on HFShowerLibrary::initRun
00168 //
00170 
00171   G4String particleName;
00172 
00173   edm::LogInfo("CastorShower") << "CastorShowerLibrary::initParticleTable"
00174                                << " ***  Accessing PDGEncoding  ***" ;
00175 
00176   emPDG       = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
00177   epPDG       = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
00178   gammaPDG    = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
00179   pi0PDG      = theParticleTable->FindParticle(particleName="pi0")->GetPDGEncoding();
00180   etaPDG      = theParticleTable->FindParticle(particleName="eta")->GetPDGEncoding();
00181   nuePDG      = theParticleTable->FindParticle(particleName="nu_e")->GetPDGEncoding();
00182   numuPDG     = theParticleTable->FindParticle(particleName="nu_mu")->GetPDGEncoding();
00183   nutauPDG    = theParticleTable->FindParticle(particleName="nu_tau")->GetPDGEncoding();
00184   anuePDG     = theParticleTable->FindParticle(particleName="anti_nu_e")->GetPDGEncoding();
00185   anumuPDG    = theParticleTable->FindParticle(particleName="anti_nu_mu")->GetPDGEncoding();
00186   anutauPDG   = theParticleTable->FindParticle(particleName="anti_nu_tau")->GetPDGEncoding();
00187   geantinoPDG = theParticleTable->FindParticle(particleName="geantino")->GetPDGEncoding();
00188   mumPDG      = theParticleTable->FindParticle(particleName="mu-")->GetPDGEncoding();
00189   mupPDG      = theParticleTable->FindParticle(particleName="mu+")->GetPDGEncoding();
00190   
00191 //#ifdef DebugLog
00192   LogDebug("CastorShower") << "CastorShowerLibrary: Particle codes for e- = " << emPDG
00193                            << ", e+ = " << epPDG << ", gamma = " << gammaPDG 
00194                            << ", pi0 = " << pi0PDG << ", eta = " << etaPDG
00195                            << ", geantino = " << geantinoPDG << "\n        nu_e = "
00196                            << nuePDG << ", nu_mu = " << numuPDG << ", nu_tau = "
00197                            << nutauPDG << ", anti_nu_e = " << anuePDG
00198                            << ", anti_nu_mu = " << anumuPDG << ", anti_nu_tau = "
00199                            << anutauPDG << ", mu- = " << mumPDG << ", mu+ = "
00200                            << mupPDG;
00201 //#endif
00202 
00203     edm::LogInfo("CastorShower") << "  *****   Successfully called:  " 
00204                                  << "CastorShowerLibrary::initParticleTable()   ***** " ;
00205 
00206 }
00207 
00208 
00209 //=============================================================================================
00210 
00211 CastorShowerEvent CastorShowerLibrary::getShowerHits(G4Step * aStep, bool & ok) {
00212 
00213   G4StepPoint * preStepPoint  = aStep->GetPreStepPoint(); 
00214   // G4StepPoint * postStepPoint = aStep->GetPostStepPoint(); 
00215   G4Track *     track         = aStep->GetTrack();
00216   // Get Z-direction 
00217   const G4DynamicParticle *aParticle = track->GetDynamicParticle();
00218   G4ThreeVector               momDir = aParticle->GetMomentumDirection();
00219   //  double mom = aParticle->GetTotalMomentum();
00220 
00221   G4ThreeVector hitPoint = preStepPoint->GetPosition();   
00222   G4String      partType = track->GetDefinition()->GetParticleName();
00223   int           parCode  = track->GetDefinition()->GetPDGEncoding();
00224 
00225   CastorShowerEvent hit;
00226   hit.Clear();
00227   
00228   ok = false;
00229   if (parCode == pi0PDG   || parCode == etaPDG    || parCode == nuePDG  ||
00230       parCode == numuPDG  || parCode == nutauPDG  || parCode == anuePDG ||
00231       parCode == anumuPDG || parCode == anutauPDG || parCode == geantinoPDG) 
00232     return hit;
00233   ok = true;
00234 
00235   double pin    = preStepPoint->GetTotalEnergy();
00236   double etain  = momDir.getEta();
00237   double phiin  = momDir.getPhi();
00238   
00239   double zint = hitPoint.z();
00240   double R=sqrt(hitPoint.x()*hitPoint.x() + hitPoint.y()*hitPoint.y());
00241   double theta = atan2(R,std::abs(zint));
00242   phiin = atan2(hitPoint.y(),hitPoint.x());
00243   etain = -1*(std::log(std::tan((M_PI-theta)/2)));
00244 
00245   // Replace "interpolation/extrapolation" by new method "select" that just randomly 
00246   // selects a record from the appropriate energy bin and fills its content to  
00247   // "showerEvent" instance of class CastorShowerEvent
00248   
00249   if (parCode == emPDG || parCode == epPDG || parCode == gammaPDG ) {
00250     select(0, pin, etain, phiin);
00251     // if (pin<SLenergies[nBinsE-1]) {
00252     //   interpolate(0, pin);
00253     // } else {
00254     //   extrapolate(0, pin);
00255     // }
00256   } else {
00257     select(1, pin, etain, phiin);
00258     // if (pin<SLenergies[nBinsE-1]) {
00259     //   interpolate(1, pin);
00260     // } else {
00261     //   extrapolate(1, pin);
00262     // }
00263   }
00264     
00265   hit = (*showerEvent);
00266   return hit;
00267 
00268 }
00269 
00270 //=============================================================================================
00271 
00272 void CastorShowerLibrary::getRecord(int type, int record) {
00274 //
00275 //  Retrieve event # "record" from the library and stores it   
00276 //  into  vector<CastorShowerHit> showerHit;
00277 //
00278 //  Based on HFShowerLibrary::getRecord
00279 //
00280 //  Modified 02/02/09 by W. Carvalho
00281 //
00283 
00284 #ifdef DebugLog
00285   LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: ";
00286 #endif  
00287   int nrc  = record;
00288   int nHit = 0;
00289   showerEvent = new CastorShowerEvent();
00290   if (type > 0) {
00291     hadBranch->SetAddress(&showerEvent);
00292     hadBranch->GetEntry(nrc);
00293   } else {
00294     emBranch->SetAddress(&showerEvent);
00295     emBranch->GetEntry(nrc);
00296   }
00297   nHit = showerEvent->getNhit();
00298 
00299 #ifdef DebugLog
00300   LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: Record " << record
00301                            << " of type " << type << " with " << nHit 
00302                            << " CastorShowerHits";
00303 
00304 #endif
00305 
00306 }
00307 
00308 //=======================================================================================
00309 
00310 void CastorShowerLibrary::select(int type, double pin, double etain, double phiin) {
00312 //
00313 //  Selects an event from the library based on 
00314 //
00315 //    type:   0 --> em
00316 //           >0 --> had
00317 //    pin  :  momentum
00318 //    etain:  eta (if not given, disregard the eta binning
00319 //    phiin:  phi (if not given, disregard the phi binning
00320 //
00321 //  Created 30/01/09 by W. Carvalho
00322 //
00324 
00325   int irec;                         // to hold record number
00326   double r = G4UniformRand();       // to randomly select within an energy bin (r=[0,1])
00327 
00328   // Randomly select a record from the right energy bin in the library, 
00329   // based on track momentum (pin) 
00330    
00331   //   pin < SLenergies[MIN]
00332 /*
00333   if (pin<SLenergies[0]) {
00334      edm::LogWarning("CastorShower") << "CastorShowerLibrary: Warning, pin = " << pin 
00335                                      << " less than minimum SLenergies " << SLenergies[0] << " in library." 
00336                                      << " For the moment, selecting hit from the first bin" ;
00337      irec = int(nEvtPerBinE*r) + 1;
00338   //   pin > SLenergies[MAX]
00339   } else if (pin>SLenergies[nBinsE]) {
00340 
00341 // This part needs rethinking because the last element of SLenergies is no longer the upper limit of the bins
00342     edm::LogWarning("CastorShower") << "CastorShowerLibrary: Warning, pin = " << pin 
00343                                     << " greater than maximum SLenergies " << SLenergies[nBinsE] << " in library." 
00344                                     << " For the moment, selecting hit from the last bin";
00345     irec = (nBinsE-1)*nEvtPerBinE + int(evtPerBin*r) + 1;
00346   //   SLenergies[MIN] < pin < SLenergies[MAX]
00347   } else {
00348      for (unsigned int j=0; j<nBinsE; j++) {
00349         if (pin >= SLenergies[j] && pin < SLenergies[j+1]) {
00350            irec = j*evtPerBin + int(evtPerBin*r) + 1 ;
00351            if (irec<0) {
00352               edm::LogWarning("CastorShower") << "CastorShowerLibrary:: Illegal irec = "
00353                                               << irec << " now set to 0";
00354               irec = 0;
00355            } else if (irec > totEvents) {
00356               edm::LogWarning("CastorShower") << "CastorShowerLibrary:: Illegal irec = "
00357                                               << irec << " now set to "<< totEvents;
00358               irec = totEvents;
00359            }
00360         }
00361      }
00362   }
00363 */
00364   int ienergy = FindEnergyBin(pin);
00365   int ieta    = FindEtaBin(etain);
00366 #ifdef DebugLog
00367   if (verbose) edm::LogInfo("CastorShower") << " ienergy = " << ienergy ;
00368   if (verbose) edm::LogInfo("CastorShower") << " ieta = " << ieta;
00369 #endif
00370 
00371   int iphi;
00372   double phiMin = 0. ;
00373   double phiMax = M_PI/4 ;     // 45 * (pi/180)  rad 
00374   if(phiin < phiMin) phiin = phiin + M_PI ;
00375   if(phiin >= phiMin && phiin < phiMax) {
00376      iphi = FindPhiBin(phiin) ;
00377   } else {
00378      double remainder = fmod(phiin , M_PI/4) ;
00379      phiin = phiMin + remainder ;
00380      iphi = FindPhiBin(phiin) ;
00381   }
00382 #ifdef DebugLog
00383   if (verbose) edm::LogInfo("CastorShower") << " iphi = " << iphi;
00384 #endif
00385   if (ienergy==-1) ienergy=0;    // if pin < first bin, choose an event in the first one
00386   if (ieta==-1)    ieta=0; // idem for eta
00387   if (iphi!=-1) irec = int(nEvtPerBinE*ienergy+nEvtPerBinEta*ieta+nEvtPerBinPhi*(iphi+r));
00388   else irec = int(nEvtPerBinE*(ienergy+r));
00389 
00390 #ifdef DebugLog
00391   edm::LogInfo("CastorShower") << "CastorShowerLibrary:: Select record " << irec 
00392                                << " of type " << type ; 
00393 #endif
00394 
00395   //  Retrieve record number "irec" from the library
00396   getRecord (type, irec);
00397   
00398 }
00399 int CastorShowerLibrary::FindEnergyBin(double energy) {
00400   //
00401   // returns the integer index of the energy bin, taken from SLenergies vector
00402   // returns -1 if ouside valid range
00403   //
00404   if (energy >= SLenergies.back()) return SLenergies.size()-1;
00405 
00406   unsigned int i = 0;
00407   for(;i<SLenergies.size()-1;i++)
00408     if (energy >= SLenergies.at(i) && energy < SLenergies.at(i+1)) return (int)i;
00409 
00410   // now i points to the last but 1 bin
00411   if (energy>=SLenergies.at(i)) return (int)i;
00412   // energy outside bin range
00413   return -1;
00414 }
00415 int CastorShowerLibrary::FindEtaBin(double eta) {
00416   //
00417   // returns the integer index of the eta bin, taken from SLetas vector
00418   // returns -1 if ouside valid range
00419   //
00420   if (eta>=SLetas.back()) return SLetas.size()-1;
00421   unsigned int i = 0;
00422   for(;i<SLetas.size()-1;i++)
00423      if (eta >= SLetas.at(i) && eta < SLetas.at(i+1)) return (int)i;
00424   // now i points to the last but 1 bin
00425   if (eta>=SLetas.at(i)) return (int)i;
00426   // eta outside bin range
00427   return -1;
00428 }
00429 int CastorShowerLibrary::FindPhiBin(double phi) {
00430   //
00431   // returns the integer index of the phi bin, taken from SLphis vector
00432   // returns -1 if ouside valid range
00433   //
00434   // needs protection in case phi is outside range -pi,pi
00435   //
00436   if (phi>=SLphis.back()) return SLphis.size()-1;
00437   unsigned int i = 0;
00438   for(;i<SLphis.size()-1;i++)
00439      if (phi >= SLphis.at(i) && phi < SLphis.at(i+1)) return (int)i;
00440   // now i points to the last but 1 bin
00441   if (phi>=SLphis.at(i)) return (int)i;
00442   // phi outside bin range
00443   return -1;
00444 }