LBNETrackingAction.cc
Go to the documentation of this file.
1 //----------------------------------------------------------------------
2 // LBNETrackingAction.cc
3 // $Id: LBNETrackingAction.cc,v 1.1.1.1.2.2 2013/10/28 12:06:50 lebrun Exp $
4 //----------------------------------------------------------------------
5 
6 
7 #include "G4TrackingManager.hh"
8 #include "G4Track.hh"
9 #include "G4Trajectory.hh"
10 #include "G4ParticleDefinition.hh"
11 #include "G4ParticleTypes.hh"
12 #include "G4EventManager.hh"
13 
14 #include "LBNETrackInformation.hh"
15 #include "LBNETrackingAction.hh"
16 #include "LBNERunManager.hh"
17 #include "LBNEAnalysis.hh"
18 #include "LBNETrajectory.hh"
20 #include "LBNEEventAction.hh"
21 #include "LBNEQuickPiToNu.hh"
22 #include "LBNESteppingAction.hh"
23 
24 
25 //--------------------------------------------------------------------------------------
27 {
28  pRunManager=(LBNERunManager*)LBNERunManager::GetRunManager();
29  NPGA=(LBNEPrimaryGeneratorAction*)pRunManager->GetUserPrimaryGeneratorAction();
30  // No message, fpTrackingManager pointer not set yet ..
31  doImportanceWeightSelection = true; // Per G4Numi tradition..
32 }
33 //--------------------------------------------------------------------------------------
35 {
36  if(fpTrackingManager->GetVerboseLevel() > 0)
37  {
38  std::cout << "LBNETrackingAction Destructor Called." << std::endl;
39  }
40 }
41 void LBNETrackingAction::OpenHadronAtVertex() const { // Pseudo const..
42  if (fOutHadronAtVectex.is_open()) {
43  std::cerr << " From LBNETrackingAction::OpenHadronAtVertex, file already opened, fatal... " << std::endl;
44  exit(2);
45  }
46  // Set a file name that can be unique.. Particularly, tailored for the use on FermiGrid
47  // But not mandated.
48  int clusterNum = 0;
49  int procNum =0;
50  const char *clusterEnv = getenv("CLUSTER");
51  if (clusterEnv != 0) clusterNum = atoi(clusterEnv);
52  const char *procEnv = getenv("PROCESS");
53  if (procEnv != 0) procNum = atoi(procEnv);
54  std::string fName("./hadronFluxPreTrackingASCII.txt");
55  if ((clusterNum != 0) || (procNum != 0)) {
56 // const char *userEnv = getenv("USER"); // assume it always works
57 // std::string aUserStr(userEnv);
58  std::ostringstream fNStrStr;
59  fNStrStr << "./hadronFluxAtPreTrackingASCII_"
60  << clusterNum << "_" << procNum << ".txt";
61  fName = fNStrStr.str();
62  }
63  fOutHadronAtVectex.open(fName.c_str());
64  fOutHadronAtVectex << " Evt X Y Z Px Py Pz Id " << std::endl;
65  std::cout << " LBNETrackingAction::OpenHadronAtVertex, Opening file " <<
66  fName << std::endl;
67 
68 }
69 
70 //--------------------------------------------------------------------------------------
71 void LBNETrackingAction::PreUserTrackingAction(const G4Track* aTrack)
72 {
73 
74  const LBNESteppingAction* LBNEUStep=
75  reinterpret_cast<const LBNESteppingAction*>(pRunManager->GetUserSteppingAction());
76  LBNEUStep->ResetFlagParticleGotToHAFront();
77  if (fOutHadronAtVectex.is_open()) {
78  int myPid = aTrack->GetParticleDefinition()->GetPDGEncoding();
79  if ((std::abs(myPid) == 211) || (myPid == 2212) ||
80  (std::abs(myPid) == 321) || (std::abs(myPid) == 311)) {
81  double pTotSq = 0; for (int k=0; k != 3; k++) pTotSq += aTrack->GetMomentum()[k]*aTrack->GetMomentum()[k];
82  double pTot = std::sqrt(pTotSq)/CLHEP::GeV;
83  if ((myPid == 2212) || ((pTot > 0.001) && (pTot < 50.))) { // take all the proton, to check we are on target, and the relevant pions
84  int evtId = G4RunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
85  fOutHadronAtVectex << " " << evtId;
86  for (int k=0; k != 3; k++) fOutHadronAtVectex << " " << aTrack->GetPosition()[k];
87  for (int k=0; k != 3; k++) fOutHadronAtVectex << " " << (aTrack->GetMomentum()[k])/CLHEP::GeV;
88  fOutHadronAtVectex << " " << myPid << std::endl;
89  }
90 
91  }
92  }
93 // const LBNETrackInformation *oldTrInfo = reinterpret_cast<const LBNETrackInformation*>(aTrack->GetUserInformation());
94 // std::cerr << " LBNETrackingAction::PreUserTrackingAction, track ID " << aTrack->GetTrackID()
95 // << " Track Ptr " << (void *) aTrack << " User info " << oldTrInfo << std::endl;
96 // if ((oldTrInfo != 0) && (oldTrInfo->GetPFFlag() == 1)) {
97 // std::cerr << " ............. skip, job done in LBNEPerfectFocusProcess " << std::endl;
98 // return;
99 // }
100  //
101  //
102  // This was a track whose Pt has been set to zero, and redefine. No further information is needed.
103  //
104 
105 // G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
106  if(fpTrackingManager->GetVerboseLevel() > 1)
107  {
108  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
109  std::cout << "Event " << evtno << ": LBNETrackingAction::PreUserTrackingAction() Called." << std::endl;
110  if(fpTrackingManager->GetVerboseLevel() > 2)
111  {
112  LBNETrackInformation* trackinfo=new LBNETrackInformation();
113  trackinfo -> Print(aTrack);
114  delete trackinfo;
115  }
116  }
117  // Debugging the infinite memory blowup..
118 // if (evtno == 32135) {
119 // std::cerr << " At track " << aTrack->GetTrackID() << " At " << aTrack->GetPosition()
120 // << " P " << aTrack->GetMomentumDirection() << " E " << aTrack->GetTotalEnergy()
121 // << " name " << aTrack->GetParticleDefinition()->GetParticleName() << std::endl;
122 // }
123 
124  const LBNESteppingAction *pStep = dynamic_cast<const LBNESteppingAction*>(pRunManager->GetUserSteppingAction());
125  pStep->ResetNumSteps();
126 
128 
129  if(fpTrackingManager->GetVerboseLevel() > 2) std::cerr << " .... ..... about to store trajectory " << std::endl;
130 
131  //Store tracks in trajectory container
132  fpTrackingManager->SetStoreTrajectory(true);
133  fpTrackingManager->SetTrajectory(new LBNETrajectory(aTrack));
134 
135  if(fpTrackingManager->GetVerboseLevel() > 2) std::cerr << " .... ..... did store trajectory " << std::endl;
137  if(fpTrackingManager->GetVerboseLevel() > 2) std::cerr << " .... ..... did Analyze if neutrino " << std::endl;
138 
139 
140 
141  if(fpTrackingManager->GetVerboseLevel() > 1)
142  {
143  std::cout << " LBNETrackingAction::PreUserTrackingAction() - done." << std::endl;
144  }
145 
146 }
147 //--------------------------------------------------------------------------------------
149 {
150  if(fpTrackingManager->GetVerboseLevel() > 1)
151  {
152  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
153  std::cout << "Event " << evtno << ": LBNETrackingAction::PostUserTrackingAction() Called." << std::endl;
154  if(fpTrackingManager->GetVerboseLevel() > 2)
155  {
156  LBNETrackInformation* trackinfo=new LBNETrackInformation();
157  trackinfo -> Print(aTrack);
158  delete trackinfo;
159  }
160  }
161 //
162 // specific checks for impotance weight.
163 //
164 // if (std::abs(aTrack->GetParticleDefinition()->GetPDGEncoding()) == 211) {
165 // G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
166 // LBNETrackInformation *trInfo = reinterpret_cast<LBNETrackInformation*> (aTrack->GetUserInformation());
167 // if (trInfo != 0) std::cerr << "LBNETrackingAction::PostUserTrackingAction, Event " << evtno
168 // << " Track ID " << aTrack->GetTrackID() << " Imp Weight " << trInfo->GetNImpWt() << std::endl;
169 // else std::cerr << "LBNETrackingAction::PostUserTrackingAction, Event " << evtno
170 // << " Track ID " << aTrack->GetTrackID() << " has no weight " << std::endl;
171 //
172 // }
174 
175 
176  if(fpTrackingManager->GetVerboseLevel() > 1)
177  {
178  std::cout << " LBNETrackingAction::PostUserTrackingAction() - done." << std::endl;
179  }
180 
181 
182 }
183 
184 
185 
186 
187 
188 //--------------------------------------------------------------------------------------
189 //--------------------------------------------------------------------------------------
190 //--------------------------------------------------------------------------------------
191 //--------------------------------------------------------------------------------------
192 //--------------------------------------------------------------------------------------
194 {
195 
196  if(fpTrackingManager->GetVerboseLevel() > 3)
197  {
198  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
199  std::cout << "Event " << evtno
200  << ": NumiTrackingAction::SetPreNumiTrackInformation() Called."
201  << std::endl;
202  }
203 
204  //set tgen (and weight for fluka nad mars input)
205  if (aTrack->GetTrackID()==1)
206  {
208  LBNERunManager* theRunManager=dynamic_cast<LBNERunManager*>(G4RunManager::GetRunManager());
209  if (theRunManager->GetUseFlukaInput() || theRunManager->GetUseMarsInput())
210  {
211  info->SetTgen(NPGA->GetTgen());
212  info->SetNImpWt(NPGA->GetWeight());
213  if (doImportanceWeightSelection) info->SetNImpWt(NPGA->GetWeight()); // set weight of the new track equal to parent weight
214  else info->SetNImpWt(1.0);
215 // std::cerr << " LBNETrackingAction::SetPreLBNETrackInformation, setting imp weight " << NPGA->GetWeight() << std::endl;
216  }
217  else
218  {
219  info->SetTgen(1);
220  info->SetNImpWt(1.);
221 // std::cerr << " LBNETrackingAction::SetPreLBNETrackInformation, setting imp weight to 1 " << std::endl;
222  }
223  G4Track* theTrack = (G4Track*)aTrack;
224  theTrack->SetUserInformation(info);
225  }
226 
227 
228 }
229 
230 //--------------------------------------------------------------------------------------
232 {
233  if(fpTrackingManager->GetVerboseLevel() > 3)
234  {
235  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
236  std::cout << "Event " << evtno
237  << ": NumiTrackingAction::SetPostNumiTrackInformation() Called."
238  << std::endl;
239  }
240 
241  // set tgen(secondary) = tgen(parent)+1
242  LBNETrackInformation* info = (LBNETrackInformation*)(aTrack->GetUserInformation());
243  if (info!=0)
244  {
245  G4int tgen = info->GetTgen();
246  G4double nimpwt = info->GetNImpWt();
247  G4TrackVector* SecVector = fpTrackingManager->GimmeSecondaries();
248  for (size_t ii=0;ii<(*SecVector).size();ii++)
249  {
251  newinfo->SetTgen(tgen+1); // set generation of daughter particles
252  if (doImportanceWeightSelection) newinfo->SetNImpWt(nimpwt); // set weight of the new track equal to parent weight
253  else newinfo->SetNImpWt(1.0);
254  newinfo->SetPFFlag(info->GetPFFlag()); // set weight of the new track equal to parent weight
255  (*SecVector)[ii]->SetUserInformation(newinfo);
256  // The above algorithm makes sense, but, in most cases, is a waste of time, as it assign a weight
257  // to electrons, ions, etc,, that were collected while the track moved though the beam line
258  // Performance could be improved..
259  //
260 // if (doImportanceWeightSelection &&
261 // ((*SecVector)[ii]->GetDynamicParticle()->GetParticleDefinition()->GetPDGEncoding() == 211)) {
262 // std::cerr << " LBNETrackingAction::SetPostLBNETrackInformation, setting imp weight "
263 // << nimpwt << " E " << (*SecVector)[ii]->GetTotalEnergy()
264 // << " name " << (*SecVector)[ii]->GetDynamicParticle()->GetParticleDefinition()->GetParticleName() << std::endl;
265 // }
266  }
267  }
268 }
269 
270 //--------------------------------------------------------------------------------------
271 void LBNETrackingAction::AnalyzeIfNeutrino(const G4Track* aTrack)
272 {
273  if(fpTrackingManager->GetVerboseLevel() > 3)
274  {
275  G4int evtno = pRunManager->GetCurrentEvent()->GetEventID();
276  std::cout << "Event " << evtno
277  << ": NumiTrackingAction::AnalyzeIfNeutrino() Called."
278  << std::endl;
279  }
280 
281  //if a particle is a neutrino then analyse and store in ntuple
282  G4ParticleDefinition * particleDefinition = aTrack->GetDefinition();
283  if ((particleDefinition == G4NeutrinoE::NeutrinoEDefinition())||
284  (particleDefinition == G4NeutrinoMu::NeutrinoMuDefinition()) ||
285  (particleDefinition == G4NeutrinoTau::NeutrinoTauDefinition()) ||
286  (particleDefinition == G4AntiNeutrinoE::AntiNeutrinoEDefinition()) ||
287  (particleDefinition == G4AntiNeutrinoMu::AntiNeutrinoMuDefinition()) ||
288  (particleDefinition == G4AntiNeutrinoTau::AntiNeutrinoTauDefinition()))
289  {
290 
291  //Following part might be temporary....implemented to better understand..
292  //..the particles through volume. A. Bashyal
293  const G4Event* event = pRunManager->GetCurrentEvent();
294  G4TrajectoryContainer* trajectories = event->GetTrajectoryContainer();
295  // Make a temporary map to make it easier to go up the trajectory stack
296  std::map<int,G4VTrajectory*> trajectoryMap;
297  for (std::size_t i = 0; i < trajectories->size(); ++i) {
298  G4VTrajectory* traj = (*trajectories)[i];
299  trajectoryMap[traj->GetTrackID()] = traj;
300  }
301 
302  std::vector<G4VTrajectory*> nuHistory;
303  G4VTrajectory* neutrino = fpTrackingManager->GimmeTrajectory();
304  nuHistory.push_back(neutrino);
305  int parentId = aTrack->GetParentID();
306  while (parentId != 0) {
307  G4VTrajectory* parentTraj = trajectoryMap[parentId];
308  if (!parentTraj) {
309  G4cerr << "Invalid trajectory object" << G4endl;
310  continue;
311  }
312  nuHistory.push_back(parentTraj);
313  LBNETrajectory* lbneTrajectory = dynamic_cast<LBNETrajectory*>(parentTraj);
314  if (!lbneTrajectory) continue;
315  G4String startVol = lbneTrajectory->GetPreStepVolumeName(0);
316  G4String stopVol = lbneTrajectory->GetPreStepVolumeName(lbneTrajectory->GetPointEntries()-1);
317  parentId = parentTraj->GetParentID();
318  }
320  analysis->FillNeutrinoNtuple(*aTrack,nuHistory);
321  }
322 }
323 
void SetPreLBNETrackInformation(const G4Track *aTrack)
void FillNeutrinoNtuple(const G4Track &track, const std::vector< G4VTrajectory * > &nuHistory)
std::string string
Definition: nybbler.cc:12
virtual void PostUserTrackingAction(const G4Track *)
bool GetUseMarsInput() const
LBNEPrimaryGeneratorAction * NPGA
LBNERunManager * pRunManager
std::ofstream fOutHadronAtVectex
void SetPostLBNETrackInformation(const G4Track *aTrack)
static LBNEAnalysis * getInstance()
T abs(T value)
void SetNImpWt(G4double nimpweight)
virtual G4String GetPreStepVolumeName(G4int i) const
std::string getenv(std::string const &name)
Definition: getenv.cc:15
void OpenHadronAtVertex() const
void ResetFlagParticleGotToHAFront() const
G4double GetNImpWt() const
void SetTgen(G4int tgeneration)
bool GetUseFlukaInput() const
void AnalyzeIfNeutrino(const G4Track *aTrack)
void ResetNumSteps() const
virtual int GetPointEntries() const
virtual void PreUserTrackingAction(const G4Track *)
QTextStream & endl(QTextStream &s)