source: trunk/examples/extended/electromagnetic/TestEm2/src/RunAction.cc @ 812

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

update

File size: 14.9 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.21 2006/06/29 16:50:47 gunter Exp $
27// GEANT4 tag $Name: geant4-09-01-patch-02 $
28//
29//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
31
32#include "RunAction.hh"
33
34#include "DetectorConstruction.hh"
35#include "PrimaryGeneratorAction.hh"
36#include "RunActionMessenger.hh"
37#include "EmAcceptance.hh"
38
39#include "G4Run.hh"
40#include "G4RunManager.hh"
41#include "G4UnitsTable.hh"
42
43#include <iomanip>
44
45#include "Randomize.hh"
46
47#ifdef G4ANALYSIS_USE
48#include "AIDA/AIDA.h"
49#endif
50
51//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
52
53RunAction::RunAction(DetectorConstruction* det, PrimaryGeneratorAction* kin)
54:Det(det),Kin(kin),af(0),tree(0)
55{
56  runMessenger = new RunActionMessenger(this);
57 
58  nLbin = nRbin = MaxBin;
59   
60  dEdL.resize(nLbin, 0.0);
61  sumELongit.resize(nLbin, 0.0);
62  sumELongitCumul.resize(nLbin, 0.0);
63  sumE2Longit.resize(nLbin, 0.0);
64  sumE2LongitCumul.resize(nLbin, 0.0);
65
66  dEdR.resize(nRbin, 0.0);
67  sumERadial.resize(nRbin, 0.0);
68  sumERadialCumul.resize(nRbin, 0.0);
69  sumE2Radial.resize(nRbin, 0.0);
70  sumE2RadialCumul.resize(nRbin, 0.0);
71   
72  edeptrue = 1.;
73  rmstrue  = 1.;
74  limittrue = DBL_MAX;
75 
76  verbose = 0;
77 
78#ifdef G4ANALYSIS_USE
79  // Creating the analysis factory
80  af = AIDA_createAnalysisFactory();
81  if(!af) {
82    G4cout << " RunAction::RunAction() :" 
83           << " problem creating the AIDA analysis factory."
84           << G4endl;
85  }         
86#endif
87   
88  histoName[0] = "testem2";
89  histoType    = "hbook"; 
90}
91
92//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
93
94RunAction::~RunAction()
95{
96  delete runMessenger;
97 
98#ifdef G4ANALYSIS_USE
99  delete af;
100#endif 
101}
102
103//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
104
105void RunAction::bookHisto()
106{
107#ifdef G4ANALYSIS_USE
108  if(!af) return;
109 
110  // Creating a tree mapped to an hbook file.
111  histoName[1] = histoName[0] + "." + histoType; 
112  G4bool readOnly  = false;
113  G4bool createNew = true;
114  G4String options = "--noErrors uncompress";
115  AIDA::ITreeFactory* tf  = af->createTreeFactory(); 
116  tree = tf->create(histoName[1], histoType, readOnly, createNew, options);
117  delete tf;
118  if(!tree) {
119    G4cout << "RunAction::bookHisto() :" 
120           << " problem creating the AIDA tree with "
121           << " storeName = " << histoName[1]
122           << " readOnly = "  << readOnly
123           << " createNew = " << createNew
124           << G4endl;
125    return;
126  }
127  // Creating a histogram factory, whose histograms will be handled by the tree
128  AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree);
129 
130  G4double Ekin = Kin->GetParticleGun()->GetParticleEnergy();
131  G4double dLradl = Det->GetdLradl();
132  G4double dRradl = Det->GetdRradl();
133
134  histo[0] = hf->createHistogram1D( "1","total energy deposit(percent of Einc)",
135                                    110,0.,110.);
136
137  histo[1] = hf->createHistogram1D( "2","total charged tracklength (radl)",
138                                    110,0.,110.*Ekin/GeV);
139
140  histo[2] = hf->createHistogram1D( "3","total neutral tracklength (radl)",
141                                    110,0.,1100.*Ekin/GeV);
142
143  histo[3] = hf->createHistogram1D( "4","longit energy profile (% of E inc)",
144                                    nLbin,0.,nLbin*dLradl);
145
146  G4double Zmin=0.5*dLradl, Zmax=Zmin+nLbin*dLradl;
147  histo[4] = hf->createHistogram1D( "5","cumul longit energy dep (% of E inc)",
148                                    nLbin,Zmin,Zmax);
149
150  histo[5] = hf->createHistogram1D( "6","rms on cumul longit Edep (% of E inc)",
151                                    nLbin,Zmin,Zmax);
152
153  histo[6] = hf->createHistogram1D("7","radial energy profile (% of E inc)",
154                                    nRbin,0.,nRbin*dRradl);
155
156  G4double Rmin=0.5*dRradl, Rmax=Rmin+nRbin*dRradl;
157  histo[7]= hf->createHistogram1D("8","cumul radial energy dep (% of E inc)",
158                                    nRbin,Rmin,Rmax);
159
160  histo[8]= hf->createHistogram1D("9","rms on cumul radial Edep (% of E inc)",
161                                    nRbin,Rmin,Rmax);
162                                   
163 delete hf;
164 G4cout << "\n----> Histogram Tree is opened in " << histoName[1] << G4endl;
165#endif
166}
167
168//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
169
170void RunAction::cleanHisto()
171{
172#ifdef G4ANALYSIS_USE
173  if(tree) {
174    tree->commit();       // Writing the histograms to the file
175    tree->close();        // and closing the tree (and the file)
176    G4cout << "\n----> Histogram Tree is saved in " << histoName[1] << G4endl; 
177   
178    delete tree;
179    tree = 0;
180  }
181#endif
182}
183
184//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
185
186void RunAction::BeginOfRunAction(const G4Run* aRun)
187{
188  G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
189
190  // save Rndm status
191  G4RunManager::GetRunManager()->SetRandomNumberStore(true);
192  CLHEP::HepRandom::showEngineStatus();
193
194  nLbin = Det->GetnLtot();
195  nRbin = Det->GetnRtot();
196
197  //initialize arrays of cumulative energy deposition
198  //
199  for (G4int i=0; i<nLbin; i++) 
200     sumELongit[i]=sumE2Longit[i]=sumELongitCumul[i]=sumE2LongitCumul[i]=0.;
201 
202  for (G4int j=0; j<nRbin; j++)
203     sumERadial[j]=sumE2Radial[j]=sumERadialCumul[j]=sumE2RadialCumul[j]=0.;
204
205  //initialize track length
206  sumChargTrLength=sum2ChargTrLength=sumNeutrTrLength=sum2NeutrTrLength=0.;
207
208  //histograms
209  //
210  if (aRun->GetRunID() == 0) bookHisto();
211}
212
213//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
214
215void RunAction::fillPerEvent()
216{
217  //accumulate statistic
218  //
219  G4double dLCumul = 0.;
220  for (G4int i=0; i<nLbin; i++)
221     {
222      sumELongit[i]  += dEdL[i];
223      sumE2Longit[i] += dEdL[i]*dEdL[i];
224      dLCumul        += dEdL[i];
225      sumELongitCumul[i]  += dLCumul;
226      sumE2LongitCumul[i] += dLCumul*dLCumul;
227     }
228
229  G4double dRCumul = 0.;
230  for (G4int j=0; j<nRbin; j++)
231     {
232      sumERadial[j]  += dEdR[j];
233      sumE2Radial[j] += dEdR[j]*dEdR[j];
234      dRCumul        += dEdR[j];
235      sumERadialCumul[j]  += dRCumul;
236      sumE2RadialCumul[j] += dRCumul*dRCumul;
237     }
238
239  sumChargTrLength  += ChargTrLength;
240  sum2ChargTrLength += ChargTrLength*ChargTrLength;
241  sumNeutrTrLength  += NeutrTrLength;
242  sum2NeutrTrLength += NeutrTrLength*NeutrTrLength;
243
244#ifdef G4ANALYSIS_USE
245  //fill histograms
246  //
247  if(tree) {
248    G4double Ekin=Kin->GetParticleGun()->GetParticleEnergy();
249    G4double mass=Kin->GetParticleGun()->GetParticleDefinition()->GetPDGMass();
250    G4double radl=Det->GetMaterial()->GetRadlen();
251
252    histo[0]->fill(100.*dLCumul/(Ekin+mass));
253    histo[1]->fill(ChargTrLength/radl);
254    histo[2]->fill(NeutrTrLength/radl);
255  }
256#endif
257}
258
259//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
260
261void RunAction::EndOfRunAction(const G4Run* aRun)
262{
263  //compute and print statistic
264  //
265  G4int NbOfEvents = aRun->GetNumberOfEvent();
266  G4double kinEnergy = Kin->GetParticleGun()->GetParticleEnergy();
267  assert(NbOfEvents*kinEnergy > 0);
268
269  G4double mass=Kin->GetParticleGun()->GetParticleDefinition()->GetPDGMass();
270  G4double norme = 100./(NbOfEvents*(kinEnergy+mass));
271
272  //longitudinal
273  //
274  G4double dLradl = Det->GetdLradl();
275
276  MyVector MeanELongit(nLbin),      rmsELongit(nLbin);
277  MyVector MeanELongitCumul(nLbin), rmsELongitCumul(nLbin);
278
279  G4int i;
280  for (i=0; i<nLbin; i++)
281   {
282    MeanELongit[i] = norme*sumELongit[i];
283     rmsELongit[i] = norme*std::sqrt(std::fabs(NbOfEvents*sumE2Longit[i]
284                                - sumELongit[i]*sumELongit[i]));
285
286    MeanELongitCumul[i] = norme*sumELongitCumul[i];
287     rmsELongitCumul[i] = norme*std::sqrt(std::fabs(NbOfEvents*sumE2LongitCumul[i]
288                                    - sumELongitCumul[i]*sumELongitCumul[i]));
289
290
291#ifdef G4ANALYSIS_USE
292    if(tree) {
293      G4double bin = i*dLradl;
294      histo[3]->fill(bin,MeanELongit[i]/dLradl);
295      bin = (i+1)*dLradl;
296      histo[4]->fill(bin,MeanELongitCumul[i]);
297      histo[5]->fill(bin, rmsELongitCumul[i]);
298    }
299#endif
300   }
301
302  //radial
303  //
304  G4double dRradl = Det->GetdRradl();
305
306  MyVector MeanERadial(nRbin),      rmsERadial(nRbin);
307  MyVector MeanERadialCumul(nRbin), rmsERadialCumul(nRbin);
308
309  for (i=0; i<nRbin; i++)
310   {
311    MeanERadial[i] = norme*sumERadial[i];
312     rmsERadial[i] = norme*std::sqrt(std::fabs(NbOfEvents*sumE2Radial[i]
313                                - sumERadial[i]*sumERadial[i]));
314
315    MeanERadialCumul[i] = norme*sumERadialCumul[i];
316     rmsERadialCumul[i] = norme*std::sqrt(std::fabs(NbOfEvents*sumE2RadialCumul[i]
317                                    - sumERadialCumul[i]*sumERadialCumul[i]));
318
319#ifdef G4ANALYSIS_USE
320    if(tree) {
321      G4double bin = i*dRradl;
322      histo[6]->fill(bin,MeanERadial[i]/dRradl);
323      bin = (i+1)*dRradl;
324      histo[7]->fill(bin,MeanERadialCumul[i]);
325      histo[8]->fill(bin, rmsERadialCumul[i]);
326    }
327#endif
328   }
329
330  //find Moliere confinement
331  //
332  const G4double EMoliere = 90.;
333  G4double iMoliere = 0.;
334  if ((MeanERadialCumul[0]       <= EMoliere) &&
335      (MeanERadialCumul[nRbin-1] >= EMoliere)) {
336    G4int imin = 0;
337    while( (imin < nRbin-1) && (MeanERadialCumul[imin] < EMoliere) ) imin++;
338    G4double ratio = (EMoliere - MeanERadialCumul[imin]) /
339                     (MeanERadialCumul[imin+1] - MeanERadialCumul[imin]);
340    iMoliere = 1. + imin + ratio;
341  }                 
342     
343  //track length
344  //
345  norme = 1./(NbOfEvents*(Det->GetMaterial()->GetRadlen()));
346  G4double MeanChargTrLength = norme*sumChargTrLength;
347  G4double  rmsChargTrLength = 
348            norme*std::sqrt(std::fabs(NbOfEvents*sum2ChargTrLength
349                                         - sumChargTrLength*sumChargTrLength));
350
351  G4double MeanNeutrTrLength = norme*sumNeutrTrLength;
352  G4double  rmsNeutrTrLength = 
353            norme*std::sqrt(std::fabs(NbOfEvents*sum2NeutrTrLength
354                                         - sumNeutrTrLength*sumNeutrTrLength));
355
356  //print
357  //
358
359  std::ios::fmtflags mode = G4cout.flags();
360  G4cout.setf(std::ios::fixed,std::ios::floatfield);
361  G4int  prec = G4cout.precision(2);
362 
363  if (verbose) {
364
365    G4cout << "                 LATERAL PROFILE                   "
366           << "      CUMULATIVE LATERAL PROFILE" << G4endl << G4endl;
367
368    G4cout << "        bin   " << "           Mean         rms         "
369           << "        bin "   << "           Mean      rms \n" << G4endl;
370
371    for (i=0; i<nLbin; i++)
372     {
373       G4double inf=i*dLradl, sup=inf+dLradl;
374
375       G4cout << std::setw(8) << inf << "->"
376              << std::setw(5) << sup << " radl: "
377              << std::setw(7) << MeanELongit[i] << "%  "
378              << std::setw(9) << rmsELongit[i] << "%       "
379              << "      0->" << std::setw(5) << sup << " radl: "
380              << std::setw(7) << MeanELongitCumul[i] << "%  "
381              << std::setw(7) << rmsELongitCumul[i] << "% "
382              <<G4endl;
383     }
384
385    G4cout << G4endl << G4endl << G4endl;
386
387    G4cout << "                  RADIAL PROFILE                   "
388           << "      CUMULATIVE  RADIAL PROFILE" << G4endl << G4endl;
389
390    G4cout << "        bin   " << "           Mean         rms         "
391           << "        bin "   << "           Mean      rms \n" << G4endl;
392
393    for (i=0; i<nRbin; i++)
394     {
395       G4double inf=i*dRradl, sup=inf+dRradl;
396
397       G4cout << std::setw(8) << inf << "->"
398              << std::setw(5) << sup << " radl: "
399              << std::setw(7) << MeanERadial[i] << "%  "
400              << std::setw(9) << rmsERadial[i] << "%       "
401              << "      0->" << std::setw(5) << sup << " radl: "
402              << std::setw(7) << MeanERadialCumul[i] << "%  "
403              << std::setw(7) << rmsERadialCumul[i] << "% "
404              <<G4endl;
405     }
406  }
407 
408  G4cout << "\n SUMMARY \n" << G4endl;
409  G4cout << " energy deposit : "
410         << std::setw(7)  << MeanELongitCumul[nLbin-1] << " % E0 +- "
411         << std::setw(7)  <<  rmsELongitCumul[nLbin-1] << " % E0" << G4endl;
412  G4cout << " charged traklen: "
413         << std::setw(7)  << MeanChargTrLength << " radl +- "
414         << std::setw(7)  <<  rmsChargTrLength << " radl" << G4endl;
415  G4cout << " neutral traklen: "
416         << std::setw(7)  << MeanNeutrTrLength << " radl +- "
417         << std::setw(7)  <<  rmsNeutrTrLength << " radl" << G4endl;
418         
419  if (iMoliere > 0. ) {
420    G4double RMoliere1 = iMoliere*Det->GetdRradl();
421    G4double RMoliere2 = iMoliere*Det->GetdRlength();   
422    G4cout << "\n " << EMoliere << " % confinement: radius = "
423           << RMoliere1 << " radl  ("
424           << G4BestUnit( RMoliere2, "Length") << ")" << G4endl;
425  }       
426
427  G4cout.setf(mode,std::ios::floatfield);
428  G4cout.precision(prec);
429
430  // save histos and close AnalysisFactory
431  cleanHisto();
432 
433  // show Rndm status
434  CLHEP::HepRandom::showEngineStatus();
435
436  // Acceptance
437  if(limittrue < DBL_MAX) {
438    EmAcceptance acc;
439    acc.BeginOfAcceptance("Total Energy in Absorber",NbOfEvents);
440    G4double e = MeanELongitCumul[nLbin-1]/100.;
441    G4double r = rmsELongitCumul[nLbin-1]/100.;
442    acc.EmAcceptanceGauss("Edep",NbOfEvents,e,edeptrue,rmstrue,limittrue);
443    acc.EmAcceptanceGauss("Erms",NbOfEvents,r,rmstrue,rmstrue,2.0*limittrue);
444    acc.EndOfAcceptance();
445  }
446}
447
448//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
449
450void RunAction::SetEdepAndRMS(G4ThreeVector Value)
451{
452  edeptrue = Value(0);
453  rmstrue  = Value(1);
454  limittrue= Value(2);
455}
456
457//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Note: See TracBrowser for help on using the repository browser.