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 
13 
14 #include "G4VPhysicalVolume.hh"
15 #include "G4Step.hh"
16 #include "G4Track.hh"
17 #include "G4ParticleTable.hh"
18 #include "Randomize.hh"
19 #include "CLHEP/Units/PhysicalConstants.h"
20 #include "CLHEP/Units/SystemOfUnits.h"
21 
22 //ROOT
23 #include "TROOT.h"
24 #include "TFile.h"
25 #include "TTree.h"
26 #include "TBranch.h"
27 
28 //#define EDM_ML_DEBUG
29 
31  : hf(nullptr),
32  emBranch(nullptr),
33  hadBranch(nullptr),
34  verbose(false),
35  nMomBin(0),
36  totEvents(0),
37  evtPerBin(0),
38  nBinsE(0),
39  nBinsEta(0),
40  nBinsPhi(0),
41  nEvtPerBinE(0),
42  nEvtPerBinEta(0),
43  nEvtPerBinPhi(0),
44  SLenergies(),
45  SLetas(),
46  SLphis() {
47  initFile(p);
48 }
49 
50 //=============================================================================================
51 
53  if (hf)
54  hf->Close();
55 }
56 
57 //=============================================================================================
58 
61  //
62  // Init TFile and associated TBranch's of CASTOR Root file
63  // holding library events
64  //
66 
67  //
68  // Read PSet for Castor shower library
69  //
70 
71  edm::ParameterSet m_CS = p.getParameter<edm::ParameterSet>("CastorShowerLibrary");
72  edm::FileInPath fp = m_CS.getParameter<edm::FileInPath>("FileName");
73  std::string pTreeName = fp.fullPath();
74  std::string branchEvInfo = m_CS.getUntrackedParameter<std::string>("BranchEvt");
75  std::string branchEM = m_CS.getUntrackedParameter<std::string>("BranchEM");
76  std::string branchHAD = m_CS.getUntrackedParameter<std::string>("BranchHAD");
77  verbose = m_CS.getUntrackedParameter<bool>("Verbosity", false);
78 
79  // Open TFile
80  if (pTreeName.find('.') == 0)
81  pTreeName.erase(0, 2);
82  const char* nTree = pTreeName.c_str();
83  hf = TFile::Open(nTree);
84 
85  // Check that TFile has been successfully opened
86  if (!hf->IsOpen()) {
87  edm::LogError("CastorShower") << "CastorShowerLibrary: opening " << nTree << " failed";
88  throw cms::Exception("Unknown", "CastorShowerLibrary") << "Opening of " << pTreeName << " fails\n";
89  } else {
90  edm::LogVerbatim("CastorShower") << "CastorShowerLibrary: opening " << nTree << " successfully";
91  }
92 
93  // Check for the TBranch holding EventVerbatim in "Events" TTree
94  TTree* event = hf->Get<TTree>("CastorCherenkovPhotons");
95  if (event) {
96  auto evtInfo = event->GetBranch(branchEvInfo.c_str());
97  if (evtInfo) {
98  loadEventInfo(evtInfo);
99  } else {
100  edm::LogError("CastorShower") << "CastorShowerLibrary: " << branchEvInfo.c_str()
101  << " Branch does not exit in Event";
102  throw cms::Exception("Unknown", "CastorShowerLibrary") << "Event information absent\n";
103  }
104  } else {
105  edm::LogError("CastorShower") << "CastorShowerLibrary: Events Tree does not exist";
106  throw cms::Exception("Unknown", "CastorShowerLibrary") << "Events tree absent\n";
107  }
108 
109  // Get EM and HAD Branchs
110  emBranch = event->GetBranch(branchEM.c_str());
111  if (verbose)
112  emBranch->Print();
113  hadBranch = event->GetBranch(branchHAD.c_str());
114  if (verbose)
115  hadBranch->Print();
116  edm::LogVerbatim("CastorShower") << "CastorShowerLibrary: Branch " << branchEM << " has " << emBranch->GetEntries()
117  << " entries and Branch " << branchHAD << " has " << hadBranch->GetEntries()
118  << " entries";
119 }
120 
121 //=============================================================================================
122 
125  //
126  // Get EventInfo from the "TBranch* branch" of Root file
127  // holding library events
128  //
129  // Based on HFShowerLibrary::loadEventInfo
130  //
132 
133  CastorShowerLibraryInfo tempInfo;
134  auto* eventInfo = &tempInfo;
135  branch->SetAddress(&eventInfo);
136  branch->GetEntry(0);
137  // Initialize shower library general parameters
138 
139  totEvents = eventInfo->Energy.getNEvts();
140  nBinsE = eventInfo->Energy.getNBins();
141  nEvtPerBinE = eventInfo->Energy.getNEvtPerBin();
142  SLenergies = eventInfo->Energy.getBin();
143  nBinsEta = eventInfo->Eta.getNBins();
144  nEvtPerBinEta = eventInfo->Eta.getNEvtPerBin();
145  SLetas = eventInfo->Eta.getBin();
146  nBinsPhi = eventInfo->Phi.getNBins();
147  nEvtPerBinPhi = eventInfo->Phi.getNEvtPerBin();
148  SLphis = eventInfo->Phi.getBin();
149 
150  // Convert from GeV to MeV
151  for (unsigned int i = 0; i < SLenergies.size(); i++) {
152  SLenergies[i] *= CLHEP::GeV;
153  }
154 
155  edm::LogVerbatim("CastorShower") << " CastorShowerLibrary::loadEventInfo : "
156  << "\n \n Total number of events : " << totEvents
157  << "\n Number of bins (E) : " << nBinsE
158  << "\n Number of events/bin (E) : " << nEvtPerBinE
159  << "\n Number of bins (Eta) : " << nBinsEta
160  << "\n Number of events/bin (Eta) : " << nEvtPerBinEta
161  << "\n Number of bins (Phi) : " << nBinsPhi
162  << "\n Number of events/bin (Phi) : " << nEvtPerBinPhi << "\n";
163 
164  std::stringstream ss1;
165  ss1 << "CastorShowerLibrary: energies in GeV:\n";
166  for (unsigned int i = 0; i < nBinsE; ++i) {
167  if (i > 0 && i / 10 * 10 == i) {
168  ss1 << "\n";
169  }
170  ss1 << " " << SLenergies[i] / CLHEP::GeV;
171  }
172  ss1 << "\nCastorShowerLibrary: etas:\n";
173  for (unsigned int i = 0; i < nBinsEta; ++i) {
174  if (i > 0 && i / 10 * 10 == i) {
175  ss1 << "\n";
176  }
177  ss1 << " " << SLetas[i];
178  }
179  ss1 << "\nCastorShowerLibrary: phis:\n";
180  for (unsigned int i = 0; i < nBinsPhi; ++i) {
181  if (i > 0 && i / 10 * 10 == i) {
182  ss1 << "\n";
183  }
184  ss1 << " " << SLphis[i];
185  }
186  edm::LogVerbatim("CastorShower") << ss1.str();
187 }
188 
189 //=============================================================================================
190 
192  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
193  G4Track* track = aStep->GetTrack();
194 
195  const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
196 
198  hit.Clear();
199 
200  ok = false;
203  return hit;
204  }
205  ok = true;
206 
207  double pin = preStepPoint->GetTotalEnergy();
208  double zint = hitPoint.z();
209  double R = sqrt(hitPoint.x() * hitPoint.x() + hitPoint.y() * hitPoint.y());
210  double theta = atan2(R, std::abs(zint));
211  double phiin = atan2(hitPoint.y(), hitPoint.x());
212  double etain = -std::log(std::tan((CLHEP::pi - theta) * 0.5));
213 
214  // Replace "interpolation/extrapolation" by new method "select" that just randomly
215  // selects a record from the appropriate energy bin and fills its content to
216  // "showerEvent" instance of class CastorShowerEvent
217 
218  if (isEM) {
219  hit = select(0, pin, etain, phiin);
220  } else {
221  hit = select(1, pin, etain, phiin);
222  }
223 
224  return hit;
225 }
226 
227 //=============================================================================================
228 
231  //
232  // Retrieve event # "record" from the library and stores it
233  // into vector<CastorShowerHit> showerHit;
234  //
235  // Based on HFShowerLibrary::getRecord
236  //
237  // Modified 02/02/09 by W. Carvalho
238  //
240 
241 #ifdef EDM_ML_DEBUG
242  LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: ";
243 #endif
244  int nrc = record;
245  CastorShowerEvent retValue;
246  CastorShowerEvent* showerEvent = &retValue;
247  if (type > 0) {
248  hadBranch->SetAddress(&showerEvent);
249  hadBranch->GetEntry(nrc);
250  } else {
251  emBranch->SetAddress(&showerEvent);
252  emBranch->GetEntry(nrc);
253  }
254 
255 #ifdef EDM_ML_DEBUG
256  int nHit = showerEvent->getNhit();
257  LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: Record " << record << " of type " << type << " with "
258  << nHit << " CastorShowerHits";
259 
260 #endif
261  return retValue;
262 }
263 
264 //=======================================================================================
265 
266 CastorShowerEvent CastorShowerLibrary::select(int type, double pin, double etain, double phiin) {
268  //
269  // Selects an event from the library based on
270  //
271  // type: 0 --> em
272  // >0 --> had
273  // pin : momentum
274  // etain: eta (if not given, disregard the eta binning
275  // phiin: phi (if not given, disregard the phi binning
276  //
277  // Created 30/01/09 by W. Carvalho
278  //
280 
281  int irec; // to hold record number
282  double r = G4UniformRand(); // to randomly select within an energy bin (r=[0,1])
283 
284  // Randomly select a record from the right energy bin in the library,
285  // based on track momentum (pin)
286 
287  int ienergy = FindEnergyBin(pin);
288  int ieta = FindEtaBin(etain);
289 #ifdef EDM_ML_DEBUG
290  if (verbose)
291  edm::LogVerbatim("CastorShower") << " ienergy = " << ienergy;
292  if (verbose)
293  edm::LogVerbatim("CastorShower") << " ieta = " << ieta;
294 #endif
295 
296  int iphi;
297  const double phiMin = 0.;
298  const double phiMax = M_PI / 4.; // 45 * (pi/180) rad
299  if (phiin < phiMin)
300  phiin = phiin + M_PI;
301  if (phiin >= phiMin && phiin < phiMax) {
302  iphi = FindPhiBin(phiin);
303  } else {
304  double remainder = fmod(phiin, phiMax);
305  phiin = phiMin + remainder;
306  iphi = FindPhiBin(phiin);
307  }
308 #ifdef EDM_ML_DEBUG
309  if (verbose)
310  edm::LogVerbatim("CastorShower") << " iphi = " << iphi;
311 #endif
312  if (ienergy == -1)
313  ienergy = 0; // if pin < first bin, choose an event in the first one
314  if (ieta == -1)
315  ieta = 0; // idem for eta
316  if (iphi != -1)
317  irec = int(nEvtPerBinE * ienergy + nEvtPerBinEta * ieta + nEvtPerBinPhi * (iphi + r));
318  else
319  irec = int(nEvtPerBinE * (ienergy + r));
320 
321 #ifdef EDM_ML_DEBUG
322  edm::LogVerbatim("CastorShower") << "CastorShowerLibrary:: Select record " << irec << " of type " << type;
323 #endif
324 
325  // Retrieve record number "irec" from the library
326  return getRecord(type, irec);
327 }
328 
329 //=======================================================================================
330 
332  //
333  // returns the integer index of the energy bin, taken from SLenergies vector
334  // returns -1 if ouside valid range
335  //
336  if (energy >= SLenergies.back())
337  return SLenergies.size() - 1;
338 
339  unsigned int i = 0;
340  for (; i < SLenergies.size() - 1; i++)
341  if (energy >= SLenergies.at(i) && energy < SLenergies.at(i + 1))
342  return (int)i;
343 
344  // now i points to the last but 1 bin
345  if (energy >= SLenergies.at(i))
346  return (int)i;
347  // energy outside bin range
348  return -1;
349 }
350 
351 //=======================================================================================
352 
354  //
355  // returns the integer index of the eta bin, taken from SLetas vector
356  // returns -1 if ouside valid range
357  //
358  if (eta >= SLetas.back())
359  return SLetas.size() - 1;
360  unsigned int i = 0;
361  for (; i < SLetas.size() - 1; i++)
362  if (eta >= SLetas.at(i) && eta < SLetas.at(i + 1))
363  return (int)i;
364  // now i points to the last but 1 bin
365  if (eta >= SLetas.at(i))
366  return (int)i;
367  // eta outside bin range
368  return -1;
369 }
370 
371 //=======================================================================================
372 
374  //
375  // returns the integer index of the phi bin, taken from SLphis vector
376  // returns -1 if ouside valid range
377  //
378  // needs protection in case phi is outside range -pi,pi
379  //
380  if (phi >= SLphis.back())
381  return SLphis.size() - 1;
382  unsigned int i = 0;
383  for (; i < SLphis.size() - 1; i++)
384  if (phi >= SLphis.at(i) && phi < SLphis.at(i + 1))
385  return (int)i;
386  // now i points to the last but 1 bin
387  if (phi >= SLphis.at(i))
388  return (int)i;
389  // phi outside bin range
390  return -1;
391 }
392 
393 //=======================================================================================
Log< level::Info, true > LogVerbatim
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
CastorShowerLibrary(const std::string &name, edm::ParameterSet const &p)
bool verbose
Log< level::Error, false > LogError
std::vector< double > SLetas
const Double_t pi
T getUntrackedParameter(std::string const &, T const &) const
CastorShowerEvent getShowerHits(const G4Step *, bool &)
std::vector< double > SLphis
T sqrt(T t)
Definition: SSEVec.h:19
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define M_PI
static bool isStableHadronIon(const G4Track *)
void initFile(edm::ParameterSet const &)
std::vector< double > SLenergies
void loadEventInfo(TBranch *)
static bool isGammaElectronPositron(int pdgCode)
CastorShowerEvent select(int, double, double=0, double=9)
CastorShowerEvent getRecord(int, int)
eventInfo
add run, event number and lumi section
Geom::Theta< T > theta() const
unsigned int getNhit()
Definition: event.py:1
#define LogDebug(id)