00001
00002
00003
00004
00005
00006
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
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
00047
00048
00050
00051
00052
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
00064 if (pTreeName.find(".") == 0) pTreeName.erase(0,2);
00065 const char* nTree = pTreeName.c_str();
00066 hf = TFile::Open(nTree);
00067
00068
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
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
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
00112
00113
00114
00115
00117
00118 eventInfo = new CastorShowerLibraryInfo();
00119 branch->SetAddress(&eventInfo);
00120 branch->GetEntry(0);
00121
00122
00123 totEvents = eventInfo->Energy.getNEvts();
00124
00125
00126
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
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
00166
00167
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
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
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
00215 G4Track * track = aStep->GetTrack();
00216
00217 const G4DynamicParticle *aParticle = track->GetDynamicParticle();
00218 G4ThreeVector momDir = aParticle->GetMomentumDirection();
00219
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
00246
00247
00248
00249 if (parCode == emPDG || parCode == epPDG || parCode == gammaPDG ) {
00250 select(0, pin, etain, phiin);
00251
00252
00253
00254
00255
00256 } else {
00257 select(1, pin, etain, phiin);
00258
00259
00260
00261
00262
00263 }
00264
00265 hit = (*showerEvent);
00266 return hit;
00267
00268 }
00269
00270
00271
00272 void CastorShowerLibrary::getRecord(int type, int record) {
00274
00275
00276
00277
00278
00279
00280
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
00314
00315
00316
00317
00318
00319
00320
00321
00322
00324
00325 int irec;
00326 double r = G4UniformRand();
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
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 ;
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;
00386 if (ieta==-1) ieta=0;
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
00396 getRecord (type, irec);
00397
00398 }
00399 int CastorShowerLibrary::FindEnergyBin(double energy) {
00400
00401
00402
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
00411 if (energy>=SLenergies.at(i)) return (int)i;
00412
00413 return -1;
00414 }
00415 int CastorShowerLibrary::FindEtaBin(double eta) {
00416
00417
00418
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
00425 if (eta>=SLetas.at(i)) return (int)i;
00426
00427 return -1;
00428 }
00429 int CastorShowerLibrary::FindPhiBin(double phi) {
00430
00431
00432
00433
00434
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
00441 if (phi>=SLphis.at(i)) return (int)i;
00442
00443 return -1;
00444 }