CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
StackingAction.cc
Go to the documentation of this file.
6 
8 
9 #include "G4VProcess.hh"
10 #include "G4EmProcessSubType.hh"
11 #include "G4LogicalVolumeStore.hh"
12 #include "G4RegionStore.hh"
13 
14 #include<algorithm>
15 
16 //#define DebugLog
17 
19 
20  trackNeutrino = p.getParameter<bool>("TrackNeutrino");
21  killHeavy = p.getParameter<bool>("KillHeavy");
22  kmaxIon = p.getParameter<double>("IonThreshold")*MeV;
23  kmaxProton = p.getParameter<double>("ProtonThreshold")*MeV;
24  kmaxNeutron = p.getParameter<double>("NeutronThreshold")*MeV;
25  killDeltaRay = p.getParameter<bool>("KillDeltaRay");
26  maxTrackTime = p.getParameter<double>("MaxTrackTime")*ns;
27  maxTrackTimes = p.getParameter<std::vector<double> >("MaxTrackTimes");
28  maxTimeNames = p.getParameter<std::vector<std::string> >("MaxTimeNames");
29  savePDandCinTracker = p.getUntrackedParameter<bool>("SavePrimaryDecayProductsAndConversionsInTracker",false);
30  savePDandCinCalo = p.getUntrackedParameter<bool>("SavePrimaryDecayProductsAndConversionsInCalo",false);
31  savePDandCinMuon = p.getUntrackedParameter<bool>("SavePrimaryDecayProductsAndConversionsInMuon",false);
32  saveFirstSecondary = p.getUntrackedParameter<bool>("SaveFirstLevelSecondary",false);
33 
34  edm::LogInfo("SimG4CoreApplication") << "StackingAction initiated with"
35  << " flag for saving decay products in "
36  << " Tracker: " << savePDandCinTracker
37  << " in Calo: " << savePDandCinCalo
38  << " in Muon: " << savePDandCinMuon
39  << "\n saveFirstSecondary"
40  << ": " << saveFirstSecondary
41  << " Flag for tracking neutrino: "
42  << trackNeutrino << " Killing Flag "
43  << killHeavy << " protons below "
44  << kmaxProton <<" MeV, neutrons below "
45  << kmaxNeutron << " MeV and ions"
46  << " below " << kmaxIon << " MeV\n"
47  << " kill tracks with "
48  << "time larger than " << maxTrackTime
49  << " ns and kill Delta Ray flag set to "
50  << killDeltaRay;
51  for (unsigned int i=0; i<maxTrackTimes.size(); i++) {
52  maxTrackTimes[i] *= ns;
53  edm::LogInfo("SimG4CoreApplication") << "StackingAction::MaxTrackTime for "
54  << maxTimeNames[i] << " is "
55  << maxTrackTimes[i];
56  }
57  initPointer();
58 }
59 
61 
62 G4ClassificationOfNewTrack StackingAction::ClassifyNewTrack(const G4Track * aTrack) {
63 
64  // G4 interface part
65  G4ClassificationOfNewTrack classification = fUrgent;
66  int flag = 0;
67 
68  NewTrackAction newTA;
69  if (aTrack->GetCreatorProcess()==0 || aTrack->GetParentID()==0) {
70  newTA.primary(aTrack);
71  } else if (aTrack->GetTouchable() == 0) {
72  edm::LogError("SimG4CoreApplication")
73  << "StackingAction: no touchable for track " << aTrack->GetTrackID()
74  << " from " << aTrack->GetParentID()
75  << " with PDG code " << aTrack->GetDefinition()->GetParticleName();
76  classification = fKill;
77  } else {
78  const G4Track * mother = CurrentG4Track::track();
79  if ((savePDandCinTracker && isThisVolume(aTrack->GetTouchable(),tracker))||
80  (savePDandCinCalo && isThisVolume(aTrack->GetTouchable(),calo)) ||
81  (savePDandCinMuon && isThisVolume(aTrack->GetTouchable(),muon)))
82  flag = isItPrimaryDecayProductOrConversion(aTrack, *mother);
83  if (saveFirstSecondary) flag = isItFromPrimary(*mother, flag);
84  newTA.secondary(aTrack, *mother, flag);
85 
86  if (aTrack->GetTrackStatus() == fStopAndKill) classification = fKill;
87  if (killHeavy) {
88  int pdg = aTrack->GetDefinition()->GetPDGEncoding();
89  double ke = aTrack->GetKineticEnergy()/MeV;
90  if (((pdg/1000000000 == 1) && (((pdg/10000)%100) > 0) &&
91  (((pdg/10)%100) > 0) && (ke<kmaxIon)) ||
92  ((pdg == 2212) && (ke < kmaxProton)) ||
93  ((pdg == 2112) && (ke < kmaxNeutron))) classification = fKill;
94  }
95  if (!trackNeutrino) {
96  int pdg = std::abs(aTrack->GetDefinition()->GetPDGEncoding());
97  if (pdg == 12 || pdg == 14 || pdg == 16 || pdg == 18)
98  classification = fKill;
99  }
100  if (isItLongLived(aTrack)) classification = fKill;
101  if (killDeltaRay) {
102  if (aTrack->GetCreatorProcess()->GetProcessType() == fElectromagnetic &&
103  aTrack->GetCreatorProcess()->GetProcessSubType() == fIonisation)
104  classification = fKill;
105  }
106 #ifdef DebugLog
107  LogDebug("SimG4CoreApplication") << "StackingAction:Classify Track "
108  << aTrack->GetTrackID() << " Parent "
109  << aTrack->GetParentID() << " Type "
110  << aTrack->GetDefinition()->GetParticleName()
111  << " K.E. " << aTrack->GetKineticEnergy()/MeV
112  << " MeV from process/subprocess "
113  << aTrack->GetCreatorProcess()->GetProcessType() << "|"
114  << aTrack->GetCreatorProcess()->GetProcessSubType()
115  << " as " << classification << " Flag " << flag;
116 #endif
117  }
118  return classification;
119 }
120 
122 
124 
126 
127  const G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
128  if (lvs) {
129  std::vector<G4LogicalVolume*>::const_iterator lvcite;
130  for (lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++) {
131  if (savePDandCinTracker) {
132  if (strcmp("Tracker",(*lvcite)->GetName().c_str()) == 0) tracker.push_back(*lvcite);
133  if (strcmp("BEAM",(*lvcite)->GetName().substr(0,4).c_str()) == 0) tracker.push_back(*lvcite);
134  }
135  if (savePDandCinCalo) {
136  if (strcmp("CALO",(*lvcite)->GetName().c_str()) == 0) calo.push_back(*lvcite);
137  if (strcmp("VCAL",(*lvcite)->GetName().c_str()) == 0) calo.push_back(*lvcite);
138  }
139  if (savePDandCinMuon) {
140  if (strcmp("MUON",(*lvcite)->GetName().c_str()) == 0) muon.push_back(*lvcite);
141  }
142  }
143  edm::LogInfo("SimG4CoreApplication") << "# of LV for Tracker "
144  << tracker.size() << " for Calo "
145  << calo.size() << " for Muon "
146  << muon.size();
147  for (unsigned int i=0; i<tracker.size(); ++i)
148  edm::LogInfo("SimG4CoreApplication") << "Tracker vol " << i << " name "
149  << tracker[i]->GetName();
150  for (unsigned int i=0; i<calo.size(); ++i)
151  edm::LogInfo("SimG4CoreApplication") << "Calorimeter vol " <<i <<" name "
152  << calo[i]->GetName();
153  for (unsigned int i=0; i<muon.size(); ++i)
154  edm::LogInfo("SimG4CoreApplication") << "Muon vol " << i << " name "
155  << muon[i]->GetName();
156  }
157 
158  const G4RegionStore * rs = G4RegionStore::GetInstance();
159  unsigned int num = maxTimeNames.size();
160  if (num > 0) {
161  std::vector<double> tofs;
162  if (rs) {
163  std::vector<G4Region*>::const_iterator rcite;
164  for (rcite = rs->begin(); rcite != rs->end(); rcite++) {
165  for (unsigned int i=0; i<num; i++) {
166  if ((*rcite)->GetName() == (G4String)(maxTimeNames[i])) {
167  maxTimeRegions.push_back(*rcite);
168  tofs.push_back(maxTrackTimes[i]);
169  break;
170  }
171  }
172  if (tofs.size() == num) break;
173  }
174  }
175  for (unsigned int i=0; i<tofs.size(); i++) {
176  maxTrackTimes[i] = tofs[i];
177  G4String name = "Unknown";
178  if (maxTimeRegions[i]) name = maxTimeRegions[i]->GetName();
179  edm::LogInfo("SimG4CoreApplication") << name << " with pointer "
180  << maxTimeRegions[i]<<" KE cut off "
181  << maxTrackTimes[i];
182  }
183  }
184 
185 }
186 
187 bool StackingAction::isThisVolume(const G4VTouchable* touch,
188  std::vector<G4LogicalVolume*> & lvs) const {
189 
190  bool flag = false;
191  if (lvs.size() > 0 && touch !=0) {
192  int level = ((touch->GetHistoryDepth())+1);
193  if (level >= 3) {
194  unsigned int ii = (unsigned int)(level - 3);
195  flag = (std::count(lvs.begin(),lvs.end(),(touch->GetVolume(ii)->GetLogicalVolume())) != 0);
196  }
197  }
198  return flag;
199 }
200 
202  const G4Track & mother) const {
203 
204  int flag = 0;
205  TrackInformationExtractor extractor;
206  const TrackInformation & motherInfo(extractor(mother));
207  // Check whether mother is a primary
208  if (motherInfo.isPrimary()) {
209  if (aTrack->GetCreatorProcess()->GetProcessType() == fDecay) flag = 1;
210  else if (aTrack->GetCreatorProcess()->GetProcessType() == fElectromagnetic &&
211  aTrack->GetCreatorProcess()->GetProcessSubType() == fGammaConversion) flag = 2;
212  }
213  return flag;
214 }
215 
216 int StackingAction::isItFromPrimary(const G4Track & mother, int flagIn) const {
217 
218  int flag = flagIn;
219  if (flag != 1) {
220  TrackInformationExtractor extractor;
221  const TrackInformation & motherInfo(extractor(mother));
222  if (motherInfo.isPrimary()) flag = 3;
223  }
224  return flag;
225 }
226 
227 bool StackingAction::isItLongLived(const G4Track * aTrack) const {
228 
229  bool flag = false;
230  double time = (aTrack->GetGlobalTime())/nanosecond;
231  double tofM = maxTrackTime;
232  if (maxTimeRegions.size() > 0) {
233  G4Region* reg = aTrack->GetTouchable()->GetVolume(0)->GetLogicalVolume()->GetRegion();
234  for (unsigned int i=0; i<maxTimeRegions.size(); i++) {
235  if (reg == maxTimeRegions[i]) {
236  tofM = maxTrackTimes[i];
237  break;
238  }
239  }
240  }
241  if (time > tofM) flag = true;
242  return flag;
243 }
#define LogDebug(id)
void primary(const G4Track *aSecondary) const
std::vector< G4LogicalVolume * > tracker
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< G4Region * > maxTimeRegions
long int flag
Definition: mlp_lapack.h:47
std::vector< double > maxTrackTimes
#define abs(x)
Definition: mlp_lapack.h:159
int isItPrimaryDecayProductOrConversion(const G4Track *, const G4Track &) const
virtual ~StackingAction()
virtual G4ClassificationOfNewTrack ClassifyNewTrack(const G4Track *aTrack)
StackingAction(const edm::ParameterSet &ps)
std::vector< std::string > maxTimeNames
virtual void PrepareNewEvent()
void secondary(const G4Track *aSecondary, const G4Track &mother, int) const
std::vector< G4LogicalVolume * > calo
long long int num
Definition: procUtils.cc:71
int ke
bool isPrimary() const
bool savePDandCinTracker
std::vector< G4LogicalVolume * > muon
bool isThisVolume(const G4VTouchable *, std::vector< G4LogicalVolume * > &) const
virtual void NewStage()
tuple level
Definition: testEve_cfg.py:34
int isItFromPrimary(const G4Track &, int) const
bool isItLongLived(const G4Track *) const
static const G4Track * track()