11 #include "G4EventManager.hh"
12 #include "G4SDManager.hh"
15 #include "G4VProcess.hh"
16 #include "G4GFlashSpot.hh"
17 #include "G4ParticleTable.hh"
24 int tSlice,
bool ignoreTkID) :
26 G4VGFlashSensitiveDetector(), theTrack(0), preStepPoint(0), eminHit(0),
27 eminHitD(0), m_trackManager(manager), currentHit(0), runInit(
false),
28 timeSlice(tSlice), ignoreTrackID(ignoreTkID), hcID(-1), theHC(0),
38 std::vector<double> eminHits = m_CaloSD.
getParameter<std::vector<double> >(
"EminHits");
39 std::vector<double> tmaxHits = m_CaloSD.
getParameter<std::vector<double> >(
"TmaxHits");
40 std::vector<std::string> hcn = m_CaloSD.
getParameter<std::vector<std::string> >(
"HCNames");
41 std::vector<int> useResMap = m_CaloSD.
getParameter<std::vector<int> >(
"UseResponseTables");
42 std::vector<double> eminHitX = m_CaloSD.
getParameter<std::vector<double> >(
"EminHitsDepth");
51 double beamZ = m_CaloSD.
getParameter<
double>(
"BeamPosition")*cm;
54 SetVerboseLevel(verbn);
55 for (
unsigned int k=0;
k<hcn.size(); ++
k) {
56 if (name == (G4String)(hcn[
k])) {
57 if (
k < eminHits.size())
eminHit = eminHits[
k]*MeV;
58 if (
k < eminHitX.size())
eminHitD= eminHitX[
k]*MeV;
59 if (
k < tmaxHits.size()) tmaxHit = tmaxHits[
k]*ns;
65 LogDebug(
"CaloSim") <<
"***************************************************"
69 <<
"* Constructing a CaloSD with name " << GetName()
73 <<
"***************************************************";
87 std::vector<std::string> lvNames = clg.
logicalNames(name);
89 for (std::vector<std::string>::iterator it=lvNames.begin(); it !=lvNames.end(); ++it) {
92 LogDebug(
"CaloSim") <<
"CaloSD : Assigns SD to LV " << (*it);
96 edm::LogInfo(
"CaloSim") <<
"CaloSD: Minimum energy of track for saving it "
97 << energyCut/GeV <<
" GeV" <<
"\n"
98 <<
" Use of HitID Map " << useMap <<
"\n"
99 <<
" Check last " << checkHits
100 <<
" before saving the hit\n"
101 <<
" Correct TOF globally by " << correctT
102 <<
" ns (Flag =" << corrTOFBeam <<
")\n"
103 <<
" Save hits recorded before " << tmaxHit
104 <<
" ns and if energy is above " <<
eminHit/MeV
105 <<
" MeV (for depth 0) or " <<
eminHitD/MeV
106 <<
" MeV (for nonzero depths); Time Slice Unit "
134 theTrack =
const_cast<G4Track *
>(aSpot->GetOriginatorTrack()->GetPrimaryTrack());
135 G4int particleCode =
theTrack->GetDefinition()->GetPDGEncoding();
137 if (particleCode ==
emPDG ||
138 particleCode ==
epPDG ||
140 edepositEM = aSpot->GetEnergySpot()->GetEnergy();
148 G4Step * fFakeStep =
new G4Step();
150 G4StepPoint * fFakePostStepPoint = fFakeStep->GetPostStepPoint();
152 fFakePostStepPoint->SetPosition(aSpot->GetPosition());
154 G4TouchableHandle fTouchableHandle = aSpot->GetTouchableHandle();
156 fFakeStep->SetTotalEnergyDeposit(aSpot->GetEnergySpot()->GetEnergy());
161 uint16_t depth =
getDepth(fFakeStep);
166 LogDebug(
"CaloSim") <<
"CaloSD:: GetSpotInfo for"
168 << std::dec <<
" Edeposit = " <<
edepositEM <<
" "
175 posGlobal = aSpot->GetEnergySpot()->GetPosition();
183 LogDebug(
"CaloSim") <<
"CaloSD: Incident energy "
201 return aStep->GetTotalEnergyDeposit();
208 edm::LogInfo(
"CaloSim") <<
"CaloSD : Initialize called for " << GetName();
225 edm::LogInfo(
"CaloSim") <<
"CaloSD: EndofEvent entered with " <<
theHC->entries()
239 theHC->PrintAllHits();
252 G4int particleCode =
theTrack->GetDefinition()->GetPDGEncoding();
253 if (particleCode ==
emPDG ||
254 particleCode ==
epPDG ||
263 double time = (aStep->GetPostStepPoint()->GetGlobalTime())/nanosecond;
268 bool flag = (unitID > 0);
272 G4TouchableHistory* touch =(G4TouchableHistory*)(
theTrack->GetTouchable());
273 LogDebug(
"CaloSim") <<
"CaloSD:: GetStepInfo for"
274 <<
" PV " << touch->GetVolume(0)->GetName()
275 <<
" PVid = " << touch->GetReplicaNumber(0)
276 <<
" MVid = " << touch->GetReplicaNumber(1)
280 G4TouchableHistory* touch =(G4TouchableHistory*)(
theTrack->GetTouchable());
281 LogDebug(
"CaloSim") <<
"CaloSD:: GetStepInfo for"
282 <<
" PV " << touch->GetVolume(0)->GetName()
283 <<
" PVid = " << touch->GetReplicaNumber(0)
284 <<
" MVid = " << touch->GetReplicaNumber(1)
285 <<
" Unit " << std::hex << unitID << std::dec
294 G4ThreeVector localPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
304 <<
" maybe detector name changed";
324 std::map<CaloHitID,CaloG4Hit*>::const_iterator it =
hitMap.find(
currentID);
332 int maxhit=
theHC->entries()-1;
334 for (
int j=maxhit;
j>minhit&&!
found; --
j) {
354 LogDebug(
"CaloSim") <<
"CaloSD::CreateNewHit for"
360 <<
" For Track " <<
theTrack->GetTrackID()
361 <<
" which is a " <<
theTrack->GetDefinition()->GetParticleName()
362 <<
" of energy " <<
theTrack->GetKineticEnergy()/GeV
363 <<
" " <<
theTrack->GetMomentum().mag()/GeV
364 <<
" daughter of part. " <<
theTrack->GetParentID()
365 <<
" and created by " ;
370 LogDebug(
"CaloSim") <<
"NO process";
394 etrack=
theTrack->GetKineticEnergy();
410 LogDebug(
"CaloSim") <<
"CaloSD : TrackwithHistory pointer for "
418 LogDebug(
"CaloSim") <<
"CaloSD: set save the track "
448 LogDebug(
"CaloSim") <<
"CaloSD: Incident energy " << incidentEnergy/GeV
456 double charge = aStep->GetPreStepPoint()->GetCharge();
458 if (charge != 0. && aStep->GetStepLength() > 0) {
459 G4Material* mat = aStep->GetPreStepPoint()->GetMaterial();
460 double density = mat->GetDensity();
461 double dedx = aStep->GetTotalEnergyDeposit()/aStep->GetStepLength();
462 double rkb = birk1/density;
463 double c = birk2*rkb*rkb;
464 if (
std::abs(charge) >= 2.) rkb /= birk3;
465 weight = 1./(1.+rkb*dedx+c*dedx*dedx);
467 LogDebug(
"CaloSim") <<
"CaloSD::getAttenuation in " << mat->GetName()
468 <<
" Charge " << charge <<
" dE/dx " << dedx
469 <<
" Birk Const " << rkb <<
", " << c <<
" Weight = "
470 << weight <<
" dE " << aStep->GetTotalEnergyDeposit();
477 G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
478 G4String particleName;
479 emPDG = theParticleTable->FindParticle(particleName=
"e-")->GetPDGEncoding();
480 epPDG = theParticleTable->FindParticle(particleName=
"e+")->GetPDGEncoding();
481 gammaPDG = theParticleTable->FindParticle(particleName=
"gamma")->GetPDGEncoding();
483 LogDebug(
"CaloSim") <<
"CaloSD: Particle code for e- = " <<
emPDG
492 LogDebug(
"CaloSim") <<
"CaloSD: Dispatched BeginOfEvent for " << GetName()
499 int id = (*trk)()->GetTrackID();
501 int lastTrackID = -1;
503 if (
id == lastTrackID) {
505 if (trksForThisEvent !=
NULL) {
506 int it = (int)(trksForThisEvent->size()) - 1;
509 if (trkH->
trackID() == (
unsigned int)(
id))
tkMap[
id] = trkH;
511 LogDebug(
"CaloSim") <<
"CaloSD: get track " << it <<
" from "
512 <<
"Container of size " << trksForThisEvent->size()
513 <<
" with ID " << trkH->
trackID();
515 LogDebug(
"CaloSim") <<
"CaloSD: get track " << it <<
" from "
516 <<
"Container of size " << trksForThisEvent->size()
525 int count = 0, wrong = 0;
530 for (
int i=0;
i<
theHC->entries(); ++
i) {
536 edm::LogInfo(
"CaloSim") <<
"CaloSD: " << GetName() <<
" store " << count
537 <<
" hits recorded with " << wrong
538 <<
" track IDs not given properly and "
539 <<
totalHits-count <<
" hits not passing cuts";
553 LogDebug(
"CaloSim") <<
"CaloSD: Clears hit vector for " << GetName() <<
" " <<
slave;
557 LogDebug(
"CaloSim") <<
"CaloSD: Initialises slave SD for " << GetName();
570 LogDebug(
"CaloSim") <<
"CaloSD: hit update from track Id on Calo Surface "
574 primaryID = aTrack->GetTrackID();
576 edm::LogWarning(
"CaloSim") <<
"CaloSD: Problem with primaryID **** set by "
577 <<
"force to TkID **** " << primaryID <<
" in "
578 <<
preStepPoint->GetTouchable()->GetVolume(0)->GetName();
590 LogDebug(
"CaloSim") <<
"Depth " << hit->
getDepth() <<
" Emin = " << emin <<
" ("
634 <<
" changed to " << tkID <<
" by SimTrackManager"
642 LogDebug(
"CaloSim") <<
"CaloSD: Store Hit at " << std::hex
644 << aHit->
getDepth() <<
" due to " << tkID
645 <<
" in time " << time <<
" of energy "
646 << aHit->
getEM()/GeV <<
" GeV (EM) and "
647 << aHit->
getHadr()/GeV <<
" GeV (Hadr)";
657 if ( trkInfo->
isPrimary() ) primary = (*trk)()->GetTrackID();
661 <<
" primary ID = " << primary
667 if (primary > 0 && primary != primAncestor) {
668 primAncestor = primary;
678 std::vector<CaloG4Hit*>* theCollection =
theHC->GetVector();
681 LogDebug(
"CaloSim") <<
"CaloSD: collection before merging, size = " <<
theHC->entries();
689 hitvec.swap(*theCollection);
692 LogDebug(
"CaloSim") <<
"CaloSD::cleanHitCollection: sort hits in buffer "
694 for (
unsigned int i = 0;
i<
hitvec.size(); ++
i)
699 for (i=cleanIndex; i<hitvec.size(); ++
i) {
702 for (j = i+1; j <hitvec.size() && equal(hitvec[i], hitvec[j]); ++
j) {
705 (*hitvec[
i]).addEnergyDeposit(*hitvec[j]);
706 (*hitvec[
j]).setEM(0.);
707 (*hitvec[
j]).setHadr(0.);
713 LogDebug(
"CaloSim") <<
"CaloSD: cleanHitCollection merge the hits in buffer ";
714 for (
unsigned int i = 0; i<hitvec.size(); ++
i)
717 for (
unsigned int i = cleanIndex; i < cleanIndex+
selIndex.size(); ++
i ) {
720 hitvec.resize(cleanIndex+
selIndex.size());
722 LogDebug(
"CaloSim") <<
"CaloSD::cleanHitCollection: remove the merged hits in buffer,"
723 <<
" new size = " << hitvec.size();
724 for (
unsigned int i = 0; i<hitvec.size(); ++
i)
727 hitvec.swap(*theCollection);
728 std::vector<CaloG4Hit*>().
swap(hitvec);
734 LogDebug(
"CaloSim") <<
"CaloSD: collection after merging, size = " <<
theHC->entries();
740 LogDebug(
"CaloSim") <<
"CaloSD: Size of reusehit after merge = " <<
reusehit.size();
745 for (
unsigned int i =
cleanIndex;
i<theCollection->size(); ++
i) {
754 LogDebug(
"CaloSim") <<
"CaloSD: dropped CaloG4Hit " <<
" " << *aHit;
759 reusehit.push_back((*theCollection)[i]);
767 LogDebug(
"CaloSim") <<
"CaloSD: Size of reusehit after selection = " <<
reusehit.size()
768 <<
" Number of added hit = " << addhit;
773 for (
int ii = addhit-1; ii>=0; --ii) {
787 LogDebug(
"CaloSim") <<
"CaloSD: hit collection after selection, size = "
789 theHC->PrintAllHits();
G4ThreeVector setToLocal(G4ThreeVector, const G4VTouchable *)
void swap(ora::Record &rh, ora::Record &lh)
virtual bool processHits(uint32_t, double, double, double, int, uint16_t depth=0)
T getParameter(std::string const &) const
virtual void Initialize()
T getUntrackedParameter(std::string const &, T const &) const
virtual uint32_t setDetUnitId(G4Step *step)=0
std::vector< PCaloHit > PCaloHitContainer
std::vector< std::string > logicalNames(std::string &readoutName)
void updateHit(CaloG4Hit *)
virtual void Initialize(G4HCofThisEvent *HCE)
void setIncidentEnergy(double e)
virtual void EndOfEvent(G4HCofThisEvent *eventHC)
void setEntryLocal(double x, double y, double z)
uint16_t getDepth() const
virtual int getTrackID(G4Track *)
void fillHits(edm::PCaloHitContainer &, std::string n)
type of data representation of DDCompactView
std::vector< CaloG4Hit * > reusehit
void cleanHitCollection()
double getWeight(int genPID, double genP)
CaloSD(G4String aSDname, const DDCompactView &cpv, SensitiveDetectorCatalog &clg, edm::ParameterSet const &p, const SimTrackManager *, int tSlice=1, bool ignoreTkID=false)
void addEnergyDeposit(double em, double hd)
const TrackContainer * trackContainer() const
std::string const collectionName[nCollections]
std::vector< TrackWithHistory * > TrackContainer
unsigned int trackID() const
bool trackExists(unsigned int i) const
virtual void ReserveMemory(unsigned int size)
virtual void update(const BeginOfRun *)
This routine will be called when the appropriate signal arrives.
void setID(uint32_t i, double d, int j, uint16_t k=0)
std::vector< CaloG4Hit * > hitvec
double getAttenuation(G4Step *aStep, double birk1, double birk2, double birk3)
int giveMotherNeeded(int i) const
std::vector< unsigned int > selIndex
CaloMeanResponse * meanResponse
unsigned int offset(bool)
void NaNTrap(G4Step *step)
void storeHit(CaloG4Hit *)
virtual bool filterHit(CaloG4Hit *, double)
virtual G4bool getStepInfo(G4Step *aStep)
void setID(uint32_t unitID, double timeSlice, int trackID, uint16_t depth=0)
G4StepPoint * preStepPoint
virtual bool ProcessHits(G4Step *step, G4TouchableHistory *tHistory)
const math::XYZVectorD & momentum() const
CSCCFEBTimeSlice const *const timeSlice(T const &data, int nCFEB, int nSample)
virtual void AssignSD(std::string &vname)
std::map< CaloHitID, CaloG4Hit * > hitMap
CaloG4HitCollection * theHC
double getResponseWt(G4Track *)
void setPosition(double x, double y, double z)
void setEntry(double x, double y, double z)
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
double getTimeSlice() const
std::map< int, TrackWithHistory * > tkMap
void resetForNewPrimary(G4ThreeVector, double)
const SimTrackManager * m_trackManager
uint32_t getUnitID() const
bool saveHit(CaloG4Hit *)
CaloG4Hit * createNewHit()
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
virtual uint16_t getDepth(G4Step *)
G4ThreeVector entrancePoint
G4ThreeVector entranceLocal
double getEnergyDeposit() const
virtual double getEnergyDeposit(G4Step *step)