Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 /// \file analysis/AnaEx02/src/HistoManager.cc 26 /// \file analysis/AnaEx02/src/HistoManager.cc 27 /// \brief Implementation of the HistoManager 27 /// \brief Implementation of the HistoManager class 28 // 28 // 29 // << 29 // $Id: HistoManager.cc 74272 2013-10-02 14:48:50Z gcosmo $ >> 30 // GEANT4 tag $Name: geant4-09-04 $ >> 31 // 30 //....oooOO0OOooo........oooOO0OOooo........oo 32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 31 //....oooOO0OOooo........oooOO0OOooo........oo 33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 34 33 #include "HistoManager.hh" << 34 << 35 #include "G4UnitsTable.hh" << 36 << 37 #include <CLHEP/Units/SystemOfUnits.h> << 38 #include <TFile.h> << 39 #include <TH1D.h> 35 #include <TH1D.h> >> 36 #include <TFile.h> 40 #include <TTree.h> 37 #include <TTree.h> >> 38 #include <CLHEP/Units/SystemOfUnits.h> >> 39 >> 40 #include "HistoManager.hh" >> 41 #include "G4UnitsTable.hh" 41 42 42 //....oooOO0OOooo........oooOO0OOooo........oo 43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 43 44 44 HistoManager::~HistoManager() << 45 HistoManager::HistoManager() 45 { << 46 :fRootFile(0), 46 delete fRootFile; << 47 fNtuple1(0), fNtuple2(0), >> 48 fEabs(0), fEgap(0) ,fLabs(0), fLgap(0) >> 49 { >> 50 >> 51 // histograms >> 52 for (G4int k=0; k<MaxHisto; k++) fHisto[k] = 0; >> 53 >> 54 // ntuple >> 55 fNtuple1 = 0; >> 56 fNtuple2 = 0; 47 } 57 } 48 58 49 //....oooOO0OOooo........oooOO0OOooo........oo 59 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 60 51 void HistoManager::Book() << 61 HistoManager::~HistoManager() 52 { 62 { 53 // Creating a tree container to handle histo << 63 if ( fRootFile ) delete fRootFile; 54 // This tree is associated to an output file << 64 } 55 // << 56 G4String fileName = "AnaEx02.root"; << 57 fRootFile = new TFile(fileName, "RECREATE"); << 58 if (fRootFile == nullptr) { << 59 G4cout << " HistoManager::Book :" << 60 << " problem creating the ROOT TFil << 61 return; << 62 } << 63 << 64 // id = 0 << 65 fHisto[0] = new TH1D("EAbs", "Edep in absorb << 66 // id = 1 << 67 fHisto[1] = new TH1D("EGap", "Edep in gap (M << 68 // id = 2 << 69 fHisto[2] = new TH1D("LAbs", "trackL in abso << 70 // id = 3 << 71 fHisto[3] = new TH1D("LGap", "trackL in gap << 72 << 73 for (G4int i = 0; i < kMaxHisto; ++i) { << 74 if (fHisto[i] == nullptr) { << 75 G4cout << "\n can't create histo " << i << 76 } << 77 } << 78 << 79 // create 1st ntuple << 80 fNtuple1 = new TTree("Ntuple1", "Edep"); << 81 fNtuple1->Branch("Eabs", &fEabs, "Eabs/D"); << 82 fNtuple1->Branch("Egap", &fEgap, "Egap/D"); << 83 65 84 // create 2nd ntuple << 66 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 85 fNtuple2 = new TTree("Ntuple2", "TrackL"); << 86 fNtuple2->Branch("Labs", &fLabs, "Labs/D"); << 87 fNtuple2->Branch("Lgap", &fLgap, "Lgap/D"); << 88 67 89 G4cout << "\n----> Output file is open in " << 68 void HistoManager::book() >> 69 { >> 70 // Creating a tree container to handle histograms and ntuples. >> 71 // This tree is associated to an output file. >> 72 // >> 73 G4String fileName = "AnaEx02.root"; >> 74 fRootFile = new TFile(fileName,"RECREATE"); >> 75 if(!fRootFile) { >> 76 G4cout << " HistoManager::book :" >> 77 << " problem creating the ROOT TFile " >> 78 << G4endl; >> 79 return; >> 80 } >> 81 >> 82 fHisto[1] = new TH1D("1", "Edep in absorber", 100, 0., 800*CLHEP::MeV); >> 83 if (!fHisto[1]) G4cout << "\n can't create histo 1" << G4endl; >> 84 fHisto[2] = new TH1D("2", "Edep in gap", 100, 0., 100*CLHEP::MeV); >> 85 if (!fHisto[2]) G4cout << "\n can't create histo 2" << G4endl; >> 86 fHisto[3] = new TH1D("3", "trackL in absorber", 100, 0., 1*CLHEP::m); >> 87 if (!fHisto[3]) G4cout << "\n can't create histo 3" << G4endl; >> 88 fHisto[4] = new TH1D("4", "trackL in gap", 100, 0., 50*CLHEP::cm); >> 89 if (!fHisto[4]) G4cout << "\n can't create histo 4" << G4endl; >> 90 >> 91 // create 1st ntuple in subdirectory "tuples" >> 92 // >> 93 fNtuple1 = new TTree("101", "Edep"); >> 94 fNtuple1->Branch("Eabs", &fEabs, "Eabs/D"); >> 95 fNtuple1->Branch("Egap", &fEgap, "Egap/D"); >> 96 >> 97 // create 2nd ntuple in subdirectory "tuples" >> 98 // >> 99 fNtuple2 = new TTree("102", "TrackL"); >> 100 fNtuple2->Branch("Labs", &fLabs, "Labs/D"); >> 101 fNtuple2->Branch("Lgap", &fLgap, "Lgap/D"); >> 102 >> 103 >> 104 G4cout << "\n----> Histogram file is opened in " << fileName << G4endl; 90 } 105 } 91 106 92 //....oooOO0OOooo........oooOO0OOooo........oo 107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 93 108 94 void HistoManager::Save() << 109 void HistoManager::save() 95 { << 110 { 96 if (fRootFile == nullptr) { << 111 if (fRootFile) { 97 return; << 112 fRootFile->Write(); // Writing the histograms to the file >> 113 fRootFile->Close(); // and closing the tree (and the file) >> 114 G4cout << "\n----> Histogram Tree is saved \n" << G4endl; 98 } 115 } 99 << 100 fRootFile->Write(); // Writing the histogra << 101 fRootFile->Close(); // and closing the tree << 102 << 103 G4cout << "\n----> Histograms and ntuples ar << 104 } 116 } 105 117 106 //....oooOO0OOooo........oooOO0OOooo........oo 118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 107 119 108 void HistoManager::FillHisto(G4int ih, G4doubl 120 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight) 109 { 121 { 110 if (ih >= kMaxHisto) { << 122 if (ih >= MaxHisto) { 111 G4cerr << "---> warning from HistoManager: << 123 G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih 112 << " does not exist. (xbin=" << xbi << 124 << " does not exist. (xbin=" << xbin << " weight=" << weight << ")" >> 125 << G4endl; 113 return; 126 return; 114 } 127 } 115 if (fHisto[ih] != nullptr) { << 128 if (fHisto[ih]) { fHisto[ih]->Fill(xbin, weight); } 116 fHisto[ih]->Fill(xbin, weight); << 117 } << 118 } 129 } 119 130 120 //....oooOO0OOooo........oooOO0OOooo........oo 131 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 121 132 122 void HistoManager::Normalize(G4int ih, G4doubl 133 void HistoManager::Normalize(G4int ih, G4double fac) 123 { 134 { 124 if (ih >= kMaxHisto) { << 135 if (ih >= MaxHisto) { 125 G4cout << "---> warning from HistoManager: 136 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih 126 << " does not exist. (fac=" << fac 137 << " does not exist. (fac=" << fac << ")" << G4endl; 127 return; 138 return; 128 } 139 } 129 if (fHisto[ih] != nullptr) { << 140 if (fHisto[ih]) fHisto[ih]->Scale(fac); 130 fHisto[ih]->Scale(fac); << 131 } << 132 } 141 } 133 142 134 //....oooOO0OOooo........oooOO0OOooo........oo 143 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 135 144 136 void HistoManager::FillNtuple(G4double energyA << 145 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, 137 G4double trackLG << 146 G4double trackLAbs , G4double trackLGap ) 138 { 147 { 139 fEabs = energyAbs; << 148 fEabs = energyAbs; 140 fEgap = energyGap; << 149 fEgap = energyGap; 141 fLabs = trackLAbs; << 150 fLabs = trackLAbs; 142 fLgap = trackLGap; << 151 fLgap = trackLGap; 143 152 144 if (fNtuple1 != nullptr) { << 153 if (fNtuple1) fNtuple1->Fill(); 145 fNtuple1->Fill(); << 154 if (fNtuple2) fNtuple2->Fill(); 146 } << 147 if (fNtuple2 != nullptr) { << 148 fNtuple2->Fill(); << 149 } << 150 } 155 } 151 156 152 //....oooOO0OOooo........oooOO0OOooo........oo 157 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 153 158 154 void HistoManager::PrintStatistic() 159 void HistoManager::PrintStatistic() 155 { 160 { 156 G4cout << "\n ----> print histograms statist << 161 if(fHisto[1]) { 157 for (auto h1 : fHisto) { << 162 G4cout << "\n ----> print histograms statistic \n" << G4endl; 158 const G4String name = h1->GetName(); << 163 159 << 164 G4cout 160 G4String unitCategory; << 165 << " EAbs : mean = " << G4BestUnit(fHisto[1]->GetMean(), "Energy") 161 if (name[0] == 'E') { << 166 << " rms = " << G4BestUnit(fHisto[1]->GetRMS(), "Energy") << G4endl; 162 unitCategory = "Energy"; << 167 G4cout 163 } << 168 << " EGap : mean = " << G4BestUnit(fHisto[2]->GetMean(), "Energy") 164 if (name[0] == 'L') { << 169 << " rms = " << G4BestUnit(fHisto[2]->GetRMS(), "Energy") << G4endl; 165 unitCategory = "Length"; << 170 G4cout 166 } << 171 << " LAbs : mean = " << G4BestUnit(fHisto[3]->GetMean(), "Length") >> 172 << " rms = " << G4BestUnit(fHisto[3]->GetRMS(), "Length") << G4endl; >> 173 G4cout >> 174 << " LGap : mean = " << G4BestUnit(fHisto[4]->GetMean(), "Length") >> 175 << " rms = " << G4BestUnit(fHisto[4]->GetRMS(), "Length") << G4endl; 167 176 168 G4cout << name << ": mean = " << G4BestUni << 169 << " rms = " << G4BestUnit(h1->GetR << 170 } 177 } 171 } 178 } 172 179 173 //....oooOO0OOooo........oooOO0OOooo........oo 180 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 181 >> 182 174 183