54 #include "G4SDManager.hh"
57 #include "G4ThreeVector.hh"
58 #include "G4VProcess.hh"
59 #include "G4HCofThisEvent.hh"
60 #include "CLHEP/Random/RandGaussQ.h"
61 #include "CLHEP/Units/GlobalSystemOfUnits.h"
62 #include "CLHEP/Units/GlobalPhysicalConstants.h"
63 #include "Randomize.hh"
69 class HepRandomEngine;
74 public Observer<const BeginOfEvent*>,
104 double scale(
int det,
int layer);
164 double beamEta = (fMaxEta + fMinEta) / 2.;
165 double beamPhi = (fMaxPhi + fMinPhi) / 2.;
166 double beamThet = 2 * atan(
exp(-beamEta));
170 icphi = (
int)(fabs(beamPhi) / 0.087) + 5;
174 produces<PHcalTB04Info>();
181 <<
"HcalTB04:: Initialised as observer of BeginOf Job/BeginOfRun/BeginOfEvent/G4Step/EndOfEvent with Parameter "
182 "values:\n \thcalOnly = "
183 <<
hcalOnly <<
"\tecalNoise = " <<
ecalNoise <<
"\n\tMode = " <<
mode <<
" (0: HB2 Standard; 1:HB2 Segmented)"
184 <<
"\tType = " <<
type <<
" (0: HB; 1 HE; 2 HB+HE)\n\tbeamOffset = " <<
beamOffset <<
"\ticeta = " <<
iceta
194 edm::LogVerbatim(
"HcalTBSim") <<
"\n --------> Total number of selected entries : " <<
count <<
"\nPointers:: QIE "
234 for (
int lay = 1; lay < 8; lay++) {
235 for (
int icr = 1; icr < 8; icr++) {
258 for (
int i = 0;
i < 5;
i++) {
262 for (
int i = 0;
i < 3;
i++) {
266 for (
int i = 0;
i < 20;
i++) {
278 int irun = (*run)()->GetRunID();
281 G4SDManager*
sd = G4SDManager::GetSDMpointerIfExist();
284 G4VSensitiveDetector* aSD =
sd->FindSensitiveDetector(sdname);
285 if (aSD ==
nullptr) {
287 <<
" with name " << sdname <<
" in this "
290 HCalSD* theCaloSD = dynamic_cast<HCalSD*>(aSD);
291 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: Finds SD with name " << theCaloSD->GetName()
295 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: set a new numbering scheme";
299 aSD =
sd->FindSensitiveDetector(sdname);
300 if (aSD ==
nullptr) {
302 <<
" with name " << sdname <<
" in this "
305 ECalSD* theCaloSD = dynamic_cast<ECalSD*>(aSD);
306 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: Finds SD with name " << theCaloSD->GetName()
310 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: set a new numbering scheme";
315 <<
"not get SD Manager!";
320 evNum = (*evt)()->GetEventID();
326 if (aStep !=
nullptr) {
328 G4ThreeVector thePreStepPoint = aStep->GetPreStepPoint()->GetPosition();
329 G4ThreeVector thePostStepPoint;
332 G4Track* aTrack = aStep->GetTrack();
333 int trackID = aTrack->GetTrackID();
334 int parentID = aTrack->GetParentID();
335 const G4ThreeVector&
position = aTrack->GetPosition();
336 G4ThreeVector momentum = aTrack->GetMomentum();
337 G4String partType = aTrack->GetDefinition()->GetParticleType();
338 G4String partSubType = aTrack->GetDefinition()->GetParticleSubType();
339 int partPDGEncoding = aTrack->GetDefinition()->GetPDGEncoding();
341 bool isPDGStable = aTrack->GetDefinition()->GetPDGStable();
343 double pDGlifetime = aTrack->GetDefinition()->GetPDGLifeTime();
344 double gammaFactor = aStep->GetPreStepPoint()->GetGamma();
347 double stepDeltaEnergy = aStep->GetDeltaEnergy();
348 double kinEnergy = aTrack->GetKineticEnergy();
351 if (trackID == 1 && parentID == 0 && ((kinEnergy == 0.) || (fabs(stepDeltaEnergy / kinEnergy) > 0.1))) {
353 if (kinEnergy == 0.) {
356 if (fabs(stepDeltaEnergy / kinEnergy) > 0.1)
366 G4String thePostPVname =
"NoName";
367 G4StepPoint* thePostPoint = aStep->GetPostStepPoint();
369 thePostStepPoint = thePostPoint->GetPosition();
370 G4VPhysicalVolume* thePostPV = thePostPoint->GetPhysicalVolume();
372 thePostPVname = thePostPV->GetName();
375 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: V1 found at: " << thePostStepPoint
376 <<
" G4VPhysicalVolume: " << thePostPVname;
383 if ((trackID != 1 && parentID == 1 && (aTrack->GetCurrentStepNumber() == 1) && (thePreStepPoint ==
pvPosition)) ||
384 (trackID == 1 && thePreStepPoint ==
pvPosition)) {
386 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::A secondary... PDG:" << partPDGEncoding
387 <<
" TrackID:" << trackID <<
" ParentID:" << parentID
388 <<
" stable: " << isPDGStable <<
" Tau: " << pDGlifetime
389 <<
" cTauGamma=" << c_light * pDGlifetime * gammaFactor * 1000.
390 <<
"um GammaFactor: " << gammaFactor;
395 secEkin.push_back(aTrack->GetKineticEnergy());
398 double ctaugamma_um = CLHEP::c_light * pDGlifetime * gammaFactor * 1000.;
399 if ((ctaugamma_um > 0.) && (ctaugamma_um < 100.)) {
407 if (aTrack->GetCurrentStepNumber() == 1) {
412 std::vector<int>::iterator
pos;
418 <<
"HcalTB04Analysis:: A tertiary... PDG:" << partPDGEncoding <<
" TrackID:" << trackID
419 <<
" ParentID:" << parentID <<
" stable: " << isPDGStable <<
" Tau: " << pDGlifetime
420 <<
" cTauGamma=" << c_light * pDGlifetime * gammaFactor * 1000. <<
"um GammaFactor: " << gammaFactor;
435 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::Fill event " << (*evt)()->GetEventID();
443 CLHEP::HepRandomEngine* engine = G4Random::getTheEngine();
460 int iEvt = (*evt)()->GetEventID();
463 else if ((iEvt < 100) && (iEvt % 10 == 0))
465 else if ((iEvt < 1000) && (iEvt % 100 == 0))
467 else if ((iEvt < 10000) && (iEvt % 1000 == 0))
472 std::vector<CaloHit> hhits, hhitl;
475 std::map<int, float, std::less<int> > primaries;
476 double etot1 = 0, etot2 = 0;
479 G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
481 idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
484 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hit Collection for " << sdName <<
" of ID " << idHC
485 <<
" is obtained at " << theHC <<
" with " << theHC->entries() <<
" entries";
487 int thehc_entries = theHC->entries();
488 if (idHC >= 0 && theHC !=
nullptr) {
489 hhits.reserve(theHC->entries());
490 hhitl.reserve(theHC->entries());
491 for (
j = 0;
j < thehc_entries;
j++) {
507 double escl =
e *
scale(det, layer);
510 hhits.push_back(
hit);
512 hhitl.push_back(hitl);
516 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hcal Hit i/p " <<
j <<
" ID 0x" << std::hex <<
id <<
" 0x"
517 <<
idx <<
std::dec <<
" time " << std::setw(6) <<
time <<
" " << std::setw(6)
518 << jitter <<
" theta " << std::setw(8) <<
theta <<
" eta " << std::setw(8) <<
eta
519 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) <<
e <<
" "
520 << std::setw(8) << escl;
526 std::vector<CaloHit>::iterator
itr;
527 int nHit = hhits.size();
528 std::vector<CaloHit*>
hits(nHit);
529 for (
j = 0,
itr = hhits.begin();
itr != hhits.end();
j++,
itr++) {
533 std::vector<CaloHit*>::iterator k1, k2;
535 for (k1 =
hits.begin(); k1 !=
hits.end(); k1++) {
536 int det = (**k1).det();
537 int layer = (**k1).layer();
538 double ehit = (**k1).e();
539 double eta = (**k1).eta();
540 double phi = (**k1).phi();
541 double jitter = (**k1).t();
542 uint32_t
unitID = (**k1).id();
544 for (k2 = k1 + 1; k2 !=
hits.end() && fabs(jitter - (**k2).t()) < 1 &&
unitID == (**k2).id(); k2++) {
554 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hcal Hit store " << nhit <<
" ID 0x" << std::hex <<
unitID
555 <<
std::dec <<
" time " << std::setw(6) << jitter <<
" eta " << std::setw(8) <<
eta
556 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) << ehit;
560 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Stores " << nhit <<
" HCal hits from " << nHit
561 <<
" input hits E(Hcal) " << etot1 <<
" " << etot2;
564 for (
j = 0,
itr = hhitl.begin();
itr != hhitl.end();
j++,
itr++) {
570 for (k1 =
hits.begin(); k1 !=
hits.end(); k1++) {
571 int det = (**k1).det();
572 int layer = (**k1).layer();
573 double ehit = (**k1).e();
574 double eta = (**k1).eta();
575 double phi = (**k1).phi();
576 double jitter = (**k1).t();
577 uint32_t
unitID = (**k1).id();
579 for (k2 = k1 + 1; k2 !=
hits.end() && fabs(jitter - (**k2).t()) < 1 &&
unitID == (**k2).id(); k2++) {
589 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hcal Hit store " << nhitl <<
" ID 0x" << std::hex <<
unitID
590 <<
std::dec <<
" time " << std::setw(6) << jitter <<
" eta " << std::setw(8) <<
eta
591 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) << ehit;
595 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Stores " << nhitl <<
" HCal hits from " << nHit
596 <<
" input hits E(Hcal) " << etot1 <<
" " << etotl;
599 std::vector<CaloHit> ehits;
601 idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
605 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hit Collection for " << sdName <<
" of ID " << idHC
606 <<
" is obtained at " << theHC <<
" with " << theHC->entries() <<
" entries";
608 if (idHC >= 0 && theHC !=
nullptr) {
609 thehc_entries = theHC->entries();
610 ehits.reserve(theHC->entries());
611 for (
j = 0;
j < thehc_entries;
j++) {
615 if (e < 0 || e > 100000.)
626 ehits.push_back(
hit);
630 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Ecal Hit i/p " <<
j <<
" ID 0x" << std::hex <<
id
631 <<
std::dec <<
" time " << std::setw(6) <<
time <<
" theta " << std::setw(8)
632 <<
theta <<
" eta " << std::setw(8) <<
eta <<
" phi " << std::setw(8) <<
phi
633 <<
" e " << std::setw(8) <<
e;
641 std::vector<CaloHit*> hite(nHit);
642 for (
j = 0,
itr = ehits.begin();
itr != ehits.end();
j++,
itr++) {
647 for (k1 = hite.begin(); k1 != hite.end(); k1++) {
648 int det = (**k1).det();
649 int layer = (**k1).layer();
650 double ehit = (**k1).e();
651 double eta = (**k1).eta();
652 double phi = (**k1).phi();
653 double jitter = (**k1).t();
654 uint32_t
unitID = (**k1).id();
656 for (k2 = k1 + 1; k2 != hite.end() && fabs(jitter - (**k2).t()) < 1 &&
unitID == (**k2).id(); k2++) {
666 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Ecal Hit store " << nhit <<
" ID 0x" << std::hex <<
unitID
667 <<
std::dec <<
" time " << std::setw(6) << jitter <<
" eta " << std::setw(8) <<
eta
668 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) << ehit;
672 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Stores " << nhit <<
" ECal hits from " << nHit
673 <<
" input hits E(Ecal) " << etot1 <<
" " << etot2;
678 G4PrimaryParticle* thePrim =
nullptr;
679 int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
681 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Event has " << nvertex <<
" verteices";
684 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::EndOfEvent ERROR: no vertex found for event " <<
evNum;
685 for (
int i = 0;
i < nvertex;
i++) {
686 G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(
i);
687 if (avertex ==
nullptr) {
688 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::EndOfEvent ERR: pointer to vertex = 0 for event " <<
evNum;
690 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::Vertex number :" <<
i <<
" " << avertex->GetPosition();
691 int npart = avertex->GetNumberOfParticle();
693 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::End Of Event ERR: no primary!";
694 if (thePrim ==
nullptr)
695 thePrim = avertex->GetPrimary(trackID);
699 if (thePrim !=
nullptr) {
700 double px = thePrim->GetPx();
701 double py = thePrim->GetPy();
702 double pz = thePrim->GetPz();
706 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis:: EndOfEvent ERR: primary has p=0 ";
708 double costheta = pz /
p;
711 if (
px != 0 ||
py != 0)
716 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::EndOfEvent ERR: could not find primary";
723 std::vector<CaloHit>
hits(hittot);
731 for (
unsigned int k1 = 0; k1 <
hcalHitCache.size(); k1++) {
733 uint32_t
id =
hit.
id();
735 double esim =
hit.e();
737 for (
unsigned int k2 = k1 + 1; k2 <
hcalHitCache.size(); k2++) {
751 <<
" energy from " << nhit <<
" hits starting with hit # " << k1
752 <<
" energy with noise " <<
eq;
754 for (
int k2 = 0; k2 <
nTower; k2++) {
764 for (
int k2 = 0; k2 <
nTower; k2++) {
772 <<
" registers " <<
esimh[k2] <<
" energy from hits and energy after QIE analysis "
780 CLHEP::RandGaussQ randGauss(*engine);
785 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::xtalAnalysis: Size " << iok.size() <<
" " <<
idEcal.size() <<
" "
788 for (
unsigned int k1 = 0; k1 <
ecalHitCache.size(); k1++) {
792 for (
unsigned int k2 = k1 + 1; k2 <
ecalHitCache.size(); k2++) {
803 <<
" energy from " << nhit <<
" hits starting with hit # " << k1
804 <<
" energy with noise " <<
eq;
806 for (
int k2 = 0; k2 <
nCrystal; k2++) {
816 for (
int k2 = 0; k2 <
nCrystal; k2++) {
822 <<
" registers " <<
esime[k2] <<
" energy from hits and energy from noise "
846 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Energy deposit at Sim Level (Total) " <<
etots <<
" (ECal) "
848 <<
"\nHcalTB04Analysis:: Energy deposit at Qie Level (Total) " <<
etotq <<
" (ECal) "
854 for (
int i = 0;
i < 5;
i++) {
858 for (
int i = 0;
i < 3;
i++) {
862 double e1 = 0, e2 = 0;
877 if (
iphi >= 0 && iphi < 3 && ieta >= 0 &&
ieta < 5) {
886 for (
int i = 0;
i < 3;
i++) {
892 for (
int i = 0;
i < 5;
i++) {
899 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Energy fraction along Eta and Phi (Sim/Qie)";
900 for (
int i = 0;
i < 5;
i++)
907 for (
int i = 0;
i < 20;
i++) {
919 if (
iphi >= 0 && iphi < 3 && layer >= 0 && layer < 20) {
926 for (
int i = 0;
i < 20;
i++) {
933 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Energy fraction along Layer";
934 for (
int i = 0;
i < 20;
i++)
962 std::vector<CaloHit>::iterator
itr;
964 uint32_t
id =
itr->id();
970 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Save Hit " << std::setw(3) <<
i + 1 <<
" ID 0x" << std::hex
971 <<
group <<
std::dec <<
" " << std::setw(2) << det <<
" " << std::setw(2) << lay
972 <<
" " << std::setw(1) <<
z <<
" " << std::setw(3) <<
ieta <<
" " << std::setw(3)
973 <<
iphi <<
" T " << std::setw(6) <<
itr->t() <<
" E " << std::setw(6) <<
itr->e();
976 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Saves " << nhit <<
" hits from Crystals";
981 uint32_t
id =
itr->
id();
987 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Save Hit " << std::setw(3) <<
i + 1 <<
" ID 0x" << std::hex
988 <<
group <<
std::dec <<
" " << std::setw(2) << det <<
" " << std::setw(2) << lay
989 <<
" " << std::setw(1) <<
z <<
" " << std::setw(3) <<
ieta <<
" " << std::setw(3)
990 <<
iphi <<
" T " << std::setw(6) <<
itr->t() <<
" E " << std::setw(6) <<
itr->e();
993 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Saves " << nhit <<
" hits from HCal";
1018 pvUVW = G4ThreeVector();
1036 esimh.push_back(0.);
1044 esime.push_back(0.);
1045 enois.push_back(0.);
1052 group = (det & 15) << 20;
1053 group += ((lay - 1) & 31) << 15;
1065 else if (layer == 17)
1067 else if (layer > 17)
1080 const double rLay[19] = {1836.0,
1099 if (layer > 0 && layer <= 19)
1100 dist += rLay[layer - 1] * mm /
sin(
theta);
1102 const double zLay[19] = {4034.0,
1121 if (layer > 0 && layer <= 19)
1122 dist += zLay[layer - 1] * mm /
cos(
theta);
1125 double tmp = dist / c_light / ns;
1127 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::timeOfFlight " <<
tmp <<
" for det/lay " << det <<
" " << layer
1128 <<
" eta/theta " <<
eta <<
" " <<
theta / deg <<
" dist " << dist;