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 double time = (aStep->GetPostStepPoint()->GetGlobalTime())/nanosecond;
257 bool flag = (unitID > 0);
261 G4TouchableHistory* touch =(G4TouchableHistory*)(
theTrack->GetTouchable());
262 LogDebug(
"CaloSim") <<
"CaloSD:: GetStepInfo for"
263 <<
" PV " << touch->GetVolume(0)->GetName()
264 <<
" PVid = " << touch->GetReplicaNumber(0)
265 <<
" MVid = " << touch->GetReplicaNumber(1)
269 G4TouchableHistory* touch =(G4TouchableHistory*)(
theTrack->GetTouchable());
270 LogDebug(
"CaloSim") <<
"CaloSD:: GetStepInfo for"
271 <<
" PV " << touch->GetVolume(0)->GetName()
272 <<
" PVid = " << touch->GetReplicaNumber(0)
273 <<
" MVid = " << touch->GetReplicaNumber(1)
274 <<
" Unit " << std::hex << unitID << std::dec
279 G4int particleCode =
theTrack->GetDefinition()->GetPDGEncoding();
280 if (particleCode ==
emPDG ||
281 particleCode ==
epPDG ||
295 G4ThreeVector localPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
302 G4ThreeVector globalPoint = touch->GetHistory()->GetTopTransform().Inverse().TransformPoint(local);
312 <<
" maybe detector name changed";
332 std::map<CaloHitID,CaloG4Hit*>::const_iterator it =
hitMap.find(
currentID);
340 int maxhit=
theHC->entries()-1;
342 for (
int j=maxhit;
j>minhit&&!
found; --
j) {
362 LogDebug(
"CaloSim") <<
"CaloSD::CreateNewHit for"
368 <<
" For Track " <<
theTrack->GetTrackID()
369 <<
" which is a " <<
theTrack->GetDefinition()->GetParticleName()
370 <<
" of energy " <<
theTrack->GetKineticEnergy()/GeV
371 <<
" " <<
theTrack->GetMomentum().mag()/GeV
372 <<
" daughter of part. " <<
theTrack->GetParentID()
373 <<
" and created by " ;
378 LogDebug(
"CaloSim") <<
"NO process";
402 etrack=
theTrack->GetKineticEnergy();
418 LogDebug(
"CaloSim") <<
"CaloSD : TrackwithHistory pointer for "
426 LogDebug(
"CaloSim") <<
"CaloSD: set save the track "
456 LogDebug(
"CaloSim") <<
"CaloSD: Incident energy " << incidentEnergy/GeV
464 double charge = aStep->GetPreStepPoint()->GetCharge();
466 if (charge != 0. && aStep->GetStepLength() > 0) {
467 G4Material* mat = aStep->GetPreStepPoint()->GetMaterial();
468 double density = mat->GetDensity();
469 double dedx = aStep->GetTotalEnergyDeposit()/aStep->GetStepLength();
470 double rkb = birk1/density;
471 double c = birk2*rkb*rkb;
472 if (
std::abs(charge) >= 2.) rkb /= birk3;
473 weight = 1./(1.+rkb*dedx+c*dedx*dedx);
475 LogDebug(
"CaloSim") <<
"CaloSD::getAttenuation in " << mat->GetName()
476 <<
" Charge " << charge <<
" dE/dx " << dedx
477 <<
" Birk Const " << rkb <<
", " << c <<
" Weight = "
478 << weight <<
" dE " << aStep->GetTotalEnergyDeposit();
485 G4ParticleTable * theParticleTable = G4ParticleTable::GetParticleTable();
486 G4String particleName;
487 emPDG = theParticleTable->FindParticle(particleName=
"e-")->GetPDGEncoding();
488 epPDG = theParticleTable->FindParticle(particleName=
"e+")->GetPDGEncoding();
489 gammaPDG = theParticleTable->FindParticle(particleName=
"gamma")->GetPDGEncoding();
491 LogDebug(
"CaloSim") <<
"CaloSD: Particle code for e- = " <<
emPDG
500 LogDebug(
"CaloSim") <<
"CaloSD: Dispatched BeginOfEvent for " << GetName()
507 int id = (*trk)()->GetTrackID();
509 int lastTrackID = -1;
511 if (
id == lastTrackID) {
513 if (trksForThisEvent !=
NULL) {
514 int it = (int)(trksForThisEvent->size()) - 1;
517 if (trkH->
trackID() == (
unsigned int)(
id))
tkMap[
id] = trkH;
519 LogDebug(
"CaloSim") <<
"CaloSD: get track " << it <<
" from "
520 <<
"Container of size " << trksForThisEvent->size()
521 <<
" with ID " << trkH->
trackID();
523 LogDebug(
"CaloSim") <<
"CaloSD: get track " << it <<
" from "
524 <<
"Container of size " << trksForThisEvent->size()
533 int count = 0, wrong = 0;
538 for (
int i=0;
i<
theHC->entries(); ++
i) {
544 edm::LogInfo(
"CaloSim") <<
"CaloSD: " << GetName() <<
" store " << count
545 <<
" hits recorded with " << wrong
546 <<
" track IDs not given properly and "
547 <<
totalHits-count <<
" hits not passing cuts";
561 LogDebug(
"CaloSim") <<
"CaloSD: Clears hit vector for " << GetName() <<
" " <<
slave;
565 LogDebug(
"CaloSim") <<
"CaloSD: Initialises slave SD for " << GetName();
578 LogDebug(
"CaloSim") <<
"CaloSD: hit update from track Id on Calo Surface "
582 primaryID = aTrack->GetTrackID();
584 edm::LogWarning(
"CaloSim") <<
"CaloSD: Problem with primaryID **** set by "
585 <<
"force to TkID **** " << primaryID <<
" in "
586 <<
preStepPoint->GetTouchable()->GetVolume(0)->GetName();
598 LogDebug(
"CaloSim") <<
"Depth " << hit->
getDepth() <<
" Emin = " << emin <<
" ("
642 <<
" changed to " << tkID <<
" by SimTrackManager"
650 LogDebug(
"CaloSim") <<
"CaloSD: Store Hit at " << std::hex
652 << aHit->
getDepth() <<
" due to " << tkID
653 <<
" in time " << time <<
" of energy "
654 << aHit->
getEM()/GeV <<
" GeV (EM) and "
655 << aHit->
getHadr()/GeV <<
" GeV (Hadr)";
665 if ( trkInfo->
isPrimary() ) primary = (*trk)()->GetTrackID();
669 <<
" primary ID = " << primary
675 if (primary > 0 && primary != primAncestor) {
676 primAncestor = primary;
686 std::vector<CaloG4Hit*>* theCollection =
theHC->GetVector();
689 LogDebug(
"CaloSim") <<
"CaloSD: collection before merging, size = " <<
theHC->entries();
697 hitvec.swap(*theCollection);
700 LogDebug(
"CaloSim") <<
"CaloSD::cleanHitCollection: sort hits in buffer "
702 for (
unsigned int i = 0;
i<
hitvec.size(); ++
i)
707 for (i=cleanIndex; i<hitvec.size(); ++
i) {
710 for (j = i+1; j <hitvec.size() && equal(hitvec[i], hitvec[j]); ++
j) {
713 (*hitvec[
i]).addEnergyDeposit(*hitvec[j]);
714 (*hitvec[
j]).setEM(0.);
715 (*hitvec[
j]).setHadr(0.);
721 LogDebug(
"CaloSim") <<
"CaloSD: cleanHitCollection merge the hits in buffer ";
722 for (
unsigned int i = 0; i<hitvec.size(); ++
i)
725 for (
unsigned int i = cleanIndex; i < cleanIndex+
selIndex.size(); ++
i ) {
728 hitvec.resize(cleanIndex+
selIndex.size());
730 LogDebug(
"CaloSim") <<
"CaloSD::cleanHitCollection: remove the merged hits in buffer,"
731 <<
" new size = " << hitvec.size();
732 for (
unsigned int i = 0; i<hitvec.size(); ++
i)
735 hitvec.swap(*theCollection);
736 std::vector<CaloG4Hit*>().
swap(hitvec);
742 LogDebug(
"CaloSim") <<
"CaloSD: collection after merging, size = " <<
theHC->entries();
748 LogDebug(
"CaloSim") <<
"CaloSD: Size of reusehit after merge = " <<
reusehit.size();
753 for (
unsigned int i =
cleanIndex;
i<theCollection->size(); ++
i) {
762 LogDebug(
"CaloSim") <<
"CaloSD: dropped CaloG4Hit " <<
" " << *aHit;
767 reusehit.push_back((*theCollection)[i]);
775 LogDebug(
"CaloSim") <<
"CaloSD: Size of reusehit after selection = " <<
reusehit.size()
776 <<
" Number of added hit = " << addhit;
781 for (
int ii = addhit-1;
ii>=0; --
ii) {
795 LogDebug(
"CaloSim") <<
"CaloSD: hit collection after selection, size = "
797 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
G4ThreeVector setToGlobal(G4ThreeVector, const G4VTouchable *)
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)