LBNEStackingAction.cc
Go to the documentation of this file.
1 //----------------------------------------------------------------------
2 // LBNEStackingAction.cc
3 // $Id: LBNEStackingAction.cc,v 1.2.2.2 2013/09/05 12:32:50 lebrun Exp $
4 //----------------------------------------------------------------------
5 
6 #include "LBNEStackingAction.hh"
7 #include "G4SDManager.hh"
8 #include "G4RunManager.hh"
9 #include "G4Event.hh"
10 #include "G4HCofThisEvent.hh"
11 #include "G4Track.hh"
12 #include "G4TrackStatus.hh"
13 #include "G4ParticleDefinition.hh"
14 #include "G4ParticleTypes.hh"
15 #include "G4ios.hh"
16 #include "LBNEImpWeight.hh"
17 #include "LBNETrackInformation.hh"
18 #include "LBNETrajectory.hh"
19 #include "LBNEAnalysis.hh"
20 #include "LBNERunManager.hh"
21 #include "LBNEQuickPiToNu.hh"
22 
23 //----------------------------------------------------------------------------------
25 {
26  pRunManager=(LBNERunManager*)LBNERunManager::GetRunManager();
27  std::cout << "LBNEStackingAction Constructor Called." << std::endl;
28  doImportanceWeightSelection = true; // Per G4Numi tradition..
29  fStackingKillingThreshold = 0.050*CLHEP::GeV;
30 }
31 
32 //----------------------------------------------------------------------------------
34 {
35  int verboseLevel = G4EventManager::GetEventManager()->GetTrackingManager()->GetVerboseLevel();
36  if(verboseLevel > 0)
37  {
38  std::cout << "LBNEStackingAction Destructor Called." << std::endl;
39  }
40 }
41 
42 void LBNEStackingAction::OpenHadronAtVertex() const { // Pseudo const..
43  if (fOutHadronAtVectex.is_open()) {
44  std::cerr << " From LBNEStackingAction::OpenHadronAtVertex, file already opened, fatal... " << std::endl;
45  exit(2);
46  }
47  // Set a file name that can be unique.. Particularly, tailored for the use on FermiGrid
48  // But not mandated.
49  int clusterNum = 0;
50  int procNum =0;
51  const char *clusterEnv = getenv("CLUSTER");
52  if (clusterEnv != 0) clusterNum = atoi(clusterEnv);
53  const char *procEnv = getenv("PROCESS");
54  if (procEnv != 0) procNum = atoi(procEnv);
55  std::string fName("./hadronFluxStackingASCII.txt");
56  if ((clusterNum != 0) || (procNum != 0)) {
57 // const char *userEnv = getenv("USER"); // assume it always works
58 // std::string aUserStr(userEnv);
59  std::ostringstream fNStrStr;
60  fNStrStr << "./hadronFluxStackingASCII_"
61  << clusterNum << "_" << procNum << ".txt";
62  fName = fNStrStr.str();
63  }
64  fOutHadronAtVectex.open(fName.c_str());
65  fOutHadronAtVectex << " Evt X Y Z Px Py Pz Id " << std::endl;
66  std::cout << " LBNEStackingAction::OpenHadronAtVertex, Opening file " <<
67  fName << std::endl;
68  fName = std::string("./hadronFluxStackingASCIICl.txt");
69  if ((clusterNum != 0) || (procNum != 0)) {
70 // const char *userEnv = getenv("USER"); // assume it always works
71 // std::string aUserStr(userEnv);
72  std::ostringstream fNStrStr;
73  fNStrStr << "./hadronFluxStackingASCIICl_"
74  << clusterNum << "_" << procNum << ".txt";
75  fName = fNStrStr.str();
76  }
77  fOutHadronAtVectexCl.open(fName.c_str());
78  fOutHadronAtVectexCl << " Evt X Y Z Px Py Pz Id " << std::endl;
79  std::cout << " LBNEStackingAction::OpenHadronAtVertex, Opening file " <<
80  fName << std::endl;
81 
82 }
83 
84 //----------------------------------------------------------------------------------
85 G4ClassificationOfNewTrack LBNEStackingAction::ClassifyNewTrack(const G4Track * aTrack)
86 {
87  //
88  // v410.2: Rare bug somewhere, we have track with bad parameters, we do not want them..
89  //
90  G4ThreeVector posTmp = aTrack->GetPosition();
91  G4ThreeVector momTmp = aTrack->GetMomentum();
92  for (size_t k=0; k != 3; k++) {
93  if (std::isnan(posTmp[k])) {
94  std::cerr << " LBNEStackingAction::ClassifyNewTrack, position has NaN, rejected " << std::endl;
95  return fKill;
96  }
97  if (std::isnan(momTmp[k])) {
98  std::cerr << " LBNEStackingAction::ClassifyNewTrack, momentum has NaN, rejected " << std::endl;
99  return fKill;
100  }
101  }
102  if (std::isnan(aTrack->GetTotalEnergy()) || std::isnan(aTrack->GetKineticEnergy())) {
103  std::cerr << " LBNEStackingAction::ClassifyNewTrack, Energy is NaN, rejected " << std::endl;
104  return fKill;
105  }
106 
107  if (fOutHadronAtVectex.is_open()) {
108  int myPid = aTrack->GetParticleDefinition()->GetPDGEncoding();
109  if ((std::abs(myPid) == 211) || (myPid == 2212) ||
110  (std::abs(myPid) == 321) || (std::abs(myPid) == 311) ||
111  (std::abs(myPid) == 130) || (std::abs(myPid) == 310)) {
112  double pTotSq = 0; for (int k=0; k != 3; k++) pTotSq += aTrack->GetMomentum()[k]*aTrack->GetMomentum()[k];
113  double pTot = std::sqrt(pTotSq)/CLHEP::GeV;
114 // if ((myPid == 2212) || ((pTot > fStackingKillingThreshold) && (pTot < 15.))) { // take all the proton, to check we are on target, and the relevant pions
115  if ((myPid == 2212) || ((pTot > 0.001) && (pTot < 50.))) { // take all the proton, to check we are on target, and the relevant pions
116  int evtId = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
117  fOutHadronAtVectex << " " << evtId;
118  for (int k=0; k != 3; k++) fOutHadronAtVectex << " " << aTrack->GetPosition()[k];
119  for (int k=0; k != 3; k++) fOutHadronAtVectex << " " << (aTrack->GetMomentum()[k])/CLHEP::GeV;
120  fOutHadronAtVectex << " " << myPid << std::endl;
121  }
122 
123  }
124  }
125 // const LBNETrackInformation *oldTrInfo = reinterpret_cast<const LBNETrackInformation*>(aTrack->GetUserInformation());
126 // std::cerr << " G4ClassificationOfNewTrack LBNEStackingAction::ClassifyNewTrack, ptr track "
127 // << (void*) aTrack << " info " << (void*) oldTrInfo << std::endl;
128 // if (oldTrInfo != 0) std::cerr << " ... PFFlag " << oldTrInfo->GetPFFlag() << std::endl;
129 
130  G4ClassificationOfNewTrack classification = fUrgent;
131  G4ParticleDefinition * particleType = aTrack->GetDefinition();
132 
133  int verboseLevel = G4EventManager::GetEventManager()->GetTrackingManager()->GetVerboseLevel();
134  if(verboseLevel > 0)
135  {
136  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
137  std::cout << "Event " << evtno << ": LBNEStackingAction::ClassifyNewTrack for Particle "
138  << "\"" << particleType->GetParticleName() << "\"" << " Called." << std::endl;
139  }
140  if (LBNEQuickPiToNuVect::Instance()->doIt() && (std::abs(particleType->GetPDGEncoding()) == 211)) {
141  int sign = (particleType->GetPDGEncoding() > 0) ? 1 : -1;
142  LBNEQuickPiToNuVect::Instance()->addPion(aTrack->GetTrackID(), sign, aTrack->GetTotalEnergy(), aTrack->GetMomentum());
143  }
144  const LBNERunAction *runUserAction = reinterpret_cast<const LBNERunAction *>(G4RunManager::GetRunManager()->GetUserRunAction());
145  //
146  // If we are in the target, and tally the energy deposition, skip the re-classification done below..
147  //
148  if (runUserAction->GetDoComputeEDepInGraphite() && (aTrack->GetVolume() != 0)) {
149  const std::string vName = aTrack->GetVolume()->GetName();
150  if(vName.find("Target") != std::string::npos) return classification;
151  }
152  if (runUserAction->GetDoComputeEDepInArgonGas()) {
153  if (aTrack->GetPosition()[2] < 9900.) return classification; // rough Z- cut
154  }
155  //
156  // Now do the reclassification, for CPU speed optimization..
157  //
158  LBNEStackingAction::KillEMParticles(classification, aTrack);
159  //LBNEStackingAction::KillltZeroPzParticles(classification, aTrack);
160  LBNEStackingAction::KillThresholdParticles(classification, aTrack);
161 // LBNEStackingAction::KillOutOfWorldParticles(classification, aTrack); Obsolete in G4
162  LBNEStackingAction::KillUnimportantParticles(classification, aTrack);
163 //
164  if (fOutHadronAtVectexCl.is_open() && (classification == fUrgent)) {
165  int myPid = aTrack->GetParticleDefinition()->GetPDGEncoding();
166  if (std::abs(myPid) == 211) {
167  double pTotSq = 0; for (int k=0; k != 3; k++) pTotSq += aTrack->GetMomentum()[k]*aTrack->GetMomentum()[k];
168  double pTot = std::sqrt(pTotSq)/CLHEP::GeV;
169  if ((myPid == 2212) || ((pTot > fStackingKillingThreshold) && (pTot < 15.))) { // take all the proton, to check we are on target, and the relevant pions
170  int evtId = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
171  fOutHadronAtVectexCl << " " << evtId;
172  for (int k=0; k != 3; k++) fOutHadronAtVectexCl << " " << aTrack->GetPosition()[k];
173  for (int k=0; k != 3; k++) fOutHadronAtVectexCl << " " << (aTrack->GetMomentum()[k])/CLHEP::GeV;
174  fOutHadronAtVectexCl << " " << myPid << std::endl;
175  }
176 
177  }
178  }
179 
180  LBNEStackingAction::UnKillMuonAlcoveParticles(classification,aTrack);
181 //
182 // specific checks for impotance weight.
183 //
184 // if (std::abs(aTrack->GetParticleDefinition()->GetPDGEncoding()) == 211) {
185 // LBNETrackInformation *trInfo = reinterpret_cast<LBNETrackInformation*> (aTrack->GetUserInformation());
186 // if (trInfo != 0) std::cerr << "LBNEStackingAction::classifyNewTrack, Track ID " << aTrack->GetTrackID()
187 // << " Imp Weight " << trInfo->GetNImpWt() << std::endl;
188 // else std::cerr << "LBNEStackingAction::classifyNewTrack, Track ID " << aTrack->GetTrackID()
189 // << " has no weight ... " << std::endl;
190 // }
191 
192  return classification;
193 }
194 //----------------------------------------------------------------------------------
196 {
197  int verboseLevel = G4EventManager::GetEventManager()->GetTrackingManager()->GetVerboseLevel();
198  if(verboseLevel > 1)
199  {
200  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
201  std::cout << "Event " << evtno << ": LBNEStackingAction::NewStage Called." << std::endl;
202  }
203 
204 
205  // stackManager->ReClassify();
206  // return;
207 }
208 //----------------------------------------------------------------------------------
210 {
211  int verboseLevel = G4EventManager::GetEventManager()->GetTrackingManager()->GetVerboseLevel();
212  if(verboseLevel > 1)
213  {
214  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
215  std::cout << "Event " << evtno << ": LBNEStackingAction::PrepareNewEvent Called." << std::endl;
216  }
217 
218 }
219 
220 void LBNEStackingAction::UnKillMuonAlcoveParticles(G4ClassificationOfNewTrack &classification, const G4Track* aTrack)
221 {
222  if (classification != fKill) return;
223 
224  //
225  LBNERunManager* runMan = dynamic_cast<LBNERunManager*>(G4RunManager::GetRunManager());
226  if (!runMan->GetCreateAlcoveTrackingOutput()) return;
227  G4String str = "";
228  if (aTrack->GetVolume()->GetLogicalVolume())
229  str = aTrack->GetVolume()->GetLogicalVolume()->GetName();
230  if (str == "HadronAbsorberSculpAirBuffer" || str =="MuonTrackingBox" || str == "HadronAbsorberSculpBackWall")
231  {
232  classification = fUrgent;
233  }
234 
235 }
236 
237 
238 
239 //----------------------------------------------------------------------------------
240 //----------------------------------------------------------------------------------
241 //----------------------------------------------------------------------------------
242 //----------------------------------------------------------------------------------
243 //----------------------------------------------------------------------------------
244 void LBNEStackingAction::KillEMParticles(G4ClassificationOfNewTrack &classification,
245  const G4Track * aTrack)
246 {
247  const LBNERunAction * runAct = dynamic_cast<const LBNERunAction*>(pRunManager->GetUserRunAction());
248  if (runAct->GetDoComputeEDepInHorns() || runAct->GetDoComputeEDepInGraphite ()
249  || runAct->GetDoComputeEDepInArgonGas()) return;
250  G4ParticleDefinition * particleType = aTrack->GetDefinition();
251  // Discard Gammas, Electrons, ...
252  if ( (particleType==G4Gamma::GammaDefinition() ||
253  particleType==G4Electron::ElectronDefinition() ||
254  particleType==G4Positron::PositronDefinition() ) &&
255  (classification != fKill) )
256  {classification = fKill;}
257 
258 }
259 
260 //----------------------------------------------------------------------------------
261 void LBNEStackingAction::KillltZeroPzParticles(G4ClassificationOfNewTrack& classification,
262  const G4Track * aTrack)
263 {
264  const LBNERunAction *runUserAction = reinterpret_cast<const LBNERunAction *>(G4RunManager::GetRunManager()->GetUserRunAction());
265  if (runUserAction->GetDoComputeEDepInHorns()) return;
266  if (runUserAction->GetDoComputeEDepInGraphite() && (aTrack->GetMaterial() != 0)) {
267  const std::string tgt("Target");
268  const std::string tg = aTrack->GetMaterial()->GetName();
269  if (tg == tgt) return; // Don't loose any track, computing E-Loss...
270  }
271  //Discard particles with pz<0
272  G4ThreeVector momentum=aTrack->GetMomentumDirection();
273  if (momentum[2]<0 && (classification != fKill)) {classification = fKill;}
274 
275 }
276 
277 //----------------------------------------------------------------------------------
278 void LBNEStackingAction::KillThresholdParticles(G4ClassificationOfNewTrack& classification,
279  const G4Track * aTrack)
280 
281 {
282 
283  const LBNERunAction * runAct = dynamic_cast<const LBNERunAction*>(pRunManager->GetUserRunAction());
284  if (runAct->GetDoComputeEDepInHorns() || runAct->GetDoComputeEDepInGraphite ()
285  || runAct->GetDoComputeEDepInArgonGas()) return;
286  G4ParticleDefinition * particleType = aTrack->GetDefinition();
287  //Discard particles with kinetic energy < KillTracking Threshold.GeV (that are not neutrinos)
288  if ((particleType!=G4NeutrinoE::NeutrinoEDefinition())&&
289  (particleType!=G4NeutrinoMu::NeutrinoMuDefinition())&&
290  (particleType!=G4NeutrinoTau::NeutrinoTauDefinition())&&
291  (particleType!=G4AntiNeutrinoE::AntiNeutrinoEDefinition())&&
292  (particleType!=G4AntiNeutrinoMu::AntiNeutrinoMuDefinition())&&
293  (particleType!=G4AntiNeutrinoTau::AntiNeutrinoTauDefinition()))
294  {
295  G4double energy = aTrack->GetKineticEnergy();
296 //
297 // We hard code this cut, as the LBNEDataInput obsolete.. Could become a data card
298 //
299  if ((energy < fStackingKillingThreshold ) && (classification != fKill))
300  {classification = fKill;}
301  }
302 
303 
304 }
305 
306 
307 //----------------------------------------------------------------------------------
308 /*
309 void LBNEStackingAction::KillOutOfWorldParticles(G4ClassificationOfNewTrack& classification,
310  const G4Track * aTrack)
311 {
312 
313  //check if track is inside world (some neutral particles make huge jumps from horns (field part) and
314  // end up decaying in ~infinity which occasionaly causes g4numi to crash
315 
316  int verboseLevel = G4TrackingManager::GetVerboseLevel()();
317 
318  G4ThreeVector position=aTrack->GetPosition();
319  if ((classification != fKill) &&
320  ((sqrt(position[0]*position[0]+position[1]*position[1])>LBNEData->GetRockRadius())||
321  position[2]>LBNEData->GetRockHalfLen()))
322  {
323  if (verboseLevel > 2)
324  {
325  G4cout <<"LBNEStackingAction: Killing Out of World Particle" <<G4endl;
326  G4cout << " Particle type: "<<aTrack->GetDefinition()->GetParticleName()
327  << " ; Parent ID: "<<aTrack->GetParentID()
328  << " ; Kinetic Energy = "<<aTrack->GetKineticEnergy()/GeV<<" GeV"<<G4endl;
329  G4cout << " Position (m) = "<<position/m<<G4endl;
330  }
331  classification =fKill;
332  }
333 
334 }
335 */
336 //----------------------------------------------------------------------------------
337 void LBNEStackingAction::KillUnimportantParticles(G4ClassificationOfNewTrack& classification,
338  const G4Track * aTrack)
339 {
340  const LBNERunAction * runAct = dynamic_cast<const LBNERunAction*>(pRunManager->GetUserRunAction());
341  if (runAct->GetDoComputeEDepInHorns() || runAct->GetDoComputeEDepInGraphite ()
342  || runAct->GetDoComputeEDepInArgonGas()) return;
343  //If importance weighting might be on:
344  if ((classification != fKill))
345  {
346  LBNETrackInformation* trackInfo=(LBNETrackInformation*)(aTrack->GetUserInformation());
348  if (trackInfo!=0)
349  {
350  G4double Nimpweight=LBNEImpWeight::CalculateImpWeight(aTrack);
351 // std::cerr << " LBNEStackingAction::KillUnimportantParticles, E "
352 // << aTrack->GetTotalEnergy() << " Imp weight " << Nimpweight << std::endl;
353  if(Nimpweight==0.) { classification = fKill; }
354  else { trackInfo->SetNImpWt(Nimpweight); }
355  }
356  } else {
357  // We do not change the classification..
358  if (trackInfo!=0) trackInfo->SetNImpWt(1.0);
359  }
360  }
361 }
362 
void UnKillMuonAlcoveParticles(G4ClassificationOfNewTrack &classification, const G4Track *aTrack)
bool GetDoComputeEDepInGraphite() const
bool GetDoComputeEDepInArgonGas() const
std::string string
Definition: nybbler.cc:12
bool GetCreateAlcoveTrackingOutput() const
std::ofstream fOutHadronAtVectexCl
void OpenHadronAtVertex() const
LBNERunManager * pRunManager
virtual G4ClassificationOfNewTrack ClassifyNewTrack(const G4Track *aTrack)
void KillUnimportantParticles(G4ClassificationOfNewTrack &classification, const G4Track *aTrack)
T abs(T value)
bool GetDoComputeEDepInHorns() const
void SetNImpWt(G4double nimpweight)
static G4double CalculateImpWeight(const G4Track *aTrack)
std::string getenv(std::string const &name)
Definition: getenv.cc:15
static LBNEQuickPiToNuVect * Instance()
virtual void PrepareNewEvent()
int sign(double val)
Definition: UtilFunc.cxx:103
std::ofstream fOutHadronAtVectex
void KillEMParticles(G4ClassificationOfNewTrack &classification, const G4Track *aTrack)
void KillThresholdParticles(G4ClassificationOfNewTrack &classification, const G4Track *aTrack)
void KillltZeroPzParticles(G4ClassificationOfNewTrack &classification, const G4Track *aTrack)
void addPion(int trNum1, int sign1, double e, G4ThreeVector p)
static QCString str
QTextStream & endl(QTextStream &s)