ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XrayFluoAnalysisManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file XrayFluoAnalysisManager.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //
28 // Author: Elena Guardincerri (Elena.Guardincerri@ge.infn.it)
29 //
30 // History:
31 // -----------
32 // 20 Jul 2007 A.Mantero, code cleaning and update. Replaced histos with tuple
33 // 11 Jul 2003 A.Mantero, code cleaning / Plotter-XML addiction
34 // Sep 2002 A.Mantero, AIDA3.0 Migration
35 // 06 Dec 2001 A.Pfeiffer updated for singleton
36 // 30 Nov 2001 Guy Barrand : migrate to AIDA-2.2.
37 // 28 Nov 2001 Elena Guardincerri Created
38 //
39 // -------------------------------------------------------------------
40 #include "g4root.hh"
41 
42 #include "G4VProcess.hh"
44 #include "G4Step.hh"
46 #include "G4VPhysicalVolume.hh"
47 #include "G4Gamma.hh"
48 #include "G4Electron.hh"
49 #include "G4Proton.hh"
50 #include "G4SystemOfUnits.hh"
51 
52 
54 
55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
56 
57 namespace {
58  //Mutex to acquire access to singleton instance check/creation
59  G4Mutex instanceMutex = G4MUTEX_INITIALIZER;
60  //Mutex to acquire accss to histograms creation/access
61  //It is also used to control all operations related to histos
62  //File writing and check analysis
63  G4Mutex dataManipulationMutex = G4MUTEX_INITIALIZER;
64 }
65 
66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
67 
69  :outputFileName("xrayfluo"), phaseSpaceFlag(false), physicFlag (false),
70  gunParticleEnergies(0), gunParticleTypes(0)
71 {
72  dataLoaded = false;
74 
75  //creating the messenger
77 
78  //Instantiate the analysis manager
80 
81  G4cout << "XrayFluoAnalysisManager created" << G4endl;
82 }
83 
84 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
85 
87 {
90  if ( gunParticleTypes ) delete gunParticleTypes;
91  gunParticleTypes = 0;
92 
93  delete instance;
94  instance = 0;
95 
97 }
98 
99 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
100 
102 
103 {
104  G4AutoLock l(&instanceMutex);
105  if (instance == 0) {instance = new XrayFluoAnalysisManager;}
106  return instance;
107 }
108 
109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
110 
112 {
113  G4AutoLock l(&dataManipulationMutex);
114  // Get analysis manager
116  // Open an output file
117  man->OpenFile(outputFileName);
118  man->SetVerboseLevel(1);
119  man->SetFirstHistoId(1);
120  man->SetFirstNtupleId(1);
121 
122  G4cout << "Open output file: " << outputFileName << G4endl;
123 
124  if (phaseSpaceFlag)
125  {
126  // Book output Tuple (ID = 1)
127  man->CreateNtuple("101","OutputNTuple");
128  man->CreateNtupleIColumn("particle");
129  man->CreateNtupleDColumn("energies");
130  man->CreateNtupleDColumn("momentumTheta");
131  man->CreateNtupleDColumn("momentumPhi");
132  man->CreateNtupleIColumn("processes");
133  man->CreateNtupleIColumn("material");
134  man->CreateNtupleIColumn("origin");
135  man->CreateNtupleDColumn("depth");
136  man->FinishNtuple();
137  G4cout << "Created ntuple for phase space" << G4endl;
138  }
139  else {
140  // Book histograms
141  man->CreateH1("h1","Energy Deposit", 500,0.,10.); //20eV def.
142  man->CreateH1("h2","Gamma born in the sample", 100,0.,10.);
143  man->CreateH1("h3","Electrons born in the sample", 100,0.,10.);
144  man->CreateH1("h4","Gammas leaving the sample", 300,0.,10.);
145  man->CreateH1("h5","Electrons leaving the sample ",200000 ,0.,10.0); // .05 eV def.
146  man->CreateH1("h6","Gammas reaching the detector", 100,0.,10.);
147  man->CreateH1("h7","Spectrum of the incident particles", 100,0.,10.);
148  man->CreateH1("h8","Protons reaching the detector", 100,0.,10.);
149  man->CreateH1("h9","Protons leaving the sample", 100,0.,10.);
150  G4cout << "Created histos" << G4endl;
151  }
152 }
153 
154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
155 
157 {
158  G4AutoLock l(&dataManipulationMutex);
159 
160  //Do not allow more than one thread to trigger the file reading
161  if (dataLoaded)
162  return;
163 
164  // Get analysis reader manager
165  G4AnalysisReader* analysisReader = G4AnalysisReader::Instance();
166  analysisReader->SetVerboseLevel(1);
167 
168  //This is for testing purposes
169  G4int ntupleId = analysisReader->GetNtuple("101",fileName);
170  G4cout << "Got ntupleId: " << ntupleId << G4endl;
171 
172  gunParticleEnergies = new std::vector<G4double>;
173  gunParticleTypes = new std::vector<G4String>;
174 
175  G4int particleID, processID;
177  analysisReader->SetNtupleIColumn("processes", processID);
178  analysisReader->SetNtupleDColumn("energies", energy);
179  analysisReader->SetNtupleIColumn("particles", particleID);
180 
181  while (analysisReader->GetNtupleRow() )
182  {
183  if (raileighFlag ^ (!raileighFlag && (processID == 1 ||
184  processID == 2)) )
185  {
186  gunParticleEnergies->push_back(energy*MeV);
187  if (particleID == 1)
188  gunParticleTypes->push_back("gamma");
189  else if (particleID == 0)
190  gunParticleTypes->push_back("e-");
191  }
192  }
193 
194  G4cout << "Maximum mumber of events: "<< gunParticleEnergies->size() <<G4endl;
195 
196  dataLoaded= true;
197 }
198 
199 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
200 
201 const std::pair<G4double,G4String> XrayFluoAnalysisManager::GetEmittedParticleEnergyAndType()
202 {
203  G4AutoLock l(&dataManipulationMutex);
204  std::pair<G4double,G4String> result;
205 
207  {
210  result.first = energy;
211  result.second = name;
212  }
213 
215  return result;
216 }
217 
218 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
219 
220 
222 {
223  G4AutoLock l(&dataManipulationMutex);
224  G4cout << "Going to save histograms" << G4endl;
225  // Save histograms
227  man->Write();
228  man->CloseFile();
229 }
230 
231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
232 
233 
235 {
236  physicFlag = val;
237 }
238 
239 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
240 
242 {
243  G4AutoLock l(&dataManipulationMutex);
245 
246  if (phaseSpaceFlag){
247  G4ParticleDefinition* particleType= 0;
248  G4String parentProcess="";
249  G4ThreeVector momentum(0.,0.,0.);
250  G4double particleEnergy=0;
251  G4String sampleMaterial="";
252  G4double particleDepth=0;
253  G4int isBornInTheSample=0;
255 
256  // Select volume from which the step starts
257  if(aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName()=="Sample"){
258  G4ThreeVector creationPos = aStep->GetTrack()->GetVertexPosition();
259  // qua serve ancora una selezione: deve essere interno al sample
260  //codice "a mano" per controllare il volume di orgine della traccia
261 
262  G4VPhysicalVolume* creationPosVolume = detector->GetGeometryNavigator()->LocateGlobalPointAndSetup(creationPos);
263 
264  // if physicflag is on, I record all the data of all the particle born in the sample.
265  // If it is off (but phase space production is on) I record data
266  // only for particles creted inside the sample and exiting it.
267  if (physicFlag ^ (!physicFlag &&
269  ))
270  {
271  // extracting information needed
272  particleType = aStep->GetTrack()->GetDynamicParticle()->GetDefinition();
273  momentum = aStep->GetTrack()->GetDynamicParticle()->GetMomentum();
274  particleEnergy = aStep->GetPreStepPoint()->GetKineticEnergy();
275  if (creationPosVolume->GetName() == "Sample" ) {
276  isBornInTheSample = 1;
277  particleDepth = creationPosVolume->GetLogicalVolume()->GetSolid()
278  ->DistanceToOut(creationPos, G4ThreeVector(0,0,-1));
279  }
280  else {
281  particleDepth = -1;
282  }
283  // metodo per ottenere la profondita' "a mano"
284  // particleDepth = std::abs(creationPos.z() - detector->GetSampleIlluminatedFaceCoord());
285 
286  G4int parent=0;
287  if(aStep->GetTrack()->GetCreatorProcess())
288  {
289  parentProcess = aStep->GetTrack()->GetCreatorProcess()->GetProcessName();
290  parent = 5;
291  // hack for HBK
292  if (parentProcess == "") parent = 0;
293  if (parentProcess == "ioni") parent = 1;
294  if (parentProcess == "LowEnPhotoElec") parent = 2;
295  if (parentProcess == "Transportation") parent = 3;// should never happen
296  if (parentProcess == "initStep") parent = 4;// should never happen
297  }
298  else {
299  parentProcess = "Not Known -- (primary generator + Rayleigh)";
300  parent = 5;
301  }
302  G4int sampleMat=0;
303  if(aStep->GetTrack()){
304  sampleMaterial = aStep->GetTrack()->GetMaterial()->GetName();
305  if (sampleMaterial == ("Dolorite" || "Anorthosite" || "Mars1" || "IceBasalt" || "HPGe")) sampleMat=1;
306  }
307 
308  G4int part = -1 ;
309  if (particleType == G4Gamma::Definition()) part =1;
310  if (particleType == G4Electron::Definition()) part = 0;
311  if (particleType == G4Proton::Definition()) part = 2;
312 
313  //Fill ntuple
314  man->FillNtupleIColumn(1,0, part);
315  man->FillNtupleDColumn(1,1,particleEnergy);
316  man->FillNtupleDColumn(1,2,momentum.theta());
317  man->FillNtupleDColumn(1,3,momentum.phi());
318  man->FillNtupleIColumn(1,4,parent);
319  man->FillNtupleIColumn(1,5,sampleMat);
320  man->FillNtupleIColumn(1,6,isBornInTheSample);
321  man->FillNtupleDColumn(1,7,particleDepth);
322  man->AddNtupleRow(1);
323 
324  }
325  }
326  }
327 
328  // Normal behaviour, without creation of phase space
329  else
330  {
331 
332  // Filling the histograms with data, passing thru stepping.
333 
334  // Select volume from wich the step starts
335  if(aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName()=="Sample"){
336  // Select volume from wich the step ends
337  if(aStep->GetTrack()->GetNextVolume()->GetName() == "World" ) {
338  // Select the particle type
339  if ((aStep->GetTrack()->GetDynamicParticle()
341 
342  {
343  G4double gammaLeavingSample =
344  aStep->GetPreStepPoint()->GetKineticEnergy();
345  man->FillH1(4,gammaLeavingSample/keV);
346 
347  }
348  else if ((aStep->GetTrack()->GetDynamicParticle()
350  {
351  G4double eleLeavingSample =
352  aStep->GetPreStepPoint()->GetKineticEnergy();
353  man->FillH1(5,eleLeavingSample/keV);
354  }
355  else if ((aStep->GetTrack()->GetDynamicParticle()
357  {
358  G4double
359  protonsLeavSam = aStep->GetPreStepPoint()->GetKineticEnergy();
360  man->FillH1(9,protonsLeavSam/keV);
361  }
362  }
363  }
364 
365  if((aStep->GetTrack()->GetDynamicParticle()
367  {
368  if(aStep->GetTrack()->GetCurrentStepNumber() == 1)
369  {
370  if(aStep->GetTrack()->GetParentID() != 0)
371  {
372  if(aStep->GetTrack()->GetVolume()->GetName() == "Sample")
373  {
374  G4double gammaBornInSample =
375  aStep->GetPreStepPoint()->GetKineticEnergy();
376  man->FillH1(2,gammaBornInSample/keV);
377  }
378  }
379  }
380  }
381  else if ((aStep->GetTrack()->GetDynamicParticle()
383  {
384  if(aStep->GetTrack()->GetCurrentStepNumber() == 1)
385  {
386  if(aStep->GetTrack()->GetParentID() != 0)
387  {
388  if(aStep->GetTrack()->GetVolume()->GetName() == "Sample")
389  {
390  G4double eleBornInSample =
391  aStep->GetPreStepPoint()->GetKineticEnergy();
392  man->FillH1(3,eleBornInSample/keV);
393  }
394  }
395  }
396  }
397 
398 
399  if(aStep->GetTrack()->GetNextVolume())
400  {
401 
402 
403  if(aStep->GetTrack()->GetNextVolume()->GetName() == "HPGeDetector")
404 
405  {
406  if ((aStep->GetTrack()->GetDynamicParticle()
408  {
409 
410  G4double gammaAtTheDetPre =
411  aStep->GetPreStepPoint()->GetKineticEnergy();
412  man->FillH1(6,gammaAtTheDetPre/keV);
413  }
414  else if ((aStep->GetTrack()->GetDynamicParticle()
415  ->GetDefinition() ) == G4Proton::Definition() )
416  {
417  G4double protonsAtTheDetPre =
418  aStep->GetPreStepPoint()->GetKineticEnergy();
419  man->FillH1(8,protonsAtTheDetPre);
420  }
421  }
422  }
423  }
424 }
425 
426 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
427 
429 {
430  G4AutoLock l(&dataManipulationMutex);
431  // Filling of Energy Deposition, called by XrayFluoEventAction
433 
434  if (!phaseSpaceFlag)
435  man->FillH1(1,energyDep/keV);
436 
437 }
438 
439 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
440 
442 {
443  G4AutoLock l(&dataManipulationMutex);
444  // Filling of energy spectrum histogram of the primary generator
446  if (!phaseSpaceFlag)
447  man->FillH1(7,energy/keV);
448 
449 }
450 
451 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
452 
454 {
455 
456  outputFileName = newName;
457 }
458 
459