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 // $Id: HistoManager.cc,v 1.1 2010-11-08 10:38:44 maire Exp $ 27 /// \brief Implementation of the HistoManager << 27 // GEANT4 tag $Name: not supported by cvs2svn $ 28 // << 28 // 29 // << 30 // << 31 //....oooOO0OOooo........oooOO0OOooo........oo 29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 //....oooOO0OOooo........oooOO0OOooo........oo 30 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 33 31 34 #include "HistoManager.hh" 32 #include "HistoManager.hh" 35 << 36 #include "G4SystemOfUnits.hh" << 37 #include "G4UnitsTable.hh" 33 #include "G4UnitsTable.hh" 38 34 >> 35 #ifdef G4ANALYSIS_USE >> 36 #include "AIDA/AIDA.h" >> 37 #endif >> 38 39 //....oooOO0OOooo........oooOO0OOooo........oo 39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 40 40 41 HistoManager::HistoManager() 41 HistoManager::HistoManager() >> 42 :af(0),tree(0) 42 { 43 { 43 // Create or get analysis manager << 44 #ifdef G4ANALYSIS_USE 44 G4AnalysisManager* analysisManager = G4Analy << 45 // Creating the analysis factory 45 analysisManager->SetDefaultFileType("root"); << 46 // 46 // the default file type can be overriden in << 47 af = AIDA_createAnalysisFactory(); >> 48 if(!af) { >> 49 G4cout << " HistoManager::HistoManager :" >> 50 << " problem creating the AIDA analysis factory." >> 51 << G4endl; >> 52 } >> 53 #endif >> 54 >> 55 // histograms >> 56 for (G4int k=0; k<MaxHisto; k++) histo[k] = 0; >> 57 >> 58 // ntuple >> 59 ntupl = 0; >> 60 } >> 61 >> 62 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 63 >> 64 HistoManager::~HistoManager() >> 65 { >> 66 #ifdef G4ANALYSIS_USE >> 67 delete af; >> 68 #endif >> 69 } >> 70 >> 71 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 72 >> 73 void HistoManager::book() >> 74 { >> 75 #ifdef G4ANALYSIS_USE >> 76 if(!af) return; >> 77 >> 78 // Creating a tree container to handle histograms and ntuples. >> 79 // This tree is associated to an output file. >> 80 // >> 81 G4String fileName = "AnaEx01"; >> 82 G4String fileType = "root"; // hbook root xml >> 83 G4String fileOption = " "; >> 84 //// G4String fileOption = "uncompress compress=no"; //for xml >> 85 //// G4String fileOption = "--noErrors"; //for hbook >> 86 >> 87 fileName = fileName + "." + fileType; >> 88 G4bool readOnly = false; >> 89 G4bool createNew = true; >> 90 AIDA::ITreeFactory* tf = af->createTreeFactory(); >> 91 tree = tf->create(fileName, fileType, readOnly, createNew, fileOption); >> 92 delete tf; >> 93 if(!tree) { >> 94 G4cout << " HistoManager::book :" >> 95 << " problem creating the AIDA tree with " >> 96 << " storeName = " << fileName >> 97 << " storeType = " << fileType >> 98 << " readOnly = " << readOnly >> 99 << " createNew = " << createNew >> 100 << " options = " << fileOption >> 101 << G4endl; >> 102 return; >> 103 } >> 104 >> 105 // Creating a histogram factory, whose histograms will be handled by the tree >> 106 // >> 107 AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree); >> 108 >> 109 // create histos in subdirectory "histograms" >> 110 // >> 111 tree->mkdir("histograms"); >> 112 tree->cd("histograms"); >> 113 >> 114 histo[1] = hf->createHistogram1D("1", "Edep in absorber", 100, 0., 800*MeV); >> 115 if (!histo[1]) G4cout << "\n can't create histo 1" << G4endl; >> 116 histo[2] = hf->createHistogram1D("2", "Edep in gap", 100, 0., 100*MeV); >> 117 if (!histo[2]) G4cout << "\n can't create histo 2" << G4endl; >> 118 histo[3] = hf->createHistogram1D("3", "trackL in absorber", 100, 0., 1*m); >> 119 if (!histo[3]) G4cout << "\n can't create histo 3" << G4endl; >> 120 histo[4] = hf->createHistogram1D("4", "trackL in gap", 100, 0., 50*cm); >> 121 if (!histo[4]) G4cout << "\n can't create histo 4" << G4endl; >> 122 >> 123 delete hf; >> 124 tree->cd(".."); >> 125 >> 126 // Creating a ntuple factory, handled by the tree >> 127 // >> 128 AIDA::ITupleFactory* ntf = af->createTupleFactory(*tree); >> 129 >> 130 // create 1 ntuple in subdirectory "tuples" >> 131 // >> 132 tree->mkdir("tuples"); >> 133 tree->cd("tuples"); >> 134 >> 135 ntupl = ntf->create("101", "Edep and TrackL", "double Eabs, Egap, Labs, Lgap"); >> 136 >> 137 delete ntf; >> 138 tree->cd(".."); >> 139 >> 140 G4cout << "\n----> Histogram Tree is opened in " << fileName << G4endl; >> 141 #endif 47 } 142 } 48 143 49 //....oooOO0OOooo........oooOO0OOooo........oo 144 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 50 145 51 void HistoManager::Book() << 146 void HistoManager::save() 52 { << 147 { 53 // Create or get analysis manager << 148 #ifdef G4ANALYSIS_USE 54 G4AnalysisManager* analysisManager = G4Analy << 149 if (af && tree) { 55 << 150 tree->commit(); // Writing the histograms to the file 56 if (!fFactoryOn) { << 151 tree->close(); // and closing the tree (and the file) 57 // << 152 G4cout << "\n----> Histogram Tree is saved \n" << G4endl; 58 analysisManager->SetVerboseLevel(1); << 59 // Only merge in MT mode to avoid warning << 60 #ifdef G4MULTITHREADED << 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 << 70 // << 71 G4bool fileOpen = analysisManager->OpenFile( << 72 if (!fileOpen) { << 73 G4cerr << "\n---> HistoManager::Book(): ca << 74 << G4endl; << 75 return; << 76 } << 77 << 78 if (!fFactoryOn) { << 79 // Create histograms. << 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 153 111 fFactoryOn = true; << 154 delete tree; >> 155 tree = 0; 112 } 156 } 113 << 157 #endif 114 G4cout << "\n----> Output file is open in " << 115 << analysisManager->GetFileType() << << 116 } 158 } 117 159 118 //....oooOO0OOooo........oooOO0OOooo........oo 160 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 119 161 120 void HistoManager::Save() << 162 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight) 121 { 163 { 122 if (!fFactoryOn) { << 164 if (ih >= MaxHisto) { >> 165 G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih >> 166 << " does not exist. (xbin=" << xbin << " weight=" << weight << ")" >> 167 << G4endl; 123 return; 168 return; 124 } 169 } 125 << 170 #ifdef G4ANALYSIS_USE 126 G4AnalysisManager* analysisManager = G4Analy << 171 if (histo[ih]) histo[ih]->fill(xbin, weight); 127 analysisManager->Write(); << 172 #endif 128 analysisManager->CloseFile(); << 129 << 130 G4cout << "\n----> Histograms and ntuples ar << 131 } 173 } 132 174 133 //....oooOO0OOooo........oooOO0OOooo........oo 175 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 134 176 135 void HistoManager::FillHisto(G4int ih, G4doubl << 177 void HistoManager::Normalize(G4int ih, G4double fac) 136 { 178 { 137 G4AnalysisManager* analysisManager = G4Analy << 179 if (ih >= MaxHisto) { 138 analysisManager->FillH1(ih, xbin, weight); << 180 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih >> 181 << " does not exist. (fac=" << fac << ")" << G4endl; >> 182 return; >> 183 } >> 184 #ifdef G4ANALYSIS_USE >> 185 if (histo[ih]) histo[ih]->scale(fac); >> 186 #endif 139 } 187 } 140 188 141 //....oooOO0OOooo........oooOO0OOooo........oo 189 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 142 190 143 void HistoManager::Normalize(G4int ih, G4doubl << 191 void HistoManager::FillNtuple(G4int column, G4double value) 144 { 192 { 145 G4AnalysisManager* analysisManager = G4Analy << 193 if (column > 3) { 146 auto h1 = analysisManager->GetH1(ih); << 194 G4cout << "---> warning from HistoManager::FillNtuple : " 147 if (h1 != nullptr) { << 195 << "column=" << column << " value=" << value << G4endl; 148 h1->scale(fac); << 196 return; 149 } 197 } >> 198 #ifdef G4ANALYSIS_USE >> 199 if (ntupl) ntupl->fill(column, value); >> 200 #endif 150 } 201 } 151 202 152 //....oooOO0OOooo........oooOO0OOooo........oo 203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 153 204 154 void HistoManager::FillNtuple(G4double energyA << 205 void HistoManager::AddRowNtuple() 155 G4double trackLG << 156 { 206 { 157 G4AnalysisManager* analysisManager = G4Analy << 207 #ifdef G4ANALYSIS_USE 158 // Fill 1st ntuple ( id = 0) << 208 if (ntupl) ntupl->addRow(); 159 analysisManager->FillNtupleDColumn(0, 0, ene << 209 #endif 160 analysisManager->FillNtupleDColumn(0, 1, ene << 161 analysisManager->AddNtupleRow(0); << 162 // Fill 2nd ntuple ( id = 1) << 163 analysisManager->FillNtupleDColumn(1, 0, tra << 164 analysisManager->FillNtupleDColumn(1, 1, tra << 165 analysisManager->AddNtupleRow(1); << 166 } 210 } 167 211 168 //....oooOO0OOooo........oooOO0OOooo........oo 212 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 169 213 170 void HistoManager::PrintStatistic() 214 void HistoManager::PrintStatistic() 171 { 215 { 172 if (!fFactoryOn) { << 216 #ifdef G4ANALYSIS_USE 173 return; << 217 if(histo[1]) { 174 } << 218 G4cout << "\n ----> print histograms statistic \n" << G4endl; >> 219 >> 220 G4cout >> 221 << " EAbs : mean = " << G4BestUnit(histo[1]->mean(), "Energy") >> 222 << " rms = " << G4BestUnit(histo[1]->rms(), "Energy") << G4endl; >> 223 G4cout >> 224 << " EGap : mean = " << G4BestUnit(histo[2]->mean(), "Energy") >> 225 << " rms = " << G4BestUnit(histo[2]->rms(), "Energy") << G4endl; >> 226 G4cout >> 227 << " LAbs : mean = " << G4BestUnit(histo[3]->mean(), "Length") >> 228 << " rms = " << G4BestUnit(histo[3]->rms(), "Length") << G4endl; >> 229 G4cout >> 230 << " LGap : mean = " << G4BestUnit(histo[4]->mean(), "Length") >> 231 << " rms = " << G4BestUnit(histo[4]->rms(), "Length") << G4endl; 175 232 176 G4AnalysisManager* analysisManager = G4Analy << 177 << 178 G4cout << "\n ----> print histograms statist << 179 for (G4int i = 0; i < analysisManager->GetNo << 180 G4String name = analysisManager->GetH1Name << 181 auto h1 = analysisManager->GetH1(i); << 182 << 183 G4String unitCategory; << 184 if (name[0U] == 'E') { << 185 unitCategory = "Energy"; << 186 } << 187 if (name[0U] == 'L') { << 188 unitCategory = "Length"; << 189 } << 190 // we use an explicit unsigned int type fo << 191 // to avoid problems with windows compiler << 192 << 193 G4cout << name << ": mean = " << G4BestUni << 194 << " rms = " << G4BestUnit(h1->rms( << 195 } 233 } >> 234 #endif 196 } 235 } 197 236 198 //....oooOO0OOooo........oooOO0OOooo........oo 237 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 238 >> 239 199 240