Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XrayTelAnalysis.cc
Go to the documentation of this file.
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 // $Id: XrayTelAnalysis.cc 68710 2013-04-05 09:04:21Z gcosmo $
28 //
29 // Author: A. Pfeiffer (Andreas.Pfeiffer@cern.ch)
30 // (copied from his UserAnalyser class)
31 //
32 // History:
33 // -----------
34 // 19 Mar 2013 LP Migrated to G4tools
35 // 8 Nov 2002 GS Migration to AIDA 3
36 // 7 Nov 2001 MGP Implementation
37 //
38 // -------------------------------------------------------------------
39 
40 #include <fstream>
41 #include <iomanip>
42 
43 #include "XrayTelAnalysis.hh"
44 #include "globals.hh"
45 #include "G4SystemOfUnits.hh"
46 #include "G4Track.hh"
47 #include "G4ios.hh"
48 #include "G4SteppingManager.hh"
49 #include "G4ThreeVector.hh"
50 
51 XrayTelAnalysis* XrayTelAnalysis::instance = 0;
52 
53 XrayTelAnalysis::XrayTelAnalysis() :
54  asciiFile(0)
55 {
56  histFileName = "xraytel";
57 
58 
59  asciiFileName="xraytel.out";
60  asciiFile = new std::ofstream(asciiFileName);
61 
62  if(asciiFile->is_open())
63  (*asciiFile) << "Energy (keV) x (mm) y (mm) z (mm)" << G4endl << G4endl;
64 }
65 
67 {
68  if (asciiFile)
69  delete asciiFile;
70 }
71 
72 
74 {
75  if (instance == 0) instance = new XrayTelAnalysis;
76  return instance;
77 }
78 
79 
81 {
82  // Get/create analysis manager
83  G4AnalysisManager* man = G4AnalysisManager::Instance();
84 
85  // Open an output file
86  G4cout << "Opening output file " << histFileName << " ... ";
87  man->OpenFile(histFileName);
88  man->SetFirstHistoId(1);
89  G4cout << " done" << G4endl;
90 
91  // Book 1D histograms
92  man->CreateH1("1","Energy, all /keV", 100,0.,100.);
93  man->CreateH1("2","Energy, entering detector /keV", 500,0.,500.);
94 
95  // Book 2D histograms (notice: the numbering is independent)
96  man->CreateH2("1","y-z, all /mm", 100,-500.,500.,100,-500.,500.);
97  man->CreateH2("2","y-z, entering detector /mm", 200,-50.,50.,200,-50.,50.);
98 
99  // Book ntuples
100  man->CreateNtuple("10", "Track ntuple");
101  man->CreateNtupleDColumn("energy");
102  man->CreateNtupleDColumn("x");
103  man->CreateNtupleDColumn("y");
104  man->CreateNtupleDColumn("z");
105  man->CreateNtupleDColumn("dirx");
106  man->CreateNtupleDColumn("diry");
107  man->CreateNtupleDColumn("dirz");
108  man->FinishNtuple();
109 }
110 
111 
113 {
114  asciiFile->close();
115 
116  // Save histograms
117  G4AnalysisManager* man = G4AnalysisManager::Instance();
118  man->Write();
119  man->CloseFile();
120  // Complete clean-up
121  delete G4AnalysisManager::Instance();
122 }
123 
124 void XrayTelAnalysis::analyseStepping(const G4Track& track, G4bool entering)
125 {
126  eKin = track.GetKineticEnergy()/keV;
127  G4ThreeVector pos = track.GetPosition()/mm;
128  y = pos.y();
129  z = pos.z();
130  G4ThreeVector dir = track.GetMomentumDirection();
131  dirX = dir.x();
132  dirY = dir.y();
133  dirZ = dir.z();
134 
135  // Fill histograms
136  G4AnalysisManager* man = G4AnalysisManager::Instance();
137  man->FillH1(1,eKin);
138  man->FillH2(1,y,z);
139 
140  // Fill histograms and ntuple, tracks entering the detector
141  if (entering) {
142  // Fill and plot histograms
143  man->FillH1(2,eKin);
144  man->FillH2(2,y,z);
145 
146  man->FillNtupleDColumn(0,eKin);
147  man->FillNtupleDColumn(1,x);
148  man->FillNtupleDColumn(2,y);
149  man->FillNtupleDColumn(3,z);
150  man->FillNtupleDColumn(4,dirX);
151  man->FillNtupleDColumn(5,dirY);
152  man->FillNtupleDColumn(6,dirZ);
153  man->AddNtupleRow();
154  }
155 
156 
157  // Write to file
158  if (entering) {
159  if(asciiFile->is_open()) {
160  (*asciiFile) << std::setiosflags(std::ios::fixed)
161  << std::setprecision(3)
162  << std::setiosflags(std::ios::right)
163  << std::setw(10);
164  (*asciiFile) << eKin;
165  (*asciiFile) << std::setiosflags(std::ios::fixed)
166  << std::setprecision(3)
167  << std::setiosflags(std::ios::right)
168  << std::setw(10);
169  (*asciiFile) << x;
170  (*asciiFile) << std::setiosflags(std::ios::fixed)
171  << std::setprecision(3)
172  << std::setiosflags(std::ios::right)
173  << std::setw(10);
174  (*asciiFile) << y;
175  (*asciiFile) << std::setiosflags(std::ios::fixed)
176  << std::setprecision(3)
177  << std::setiosflags(std::ios::right)
178  << std::setw(10);
179  (*asciiFile) << z
180  << G4endl;
181  }
182  }
183 }
184 
G4bool SetFirstHistoId(G4int firstId)
G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
double x() const
const G4ThreeVector & GetPosition() const
G4int CreateNtuple(const G4String &name, const G4String &title)
double z() const
G4double GetKineticEnergy() const
G4GLOB_DLL std::ostream G4cout
G4bool FillNtupleDColumn(G4int id, G4double value)
bool G4bool
Definition: G4Types.hh:79
const G4ThreeVector & GetMomentumDirection() const
G4bool FillH2(G4int id, G4double xvalue, G4double yvalue, G4double weight=1.0)
G4bool FillH1(G4int id, G4double value, G4double weight=1.0)
double y() const
#define G4endl
Definition: G4ios.hh:61
void analyseStepping(const G4Track &track, G4bool entering)
G4int CreateNtupleDColumn(const G4String &name)
static XrayTelAnalysis * getInstance()
G4int CreateH2(const G4String &name, const G4String &title, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none", const G4String &xbinScheme="linear", const G4String &ybinScheme="linear")