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) {
34 backProb = m_HS.getParameter<
double>(
"BackProbability");
38 "BranchEvt",
"HFShowerLibraryEventInfos_hfshowerlib_HFShowerLibraryEventInfo");
39 std::string branchPre = m_HS.getUntrackedParameter<
std::string>(
"BranchPre",
"HFShowerPhotons_hfshowerlib_");
41 verbose = m_HS.getUntrackedParameter<
bool>(
"Verbosity",
false);
42 applyFidCut = m_HS.getParameter<
bool>(
"ApplyFiducialCut");
44 if (pTreeName.find(
'.') == 0)
45 pTreeName.erase(0, 2);
46 const char* nTree = pTreeName.c_str();
47 hf = TFile::Open(nTree);
50 edm::LogError(
"HFShower") <<
"HFShowerLibrary: opening " << nTree <<
" failed";
51 throw cms::Exception(
"Unknown",
"HFShowerLibrary") <<
"Opening of " << pTreeName <<
" fails\n";
53 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: opening " << nTree <<
" successfully";
56 newForm = (branchEvInfo.empty());
57 TTree*
event(
nullptr);
59 event = (TTree*)
hf->Get(
"HFSimHits");
61 event = (TTree*)
hf->Get(
"Events");
63 TBranch* evtInfo(
nullptr);
66 evtInfo =
event->GetBranch(
info.c_str());
71 edm::LogError(
"HFShower") <<
"HFShowerLibrary: HFShowerLibrayEventInfo"
72 <<
" Branch does not exist in Event";
73 throw cms::Exception(
"Unknown",
"HFShowerLibrary") <<
"Event information absent\n";
76 edm::LogError(
"HFShower") <<
"HFShowerLibrary: Events Tree does not "
78 throw cms::Exception(
"Unknown",
"HFShowerLibrary") <<
"Events tree absent\n";
85 ss <<
"HFShowerLibrary: Energies (GeV) with " <<
nMomBin <<
" bins\n";
87 if (
i / 10 * 10 ==
i &&
i > 0) {
94 std::string nameBr = branchPre + emName + branchPost;
95 emBranch =
event->GetBranch(nameBr.c_str());
98 nameBr = branchPre + hadName + branchPost;
99 hadBranch =
event->GetBranch(nameBr.c_str());
110 <<
" entries and Branch " << hadName <<
" has " <<
hadBranch->GetEntries() <<
" entries"
111 <<
"\n HFShowerLibrary::No packing information -"
112 <<
" Assume x, y, z are not in packed form"
113 <<
"\n Maximum probability cut off " <<
probMax <<
" Back propagation of light prob. "
122 rMax = rTable[rTable.size() - 1];
129 <<
" (Half) Phi Width of wedge " <<
dphi / CLHEP::deg;
145 auto const preStepPoint = aStep->GetPreStepPoint();
146 auto const postStepPoint = aStep->GetPostStepPoint();
147 auto const track = aStep->GetTrack();
149 auto const aParticle =
track->GetDynamicParticle();
150 const G4ThreeVector& momDir = aParticle->GetMomentumDirection();
151 const G4ThreeVector& hitPoint = preStepPoint->GetPosition();
152 int parCode =
track->GetDefinition()->GetPDGEncoding();
156 if (
track->GetDefinition()->IsGeneralIon()) {
157 parCode = 1000020040;
161 G4String partType =
track->GetDefinition()->GetParticleName();
162 const G4ThreeVector localPos = preStepPoint->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(hitPoint);
163 double zoff = localPos.z() + 0.5 *
gpar[1];
165 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::getHits " << partType <<
" of energy "
167 <<
" onlyLong: " << onlyLong <<
" dir.orts " << momDir.x() <<
", " << momDir.y() <<
", "
168 << momDir.z() <<
" Pos x,y,z = " << hitPoint.x() <<
"," << hitPoint.y() <<
","
169 << hitPoint.z() <<
" (" << zoff <<
") sphi,cphi,stheta,ctheta = " <<
sin(momDir.phi())
170 <<
"," <<
cos(momDir.phi()) <<
", " <<
sin(momDir.theta()) <<
"," <<
cos(momDir.theta());
173 double tSlice = (postStepPoint->GetGlobalTime()) / CLHEP::nanosecond;
176 double pin = (
track->GetDefinition()->GetBaryonNumber() > 0) ? preStepPoint->GetKineticEnergy()
177 : preStepPoint->GetTotalEnergy();
179 return fillHits(hitPoint, momDir, parCode, pin, isKilled,
weight, tSlice, onlyLong);
183 const G4ThreeVector& momDir,
190 std::vector<HFShowerLibrary::Hit>
hit;
205 double pz = momDir.
z();
206 double zint = hitPoint.z();
209 bool backward = (pz * zint < 0.) ?
true :
false;
211 double sphi =
sin(momDir.phi());
212 double cphi =
cos(momDir.phi());
213 double ctheta =
cos(momDir.theta());
214 double stheta =
sin(momDir.theta());
232 for (
int i = 0;
i <
npe; ++
i) {
237 if (zv <=
gpar[1] &&
pe[
i].lambda() > 0 && (
pe[
i].
z() >= 0 || (zv >
gpar[0] && (!onlyLong)))) {
240 }
else if (!backward) {
244 double r = G4UniformRand();
251 double pex =
pe[
i].x();
252 double pey =
pe[
i].y();
254 double xx = pex * ctheta * cphi - pey * sphi + zv * stheta * cphi;
255 double yy = pex * ctheta * sphi + pey * cphi + zv * stheta * sphi;
256 double zz = -pex * stheta + zv * ctheta;
258 G4ThreeVector
pos = hitPoint + G4ThreeVector(
xx,
yy,
zz);
260 G4ThreeVector lpos = G4ThreeVector(
pos.x(),
pos.y(), zv);
264 double r =
pos.perp();
266 double fi =
pos.phi();
269 int isect =
int(fi /
dphi) + 1;
270 isect = (isect + 1) / 2;
271 double dfi = ((isect * 2 - 1) *
dphi - fi);
274 double dfir =
r *
sin(dfi);
276 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Position shift " <<
xx <<
", " <<
yy <<
", " <<
zz <<
": "
277 <<
pos <<
" R " <<
r <<
" Phi " << fi <<
" Section " << isect <<
" R*Dfi " << dfir
281 double r1 = G4UniformRand();
282 double r2 = G4UniformRand();
283 double r3 = backward ? G4UniformRand() : -9999.;
289 <<
exp(-
p * zv) <<
" r2 " <<
r2 <<
" r3 " << r3 <<
" rDfi " <<
gpar[5] <<
" zz "
291 <<
" rInside(r) :" <<
rInside(
r) <<
" r1 <= exp(-p*zv) :" << (
r1 <=
exp(-
p * zv))
293 <<
" r3 <= backProb :" << (r3 <=
backProb)
294 <<
" dfir > gpar[5] :" << (dfir >
gpar[5]) <<
" zz >= gpar[4] :" << (
zz >=
gpar[4])
295 <<
" zz <= gpar[4]+gpar[1] :" << (
zz <=
gpar[4] +
gpar[1]);
302 hit.push_back(oneHit);
304 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Final Hit " << nHit <<
" position " << (
hit[nHit].position)
305 <<
" Depth " << (
hit[nHit].
depth) <<
" Time " << tSlice <<
":" <<
pe[
i].t() <<
":"
315 r1 = G4UniformRand();
316 r2 = G4UniformRand();
320 oneHit.
time = (tSlice + (
pe[
i].t()) + (
fibre_->tShift(lpos, 2, 1)));
321 hit.push_back(oneHit);
323 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Final Hit " << nHit <<
" position " << (
hit[nHit].position)
324 <<
" Depth " << (
hit[nHit].
depth) <<
" Time " << tSlice <<
":" <<
pe[
i].t()
325 <<
":" <<
fibre_->tShift(lpos, 2, 1) <<
":" << (
hit[nHit].time);
334 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Total Hits " << nHit <<
" out of " <<
npe <<
" PE";
336 if (nHit >
npe && !onlyLong) {
337 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary: Hit buffer " <<
npe <<
" smaller than " << nHit <<
" Hits";
354 std::vector<float>
t;
355 std::vector<float>*
tp = &
t;
358 unsigned int tSize =
t.size() / 5;
359 photo->reserve(tSize);
360 for (
unsigned int i = 0;
i < tSize;
i++) {
375 std::vector<float>
t;
376 std::vector<float>*
tp = &
t;
379 unsigned int tSize =
t.size() / 5;
380 photo->reserve(tSize);
381 for (
unsigned int i = 0;
i < tSize;
i++) {
394 << nPhoton <<
" photons";
395 for (
int j = 0;
j < nPhoton;
j++)
405 std::vector<HFShowerLibraryEventInfo> eventInfoCollection;
406 branch->SetAddress(&eventInfoCollection);
409 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::loadEventInfo loads EventInfo Collection of size "
410 << eventInfoCollection.size() <<
" records";
412 totEvents = eventInfoCollection[0].totalEvents();
413 nMomBin = eventInfoCollection[0].numberOfBins();
414 evtPerBin = eventInfoCollection[0].eventsPerBin();
415 libVers = eventInfoCollection[0].showerLibraryVersion();
416 listVersion = eventInfoCollection[0].physListVersion();
417 pmom = eventInfoCollection[0].energyBins();
420 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::loadEventInfo loads EventInfo from hardwired"
428 pmom = {2, 3, 5, 7, 10, 15, 20, 30, 50, 75, 100, 150, 250, 350, 500, 1000};
441 double r = G4UniformRand();
460 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[0] = " << irc[0] <<
" now set to 0";
470 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[1] = " << irc[1] <<
" now set to 1";
478 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary:: Select records " << irc[0] <<
" and " << irc[1] <<
" with weights "
479 << 1 -
w <<
" and " <<
w;
484 for (
int ir = 0; ir < 2; ir++) {
489 for (
int j = 0;
j < nPhoton;
j++) {
491 if ((ir == 0 &&
r >
w) || (ir > 0 &&
r <
w)) {
498 if ((
npe > npold || (npold == 0 && irc[0] > 0)) && !(
npe == 0 && npold == 0))
499 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary: Interpolation Warning =="
500 <<
" records " << irc[0] <<
" and " << irc[1] <<
" gives a buffer of " << npold
501 <<
" photons and fills " <<
npe <<
" *****";
504 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Interpolation == records " << irc[0] <<
" and " << irc[1]
505 <<
" gives a buffer of " << npold <<
" photons and fills " <<
npe <<
" PE";
506 for (
int j = 0;
j <
npe;
j++)
517 <<
" momentum bins and " <<
evtPerBin <<
" entries/bin -- "
518 <<
"total " <<
totEvents <<
" using " << nrec <<
" records";
520 std::vector<int> irc(nrec);
522 for (
int ir = 0; ir < nrec; ir++) {
523 double r = G4UniformRand();
526 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[" << ir <<
"] = " << irc[ir] <<
" now set to 1";
529 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary:: Illegal irc[" << ir <<
"] = " << irc[ir] <<
" now set to "
534 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary::Extrapolation use irc[" << ir <<
"] = " << irc[ir];
542 for (
int ir = 0; ir < nrec; ir++) {
547 for (
int j = 0;
j < nPhoton;
j++) {
548 double r = G4UniformRand();
549 if (ir != nrec - 1 ||
r <
w) {
554 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Record [" << ir <<
"] = " << irc[ir] <<
" npold = " << npold;
559 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary:: uses " << npold <<
" photons";
562 if (
npe > npold || npold == 0)
563 edm::LogWarning(
"HFShower") <<
"HFShowerLibrary: Extrapolation Warning == " << nrec <<
" records " << irc[0] <<
", "
564 << irc[1] <<
", ... gives a buffer of " << npold <<
" photons and fills " <<
npe
568 edm::LogVerbatim(
"HFShower") <<
"HFShowerLibrary: Extrapolation == " << nrec <<
" records " << irc[0] <<
", "
569 << irc[1] <<
", ... gives a buffer of " << npold <<
" photons and fills " <<
npe
571 for (
int j = 0;
j <
npe;
j++)