12 #include "G4VPhysicalVolume.hh"
13 #include "G4NavigationHistory.hh"
16 #include "G4ParticleTable.hh"
17 #include "Randomize.hh"
18 #include "CLHEP/Units/SystemOfUnits.h"
19 #include "CLHEP/Units/PhysicalConstants.h"
27 : hcalConstant_(hcons),
hf(nullptr), emBranch(nullptr), hadBranch(nullptr), npe(0) {
37 backProb = m_HS.getParameter<
double>(
"BackProbability");
41 "BranchEvt",
"HFShowerLibraryEventInfos_hfshowerlib_HFShowerLibraryEventInfo");
42 std::string branchPre = m_HS.getUntrackedParameter<
std::string>(
"BranchPre",
"HFShowerPhotons_hfshowerlib_");
44 verbose = m_HS.getUntrackedParameter<
bool>(
"Verbosity",
false);
45 applyFidCut = m_HS.getParameter<
bool>(
"ApplyFiducialCut");
48 if (pTreeName.find(
'.') == 0)
49 pTreeName.erase(0, 2);
50 const char* nTree = pTreeName.c_str();
51 hf = TFile::Open(nTree);
54 edm::LogError(
"HFShower") <<
"HFShowerLibrary: opening " << nTree <<
" failed";
55 throw cms::Exception(
"Unknown",
"HFShowerLibrary") <<
"Opening of " << pTreeName <<
" fails\n";
57 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: opening " << nTree <<
" successfully";
60 newForm = (branchEvInfo.empty());
61 TTree*
event(
nullptr);
63 event = (TTree*)
hf->Get(
"HFSimHits");
65 event = (TTree*)
hf->Get(
"Events");
67 TBranch* evtInfo(
nullptr);
70 evtInfo =
event->GetBranch(
info.c_str());
75 edm::LogError(
"HFShower") <<
"HFShowerLibrary: HFShowerLibrayEventInfo"
76 <<
" Branch does not exist in Event";
77 throw cms::Exception(
"Unknown",
"HFShowerLibrary") <<
"Event information absent\n";
80 edm::LogError(
"HFShower") <<
"HFShowerLibrary: Events Tree does not "
82 throw cms::Exception(
"Unknown",
"HFShowerLibrary") <<
"Events tree absent\n";
88 ss <<
"HFShowerLibrary: Energies (GeV) with " <<
nMomBin <<
" bins\n";
90 if (
i / 10 * 10 ==
i &&
i > 0) {
97 std::string nameBr = branchPre + emName + branchPost;
98 emBranch =
event->GetBranch(nameBr.c_str());
101 nameBr = branchPre + hadName + branchPost;
102 hadBranch =
event->GetBranch(nameBr.c_str());
112 <<
" entries and Branch " << hadName <<
" has " <<
hadBranch->GetEntries()
113 <<
" entries\n HFShowerLibrary::No packing information - Assume x, y, z are not in "
114 "packed form\n Maximum probability cut off "
124 rMax = rTable[rTable.size() - 1];
131 <<
" (Half) Phi Width of wedge " <<
dphi / CLHEP::deg;
147 auto const preStepPoint = aStep->GetPreStepPoint();
148 auto const postStepPoint = aStep->GetPostStepPoint();
149 auto const track = aStep->GetTrack();
151 auto const aParticle =
track->GetDynamicParticle();
152 const G4ThreeVector& momDir = aParticle->GetMomentumDirection();
153 const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
154 int parCode =
track->GetDefinition()->GetPDGEncoding();
158 if (
track->GetDefinition()->IsGeneralIon()) {
159 parCode = 1000020040;
163 G4String partType =
track->GetDefinition()->GetParticleName();
164 const G4ThreeVector localPos = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
165 double zoff = localPos.z() + 0.5 *
gpar[1];
167 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::getHits " << partType <<
" of energy "
169 <<
" onlyLong: " << onlyLong <<
" dir.orts " << momDir.x() <<
", " << momDir.y() <<
", "
170 << momDir.z() <<
" Pos x,y,z = " << hitPoint.x() <<
"," << hitPoint.y() <<
","
171 << hitPoint.z() <<
" (" << zoff <<
") sphi,cphi,stheta,ctheta = " <<
sin(momDir.phi())
172 <<
"," <<
cos(momDir.phi()) <<
", " <<
sin(momDir.theta()) <<
"," <<
cos(momDir.theta());
175 double tSlice = (postStepPoint->GetGlobalTime()) / CLHEP::nanosecond;
178 double pin = (
track->GetDefinition()->GetBaryonNumber() > 0) ? preStepPoint->GetKineticEnergy()
179 : preStepPoint->GetTotalEnergy();
181 return fillHits(hitPoint, momDir, parCode, pin, isKilled,
weight, tSlice, onlyLong);
185 const G4ThreeVector& momDir,
192 std::vector<HFShowerLibrary::Hit>
hit;
207 double pz = momDir.
z();
208 double zint = hitPoint.z();
211 bool backward = (pz * zint < 0.) ?
true :
false;
213 double sphi =
sin(momDir.phi());
214 double cphi =
cos(momDir.phi());
215 double ctheta =
cos(momDir.theta());
216 double stheta =
sin(momDir.theta());
234 for (
int i = 0;
i <
npe; ++
i) {
239 if (
zv <=
gpar[1] &&
pe[
i].lambda() > 0 && (
pe[
i].
z() >= 0 || (
zv >
gpar[0] && (!onlyLong)))) {
242 }
else if (!backward) {
246 double r = G4UniformRand();
253 double pex =
pe[
i].x();
254 double pey =
pe[
i].y();
256 double xx = pex * ctheta * cphi - pey * sphi +
zv * stheta * cphi;
257 double yy = pex * ctheta * sphi + pey * cphi +
zv * stheta * sphi;
258 double zz = -pex * stheta +
zv * ctheta;
260 G4ThreeVector
pos = hitPoint + G4ThreeVector(
xx,
yy,
zz);
262 G4ThreeVector lpos = G4ThreeVector(
pos.x(),
pos.y(),
zv);
266 double r =
pos.perp();
268 double fi =
pos.phi();
271 int isect =
int(fi /
dphi) + 1;
272 isect = (isect + 1) / 2;
273 double dfi = ((isect * 2 - 1) *
dphi - fi);
276 double dfir =
r *
sin(dfi);
278 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Position shift " <<
xx <<
", " <<
yy <<
", " <<
zz <<
": "
279 <<
pos <<
" R " <<
r <<
" Phi " << fi <<
" Section " << isect <<
" R*Dfi " << dfir
283 double r1 = G4UniformRand();
284 double r2 = G4UniformRand();
285 double r3 = backward ? G4UniformRand() : -9999.;
291 <<
exp(-
p *
zv) <<
" r2 " <<
r2 <<
" r3 " << r3 <<
" rDfi " <<
gpar[5] <<
" zz "
293 <<
" rInside(r) :" <<
rInside(
r) <<
" r1 <= exp(-p*zv) :" << (
r1 <=
exp(-
p *
zv))
295 <<
" r3 <= backProb :" << (r3 <=
backProb)
296 <<
" dfir > gpar[5] :" << (dfir >
gpar[5]) <<
" zz >= gpar[4] :" << (
zz >=
gpar[4])
297 <<
" zz <= gpar[4]+gpar[1] :" << (
zz <=
gpar[4] +
gpar[1]);
304 oneHit.
time = (tSlice + (
pe[
i].t()) + tdiff);
305 hit.push_back(oneHit);
308 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Final Hit " << nHit <<
" position " << (
hit[nHit].position)
309 <<
" Depth " << (
hit[nHit].
depth) <<
" Time " << tSlice <<
":" <<
pe[
i].t() <<
":"
310 << tdiff <<
":" << (
hit[nHit].time);
319 r1 = G4UniformRand();
320 r2 = G4UniformRand();
325 oneHit.
time = (tSlice + (
pe[
i].t()) + tdiff);
326 hit.push_back(oneHit);
328 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Final Hit " << nHit <<
" position " << (
hit[nHit].position)
329 <<
" Depth " << (
hit[nHit].
depth) <<
" Time " << tSlice <<
":" <<
pe[
i].t()
330 <<
":" << tdiff <<
":" << (
hit[nHit].time);
339 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Total Hits " << nHit <<
" out of " <<
npe <<
" PE";
341 if (nHit >
npe && !onlyLong) {
342 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary: Hit buffer " <<
npe <<
" smaller than " << nHit <<
" Hits";
359 std::vector<float>
t;
360 std::vector<float>*
tp = &
t;
363 unsigned int tSize =
t.size() / 5;
364 photo->reserve(tSize);
365 for (
unsigned int i = 0;
i < tSize;
i++) {
380 std::vector<float>
t;
381 std::vector<float>*
tp = &
t;
384 unsigned int tSize =
t.size() / 5;
385 photo->reserve(tSize);
386 for (
unsigned int i = 0;
i < tSize;
i++) {
399 << nPhoton <<
" photons";
400 for (
int j = 0;
j < nPhoton;
j++)
410 std::vector<HFShowerLibraryEventInfo> eventInfoCollection;
411 branch->SetAddress(&eventInfoCollection);
413 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::loadEventInfo loads EventInfo Collection of size "
414 << eventInfoCollection.size() <<
" records";
416 totEvents = eventInfoCollection[0].totalEvents();
417 nMomBin = eventInfoCollection[0].numberOfBins();
418 evtPerBin = eventInfoCollection[0].eventsPerBin();
419 libVers = eventInfoCollection[0].showerLibraryVersion();
420 listVersion = eventInfoCollection[0].physListVersion();
421 pmom = eventInfoCollection[0].energyBins();
423 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::loadEventInfo loads EventInfo from hardwired"
431 pmom = {2, 3, 5, 7, 10, 15, 20, 30, 50, 75, 100, 150, 250, 350, 500, 1000};
440 <<
nMomBin <<
" momentum bins and " <<
evtPerBin <<
" entries/bin -- total "
445 double r = G4UniformRand();
464 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[0] = " << irc[0] <<
" now set to 0";
474 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[1] = " << irc[1] <<
" now set to 1";
482 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary:: Select records " << irc[0] <<
" and " << irc[1] <<
" with weights "
483 << 1 -
w <<
" and " <<
w;
488 for (
int ir = 0; ir < 2; ir++) {
493 for (
int j = 0;
j < nPhoton;
j++) {
495 if ((ir == 0 &&
r >
w) || (ir > 0 &&
r <
w)) {
502 if ((
npe > npold || (npold == 0 && irc[0] > 0)) && !(
npe == 0 && npold == 0))
503 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary: Interpolation Warning =="
504 <<
" records " << irc[0] <<
" and " << irc[1] <<
" gives a buffer of " << npold
505 <<
" photons and fills " <<
npe <<
" *****";
508 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Interpolation == records " << irc[0] <<
" and " << irc[1]
509 <<
" gives a buffer of " << npold <<
" photons and fills " <<
npe <<
" PE";
510 for (
int j = 0;
j <
npe;
j++)
522 <<
"total " <<
totEvents <<
" using " << nrec <<
" records";
524 std::vector<int> irc(nrec);
526 for (
int ir = 0; ir < nrec; ir++) {
527 double r = G4UniformRand();
530 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[" << ir <<
"] = " << irc[ir] <<
" now set to 1";
533 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[" << ir <<
"] = " << irc[ir] <<
" now set to "
538 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::Extrapolation use irc[" << ir <<
"] = " << irc[ir];
546 for (
int ir = 0; ir < nrec; ir++) {
551 for (
int j = 0;
j < nPhoton;
j++) {
552 double r = G4UniformRand();
553 if (ir != nrec - 1 ||
r <
w) {
558 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Record [" << ir <<
"] = " << irc[ir] <<
" npold = " << npold;
563 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary:: uses " << npold <<
" photons";
566 if (
npe > npold || npold == 0)
567 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary: Extrapolation Warning == " << nrec <<
" records " << irc[0] <<
", "
568 << irc[1] <<
", ... gives a buffer of " << npold <<
" photons and fills " <<
npe
572 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Extrapolation == " << nrec <<
" records " << irc[0] <<
", "
573 << irc[1] <<
", ... gives a buffer of " << npold <<
" photons and fills " <<
npe
575 for (
int j = 0;
j <
npe;
j++)