CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CaloSD.cc
Go to the documentation of this file.
1 // File: CaloSD.cc
3 // Description: Sensitive Detector class for calorimeters
5 
10 
11 #include "G4EventManager.hh"
12 #include "G4SDManager.hh"
13 #include "G4Step.hh"
14 #include "G4Track.hh"
15 #include "G4VProcess.hh"
16 #include "G4GFlashSpot.hh"
17 #include "G4ParticleTable.hh"
18 
19 //#define DebugLog
20 
21 CaloSD::CaloSD(G4String name, const DDCompactView & cpv,
23  edm::ParameterSet const & p, const SimTrackManager* manager,
24  int tSlice, bool ignoreTkID) :
25  SensitiveCaloDetector(name, cpv, clg, p),
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),
29  meanResponse(0) {
30  //Add Hcal Sentitive Detector Names
31 
32  collectionName.insert(name);
33 
34  //Parameters
35  edm::ParameterSet m_CaloSD = p.getParameter<edm::ParameterSet>("CaloSD");
36  energyCut = m_CaloSD.getParameter<double>("EminTrack")*GeV;
37  tmaxHit = m_CaloSD.getParameter<double>("TmaxHit")*ns;
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");
43  suppressHeavy= m_CaloSD.getParameter<bool>("SuppressHeavy");
44  kmaxIon = m_CaloSD.getParameter<double>("IonThreshold")*MeV;
45  kmaxProton = m_CaloSD.getParameter<double>("ProtonThreshold")*MeV;
46  kmaxNeutron = m_CaloSD.getParameter<double>("NeutronThreshold")*MeV;
47  checkHits = m_CaloSD.getUntrackedParameter<int>("CheckHits", 25);
48  useMap = m_CaloSD.getUntrackedParameter<bool>("UseMap", true);
49  int verbn = m_CaloSD.getUntrackedParameter<int>("Verbosity", 0);
50  corrTOFBeam = m_CaloSD.getParameter<bool>("CorrectTOFBeam");
51  double beamZ = m_CaloSD.getParameter<double>("BeamPosition")*cm;
52  correctT = beamZ/c_light/nanosecond;
53 
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;
60  if (k < useResMap.size() && useResMap[k] > 0) meanResponse = new CaloMeanResponse(p);
61  break;
62  }
63  }
64 #ifdef DebugLog
65  LogDebug("CaloSim") << "***************************************************"
66  << "\n"
67  << "* *"
68  << "\n"
69  << "* Constructing a CaloSD with name " << GetName()
70  << "\n"
71  << "* *"
72  << "\n"
73  << "***************************************************";
74 #endif
75  slave = new CaloSlaveSD(name);
78 
79  primAncestor = 0;
80  cleanIndex = 0;
81  totalHits = 0;
82  forceSave = false;
83 
84  //
85  // Now attach the right detectors (LogicalVolumes) to me
86  //
87  std::vector<std::string> lvNames = clg.logicalNames(name);
88  this->Register();
89  for (std::vector<std::string>::iterator it=lvNames.begin(); it !=lvNames.end(); ++it) {
90  this->AssignSD(*it);
91 #ifdef DebugLog
92  LogDebug("CaloSim") << "CaloSD : Assigns SD to LV " << (*it);
93 #endif
94  }
95 
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 "
107  << timeSlice << " Ignore TrackID Flag " << ignoreTrackID;
108 }
109 
111  if (slave) delete slave;
112  if (theHC) delete theHC;
113  if (meanResponse) delete meanResponse;
114 }
115 
116 bool CaloSD::ProcessHits(G4Step * aStep, G4TouchableHistory * ) {
117 
118  NaNTrap( aStep ) ;
119 
120  if (aStep == NULL) {
121  return true;
122  } else {
123  if (getStepInfo(aStep)) {
124  if (hitExists() == false && edepositEM+edepositHAD>0.)
126  }
127  }
128  return true;
129 }
130 
131 bool CaloSD::ProcessHits(G4GFlashSpot* aSpot, G4TouchableHistory*) {
132 
133  if (aSpot != NULL) {
134  theTrack = const_cast<G4Track *>(aSpot->GetOriginatorTrack()->GetPrimaryTrack());
135  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
136 
137  if (particleCode == emPDG ||
138  particleCode == epPDG ||
139  particleCode == gammaPDG ) {
140  edepositEM = aSpot->GetEnergySpot()->GetEnergy();
141  edepositHAD = 0.;
142  } else {
143  edepositEM = 0.;
144  edepositHAD = 0.;
145  }
146 
147  if (edepositEM>0.) {
148  G4Step * fFakeStep = new G4Step();
149  preStepPoint = fFakeStep->GetPreStepPoint();
150  G4StepPoint * fFakePostStepPoint = fFakeStep->GetPostStepPoint();
151  preStepPoint->SetPosition(aSpot->GetPosition());
152  fFakePostStepPoint->SetPosition(aSpot->GetPosition());
153 
154  G4TouchableHandle fTouchableHandle = aSpot->GetTouchableHandle();
155  preStepPoint->SetTouchableHandle(fTouchableHandle);
156  fFakeStep->SetTotalEnergyDeposit(aSpot->GetEnergySpot()->GetEnergy());
157 
158  double time = 0;
159  unsigned int unitID = setDetUnitId(fFakeStep);
160  int primaryID = getTrackID(theTrack);
161  uint16_t depth = getDepth(fFakeStep);
162 
163  if (unitID > 0) {
164  currentID.setID(unitID, time, primaryID, depth);
165 #ifdef DebugLog
166  LogDebug("CaloSim") << "CaloSD:: GetSpotInfo for"
167  << " Unit 0x" << std::hex << currentID.unitID()
168  << std::dec << " Edeposit = " << edepositEM << " "
169  << edepositHAD;
170 #endif
171  // Update if in the same detector, time-slice and for same track
172  if (currentID == previousID) {
174  } else {
175  posGlobal = aSpot->GetEnergySpot()->GetPosition();
176  // Reset entry point for new primary
177  if (currentID.trackID() != previousID.trackID()) {
178  entrancePoint = aSpot->GetPosition();
179  entranceLocal = aSpot->GetTouchableHandle()->GetHistory()->
180  GetTopTransform().TransformPoint(entrancePoint);
181  incidentEnergy = theTrack->GetKineticEnergy();
182 #ifdef DebugLog
183  LogDebug("CaloSim") << "CaloSD: Incident energy "
184  << incidentEnergy/GeV << " GeV and"
185  << " entrance point " << entrancePoint
186  << " (Global) " << entranceLocal << " (Local)";
187 #endif
188  }
189 
190  if (checkHit() == false) currentHit = createNewHit();
191  }
192  }
193  delete fFakeStep;
194  }
195  return true;
196  }
197  return false;
198 }
199 
200 double CaloSD::getEnergyDeposit(G4Step* aStep) {
201  return aStep->GetTotalEnergyDeposit();
202 }
203 
204 void CaloSD::Initialize(G4HCofThisEvent * HCE) {
205  totalHits = 0;
206 
207 #ifdef DebugLog
208  edm::LogInfo("CaloSim") << "CaloSD : Initialize called for " << GetName();
209 #endif
210 
211  //This initialization is performed at the beginning of an event
212  //------------------------------------------------------------
213  theHC = new CaloG4HitCollection(GetName(), collectionName[0]);
214 
215  if (hcID<0) hcID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
216  HCE->AddHitsCollection(hcID, theHC);
217 }
218 
219 void CaloSD::EndOfEvent(G4HCofThisEvent* ) {
220  // clean the hits for the last tracks
221 
223 
224 #ifdef DebugLog
225  edm::LogInfo("CaloSim") << "CaloSD: EndofEvent entered with " << theHC->entries()
226  << " entries";
227 #endif
228  // TimeMe("CaloSD:sortAndMergeHits",false);
229 }
230 
231 void CaloSD::clear() {}
232 
233 void CaloSD::DrawAll() {}
234 
236 #ifdef DebugLog
237  edm::LogInfo("CaloSim") << "CaloSD: Collection " << theHC->GetName();
238 #endif
239  theHC->PrintAllHits();
240 }
241 
243  if (slave->name() == n) c=slave->hits();
244  slave->Clean();
245 }
246 
247 bool CaloSD::getStepInfo(G4Step* aStep) {
248 
249  preStepPoint = aStep->GetPreStepPoint();
250  theTrack = aStep->GetTrack();
251 
252  G4int particleCode = theTrack->GetDefinition()->GetPDGEncoding();
253  if (particleCode == emPDG ||
254  particleCode == epPDG ||
255  particleCode == gammaPDG ) {
256  edepositEM = getEnergyDeposit(aStep);
257  edepositHAD = 0.;
258  } else {
259  edepositEM = 0.;
260  edepositHAD = getEnergyDeposit(aStep);
261  }
262 
263  double time = (aStep->GetPostStepPoint()->GetGlobalTime())/nanosecond;
264  unsigned int unitID= setDetUnitId(aStep);
265  uint16_t depth = getDepth(aStep);
266  int primaryID = getTrackID(theTrack);
267 
268  bool flag = (unitID > 0);
269  if (flag) {
270  currentID.setID(unitID, time, primaryID, depth);
271 #ifdef DebugLog
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)
277  << " Unit " << currentID.unitID()
278  << " Edeposit = " << edepositEM << " " << edepositHAD;
279  } else {
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
286  << " Edeposit = " << edepositEM << " " << edepositHAD;
287 #endif
288  }
289  return flag;
290 }
291 
292 G4ThreeVector CaloSD::setToLocal(G4ThreeVector global, const G4VTouchable* touch) {
293 
294  G4ThreeVector localPoint = touch->GetHistory()->GetTopTransform().TransformPoint(global);
295 
296  return localPoint;
297 }
298 
300 #ifdef DebugLog
301  if (currentID.trackID()<1)
302  edm::LogWarning("CaloSim") << "***** CaloSD error: primaryID = "
303  << currentID.trackID()
304  << " maybe detector name changed";
305 #endif
306  // Update if in the same detector, time-slice and for same track
307  if (currentID == previousID) {
309  return true;
310  }
311 
312  // Reset entry point for new primary
313  posGlobal = preStepPoint->GetPosition();
314  if (currentID.trackID() != previousID.trackID())
315  resetForNewPrimary(preStepPoint->GetPosition(), preStepPoint->GetKineticEnergy());
316 
317  return checkHit();
318 }
319 
320 G4bool CaloSD::checkHit() {
321  //look in the HitContainer whether a hit with the same ID already exists:
322  bool found = false;
323  if (useMap) {
324  std::map<CaloHitID,CaloG4Hit*>::const_iterator it = hitMap.find(currentID);
325  if (it != hitMap.end()) {
326  currentHit = it->second;
327  found = true;
328  }
329  } else {
330  if (checkHits <= 0) return false;
331  int minhit= (theHC->entries()>checkHits ? theHC->entries()-checkHits : 0);
332  int maxhit= theHC->entries()-1;
333 
334  for (int j=maxhit; j>minhit&&!found; --j) {
335  if ((*theHC)[j]->getID() == currentID) {
336  currentHit = (*theHC)[j];
337  found = true;
338  }
339  }
340  }
341 
342  if (found) {
344  return true;
345  } else {
346  return false;
347  }
348 }
349 
350 int CaloSD::getNumberOfHits() { return theHC->entries(); }
351 
353 #ifdef DebugLog
354  LogDebug("CaloSim") << "CaloSD::CreateNewHit for"
355  << " Unit " << currentID.unitID()
356  << " " << currentID.depth()
357  << " Edeposit = " << edepositEM << " " << edepositHAD;
358  LogDebug("CaloSim") << " primary " << currentID.trackID()
359  << " time slice " << currentID.timeSliceID()
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 " ;
366 
367  if (theTrack->GetCreatorProcess()!=NULL)
368  LogDebug("CaloSim") << theTrack->GetCreatorProcess()->GetProcessName() ;
369  else
370  LogDebug("CaloSim") << "NO process";
371 #endif
372 
373  CaloG4Hit* aHit;
374  if (reusehit.size() > 0) {
375  aHit = reusehit[0];
376  aHit->setEM(0.);
377  aHit->setHadr(0.);
378  reusehit.erase(reusehit.begin());
379  } else {
380  aHit = new CaloG4Hit;
381  }
382 
383  aHit->setID(currentID);
386  aHit->setPosition(posGlobal.x(),posGlobal.y(),posGlobal.z());
388  updateHit(aHit);
389 
390  storeHit(aHit);
391  double etrack = 0;
392  if (currentID.trackID() == primIDSaved) { // The track is saved; nothing to be done
393  } else if (currentID.trackID() == theTrack->GetTrackID()) {
394  etrack= theTrack->GetKineticEnergy();
395  //edm::LogInfo("CaloSim") << "CaloSD: set save the track " << currentID.trackID()
396  // << " etrack " << etrack << " eCut " << energyCut << " flag " << forceSave;
397  if (etrack >= energyCut || forceSave) {
398  TrackInformation* trkInfo = (TrackInformation *)(theTrack->GetUserInformation());
399  trkInfo->storeTrack(true);
400  trkInfo->putInHistory();
401  // trkInfo->setAncestor();
402 #ifdef DebugLog
403  LogDebug("CaloSim") << "CaloSD: set save the track " << currentID.trackID()
404  << " with Hit";
405 #endif
406  }
407  } else {
409 #ifdef DebugLog
410  LogDebug("CaloSim") << "CaloSD : TrackwithHistory pointer for "
411  << currentID.trackID() << " is " << trkh;
412 #endif
413  if (trkh != NULL) {
414  etrack = sqrt(trkh->momentum().Mag2());
415  if (etrack >= energyCut) {
416  trkh->save();
417 #ifdef DebugLog
418  LogDebug("CaloSim") << "CaloSD: set save the track "
419  << currentID.trackID() << " with Hit";
420 #endif
421  }
422  }
423  }
425  if (useMap) totalHits++;
426  return aHit;
427 }
428 
430  if (edepositEM+edepositHAD != 0) {
432 #ifdef DebugLog
433  LogDebug("CaloSim") << "CaloSD: Add energy deposit in " << currentID
434  << " em " << edepositEM/MeV << " hadronic "
435  << edepositHAD/MeV << " MeV";
436 #endif
437  }
438 
439  // buffer for next steps:
441 }
442 
443 void CaloSD::resetForNewPrimary(G4ThreeVector point, double energy) {
447 #ifdef DebugLog
448  LogDebug("CaloSim") << "CaloSD: Incident energy " << incidentEnergy/GeV
449  << " GeV and" << " entrance point " << entrancePoint
450  << " (Global) " << entranceLocal << " (Local)";
451 #endif
452 }
453 
454 double CaloSD::getAttenuation(G4Step* aStep, double birk1, double birk2, double birk3) {
455  double weight = 1.;
456  double charge = aStep->GetPreStepPoint()->GetCharge();
457 
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; // based on alpha particle data
465  weight = 1./(1.+rkb*dedx+c*dedx*dedx);
466 #ifdef DebugLog
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();
471 #endif
472  }
473  return weight;
474 }
475 
476 void CaloSD::update(const BeginOfRun *) {
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();
482 #ifdef DebugLog
483  LogDebug("CaloSim") << "CaloSD: Particle code for e- = " << emPDG
484  << " for e+ = " << epPDG << " for gamma = " << gammaPDG;
485 #endif
486  initRun();
487  runInit = true;
488 }
489 
491 #ifdef DebugLog
492  LogDebug("CaloSim") << "CaloSD: Dispatched BeginOfEvent for " << GetName()
493  << " !" ;
494 #endif
495  clearHits();
496 }
497 
498 void CaloSD::update(const EndOfTrack * trk) {
499  int id = (*trk)()->GetTrackID();
500  TrackInformation *trkI =(TrackInformation *)((*trk)()->GetUserInformation());
501  int lastTrackID = -1;
502  if (trkI) lastTrackID = trkI->getIDonCaloSurface();
503  if (id == lastTrackID) {
504  const TrackContainer * trksForThisEvent = m_trackManager->trackContainer();
505  if (trksForThisEvent != NULL) {
506  int it = (int)(trksForThisEvent->size()) - 1;
507  if (it >= 0) {
508  TrackWithHistory * trkH = (*trksForThisEvent)[it];
509  if (trkH->trackID() == (unsigned int)(id)) tkMap[id] = trkH;
510 #ifdef DebugLog
511  LogDebug("CaloSim") << "CaloSD: get track " << it << " from "
512  << "Container of size " << trksForThisEvent->size()
513  << " with ID " << trkH->trackID();
514  } else {
515  LogDebug("CaloSim") << "CaloSD: get track " << it << " from "
516  << "Container of size " << trksForThisEvent->size()
517  << " with no ID";
518 #endif
519  }
520  }
521  }
522 }
523 
524 void CaloSD::update(const ::EndOfEvent * ) {
525  int count = 0, wrong = 0;
526  bool ok;
527 
528  slave->ReserveMemory(theHC->entries());
529 
530  for (int i=0; i<theHC->entries(); ++i) {
531  ok = saveHit((*theHC)[i]);
532  ++count;
533  if (!ok) ++wrong;
534  }
535 
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";
540  summarize();
541 
542  tkMap.erase (tkMap.begin(), tkMap.end());
543 }
544 
546  if (useMap) hitMap.erase (hitMap.begin(), hitMap.end());
547  for (unsigned int i = 0; i<reusehit.size(); ++i) delete reusehit[i];
548  std::vector<CaloG4Hit*>().swap(reusehit);
549  cleanIndex = 0;
550  previousID.reset();
551  primIDSaved = -99;
552 #ifdef DebugLog
553  LogDebug("CaloSim") << "CaloSD: Clears hit vector for " << GetName() << " " << slave;
554 #endif
555  slave->Initialize();
556 #ifdef DebugLog
557  LogDebug("CaloSim") << "CaloSD: Initialises slave SD for " << GetName();
558 #endif
559 }
560 
562 
563 int CaloSD::getTrackID(G4Track* aTrack) {
564  int primaryID = 0;
565  forceSave = false;
566  TrackInformation* trkInfo=(TrackInformation *)(aTrack->GetUserInformation());
567  if (trkInfo) {
568  primaryID = trkInfo->getIDonCaloSurface();
569 #ifdef DebugLog
570  LogDebug("CaloSim") << "CaloSD: hit update from track Id on Calo Surface "
571  << trkInfo->getIDonCaloSurface();
572 #endif
573  } else {
574  primaryID = aTrack->GetTrackID();
575 #ifdef DebugLog
576  edm::LogWarning("CaloSim") << "CaloSD: Problem with primaryID **** set by "
577  << "force to TkID **** " << primaryID << " in "
578  << preStepPoint->GetTouchable()->GetVolume(0)->GetName();
579 #endif
580  }
581  return primaryID;
582 }
583 
584 uint16_t CaloSD::getDepth(G4Step*) { return 0; }
585 
587  double emin(eminHit);
588  if (hit->getDepth() > 0) emin = eminHitD;
589 #ifdef DebugLog
590  LogDebug("CaloSim") << "Depth " << hit->getDepth() << " Emin = " << emin << " ("
591  << eminHit << ", " << eminHitD << ")";
592 #endif
593  return ((time <= tmaxHit) && (hit->getEnergyDeposit() > emin));
594 }
595 
596 double CaloSD::getResponseWt(G4Track* aTrack) {
597  if (meanResponse) {
598  TrackInformation * trkInfo = (TrackInformation *)(aTrack->GetUserInformation());
599  return meanResponse->getWeight(trkInfo->genParticlePID(), trkInfo->genParticleP());
600  } else {
601  return 1;
602  }
603 }
604 
606  if (previousID.trackID()<0) return;
607  if (hit == 0) {
608  edm::LogWarning("CaloSim") << "CaloSD: hit to be stored is NULL !!";
609  return;
610  }
611 
612  theHC->insert(hit);
613  if (useMap) hitMap.insert(std::pair<CaloHitID,CaloG4Hit*>(previousID,hit));
614 }
615 
617  int tkID;
618  bool ok = true;
619  if (m_trackManager) {
620  tkID = m_trackManager->giveMotherNeeded(aHit->getTrackID());
621  if (tkID == 0) {
622  if (m_trackManager->trackExists(aHit->getTrackID())) tkID = (aHit->getTrackID());
623  else {
624  ok = false;
625  }
626  }
627  } else {
628  tkID = aHit->getTrackID();
629  ok = false;
630  }
631  // edm::LogInfo("CaloSim") << "CalosD: Track ID " << aHit->getTrackID() << " changed to " << tkID << " by SimTrackManager" << " Status " << ok;
632 #ifdef DebugLog
633  LogDebug("CaloSim") << "CalosD: Track ID " << aHit->getTrackID()
634  << " changed to " << tkID << " by SimTrackManager"
635  << " Status " << ok;
636 #endif
637  double time = aHit->getTimeSlice();
638  if (corrTOFBeam) time += correctT;
639  slave->processHits(aHit->getUnitID(), aHit->getEM()/GeV,
640  aHit->getHadr()/GeV, time, tkID, aHit->getDepth());
641 #ifdef DebugLog
642  LogDebug("CaloSim") << "CaloSD: Store Hit at " << std::hex
643  << aHit->getUnitID() << std::dec << " "
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)";
648 #endif
649  return ok;
650 }
651 
653 
654 void CaloSD::update(const BeginOfTrack * trk) {
655  int primary = -1;
656  TrackInformation * trkInfo = (TrackInformation *)((*trk)()->GetUserInformation());
657  if ( trkInfo->isPrimary() ) primary = (*trk)()->GetTrackID();
658 
659 #ifdef DebugLog
660  LogDebug("CaloSim") << "New track: isPrimary " << trkInfo->isPrimary()
661  << " primary ID = " << primary
662  << " primary ancestor ID " << primAncestor;
663 #endif
664 
665  // update the information if a different primary track ID
666 
667  if (primary > 0 && primary != primAncestor) {
668  primAncestor = primary;
669 
670  // clean the hits information
671 
672  if (theHC->entries()>0) cleanHitCollection();
673 
674  }
675 }
676 
678  std::vector<CaloG4Hit*>* theCollection = theHC->GetVector();
679 
680 #ifdef DebugLog
681  LogDebug("CaloSim") << "CaloSD: collection before merging, size = " << theHC->entries();
682 #endif
683 
684  selIndex.reserve(theHC->entries()-cleanIndex);
685  if ( reusehit.size() == 0 ) reusehit.reserve(theHC->entries()-cleanIndex);
686 
687  // if no map used, merge before hits to have the save situation as a map
688  if ( !useMap ) {
689  hitvec.swap(*theCollection);
690  sort((hitvec.begin()+cleanIndex), hitvec.end(), CaloG4HitLess());
691 #ifdef DebugLog
692  LogDebug("CaloSim") << "CaloSD::cleanHitCollection: sort hits in buffer "
693  << "starting from element = " << cleanIndex;
694  for (unsigned int i = 0; i<hitvec.size(); ++i)
695  LogDebug("CaloSim")<<i<<" "<<*hitvec[i];
696 #endif
697  unsigned int i, j;
698  CaloG4HitEqual equal;
699  for (i=cleanIndex; i<hitvec.size(); ++i) {
700  selIndex.push_back(i-cleanIndex);
701  int jump = 0;
702  for (j = i+1; j <hitvec.size() && equal(hitvec[i], hitvec[j]); ++j) {
703  ++jump;
704  // merge j to i
705  (*hitvec[i]).addEnergyDeposit(*hitvec[j]);
706  (*hitvec[j]).setEM(0.);
707  (*hitvec[j]).setHadr(0.);
708  reusehit.push_back(hitvec[j]);
709  }
710  i+=jump;
711  }
712 #ifdef DebugLog
713  LogDebug("CaloSim") << "CaloSD: cleanHitCollection merge the hits in buffer ";
714  for (unsigned int i = 0; i<hitvec.size(); ++i)
715  LogDebug("CaloSim")<<i<<" "<<*hitvec[i];
716 #endif
717  for ( unsigned int i = cleanIndex; i < cleanIndex+selIndex.size(); ++i ) {
718  hitvec[i] = hitvec[selIndex[i-cleanIndex]+cleanIndex];
719  }
720  hitvec.resize(cleanIndex+selIndex.size());
721 #ifdef DebugLog
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)
725  LogDebug("CaloSim")<<i<<" "<<*hitvec[i];
726 #endif
727  hitvec.swap(*theCollection);
728  std::vector<CaloG4Hit*>().swap(hitvec);
729  selIndex.clear();
730  totalHits = theHC->entries();
731  }
732 
733 #ifdef DebugLog
734  LogDebug("CaloSim") << "CaloSD: collection after merging, size = " << theHC->entries();
735 #endif
736 
737  int addhit = 0;
738 
739 #ifdef DebugLog
740  LogDebug("CaloSim") << "CaloSD: Size of reusehit after merge = " << reusehit.size();
741  LogDebug("CaloSim") << "CaloSD: Starting hit selection from index = " << cleanIndex;
742 #endif
743 
744  selIndex.reserve(theCollection->size()-cleanIndex);
745  for (unsigned int i = cleanIndex; i<theCollection->size(); ++i) {
746  CaloG4Hit* aHit((*theCollection)[i]);
747 
748  // selection
749 
750  double time = aHit->getTimeSlice();
751  if (corrTOFBeam) time += correctT;
752  if (!filterHit(aHit,time)) {
753 #ifdef DebugLog
754  LogDebug("CaloSim") << "CaloSD: dropped CaloG4Hit " << " " << *aHit;
755 #endif
756 
757  // create the list of hits to be reused
758 
759  reusehit.push_back((*theCollection)[i]);
760  ++addhit;
761  } else {
762  selIndex.push_back(i-cleanIndex);
763  }
764  }
765 
766 #ifdef DebugLog
767  LogDebug("CaloSim") << "CaloSD: Size of reusehit after selection = " << reusehit.size()
768  << " Number of added hit = " << addhit;
769 #endif
770  if (useMap) {
771  if ( addhit>0 ) {
772  int offset = reusehit.size()-addhit;
773  for (int ii = addhit-1; ii>=0; --ii) {
774  CaloHitID theID = reusehit[offset+ii]->getID();
775  hitMap.erase(theID);
776  }
777  }
778  }
779  for (unsigned int j = 0; j<selIndex.size(); ++j) {
780  (*theCollection)[cleanIndex+j] = (*theCollection)[cleanIndex+selIndex[j]];
781  }
782 
783  theCollection->resize(cleanIndex+selIndex.size());
784  std::vector<unsigned int>().swap(selIndex);
785 
786 #ifdef DebugLog
787  LogDebug("CaloSim") << "CaloSD: hit collection after selection, size = "
788  << theHC->entries();
789  theHC->PrintAllHits();
790 #endif
791 
792  cleanIndex = theHC->entries();
793 }
G4ThreeVector setToLocal(G4ThreeVector, const G4VTouchable *)
Definition: CaloSD.cc:292
#define LogDebug(id)
float edepositEM
Definition: CaloSD.h:120
double energyCut
Definition: CaloSD.h:122
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
virtual bool processHits(uint32_t, double, double, double, int, uint16_t depth=0)
Definition: CaloSlaveSD.cc:34
T getParameter(std::string const &) const
virtual void Initialize()
Definition: CaloSlaveSD.cc:21
T getUntrackedParameter(std::string const &, T const &) const
int timeSlice
Definition: CaloSD.h:140
int i
Definition: DBlmapReader.cc:9
virtual uint32_t setDetUnitId(G4Step *step)=0
G4int emPDG
Definition: CaloSD.h:135
long int flag
Definition: mlp_lapack.h:47
std::vector< PCaloHit > PCaloHitContainer
std::vector< std::string > logicalNames(std::string &readoutName)
double kmaxNeutron
Definition: CaloSD.h:133
void updateHit(CaloG4Hit *)
Definition: CaloSD.cc:429
bool storeTrack() const
int getIDonCaloSurface() const
virtual void Initialize(G4HCofThisEvent *HCE)
Definition: CaloSD.cc:204
bool corrTOFBeam
Definition: CaloSD.h:131
int totalHits
Definition: CaloSD.h:155
void setIncidentEnergy(double e)
Definition: CaloG4Hit.h:66
virtual void EndOfEvent(G4HCofThisEvent *eventHC)
Definition: CaloSD.cc:219
void setEntryLocal(double x, double y, double z)
Definition: CaloG4Hit.h:54
Collection & hits()
Definition: CaloSlaveSD.h:29
double genParticleP() const
bool useMap
Definition: CaloSD.h:124
uint16_t getDepth() const
Definition: CaloG4Hit.h:72
virtual int getTrackID(G4Track *)
Definition: CaloSD.cc:563
int hcID
Definition: CaloSD.h:143
double eminHitD
Definition: CaloSD.h:122
#define abs(x)
Definition: mlp_lapack.h:159
#define NULL
Definition: scimark2.h:8
G4ThreeVector posGlobal
Definition: CaloSD.h:112
int primIDSaved
Definition: CaloSD.h:114
int primAncestor
Definition: CaloSD.h:150
virtual void Clean()
Definition: CaloSlaveSD.cc:43
void fillHits(edm::PCaloHitContainer &, std::string n)
Definition: CaloSD.cc:242
uint16_t depth() const
Definition: CaloHitID.h:26
type of data representation of DDCompactView
Definition: DDCompactView.h:77
double kmaxProton
Definition: CaloSD.h:133
double charge(const std::vector< uint8_t > &Ampls)
double eminHit
Definition: CaloSD.h:122
std::vector< CaloG4Hit * > reusehit
Definition: CaloSD.h:152
G4bool checkHit()
Definition: CaloSD.cc:320
void cleanHitCollection()
Definition: CaloSD.cc:677
void reset()
Definition: CaloHitID.cc:53
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)
Definition: CaloSD.cc:21
bool forceSave
Definition: CaloSD.h:136
void addEnergyDeposit(double em, double hd)
Definition: CaloG4Hit.cc:45
virtual void clearHits()
Definition: CaloSD.cc:545
const TrackContainer * trackContainer() const
int genParticlePID() const
virtual void DrawAll()
Definition: CaloSD.cc:233
virtual void clear()
Definition: CaloSD.cc:231
std::string const collectionName[nCollections]
Definition: Collections.h:39
std::string name() const
Definition: CaloSlaveSD.h:26
double kmaxIon
Definition: CaloSD.h:133
bool suppressHeavy
Definition: CaloSD.h:131
int timeSliceID() const
Definition: CaloHitID.h:23
std::vector< TrackWithHistory * > TrackContainer
Definition: TrackContainer.h:8
float edepositHAD
Definition: CaloSD.h:120
T sqrt(T t)
Definition: SSEVec.h:46
unsigned int trackID() const
int trackID() const
Definition: CaloHitID.h:25
bool trackExists(unsigned int i) const
G4int epPDG
Definition: CaloSD.h:135
virtual void ReserveMemory(unsigned int size)
Definition: CaloSlaveSD.cc:50
CaloSlaveSD * slave
Definition: CaloSD.h:142
void setEM(double e)
Definition: CaloG4Hit.h:60
G4int gammaPDG
Definition: CaloSD.h:135
virtual void update(const BeginOfRun *)
This routine will be called when the appropriate signal arrives.
Definition: CaloSD.cc:476
void setID(uint32_t i, double d, int j, uint16_t k=0)
Definition: CaloG4Hit.h:75
int j
Definition: DBlmapReader.cc:9
std::vector< CaloG4Hit * > hitvec
Definition: CaloSD.h:153
int getNumberOfHits()
Definition: CaloSD.cc:350
CaloHitID previousID
Definition: CaloSD.h:116
virtual void PrintAll()
Definition: CaloSD.cc:235
double getAttenuation(G4Step *aStep, double birk1, double birk2, double birk3)
Definition: CaloSD.cc:454
CaloG4Hit * currentHit
Definition: CaloSD.h:127
int giveMotherNeeded(int i) const
std::vector< unsigned int > selIndex
Definition: CaloSD.h:154
CaloMeanResponse * meanResponse
Definition: CaloSD.h:148
unsigned int offset(bool)
void NaNTrap(G4Step *step)
void storeHit(CaloG4Hit *)
Definition: CaloSD.cc:605
virtual bool filterHit(CaloG4Hit *, double)
Definition: CaloSD.cc:586
G4Track * theTrack
Definition: CaloSD.h:117
virtual G4bool getStepInfo(G4Step *aStep)
Definition: CaloSD.cc:247
double tmaxHit
Definition: CaloSD.h:122
int k[5][pyjets_maxn]
void setID(uint32_t unitID, double timeSlice, int trackID, uint16_t depth=0)
Definition: CaloHitID.cc:44
G4StepPoint * preStepPoint
Definition: CaloSD.h:119
virtual bool ProcessHits(G4Step *step, G4TouchableHistory *tHistory)
Definition: CaloSD.cc:116
void summarize()
Definition: CaloSD.cc:652
int getTrackID() const
Definition: CaloG4Hit.h:68
CaloHitID currentID
Definition: CaloSD.h:116
virtual void initRun()
Definition: CaloSD.cc:561
double correctT
Definition: CaloSD.h:132
bool runInit
Definition: CaloSD.h:129
const math::XYZVectorD & momentum() const
bool isPrimary() const
CSCCFEBTimeSlice const *const timeSlice(T const &data, int nCFEB, int nSample)
virtual void AssignSD(std::string &vname)
std::map< CaloHitID, CaloG4Hit * > hitMap
Definition: CaloSD.h:145
double getEM() const
Definition: CaloG4Hit.h:59
virtual ~CaloSD()
Definition: CaloSD.cc:110
CaloG4HitCollection * theHC
Definition: CaloSD.h:144
void setHadr(double e)
Definition: CaloG4Hit.h:63
float incidentEnergy
Definition: CaloSD.h:113
double getResponseWt(G4Track *)
Definition: CaloSD.cc:596
void setPosition(double x, double y, double z)
Definition: CaloG4Hit.h:57
void setEntry(double x, double y, double z)
Definition: CaloG4Hit.h:51
G4THitsCollection< CaloG4Hit > CaloG4HitCollection
int checkHits
Definition: CaloSD.h:123
G4bool hitExists()
Definition: CaloSD.cc:299
double getTimeSlice() const
Definition: CaloG4Hit.h:70
std::map< int, TrackWithHistory * > tkMap
Definition: CaloSD.h:147
void resetForNewPrimary(G4ThreeVector, double)
Definition: CaloSD.cc:443
const SimTrackManager * m_trackManager
Definition: CaloSD.h:126
uint32_t getUnitID() const
Definition: CaloG4Hit.h:69
uint32_t unitID() const
Definition: CaloHitID.h:22
bool saveHit(CaloG4Hit *)
Definition: CaloSD.cc:616
CaloG4Hit * createNewHit()
Definition: CaloSD.cc:352
*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
Definition: invegas.h:5
virtual uint16_t getDepth(G4Step *)
Definition: CaloSD.cc:584
G4ThreeVector entrancePoint
Definition: CaloSD.h:110
G4ThreeVector entranceLocal
Definition: CaloSD.h:111
double getHadr() const
Definition: CaloG4Hit.h:62
double getEnergyDeposit() const
Definition: CaloG4Hit.h:81
int cleanIndex
Definition: CaloSD.h:151
virtual double getEnergyDeposit(G4Step *step)
Definition: CaloSD.cc:200
bool ignoreTrackID
Definition: CaloSD.h:141