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/AnaEx01/src/HistoManager.cc 26 /// \file analysis/AnaEx01/src/HistoManager.cc 27 /// \brief Implementation of the HistoManager 27 /// \brief Implementation of the HistoManager class 28 // 28 // 29 // 29 // 30 // << 30 // $Id: HistoManager.cc 92443 2015-09-01 13:56:16Z gcosmo $ 31 //....oooOO0OOooo........oooOO0OOooo........oo << 31 // 32 //....oooOO0OOooo........oooOO0OOooo........oo 32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 33 34 34 #include "HistoManager.hh" 35 #include "HistoManager.hh" 35 << 36 #include "G4SystemOfUnits.hh" << 37 #include "G4UnitsTable.hh" 36 #include "G4UnitsTable.hh" >> 37 #include "G4SystemOfUnits.hh" 38 38 39 //....oooOO0OOooo........oooOO0OOooo........oo 39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 40 40 41 HistoManager::HistoManager() 41 HistoManager::HistoManager() 42 { << 42 : fFactoryOn(false) 43 // Create or get analysis manager << 43 {} 44 G4AnalysisManager* analysisManager = G4Analy << 44 45 analysisManager->SetDefaultFileType("root"); << 45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 46 // the default file type can be overriden in << 46 47 } << 47 HistoManager::~HistoManager() >> 48 {} 48 49 49 //....oooOO0OOooo........oooOO0OOooo........oo 50 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 51 51 void HistoManager::Book() 52 void HistoManager::Book() 52 { 53 { 53 // Create or get analysis manager 54 // Create or get analysis manager >> 55 // The choice of analysis technology is done via selection of a namespace >> 56 // in HistoManager.hh 54 G4AnalysisManager* analysisManager = G4Analy 57 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 55 << 58 analysisManager->SetVerboseLevel(1); 56 if (!fFactoryOn) { << 59 57 // << 60 // Create directories 58 analysisManager->SetVerboseLevel(1); << 61 analysisManager->SetHistoDirectoryName("histo"); 59 // Only merge in MT mode to avoid warning << 62 analysisManager->SetNtupleDirectoryName("ntuple"); 60 #ifdef G4MULTITHREADED << 63 61 analysisManager->SetNtupleMerging(true); << 62 #endif << 63 << 64 // Create directories << 65 analysisManager->SetHistoDirectoryName("hi << 66 analysisManager->SetNtupleDirectoryName("n << 67 } << 68 << 69 // Open an output file 64 // Open an output file 70 // 65 // 71 G4bool fileOpen = analysisManager->OpenFile( 66 G4bool fileOpen = analysisManager->OpenFile("AnaEx01"); 72 if (!fileOpen) { << 67 if (! fileOpen) { 73 G4cerr << "\n---> HistoManager::Book(): ca << 68 G4cerr << "\n---> HistoManager::Book(): cannot open " 74 << G4endl; << 69 << analysisManager->GetFileName() << G4endl; 75 return; 70 return; 76 } 71 } >> 72 >> 73 // Create histograms. >> 74 // Histogram ids are generated automatically starting from 0. >> 75 // The start value can be changed by: >> 76 // analysisManager->SetFirstHistoId(1); >> 77 >> 78 // id = 0 >> 79 analysisManager->CreateH1("EAbs","Edep in absorber (MeV)", 100, 0., 800*MeV); >> 80 // id = 1 >> 81 analysisManager->CreateH1("EGap","Edep in gap (MeV)", 100, 0., 100*MeV); >> 82 // id = 2 >> 83 analysisManager->CreateH1("LAbs","trackL in absorber (mm)", 100, 0., 1*m); >> 84 // id = 3 >> 85 analysisManager->CreateH1("LGap","trackL in gap (mm)", 100, 0., 50*cm); >> 86 >> 87 // Create ntuples. >> 88 // Ntuples ids are generated automatically starting from 0. >> 89 // The start value can be changed by: >> 90 // analysisManager->SetFirstMtupleId(1); >> 91 >> 92 // Create 1st ntuple (id = 0) >> 93 analysisManager->CreateNtuple("Ntuple1", "Edep"); >> 94 analysisManager->CreateNtupleDColumn("Eabs"); // column Id = 0 >> 95 analysisManager->CreateNtupleDColumn("Egap"); // column Id = 1 >> 96 analysisManager->FinishNtuple(); >> 97 >> 98 // Create 2nd ntuple (id = 1) >> 99 // >> 100 analysisManager->CreateNtuple("Ntuple2", "TrackL"); >> 101 analysisManager->CreateNtupleDColumn("Labs"); // column Id = 0 >> 102 analysisManager->CreateNtupleDColumn("Lgap"); // column Id = 1 >> 103 analysisManager->FinishNtuple(); >> 104 >> 105 fFactoryOn = true; 77 106 78 if (!fFactoryOn) { << 107 G4cout << "\n----> Output file is open in " 79 // Create histograms. << 108 << analysisManager->GetFileName() << "." 80 // Histogram ids are generated automatical << 81 // The start value can be changed by: << 82 // analysisManager->SetFirstHistoId(1); << 83 << 84 // id = 0 << 85 analysisManager->CreateH1("EAbs", "Edep in << 86 // id = 1 << 87 analysisManager->CreateH1("EGap", "Edep in << 88 // id = 2 << 89 analysisManager->CreateH1("LAbs", "trackL << 90 // id = 3 << 91 analysisManager->CreateH1("LGap", "trackL << 92 << 93 // Create ntuples. << 94 // Ntuples ids are generated automatically << 95 // The start value can be changed by: << 96 // analysisManager->SetFirstMtupleId(1); << 97 << 98 // Create 1st ntuple (id = 0) << 99 analysisManager->CreateNtuple("Ntuple1", " << 100 analysisManager->CreateNtupleDColumn("Eabs << 101 analysisManager->CreateNtupleDColumn("Egap << 102 analysisManager->FinishNtuple(); << 103 << 104 // Create 2nd ntuple (id = 1) << 105 // << 106 analysisManager->CreateNtuple("Ntuple2", " << 107 analysisManager->CreateNtupleDColumn("Labs << 108 analysisManager->CreateNtupleDColumn("Lgap << 109 analysisManager->FinishNtuple(); << 110 << 111 fFactoryOn = true; << 112 } << 113 << 114 G4cout << "\n----> Output file is open in " << 115 << analysisManager->GetFileType() << 109 << analysisManager->GetFileType() << G4endl; 116 } 110 } 117 111 118 //....oooOO0OOooo........oooOO0OOooo........oo 112 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 119 113 120 void HistoManager::Save() 114 void HistoManager::Save() 121 { 115 { 122 if (!fFactoryOn) { << 116 if (! fFactoryOn) return; 123 return; << 117 124 } << 118 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 125 << 126 G4AnalysisManager* analysisManager = G4Analy << 127 analysisManager->Write(); 119 analysisManager->Write(); 128 analysisManager->CloseFile(); << 120 analysisManager->CloseFile(); 129 << 121 130 G4cout << "\n----> Histograms and ntuples ar 122 G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl; >> 123 >> 124 delete G4AnalysisManager::Instance(); >> 125 fFactoryOn = false; 131 } 126 } 132 127 133 //....oooOO0OOooo........oooOO0OOooo........oo 128 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 134 129 135 void HistoManager::FillHisto(G4int ih, G4doubl 130 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight) 136 { 131 { 137 G4AnalysisManager* analysisManager = G4Analy << 132 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 138 analysisManager->FillH1(ih, xbin, weight); 133 analysisManager->FillH1(ih, xbin, weight); 139 } 134 } 140 135 141 //....oooOO0OOooo........oooOO0OOooo........oo 136 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 142 137 143 void HistoManager::Normalize(G4int ih, G4doubl 138 void HistoManager::Normalize(G4int ih, G4double fac) 144 { 139 { 145 G4AnalysisManager* analysisManager = G4Analy << 140 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 146 auto h1 = analysisManager->GetH1(ih); << 141 G4H1* h1 = analysisManager->GetH1(ih); 147 if (h1 != nullptr) { << 142 if (h1) h1->scale(fac); 148 h1->scale(fac); << 149 } << 150 } 143 } 151 144 152 //....oooOO0OOooo........oooOO0OOooo........oo 145 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 153 146 154 void HistoManager::FillNtuple(G4double energyA << 147 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, 155 G4double trackLG << 148 G4double trackLAbs, G4double trackLGap) 156 { << 149 { 157 G4AnalysisManager* analysisManager = G4Analy 150 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 158 // Fill 1st ntuple ( id = 0) 151 // Fill 1st ntuple ( id = 0) 159 analysisManager->FillNtupleDColumn(0, 0, ene 152 analysisManager->FillNtupleDColumn(0, 0, energyAbs); 160 analysisManager->FillNtupleDColumn(0, 1, ene 153 analysisManager->FillNtupleDColumn(0, 1, energyGap); 161 analysisManager->AddNtupleRow(0); << 154 analysisManager->AddNtupleRow(0); 162 // Fill 2nd ntuple ( id = 1) 155 // Fill 2nd ntuple ( id = 1) 163 analysisManager->FillNtupleDColumn(1, 0, tra 156 analysisManager->FillNtupleDColumn(1, 0, trackLAbs); 164 analysisManager->FillNtupleDColumn(1, 1, tra 157 analysisManager->FillNtupleDColumn(1, 1, trackLGap); 165 analysisManager->AddNtupleRow(1); << 158 analysisManager->AddNtupleRow(1); 166 } << 159 } 167 160 168 //....oooOO0OOooo........oooOO0OOooo........oo 161 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 169 162 170 void HistoManager::PrintStatistic() 163 void HistoManager::PrintStatistic() 171 { 164 { 172 if (!fFactoryOn) { << 165 if (! fFactoryOn) return; 173 return; << 174 } << 175 166 176 G4AnalysisManager* analysisManager = G4Analy 167 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 177 << 168 178 G4cout << "\n ----> print histograms statist 169 G4cout << "\n ----> print histograms statistic \n" << G4endl; 179 for (G4int i = 0; i < analysisManager->GetNo << 170 for ( G4int i=0; i<analysisManager->GetNofH1s(); ++i ) { 180 G4String name = analysisManager->GetH1Name 171 G4String name = analysisManager->GetH1Name(i); 181 auto h1 = analysisManager->GetH1(i); << 172 G4H1* h1 = analysisManager->GetH1(i); 182 << 173 183 G4String unitCategory; 174 G4String unitCategory; 184 if (name[0U] == 'E') { << 175 if (name[0U] == 'E' ) unitCategory = "Energy"; 185 unitCategory = "Energy"; << 176 if (name[0U] == 'L' ) unitCategory = "Length"; 186 } << 177 // we use an explicit unsigned int type for operator [] argument 187 if (name[0U] == 'L') { << 178 // to avoid problems with windows compiler 188 unitCategory = "Length"; << 179 189 } << 180 G4cout << name 190 // we use an explicit unsigned int type fo << 181 << ": mean = " << G4BestUnit(h1->mean(), unitCategory) 191 // to avoid problems with windows compiler << 182 << " rms = " << G4BestUnit(h1->rms(), unitCategory ) 192 << 183 << G4endl; 193 G4cout << name << ": mean = " << G4BestUni << 194 << " rms = " << G4BestUnit(h1->rms( << 195 } 184 } 196 } 185 } 197 186 198 //....oooOO0OOooo........oooOO0OOooo........oo 187 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 188 >> 189 199 190