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 "TBranchObject.h"
27 
28 //#define DebugLog
29 
31  : hf(nullptr),
32  evtInfo(nullptr),
33  emBranch(nullptr),
34  hadBranch(nullptr),
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 = (TTree*)hf->Get("CastorCherenkovPhotons");
95  if (event) {
96  evtInfo = (TBranchObject*)event->GetBranch(branchEvInfo.c_str());
97  if (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 = (TBranchObject*)event->GetBranch(branchEM.c_str());
111  if (verbose)
112  emBranch->Print();
113  hadBranch = (TBranchObject*)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 
134  branch->SetAddress(&eventInfo);
135  branch->GetEntry(0);
136  // Initialize shower library general parameters
137 
144  SLetas = eventInfo->Eta.getBin();
147  SLphis = eventInfo->Phi.getBin();
148 
149  // Convert from GeV to MeV
150  for (unsigned int i = 0; i < SLenergies.size(); i++) {
151  SLenergies[i] *= CLHEP::GeV;
152  }
153 
154  edm::LogVerbatim("CastorShower") << " CastorShowerLibrary::loadEventInfo : "
155  << "\n \n Total number of events : " << totEvents
156  << "\n Number of bins (E) : " << nBinsE
157  << "\n Number of events/bin (E) : " << nEvtPerBinE
158  << "\n Number of bins (Eta) : " << nBinsEta
159  << "\n Number of events/bin (Eta) : " << nEvtPerBinEta
160  << "\n Number of bins (Phi) : " << nBinsPhi
161  << "\n Number of events/bin (Phi) : " << nEvtPerBinPhi << "\n";
162 
163  std::stringstream ss1;
164  ss1 << "CastorShowerLibrary: energies in GeV:\n";
165  for (unsigned int i = 0; i < nBinsE; ++i) {
166  if (i > 0 && i / 10 * 10 == i) {
167  ss1 << "\n";
168  }
169  ss1 << " " << SLenergies[i] / CLHEP::GeV;
170  }
171  ss1 << "\nCastorShowerLibrary: etas:\n";
172  for (unsigned int i = 0; i < nBinsEta; ++i) {
173  if (i > 0 && i / 10 * 10 == i) {
174  ss1 << "\n";
175  }
176  ss1 << " " << SLetas[i];
177  }
178  ss1 << "\nCastorShowerLibrary: phis:\n";
179  for (unsigned int i = 0; i < nBinsPhi; ++i) {
180  if (i > 0 && i / 10 * 10 == i) {
181  ss1 << "\n";
182  }
183  ss1 << " " << SLphis[i];
184  }
185  edm::LogVerbatim("CastorShower") << ss1.str();
186 }
187 
188 //=============================================================================================
189 
191  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
192  G4Track* track = aStep->GetTrack();
193 
194  const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
195 
197  hit.Clear();
198 
199  ok = false;
202  return hit;
203  }
204  ok = true;
205 
206  double pin = preStepPoint->GetTotalEnergy();
207  double zint = hitPoint.z();
208  double R = sqrt(hitPoint.x() * hitPoint.x() + hitPoint.y() * hitPoint.y());
209  double theta = atan2(R, std::abs(zint));
210  double phiin = atan2(hitPoint.y(), hitPoint.x());
211  double etain = -std::log(std::tan((CLHEP::pi - theta) * 0.5));
212 
213  // Replace "interpolation/extrapolation" by new method "select" that just randomly
214  // selects a record from the appropriate energy bin and fills its content to
215  // "showerEvent" instance of class CastorShowerEvent
216 
217  if (isEM) {
218  select(0, pin, etain, phiin);
219  } else {
220  select(1, pin, etain, phiin);
221  }
222 
223  hit = (*showerEvent);
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 DebugLog
242  LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: ";
243 #endif
244  int nrc = record;
246  if (type > 0) {
247  hadBranch->SetAddress(&showerEvent);
248  hadBranch->GetEntry(nrc);
249  } else {
250  emBranch->SetAddress(&showerEvent);
251  emBranch->GetEntry(nrc);
252  }
253 
254 #ifdef DebugLog
255  int nHit = showerEvent->getNhit();
256  LogDebug("CastorShower") << "CastorShowerLibrary::getRecord: Record " << record << " of type " << type << " with "
257  << nHit << " CastorShowerHits";
258 
259 #endif
260 }
261 
262 //=======================================================================================
263 
264 void CastorShowerLibrary::select(int type, double pin, double etain, double phiin) {
266  //
267  // Selects an event from the library based on
268  //
269  // type: 0 --> em
270  // >0 --> had
271  // pin : momentum
272  // etain: eta (if not given, disregard the eta binning
273  // phiin: phi (if not given, disregard the phi binning
274  //
275  // Created 30/01/09 by W. Carvalho
276  //
278 
279  int irec; // to hold record number
280  double r = G4UniformRand(); // to randomly select within an energy bin (r=[0,1])
281 
282  // Randomly select a record from the right energy bin in the library,
283  // based on track momentum (pin)
284 
285  int ienergy = FindEnergyBin(pin);
286  int ieta = FindEtaBin(etain);
287 #ifdef DebugLog
288  if (verbose)
289  edm::LogVerbatim("CastorShower") << " ienergy = " << ienergy;
290  if (verbose)
291  edm::LogVerbatim("CastorShower") << " ieta = " << ieta;
292 #endif
293 
294  int iphi;
295  const double phiMin = 0.;
296  const double phiMax = M_PI / 4.; // 45 * (pi/180) rad
297  if (phiin < phiMin)
298  phiin = phiin + M_PI;
299  if (phiin >= phiMin && phiin < phiMax) {
300  iphi = FindPhiBin(phiin);
301  } else {
302  double remainder = fmod(phiin, phiMax);
303  phiin = phiMin + remainder;
304  iphi = FindPhiBin(phiin);
305  }
306 #ifdef DebugLog
307  if (verbose)
308  edm::LogVerbatim("CastorShower") << " iphi = " << iphi;
309 #endif
310  if (ienergy == -1)
311  ienergy = 0; // if pin < first bin, choose an event in the first one
312  if (ieta == -1)
313  ieta = 0; // idem for eta
314  if (iphi != -1)
315  irec = int(nEvtPerBinE * ienergy + nEvtPerBinEta * ieta + nEvtPerBinPhi * (iphi + r));
316  else
317  irec = int(nEvtPerBinE * (ienergy + r));
318 
319 #ifdef DebugLog
320  edm::LogVerbatim("CastorShower") << "CastorShowerLibrary:: Select record " << irec << " of type " << type;
321 #endif
322 
323  // Retrieve record number "irec" from the library
324  getRecord(type, irec);
325 }
326 
327 //=======================================================================================
328 
330  //
331  // returns the integer index of the energy bin, taken from SLenergies vector
332  // returns -1 if ouside valid range
333  //
334  if (energy >= SLenergies.back())
335  return SLenergies.size() - 1;
336 
337  unsigned int i = 0;
338  for (; i < SLenergies.size() - 1; i++)
339  if (energy >= SLenergies.at(i) && energy < SLenergies.at(i + 1))
340  return (int)i;
341 
342  // now i points to the last but 1 bin
343  if (energy >= SLenergies.at(i))
344  return (int)i;
345  // energy outside bin range
346  return -1;
347 }
348 
349 //=======================================================================================
350 
352  //
353  // returns the integer index of the eta bin, taken from SLetas vector
354  // returns -1 if ouside valid range
355  //
356  if (eta >= SLetas.back())
357  return SLetas.size() - 1;
358  unsigned int i = 0;
359  for (; i < SLetas.size() - 1; i++)
360  if (eta >= SLetas.at(i) && eta < SLetas.at(i + 1))
361  return (int)i;
362  // now i points to the last but 1 bin
363  if (eta >= SLetas.at(i))
364  return (int)i;
365  // eta outside bin range
366  return -1;
367 }
368 
369 //=======================================================================================
370 
372  //
373  // returns the integer index of the phi bin, taken from SLphis vector
374  // returns -1 if ouside valid range
375  //
376  // needs protection in case phi is outside range -pi,pi
377  //
378  if (phi >= SLphis.back())
379  return SLphis.size() - 1;
380  unsigned int i = 0;
381  for (; i < SLphis.size() - 1; i++)
382  if (phi >= SLphis.at(i) && phi < SLphis.at(i + 1))
383  return (int)i;
384  // now i points to the last but 1 bin
385  if (phi >= SLphis.at(i))
386  return (int)i;
387  // phi outside bin range
388  return -1;
389 }
390 
391 //=======================================================================================
CastorShowerLibrary::SLphis
std::vector< double > SLphis
Definition: CastorShowerLibrary.h:65
CastorShowerLibrary::nBinsEta
unsigned int nBinsEta
Definition: CastorShowerLibrary.h:61
CastorShowerLibrary::emBranch
TBranchObject * emBranch
Definition: CastorShowerLibrary.h:49
mps_fire.i
i
Definition: mps_fire.py:428
HLT_FULL_cff.track
track
Definition: HLT_FULL_cff.py:11776
CastorShowerLibrary::initFile
void initFile(edm::ParameterSet const &)
Definition: CastorShowerLibrary.cc:59
CastorShowerEvent::getNhit
unsigned int getNhit()
Definition: CastorShowerEvent.h:50
MicroEventContent_cff.branch
branch
Definition: MicroEventContent_cff.py:178
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CastorShowerLibrary.h
GlobalPosition_Frontier_DevDB_cff.record
record
Definition: GlobalPosition_Frontier_DevDB_cff.py:10
CastorShowerLibrary::SLetas
std::vector< double > SLetas
Definition: CastorShowerLibrary.h:64
CastorShowerLibrary::FindPhiBin
int FindPhiBin(double)
Definition: CastorShowerLibrary.cc:371
personalPlayback.fp
fp
Definition: personalPlayback.py:523
CastorShowerLibrary::getShowerHits
CastorShowerEvent getShowerHits(const G4Step *, bool &)
Definition: CastorShowerLibrary.cc:190
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
convertSQLiteXML.ok
bool ok
Definition: convertSQLiteXML.py:98
CastorShowerLibrary::evtInfo
TBranchObject * evtInfo
Definition: CastorShowerLibrary.h:48
CastorShowerLibrary::FindEnergyBin
int FindEnergyBin(double)
Definition: CastorShowerLibrary.cc:329
G4TrackToParticleID::isGammaElectronPositron
static bool isGammaElectronPositron(int pdgCode)
Definition: G4TrackToParticleID.cc:17
CastorShowerLibrary::hf
TFile * hf
Definition: CastorShowerLibrary.h:47
LEDCalibrationChannels.iphi
iphi
Definition: LEDCalibrationChannels.py:64
CastorShowerLibrary::FindEtaBin
int FindEtaBin(double)
Definition: CastorShowerLibrary.cc:351
CastorShowerLibrary::nEvtPerBinPhi
unsigned int nEvtPerBinPhi
Definition: CastorShowerLibrary.h:62
CastorShowerLibrary::nEvtPerBinE
unsigned int nEvtPerBinE
Definition: CastorShowerLibrary.h:62
edm::FileInPath
Definition: FileInPath.h:64
CastorShowerLibraryInfo
Definition: CastorShowerLibraryInfo.h:41
CastorShowerLibrary::CastorShowerLibrary
CastorShowerLibrary(const std::string &name, edm::ParameterSet const &p)
Definition: CastorShowerLibrary.cc:30
photonIsolationHIProducer_cfi.hf
hf
Definition: photonIsolationHIProducer_cfi.py:9
PVValHelper::eta
Definition: PVValidationHelpers.h:69
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
hgcalTowerMapProducer_cfi.nBinsEta
nBinsEta
Definition: hgcalTowerMapProducer_cfi.py:9
AlignmentTrackSelector_cfi.phiMin
phiMin
Definition: AlignmentTrackSelector_cfi.py:18
CastorShowerLibrary::totEvents
unsigned int totEvents
Definition: CastorShowerLibrary.h:56
CastorShowerLibrary::select
void select(int, double, double=0, double=9)
Definition: CastorShowerLibrary.cc:264
AlignmentTrackSelector_cfi.phiMax
phiMax
Definition: AlignmentTrackSelector_cfi.py:17
SLBin::getNEvts
unsigned int getNEvts()
Definition: CastorShowerLibraryInfo.h:27
theta
Geom::Theta< T > theta() const
Definition: Basic3DVectorLD.h:150
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
SLBin::getNEvtPerBin
unsigned int getNEvtPerBin()
Definition: CastorShowerLibraryInfo.h:29
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
SLBin::getNBins
unsigned int getNBins()
Definition: CastorShowerLibraryInfo.h:28
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
edm::ParameterSet
Definition: ParameterSet.h:47
PVValHelper::phi
Definition: PVValidationHelpers.h:68
CastorShowerEvent
Definition: CastorShowerEvent.h:15
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
GeV
const double GeV
Definition: MathUtil.h:16
gainCalibHelper::gainCalibPI::type
type
Definition: SiPixelGainCalibHelper.h:39
CastorShowerLibrary::verbose
bool verbose
Definition: CastorShowerLibrary.h:55
funct::tan
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
CastorShowerLibrary::hadBranch
TBranchObject * hadBranch
Definition: CastorShowerLibrary.h:49
createfilelist.int
int
Definition: createfilelist.py:10
M_PI
#define M_PI
Definition: BXVectorInputProducer.cc:49
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
CastorShowerLibrary::nBinsPhi
unsigned int nBinsPhi
Definition: CastorShowerLibrary.h:61
alignCSCRings.r
r
Definition: alignCSCRings.py:93
DDAxes::phi
CastorShowerLibraryInfo::Phi
SLBin Phi
Definition: CastorShowerLibraryInfo.h:51
G4TrackToParticleID.h
CastorShowerLibrary::nEvtPerBinEta
unsigned int nEvtPerBinEta
Definition: CastorShowerLibrary.h:62
CastorShowerLibrary::nBinsE
unsigned int nBinsE
Definition: CastorShowerLibrary.h:61
G4TrackToParticleID::isStableHadronIon
static bool isStableHadronIon(const G4Track *)
Definition: G4TrackToParticleID.cc:40
CastorShowerLibrary::SLenergies
std::vector< double > SLenergies
Definition: CastorShowerLibrary.h:63
CastorShowerLibrary::eventInfo
CastorShowerLibraryInfo * eventInfo
Definition: CastorShowerLibrary.h:52
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
Exception
Definition: hltDiff.cc:246
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
Exception.h
Phase1L1TJetProducer_cfi.nBinsPhi
nBinsPhi
Definition: Phase1L1TJetProducer_cfi.py:20
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
CastorShowerLibrary::loadEventInfo
void loadEventInfo(TBranchObject *)
Definition: CastorShowerLibrary.cc:123
CastorShowerLibrary::getRecord
void getRecord(int, int)
Definition: CastorShowerLibrary.cc:229
CastorShowerLibrary::showerEvent
CastorShowerEvent * showerEvent
Definition: CastorShowerLibrary.h:53
CastorShowerLibraryInfo::Eta
SLBin Eta
Definition: CastorShowerLibraryInfo.h:50
pi
const Double_t pi
Definition: trackSplitPlot.h:36
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
event
Definition: event.py:1
dttmaxenums::R
Definition: DTTMax.h:29
CastorShowerLibraryInfo::Energy
SLBin Energy
Definition: CastorShowerLibraryInfo.h:49
CastorShowerLibrary::~CastorShowerLibrary
~CastorShowerLibrary()
Definition: CastorShowerLibrary.cc:52
SLBin::getBin
double getBin(int i)
Definition: CastorShowerLibraryInfo.h:30
hit
Definition: SiStripHitEffFromCalibTree.cc:88