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

Last change on this file since 807 was 807, checked in by garnier, 16 years ago

update

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