source: trunk/examples/extended/exoticphysics/monopole/src/RunAction.cc @ 1358

Last change on this file since 1358 was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 8.5 KB
Line 
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// $Id: RunAction.cc,v 1.4 2010/06/04 19:03:36 vnivanch Exp $
27// GEANT4 tag $Name: geant4-09-04-beta-01 $
28//
29//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
31
32#include "RunAction.hh"
33#include "DetectorConstruction.hh"
34#include "QGSP.hh"
35#include "PrimaryGeneratorAction.hh"
36#include "RunActionMessenger.hh"
37
38#include "G4Run.hh"
39#include "G4RunManager.hh"
40#include "G4UnitsTable.hh"
41#include "G4ios.hh"
42
43#include "Randomize.hh"
44#include "G4EmCalculator.hh"
45
46#ifdef G4ANALYSIS_USE
47#include "AIDA/AIDA.h"
48#endif
49
50//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
51
52RunAction::RunAction(DetectorConstruction* det, PrimaryGeneratorAction* kin)
53  :detector(det), kinematic(kin), af(0), tree(0)
54{ 
55  verboseLevel = 1;
56  binLength = offsetX = 0.;
57  histo[0] = 0;
58  tree = 0;
59  af   = 0; 
60#ifdef G4ANALYSIS_USE
61  // Creating the analysis factory
62  af = AIDA_createAnalysisFactory();
63  ftype   = "root";
64  fname   = "monopole";
65#endif
66
67  // create commands for interactive definition of the detector 
68  runActionMessenger = new RunActionMessenger(this);
69}
70
71//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72
73RunAction::~RunAction()
74{
75#ifdef G4ANALYSIS_USE
76  delete af; 
77#endif     
78}
79
80//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
81
82void RunAction::bookHisto()
83{
84  G4double length  = detector->GetAbsorSizeX();
85  if(!binLength) { binLength = 5 * mm; }
86  if(binLength > detector->GetMaxStepSize()) { 
87    binLength = detector->GetMaxStepSize();
88  }
89  offsetX   = 0.5 * length;
90 
91#ifdef G4ANALYSIS_USE
92  if(GetVerbose() > 0) { G4cout << "\n----> Histogram Tree opened" << G4endl; }
93
94  G4int nbBins = (G4int)(0.5 + length / binLength);
95
96  // Create the tree factory
97  AIDA::ITreeFactory* tf  = af->createTreeFactory();
98
99  // Create a tree mapped to an hbook file.
100  G4bool readOnly  = false;
101  G4bool createNew = true;
102  //G4String ftype   = "hbook";
103  //G4String fname   = "monopole";
104        G4String fName   = fname;
105  fName += ".";
106  fName += ftype;
107  G4String option  = "--noErrors uncompress";
108  tree = tf->create(fName,ftype, readOnly, createNew, option);
109
110  // Create a histogram factory, whose histograms will be handled by the tree
111  AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree);
112
113  // Create histograms
114  histo[0] = hf->createHistogram1D("1","Edep (MeV/mm) along absorber (mm)", nbBins, 0, length);
115  histo[1] = hf->createHistogram1D("2","DEDX (MeV/mm) of proton", 100, -3., 7.);
116  histo[2] = hf->createHistogram1D("3","DEDX (MeV/mm) of monopole", 100, -3., 7.);
117  histo[3] = hf->createHistogram1D("4","Range(mm) of proton", 100, -3., 7.);
118  histo[4] = hf->createHistogram1D("5","Range(mm) of monopole", 100, -3., 7.);
119
120  delete tf;
121  delete hf;
122#endif
123}
124
125//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
126
127void RunAction::saveHisto()
128{
129#ifdef G4ANALYSIS_USE
130  tree->commit();       // Writing the histograms to the file
131  tree->close();        // and closing the tree (and the file)
132  delete tree;
133  tree = 0;
134  if(GetVerbose() > 0) G4cout << "\n----> Histogram Tree saved" << G4endl; 
135#endif
136}
137
138//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
139
140void RunAction::SetBinSize(G4double size)
141{
142  binLength =  size;
143}
144
145//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
146
147void RunAction::FillHisto(G4int ih, G4double x, G4double weight)
148{
149  if(GetVerbose() > 1) {
150    G4cout << "FillHisto " << ih << "  x=" << x << " weight= " << weight
151           << G4endl;
152  }
153#ifdef G4ANALYSIS_USE
154  if(histo[ih]) histo[ih]->fill(x, weight);
155#endif
156}
157
158//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
159
160void RunAction::BeginOfRunAction(const G4Run* aRun)
161{ 
162  if(GetVerbose() > 0) G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
163 
164  // save Rndm status
165  G4RunManager::GetRunManager()->SetRandomNumberStore(true);
166  CLHEP::HepRandom::showEngineStatus();
167     
168  //initialize projected range, tallies, Ebeam, and book histograms
169  projRange = projRange2 = 0.;
170  kinematic->ResetEbeamCumul();
171  bookHisto();
172}
173
174//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
175
176void RunAction::EndOfRunAction(const G4Run* aRun)
177{
178  G4int NbofEvents = aRun->GetNumberOfEvent();
179  if (NbofEvents == 0) return;
180
181  //run conditions
182  // 
183  G4Material* material = detector->GetAbsorMaterial();
184  G4double density = material->GetDensity();
185  const G4ParticleDefinition* part = 
186    kinematic->GetParticleGun()->GetParticleDefinition();
187  G4String particle = part->GetParticleName();   
188  G4double energy = kinematic->GetParticleGun()->GetParticleEnergy();
189
190  if(GetVerbose() > 0){
191    G4cout << "\n The run consists of " << NbofEvents << " "<< particle << " of "
192           << G4BestUnit(energy,"Energy") << " through " 
193           << G4BestUnit(detector->GetAbsorSizeX(),"Length") << " of "
194           << material->GetName() << " (density: " 
195           << G4BestUnit(density,"Volumic Mass") << ")" << G4endl;
196  };
197         
198  //compute projected range and straggling
199
200  projRange /= NbofEvents; projRange2 /= NbofEvents;
201  G4double rms = projRange2 - projRange*projRange;       
202  if (rms>0.) rms = std::sqrt(rms); else rms = 0.;
203
204  if(GetVerbose() > 0){
205    G4cout.precision(5);       
206    G4cout << "\n projected Range= " << G4BestUnit(projRange, "Length")
207           << "   rms= "             << G4BestUnit(rms, "Length")
208           << G4endl;
209  };
210
211  G4double ekin[100], dedxproton[100], dedxmp[100];
212  G4EmCalculator calc;
213  calc.SetVerbose(0);
214  G4int i;
215  for(i = 0; i < 100; ++i) {
216    ekin[i] = std::pow(10., 0.1*G4double(i)) * keV;
217    dedxproton[i] = calc.ComputeElectronicDEDX(ekin[i], "proton",  material->GetName());
218    dedxmp[i] = calc.ComputeElectronicDEDX(ekin[i], "monopole",  material->GetName());
219  }
220
221  if(GetVerbose() > 0){
222    G4cout << "### Stopping Powers" << G4endl;
223    for(i=0; i<100; i++) {
224      G4cout << " E(MeV)= " << ekin[i] << "  dedxp= " << dedxproton[i]
225             << " dedxmp= " << dedxmp[i]
226             << G4endl;
227    }
228  };
229  G4cout << "### End of stopping power table" << G4endl;
230#ifdef G4ANALYSIS_USE
231  // normalize histogram
232  G4double fac = (mm/MeV) / (NbofEvents * binLength);
233  histo[0]->scale(fac);
234
235        G4String matName = detector->GetAbsorMaterial()->GetName();
236        if(GetVerbose() > 0){
237                G4cout << "Range table for " << matName << G4endl;
238        };
239
240  for(i=0; i<100; ++i) {
241    G4double e = std::log10(ekin[i] / MeV) + 0.05;
242    histo[1]->fill(e, dedxproton[i]);
243    histo[2]->fill(e, dedxmp[i]);
244    histo[3]->fill(e, std::log10(calc.GetRange(ekin[i], "proton", matName) / mm));
245    histo[4]->fill(e, std::log10(calc.GetRange(ekin[i], "monopole", matName) / mm));
246  }
247 
248#endif
249 
250  // save and clean histo
251  saveHisto();
252 
253  // show Rndm status
254  CLHEP::HepRandom::showEngineStatus();
255}
256
257//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Note: See TracBrowser for help on using the repository browser.