53 #include "G4SDManager.hh"
56 #include "G4ThreeVector.hh"
57 #include "G4VProcess.hh"
58 #include "G4HCofThisEvent.hh"
59 #include "CLHEP/Random/RandGaussQ.h"
60 #include "CLHEP/Units/GlobalSystemOfUnits.h"
61 #include "CLHEP/Units/GlobalPhysicalConstants.h"
62 #include "Randomize.hh"
68 class HepRandomEngine;
73 public Observer<const BeginOfEvent*>,
163 double beamEta = (fMaxEta + fMinEta) / 2.;
164 double beamPhi = (fMaxPhi + fMinPhi) / 2.;
165 double beamThet = 2 * atan(
exp(-beamEta));
169 icphi = (
int)(fabs(beamPhi) / 0.087) + 5;
173 produces<PHcalTB04Info>();
180 <<
"HcalTB04:: Initialised as observer of BeginOf Job/BeginOfRun/BeginOfEvent/G4Step/EndOfEvent with Parameter "
181 "values:\n \thcalOnly = "
182 <<
hcalOnly <<
"\tecalNoise = " <<
ecalNoise <<
"\n\tMode = " <<
mode <<
" (0: HB2 Standard; 1:HB2 Segmented)"
183 <<
"\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 "
237 for (
int lay = 1; lay < 8; lay++) {
238 for (
int icr = 1; icr < 8; icr++) {
261 for (
int i = 0;
i < 5;
i++) {
265 for (
int i = 0;
i < 3;
i++) {
269 for (
int i = 0;
i < 20;
i++) {
281 int irun = (*run)()->GetRunID();
284 G4SDManager*
sd = G4SDManager::GetSDMpointerIfExist();
287 G4VSensitiveDetector* aSD =
sd->FindSensitiveDetector(sdname);
288 if (aSD ==
nullptr) {
290 <<
" with name " << sdname <<
" in this "
293 HCalSD* theCaloSD = dynamic_cast<HCalSD*>(aSD);
294 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: Finds SD with name " << theCaloSD->GetName()
298 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: set a new numbering scheme";
302 aSD =
sd->FindSensitiveDetector(sdname);
303 if (aSD ==
nullptr) {
305 <<
" with name " << sdname <<
" in this "
308 ECalSD* theCaloSD = dynamic_cast<ECalSD*>(aSD);
309 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: Finds SD with name " << theCaloSD->GetName()
313 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::beginOfRun: set a new numbering scheme";
318 <<
"not get SD Manager!";
325 evNum = (*evt)()->GetEventID();
331 if (aStep !=
nullptr) {
333 G4ThreeVector thePreStepPoint = aStep->GetPreStepPoint()->GetPosition();
334 G4ThreeVector thePostStepPoint;
337 G4Track* aTrack = aStep->GetTrack();
338 int trackID = aTrack->GetTrackID();
339 int parentID = aTrack->GetParentID();
340 const G4ThreeVector&
position = aTrack->GetPosition();
341 G4ThreeVector momentum = aTrack->GetMomentum();
342 G4String partType = aTrack->GetDefinition()->GetParticleType();
343 G4String partSubType = aTrack->GetDefinition()->GetParticleSubType();
344 int partPDGEncoding = aTrack->GetDefinition()->GetPDGEncoding();
346 bool isPDGStable = aTrack->GetDefinition()->GetPDGStable();
348 double pDGlifetime = aTrack->GetDefinition()->GetPDGLifeTime();
349 double gammaFactor = aStep->GetPreStepPoint()->GetGamma();
352 double stepDeltaEnergy = aStep->GetDeltaEnergy();
353 double kinEnergy = aTrack->GetKineticEnergy();
356 if (trackID == 1 && parentID == 0 && ((kinEnergy == 0.) || (fabs(stepDeltaEnergy / kinEnergy) > 0.1))) {
358 if (kinEnergy == 0.) {
361 if (fabs(stepDeltaEnergy / kinEnergy) > 0.1)
371 G4String thePostPVname =
"NoName";
372 G4StepPoint* thePostPoint = aStep->GetPostStepPoint();
374 thePostStepPoint = thePostPoint->GetPosition();
375 G4VPhysicalVolume* thePostPV = thePostPoint->GetPhysicalVolume();
377 thePostPVname = thePostPV->GetName();
380 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: V1 found at: " << thePostStepPoint
381 <<
" G4VPhysicalVolume: " << thePostPVname;
388 if ((trackID != 1 && parentID == 1 && (aTrack->GetCurrentStepNumber() == 1) && (thePreStepPoint ==
pvPosition)) ||
389 (trackID == 1 && thePreStepPoint ==
pvPosition)) {
391 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::A secondary... PDG:" << partPDGEncoding
392 <<
" TrackID:" << trackID <<
" ParentID:" << parentID
393 <<
" stable: " << isPDGStable <<
" Tau: " << pDGlifetime
394 <<
" cTauGamma=" << c_light * pDGlifetime * gammaFactor * 1000.
395 <<
"um GammaFactor: " << gammaFactor;
400 secEkin.push_back(aTrack->GetKineticEnergy());
403 double ctaugamma_um = CLHEP::c_light * pDGlifetime * gammaFactor * 1000.;
404 if ((ctaugamma_um > 0.) && (ctaugamma_um < 100.)) {
412 if (aTrack->GetCurrentStepNumber() == 1) {
417 std::vector<int>::iterator
pos;
423 <<
"HcalTB04Analysis:: A tertiary... PDG:" << partPDGEncoding <<
" TrackID:" << trackID
424 <<
" ParentID:" << parentID <<
" stable: " << isPDGStable <<
" Tau: " << pDGlifetime
425 <<
" cTauGamma=" << c_light * pDGlifetime * gammaFactor * 1000. <<
"um GammaFactor: " << gammaFactor;
440 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::Fill event " << (*evt)()->GetEventID();
448 CLHEP::HepRandomEngine* engine = G4Random::getTheEngine();
465 int iEvt = (*evt)()->GetEventID();
468 else if ((iEvt < 100) && (iEvt % 10 == 0))
470 else if ((iEvt < 1000) && (iEvt % 100 == 0))
472 else if ((iEvt < 10000) && (iEvt % 1000 == 0))
477 std::vector<CaloHit> hhits, hhitl;
480 std::map<int, float, std::less<int> > primaries;
481 double etot1 = 0, etot2 = 0;
484 G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
486 idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
489 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hit Collection for " << sdName <<
" of ID " << idHC
490 <<
" is obtained at " << theHC <<
" with " << theHC->entries() <<
" entries";
492 int thehc_entries = theHC->entries();
493 if (idHC >= 0 && theHC !=
nullptr) {
494 hhits.reserve(theHC->entries());
495 hhitl.reserve(theHC->entries());
496 for (
j = 0;
j < thehc_entries;
j++) {
515 hhits.push_back(
hit);
517 hhitl.push_back(hitl);
521 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hcal Hit i/p " <<
j <<
" ID 0x" << std::hex <<
id <<
" 0x"
522 <<
idx <<
std::dec <<
" time " << std::setw(6) <<
time <<
" " << std::setw(6)
523 << jitter <<
" theta " << std::setw(8) <<
theta <<
" eta " << std::setw(8) <<
eta
524 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) <<
e <<
" "
525 << std::setw(8) << escl;
531 std::vector<CaloHit>::iterator itr;
532 int nHit = hhits.size();
533 std::vector<CaloHit*>
hits(nHit);
534 for (
j = 0, itr = hhits.begin(); itr != hhits.end();
j++, itr++) {
538 std::vector<CaloHit*>::iterator k1, k2;
540 for (k1 =
hits.begin(); k1 !=
hits.end(); k1++) {
541 int det = (**k1).det();
542 int layer = (**k1).layer();
543 double ehit = (**k1).e();
544 double eta = (**k1).eta();
545 double phi = (**k1).phi();
546 double jitter = (**k1).t();
547 uint32_t
unitID = (**k1).id();
549 for (k2 = k1 + 1; k2 !=
hits.end() && fabs(jitter - (**k2).t()) < 1 &&
unitID == (**k2).id(); k2++) {
559 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hcal Hit store " << nhit <<
" ID 0x" << std::hex <<
unitID
560 <<
std::dec <<
" time " << std::setw(6) << jitter <<
" eta " << std::setw(8) <<
eta
561 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) << ehit;
565 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Stores " << nhit <<
" HCal hits from " << nHit
566 <<
" input hits E(Hcal) " << etot1 <<
" " << etot2;
569 for (
j = 0, itr = hhitl.begin(); itr != hhitl.end();
j++, itr++) {
575 for (k1 =
hits.begin(); k1 !=
hits.end(); k1++) {
576 int det = (**k1).det();
577 int layer = (**k1).layer();
578 double ehit = (**k1).e();
579 double eta = (**k1).eta();
580 double phi = (**k1).phi();
581 double jitter = (**k1).t();
582 uint32_t
unitID = (**k1).id();
584 for (k2 = k1 + 1; k2 !=
hits.end() && fabs(jitter - (**k2).t()) < 1 &&
unitID == (**k2).id(); k2++) {
594 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hcal Hit store " << nhitl <<
" ID 0x" << std::hex <<
unitID
595 <<
std::dec <<
" time " << std::setw(6) << jitter <<
" eta " << std::setw(8) <<
eta
596 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) << ehit;
600 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Stores " << nhitl <<
" HCal hits from " << nHit
601 <<
" input hits E(Hcal) " << etot1 <<
" " << etotl;
604 std::vector<CaloHit> ehits;
606 idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
610 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Hit Collection for " << sdName <<
" of ID " << idHC
611 <<
" is obtained at " << theHC <<
" with " << theHC->entries() <<
" entries";
613 if (idHC >= 0 && theHC !=
nullptr) {
614 thehc_entries = theHC->entries();
615 ehits.reserve(theHC->entries());
616 for (
j = 0;
j < thehc_entries;
j++) {
620 if (e < 0 || e > 100000.)
631 ehits.push_back(
hit);
635 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Ecal Hit i/p " <<
j <<
" ID 0x" << std::hex <<
id
636 <<
std::dec <<
" time " << std::setw(6) <<
time <<
" theta " << std::setw(8)
637 <<
theta <<
" eta " << std::setw(8) <<
eta <<
" phi " << std::setw(8) <<
phi
638 <<
" e " << std::setw(8) <<
e;
646 std::vector<CaloHit*> hite(nHit);
647 for (
j = 0, itr = ehits.begin(); itr != ehits.end();
j++, itr++) {
652 for (k1 = hite.begin(); k1 != hite.end(); k1++) {
653 int det = (**k1).det();
654 int layer = (**k1).layer();
655 double ehit = (**k1).e();
656 double eta = (**k1).eta();
657 double phi = (**k1).phi();
658 double jitter = (**k1).t();
659 uint32_t
unitID = (**k1).id();
661 for (k2 = k1 + 1; k2 != hite.end() && fabs(jitter - (**k2).t()) < 1 &&
unitID == (**k2).id(); k2++) {
671 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Ecal Hit store " << nhit <<
" ID 0x" << std::hex <<
unitID
672 <<
std::dec <<
" time " << std::setw(6) << jitter <<
" eta " << std::setw(8) <<
eta
673 <<
" phi " << std::setw(8) <<
phi <<
" e " << std::setw(8) << ehit;
677 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Stores " << nhit <<
" ECal hits from " << nHit
678 <<
" input hits E(Ecal) " << etot1 <<
" " << etot2;
683 G4PrimaryParticle* thePrim =
nullptr;
684 int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
686 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Event has " << nvertex <<
" verteices";
689 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::EndOfEvent ERROR: no vertex found for event " <<
evNum;
690 for (
int i = 0;
i < nvertex;
i++) {
691 G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(
i);
692 if (avertex ==
nullptr) {
693 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::EndOfEvent ERR: pointer to vertex = 0 for event " <<
evNum;
695 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::Vertex number :" <<
i <<
" " << avertex->GetPosition();
696 int npart = avertex->GetNumberOfParticle();
698 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::End Of Event ERR: no primary!";
699 if (thePrim ==
nullptr)
700 thePrim = avertex->GetPrimary(trackID);
704 if (thePrim !=
nullptr) {
705 double px = thePrim->GetPx();
706 double py = thePrim->GetPy();
707 double pz = thePrim->GetPz();
711 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis:: EndOfEvent ERR: primary has p=0 ";
713 double costheta = pz /
p;
716 if (
px != 0 ||
py != 0)
721 edm::LogWarning(
"HcalTBSim") <<
"HcalTB04Analysis::EndOfEvent ERR: could not find primary";
728 std::vector<CaloHit>
hits(hittot);
736 for (
unsigned int k1 = 0; k1 <
hcalHitCache.size(); k1++) {
738 uint32_t
id =
hit.
id();
740 double esim =
hit.e();
742 for (
unsigned int k2 = k1 + 1; k2 <
hcalHitCache.size(); k2++) {
756 <<
" energy from " << nhit <<
" hits starting with hit # " << k1
757 <<
" energy with noise " << eq;
759 for (
int k2 = 0; k2 <
nTower; k2++) {
769 for (
int k2 = 0; k2 <
nTower; k2++) {
777 <<
" registers " <<
esimh[k2] <<
" energy from hits and energy after QIE analysis "
785 CLHEP::RandGaussQ randGauss(*engine);
790 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::xtalAnalysis: Size " << iok.size() <<
" " <<
idEcal.size() <<
" "
793 for (
unsigned int k1 = 0; k1 <
ecalHitCache.size(); k1++) {
797 for (
unsigned int k2 = k1 + 1; k2 <
ecalHitCache.size(); k2++) {
805 double eq = esim + randGauss.fire(0.,
ecalNoise);
808 <<
" energy from " << nhit <<
" hits starting with hit # " << k1
809 <<
" energy with noise " << eq;
811 for (
int k2 = 0; k2 <
nCrystal; k2++) {
821 for (
int k2 = 0; k2 <
nCrystal; k2++) {
827 <<
" registers " <<
esime[k2] <<
" energy from hits and energy from noise "
851 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Energy deposit at Sim Level (Total) " <<
etots <<
" (ECal) "
853 <<
"\nHcalTB04Analysis:: Energy deposit at Qie Level (Total) " <<
etotq <<
" (ECal) "
859 for (
int i = 0;
i < 5;
i++) {
863 for (
int i = 0;
i < 3;
i++) {
867 double e1 = 0, e2 = 0;
882 if (
iphi >= 0 && iphi < 3 && ieta >= 0 &&
ieta < 5) {
891 for (
int i = 0;
i < 3;
i++) {
897 for (
int i = 0;
i < 5;
i++) {
904 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Energy fraction along Eta and Phi (Sim/Qie)";
905 for (
int i = 0;
i < 5;
i++)
912 for (
int i = 0;
i < 20;
i++) {
924 if (
iphi >= 0 && iphi < 3 && layer >= 0 &&
layer < 20) {
931 for (
int i = 0;
i < 20;
i++) {
938 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Energy fraction along Layer";
939 for (
int i = 0;
i < 20;
i++)
967 std::vector<CaloHit>::iterator itr;
969 uint32_t
id = itr->id();
975 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Save Hit " << std::setw(3) <<
i + 1 <<
" ID 0x" << std::hex
976 <<
group <<
std::dec <<
" " << std::setw(2) << det <<
" " << std::setw(2) << lay
977 <<
" " << std::setw(1) <<
z <<
" " << std::setw(3) <<
ieta <<
" " << std::setw(3)
978 <<
iphi <<
" T " << std::setw(6) << itr->t() <<
" E " << std::setw(6) << itr->e();
981 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Saves " << nhit <<
" hits from Crystals";
986 uint32_t
id = itr->
id();
992 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Save Hit " << std::setw(3) <<
i + 1 <<
" ID 0x" << std::hex
993 <<
group <<
std::dec <<
" " << std::setw(2) << det <<
" " << std::setw(2) << lay
994 <<
" " << std::setw(1) <<
z <<
" " << std::setw(3) <<
ieta <<
" " << std::setw(3)
995 <<
iphi <<
" T " << std::setw(6) << itr->t() <<
" E " << std::setw(6) << itr->e();
998 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis:: Saves " << nhit <<
" hits from HCal";
1023 pvUVW = G4ThreeVector();
1041 esimh.push_back(0.);
1049 esime.push_back(0.);
1050 enois.push_back(0.);
1057 group = (det & 15) << 20;
1058 group += ((lay - 1) & 31) << 15;
1070 else if (
layer == 17)
1072 else if (
layer > 17)
1085 const double rLay[19] = {1836.0,
1107 const double zLay[19] = {4034.0,
1130 double tmp = dist / c_light / ns;
1132 edm::LogVerbatim(
"HcalTBSim") <<
"HcalTB04Analysis::timeOfFlight " <<
tmp <<
" for det/lay " << det <<
" " <<
layer
1133 <<
" eta/theta " <<
eta <<
" " <<
theta / deg <<
" dist " << dist;