CMS 3D CMS Logo

CastorShowerLibrary.cc
Go to the documentation of this file.
1 // File: CastorShowerLibrary.cc
3 // Description: Shower library for CASTOR calorimeter
4 // Adapted from HFShowerLibrary
5 //
6 // Wagner Carvalho
7 //
9 
12 
13 #include "G4VPhysicalVolume.hh"
14 #include "G4Step.hh"
15 #include "G4Track.hh"
16 #include "G4ParticleTable.hh"
17 #include "Randomize.hh"
18 #include "G4PhysicalConstants.hh"
19 #include "CLHEP/Units/GlobalSystemOfUnits.h"
20 
21 //#define DebugLog
22 
24  : hf(0), evtInfo(0), emBranch(0), hadBranch(0),
25  nMomBin(0), totEvents(0), evtPerBin(0),
26  nBinsE(0),nBinsEta(0),nBinsPhi(0),
27  nEvtPerBinE(0),nEvtPerBinEta(0),nEvtPerBinPhi(0),
28  SLenergies(),SLetas(),SLphis() {
29 
30  initFile(p);
31 
32 }
33 
34 //=============================================================================================
35 
36 
38  if (hf) hf->Close();
39 }
40 
41 
42 //=============================================================================================
43 
46 //
47 // Init TFile and associated TBranch's of CASTOR Root file
48 // holding library events
49 //
51 
52  //
53  // Read PSet for Castor shower library
54  //
55 
56  edm::ParameterSet m_CS = p.getParameter<edm::ParameterSet>("CastorShowerLibrary");
57  edm::FileInPath fp = m_CS.getParameter<edm::FileInPath>("FileName");
58  std::string pTreeName = fp.fullPath();
59  std::string branchEvInfo = m_CS.getUntrackedParameter<std::string>("BranchEvt");
60  std::string branchEM = m_CS.getUntrackedParameter<std::string>("BranchEM");
61  std::string branchHAD = m_CS.getUntrackedParameter<std::string>("BranchHAD");
62  verbose = m_CS.getUntrackedParameter<bool>("Verbosity",false);
63 
64  // Open TFile
65  if (pTreeName.find(".") == 0) pTreeName.erase(0,2);
66  const char* nTree = pTreeName.c_str();
67  hf = TFile::Open(nTree);
68 
69  // Check that TFile has been successfully opened
70  if (!hf->IsOpen()) {
71  edm::LogError("CastorShower") << "CastorShowerLibrary: opening " << nTree << " failed";
72  throw cms::Exception("Unknown", "CastorShowerLibrary")
73  << "Opening of " << pTreeName << " fails\n";
74  } else {
75  edm::LogInfo("CastorShower") << "CastorShowerLibrary: opening " << nTree << " successfully";
76  }
77 
78  // Check for the TBranch holding EventInfo in "Events" TTree
79  TTree* event = (TTree *) hf ->Get("CastorCherenkovPhotons");
80  if (event) {
81  evtInfo = (TBranchObject *) event->GetBranch(branchEvInfo.c_str());
82  if (evtInfo) {
84  } else {
85  edm::LogError("CastorShower") << "CastorShowerLibrary: " << branchEvInfo.c_str()
86  << " Branch does not exit in Event";
87  throw cms::Exception("Unknown", "CastorShowerLibrary") << "Event information absent\n";
88  }
89  } else {
90  edm::LogError("CastorShower") << "CastorShowerLibrary: Events Tree does not exist";
91  throw cms::Exception("Unknown", "CastorShowerLibrary") << "Events tree absent\n";
92  }
93 
94  // Get EM and HAD Branchs
95  emBranch = (TBranchObject *) event->GetBranch(branchEM.c_str());
96  if (verbose) emBranch->Print();
97  hadBranch = (TBranchObject *) event->GetBranch(branchHAD.c_str());
98  if (verbose) hadBranch->Print();
99  edm::LogInfo("CastorShower") << "CastorShowerLibrary: Branch " << branchEM
100  << " has " << emBranch->GetEntries()
101  << " entries and Branch " << branchHAD
102  << " has " << hadBranch->GetEntries()
103  << " entries";
104 
105 }
106 
107 //=============================================================================================
108 
111 //
112 // Get EventInfo from the "TBranch* branch" of Root file
113 // holding library events
114 //
115 // Based on HFShowerLibrary::loadEventInfo
116 //
118 
120  branch->SetAddress(&eventInfo);
121  branch->GetEntry(0);
122  // Initialize shower library general parameters
123 
125 // nMomBin = eventInfo->Energy.getNBins();
126 // evtPerBin = eventInfo->Energy.getNEvtPerBin();
127 // pmom = eventInfo->Energy.getBin();
133  SLetas = eventInfo->Eta.getBin();
136  SLphis = eventInfo->Phi.getBin();
137 
138  // Convert from GeV to MeV
139  for (unsigned int i=0; i<SLenergies.size(); i++) SLenergies[i] *= GeV;
140 
141  edm::LogInfo("CastorShower") << " CastorShowerLibrary::loadEventInfo : "
142  << "\n \n Total number of events : " << totEvents
143  << "\n Number of bins (E) : " << nBinsE
144  << "\n Number of events/bin (E) : " << nEvtPerBinE
145  << "\n Number of bins (Eta) : " << nBinsEta
146  << "\n Number of events/bin (Eta) : " << nEvtPerBinEta
147  << "\n Number of bins (Phi) : " << nBinsPhi
148  << "\n Number of events/bin (Phi) : " << nEvtPerBinPhi << "\n";
149 
150  for (unsigned int i=0; i<nBinsE; i++)
151  edm::LogInfo("CastorShower") << "CastorShowerLibrary: SLenergies[" << i << "] = "
152  << SLenergies[i]/GeV << " GeV";
153  for (unsigned int i=0; i<nBinsEta; i++)
154  edm::LogInfo("CastorShower") << "CastorShowerLibrary: SLetas[" << i << "] = "
155  << SLetas[i];
156  for (unsigned int i=0; i<nBinsPhi; i++)
157  edm::LogInfo("CastorShower") << "CastorShowerLibrary: SLphis[" << i << "] = "
158  << SLphis[i] << " rad";
159 }
160 
161 //=============================================================================================
162 
163 void CastorShowerLibrary::initParticleTable(G4ParticleTable * theParticleTable) {
165 //
166 // Set particle codes according to PDG encoding
167 //
168 // Based on HFShowerLibrary::initRun
169 //
171 
172  G4String particleName;
173 
174  edm::LogInfo("CastorShower") << "CastorShowerLibrary::initParticleTable"
175  << " *** Accessing PDGEncoding ***" ;
176 
177  emPDG = theParticleTable->FindParticle(particleName="e-")->GetPDGEncoding();
178  epPDG = theParticleTable->FindParticle(particleName="e+")->GetPDGEncoding();
179  gammaPDG = theParticleTable->FindParticle(particleName="gamma")->GetPDGEncoding();
180  pi0PDG = theParticleTable->FindParticle(particleName="pi0")->GetPDGEncoding();
181  etaPDG = theParticleTable->FindParticle(particleName="eta")->GetPDGEncoding();
182  nuePDG = theParticleTable->FindParticle(particleName="nu_e")->GetPDGEncoding();
183  numuPDG = theParticleTable->FindParticle(particleName="nu_mu")->GetPDGEncoding();
184  nutauPDG = theParticleTable->FindParticle(particleName="nu_tau")->GetPDGEncoding();
185  anuePDG = theParticleTable->FindParticle(particleName="anti_nu_e")->GetPDGEncoding();
186  anumuPDG = theParticleTable->FindParticle(particleName="anti_nu_mu")->GetPDGEncoding();
187  anutauPDG = theParticleTable->FindParticle(particleName="anti_nu_tau")->GetPDGEncoding();
188  geantinoPDG = theParticleTable->FindParticle(particleName="geantino")->GetPDGEncoding();
189  mumPDG = theParticleTable->FindParticle(particleName="mu-")->GetPDGEncoding();
190  mupPDG = theParticleTable->FindParticle(particleName="mu+")->GetPDGEncoding();
191 
192 //#ifdef DebugLog
193  LogDebug("CastorShower") << "CastorShowerLibrary: Particle codes for e- = " << emPDG
194  << ", e+ = " << epPDG << ", gamma = " << gammaPDG
195  << ", pi0 = " << pi0PDG << ", eta = " << etaPDG
196  << ", geantino = " << geantinoPDG << "\n nu_e = "
197  << nuePDG << ", nu_mu = " << numuPDG << ", nu_tau = "
198  << nutauPDG << ", anti_nu_e = " << anuePDG
199  << ", anti_nu_mu = " << anumuPDG << ", anti_nu_tau = "
200  << anutauPDG << ", mu- = " << mumPDG << ", mu+ = "
201  << mupPDG;
202 //#endif
203 
204  edm::LogInfo("CastorShower") << " ***** Successfully called: "
205  << "CastorShowerLibrary::initParticleTable() ***** " ;
206 
207 }
208 
209 
210 //=============================================================================================
211 
213 
214  G4StepPoint * preStepPoint = aStep->GetPreStepPoint();
215  // G4StepPoint * postStepPoint = aStep->GetPostStepPoint();
216  G4Track * track = aStep->GetTrack();
217  // Get Z-direction
218  const G4DynamicParticle *aParticle = track->GetDynamicParticle();
219  G4ThreeVector momDir = aParticle->GetMomentumDirection();
220  // double mom = aParticle->GetTotalMomentum();
221 
222  G4ThreeVector hitPoint = preStepPoint->GetPosition();
223  G4String partType = track->GetDefinition()->GetParticleName();
224  int parCode = track->GetDefinition()->GetPDGEncoding();
225 
227  hit.Clear();
228 
229  ok = false;
230  if (parCode == pi0PDG || parCode == etaPDG || parCode == nuePDG ||
231  parCode == numuPDG || parCode == nutauPDG || parCode == anuePDG ||
232  parCode == anumuPDG || parCode == anutauPDG || parCode == geantinoPDG)
233  return hit;
234  ok = true;
235 
236  double pin = preStepPoint->GetTotalEnergy();
237  // double etain = momDir.getEta();
238  //double phiin = momDir.getPhi();
239 
240  double zint = hitPoint.z();
241  double R=sqrt(hitPoint.x()*hitPoint.x() + hitPoint.y()*hitPoint.y());
242  double theta = atan2(R,std::abs(zint));
243  double phiin = atan2(hitPoint.y(),hitPoint.x());
244  double etain = -std::log(std::tan((pi-theta)*0.5));
245 
246  // Replace "interpolation/extrapolation" by new method "select" that just randomly
247  // selects a record from the appropriate energy bin and fills its content to
248  // "showerEvent" instance of class CastorShowerEvent
249 
250  if (parCode == emPDG || parCode == epPDG || parCode == gammaPDG ) {
251  select(0, pin, etain, phiin);
252  // if (pin<SLenergies[nBinsE-1]) {
253  // interpolate(0, pin);
254  // } else {
255  // extrapolate(0, pin);
256  // }
257  } else {
258  select(1, pin, etain, phiin);
259  // if (pin<SLenergies[nBinsE-1]) {
260  // interpolate(1, pin);
261  // } else {
262  // extrapolate(1, pin);
263  // }
264  }
265 
266  hit = (*showerEvent);
267  return hit;
268 
269 }
270 
271 //=============================================================================================
272 
275 //
276 // Retrieve event # "record" from the library and stores it
277 // into vector<CastorShowerHit> showerHit;
278 //
279 // Based on HFShowerLibrary::getRecord
280 //
281 // Modified 02/02/09 by W. Carvalho
282 //
284 
285 #ifdef DebugLog
286  LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: ";
287 #endif
288  int nrc = record;
290  if (type > 0) {
291  hadBranch->SetAddress(&showerEvent);
292  hadBranch->GetEntry(nrc);
293  } else {
294  emBranch->SetAddress(&showerEvent);
295  emBranch->GetEntry(nrc);
296  }
297 
298 #ifdef DebugLog
299  int nHit = showerEvent->getNhit();
300  LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: Record " << record
301  << " of type " << type << " with " << nHit
302  << " CastorShowerHits";
303 
304 #endif
305 
306 }
307 
308 //=======================================================================================
309 
310 void CastorShowerLibrary::select(int type, double pin, double etain, double phiin) {
312 //
313 // Selects an event from the library based on
314 //
315 // type: 0 --> em
316 // >0 --> had
317 // pin : momentum
318 // etain: eta (if not given, disregard the eta binning
319 // phiin: phi (if not given, disregard the phi binning
320 //
321 // Created 30/01/09 by W. Carvalho
322 //
324 
325  int irec; // to hold record number
326  double r = G4UniformRand(); // to randomly select within an energy bin (r=[0,1])
327 
328  // Randomly select a record from the right energy bin in the library,
329  // based on track momentum (pin)
330 
331  // pin < SLenergies[MIN]
332 /*
333  if (pin<SLenergies[0]) {
334  edm::LogWarning("CastorShower") << "CastorShowerLibrary: Warning, pin = " << pin
335  << " less than minimum SLenergies " << SLenergies[0] << " in library."
336  << " For the moment, selecting hit from the first bin" ;
337  irec = int(nEvtPerBinE*r) + 1;
338  // pin > SLenergies[MAX]
339  } else if (pin>SLenergies[nBinsE]) {
340 
341 // This part needs rethinking because the last element of SLenergies is no longer the upper limit of the bins
342  edm::LogWarning("CastorShower") << "CastorShowerLibrary: Warning, pin = " << pin
343  << " greater than maximum SLenergies " << SLenergies[nBinsE] << " in library."
344  << " For the moment, selecting hit from the last bin";
345  irec = (nBinsE-1)*nEvtPerBinE + int(evtPerBin*r) + 1;
346  // SLenergies[MIN] < pin < SLenergies[MAX]
347  } else {
348  for (unsigned int j=0; j<nBinsE; j++) {
349  if (pin >= SLenergies[j] && pin < SLenergies[j+1]) {
350  irec = j*evtPerBin + int(evtPerBin*r) + 1 ;
351  if (irec<0) {
352  edm::LogWarning("CastorShower") << "CastorShowerLibrary:: Illegal irec = "
353  << irec << " now set to 0";
354  irec = 0;
355  } else if (irec > totEvents) {
356  edm::LogWarning("CastorShower") << "CastorShowerLibrary:: Illegal irec = "
357  << irec << " now set to "<< totEvents;
358  irec = totEvents;
359  }
360  }
361  }
362  }
363 */
364  int ienergy = FindEnergyBin(pin);
365  int ieta = FindEtaBin(etain);
366 #ifdef DebugLog
367  if (verbose) edm::LogInfo("CastorShower") << " ienergy = " << ienergy ;
368  if (verbose) edm::LogInfo("CastorShower") << " ieta = " << ieta;
369 #endif
370 
371  int iphi;
372  double phiMin = 0. ;
373  double phiMax = M_PI/4 ; // 45 * (pi/180) rad
374  if(phiin < phiMin) phiin = phiin + M_PI ;
375  if(phiin >= phiMin && phiin < phiMax) {
376  iphi = FindPhiBin(phiin) ;
377  } else {
378  double remainder = fmod(phiin , M_PI/4) ;
379  phiin = phiMin + remainder ;
380  iphi = FindPhiBin(phiin) ;
381  }
382 #ifdef DebugLog
383  if (verbose) edm::LogInfo("CastorShower") << " iphi = " << iphi;
384 #endif
385  if (ienergy==-1) ienergy=0; // if pin < first bin, choose an event in the first one
386  if (ieta==-1) ieta=0; // idem for eta
387  if (iphi!=-1) irec = int(nEvtPerBinE*ienergy+nEvtPerBinEta*ieta+nEvtPerBinPhi*(iphi+r));
388  else irec = int(nEvtPerBinE*(ienergy+r));
389 
390 #ifdef DebugLog
391  edm::LogInfo("CastorShower") << "CastorShowerLibrary:: Select record " << irec
392  << " of type " << type ;
393 #endif
394 
395  // Retrieve record number "irec" from the library
396  getRecord (type, irec);
397 
398 }
400  //
401  // returns the integer index of the energy bin, taken from SLenergies vector
402  // returns -1 if ouside valid range
403  //
404  if (energy >= SLenergies.back()) return SLenergies.size()-1;
405 
406  unsigned int i = 0;
407  for(;i<SLenergies.size()-1;i++)
408  if (energy >= SLenergies.at(i) && energy < SLenergies.at(i+1)) return (int)i;
409 
410  // now i points to the last but 1 bin
411  if (energy>=SLenergies.at(i)) return (int)i;
412  // energy outside bin range
413  return -1;
414 }
416  //
417  // returns the integer index of the eta bin, taken from SLetas vector
418  // returns -1 if ouside valid range
419  //
420  if (eta>=SLetas.back()) return SLetas.size()-1;
421  unsigned int i = 0;
422  for(;i<SLetas.size()-1;i++)
423  if (eta >= SLetas.at(i) && eta < SLetas.at(i+1)) return (int)i;
424  // now i points to the last but 1 bin
425  if (eta>=SLetas.at(i)) return (int)i;
426  // eta outside bin range
427  return -1;
428 }
430  //
431  // returns the integer index of the phi bin, taken from SLphis vector
432  // returns -1 if ouside valid range
433  //
434  // needs protection in case phi is outside range -pi,pi
435  //
436  if (phi>=SLphis.back()) return SLphis.size()-1;
437  unsigned int i = 0;
438  for(;i<SLphis.size()-1;i++)
439  if (phi >= SLphis.at(i) && phi < SLphis.at(i+1)) return (int)i;
440  // now i points to the last but 1 bin
441  if (phi>=SLphis.at(i)) return (int)i;
442  // phi outside bin range
443  return -1;
444 }
#define LogDebug(id)
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
void initParticleTable(G4ParticleTable *)
T getUntrackedParameter(std::string const &, T const &) const
const double GeV
Definition: MathUtil.h:16
void select(int, double, double=0, double=9)
JetCorrectorParameters::Record record
Definition: classes.h:7
Geom::Theta< T > theta() const
std::vector< double > SLetas
const Double_t pi
unsigned int getNBins()
TBranchObject * emBranch
std::vector< double > SLphis
TBranchObject * hadBranch
T sqrt(T t)
Definition: SSEVec.h:18
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
CastorShowerLibrary(std::string &name, edm::ParameterSet const &p)
unsigned int getNEvts()
#define M_PI
unsigned int getNEvtPerBin()
void initFile(edm::ParameterSet const &)
std::vector< double > SLenergies
CastorShowerLibraryInfo * eventInfo
void loadEventInfo(TBranchObject *)
CastorShowerEvent getShowerHits(G4Step *, bool &)
CastorShowerEvent * showerEvent
TBranchObject * evtInfo
double getBin(int i)
unsigned int getNhit()
Definition: event.py:1