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 hadronic/Hadr02/src/Histo.cc << 27 /// \brief Implementation of the Histo class << 28 // << 29 // << 30 //-------------------------------------------- 26 //--------------------------------------------------------------------------- 31 // 27 // 32 // ClassName: Histo - Generic histogram/ntup 28 // ClassName: Histo - Generic histogram/ntuple manager class 33 // 29 // 34 // 30 // 35 // Author: V.Ivanchenko 30.10.03 31 // Author: V.Ivanchenko 30.10.03 36 // 32 // 37 //-------------------------------------------- 33 //---------------------------------------------------------------------------- 38 // 34 // 39 35 40 //....oooOO0OOooo........oooOO0OOooo........oo 36 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 41 //....oooOO0OOooo........oooOO0OOooo........oo 37 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 42 38 43 #include "Histo.hh" 39 #include "Histo.hh" 44 40 >> 41 #ifdef G4ANALYSIS_USE >> 42 #include <AIDA/AIDA.h> 45 #include "HistoMessenger.hh" 43 #include "HistoMessenger.hh" >> 44 #endif >> 45 >> 46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... >> 47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 46 48 47 #include "G4RootAnalysisManager.hh" << 49 Histo::Histo(G4int ver) 48 #include "G4SystemOfUnits.hh" << 50 { 49 #include "G4XmlAnalysisManager.hh" << 51 verbose = ver; 50 #include "G4ios.hh" << 52 histName = "histo"; 51 << 53 histType = "root"; 52 //....oooOO0OOooo........oooOO0OOooo........oo << 54 option = ""; 53 //....oooOO0OOooo........oooOO0OOooo........oo << 55 nHisto = 0; 54 << 56 defaultAct = 1; 55 Histo::Histo() << 57 tupleName = "tree.root"; 56 { << 58 tupleId = "100"; 57 fManager = 0; << 59 tupleList = ""; 58 fMessenger = new HistoMessenger(this); << 60 ntup = 0; 59 << 61 messenger = 0; 60 fHistName = "test"; << 62 61 fHistType = "root"; << 63 #ifdef G4ANALYSIS_USE 62 fTupleName = "tuple"; << 64 messenger = new HistoMessenger(this); 63 fTupleTitle = "test"; << 65 tree = 0; 64 fNHisto = 0; << 66 af = 0; 65 fVerbose = 0; << 67 #endif 66 fDefaultAct = true; << 67 fHistoActive = false; << 68 fNtupleActive = false; << 69 } 68 } 70 69 71 //....oooOO0OOooo........oooOO0OOooo........oo << 70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 72 71 73 Histo::~Histo() 72 Histo::~Histo() 74 { 73 { 75 delete fMessenger; << 74 #ifdef G4ANALYSIS_USE >> 75 delete messenger; >> 76 delete af; >> 77 #endif 76 } 78 } 77 79 78 //....oooOO0OOooo........oooOO0OOooo........oo << 80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 79 81 80 void Histo::Book() << 82 void Histo::book() 81 { 83 { 82 if (!(fHistoActive || fNtupleActive)) { << 84 #ifdef G4ANALYSIS_USE 83 return; << 85 G4cout << "### Histo books " << nHisto << " histograms " << G4endl; 84 } << 86 // Creating the analysis factory 85 << 87 if(!af) af = AIDA_createAnalysisFactory(); 86 // Always creating analysis manager << 88 if(verbose>0) { 87 if (fHistType == "root") { << 89 G4cout<<"HIsto books analysis factory ......... "<<G4endl; 88 fManager = G4RootAnalysisManager::Instance << 90 } 89 } << 91 // Creating the tree factory 90 else { << 92 AIDA::ITreeFactory* tf = af->createTreeFactory(); 91 fManager = G4XmlAnalysisManager::Instance( << 93 92 } << 94 if(verbose>0) { >> 95 G4cout<<"Histo books tree factory ......... "<<G4endl; >> 96 } >> 97 G4String histExt = ""; >> 98 char* path = getenv("PHYSLIST"); >> 99 if (path) { histExt = "_" + G4String(path); } >> 100 >> 101 G4String histDir = ""; >> 102 path = getenv("HISTODIR"); >> 103 if (path) histDir = G4String(path) + "/"; 93 104 94 // Creating a tree mapped to a new hbook fil 105 // Creating a tree mapped to a new hbook file. 95 G4String nam = fHistName; // + "." + fHistT << 106 G4String name = histDir + histName + histExt + "." + histType; >> 107 tree = tf->create(name, histType, false, true, option); >> 108 G4cout << "Histo: tree store : " << tree->storeName() << G4endl; >> 109 delete tf; 96 110 97 // Open file histogram file << 111 // Creating a histogram factory, whose histograms will be handled by the tree 98 if (!fManager->OpenFile(nam)) { << 112 AIDA::IHistogramFactory* hf = af->createHistogramFactory( *tree ); 99 G4cout << "Histo::Book: ERROR open file <" << 100 fHistoActive = false; << 101 fNtupleActive = false; << 102 return; << 103 } << 104 G4cout << "### Histo::Save: Opended file <" << 105 << G4endl; << 106 113 107 // Creating an 1-dimensional histograms in t 114 // Creating an 1-dimensional histograms in the root directory of the tree 108 for (G4int i = 0; i < fNHisto; ++i) { << 115 for(G4int i=0; i<nHisto; i++) { 109 if (fActive[i]) { << 116 if(active[i]) { 110 G4String ss = "h" + fIds[i]; << 117 if(verbose>0) { 111 fHisto[i] = fManager->CreateH1(ss, fTitl << 118 G4cout<<" I am in book: histogram "<< i << " id= " << ids[i] <<G4endl; 112 if (fVerbose > 0) { << 113 G4cout << "Created histogram #" << i < << 114 << fTitles[i] << G4endl; << 115 } 119 } >> 120 G4String idd; >> 121 if(histType == "root") { idd = "h" + ids[i]; } >> 122 else { idd = ids[i]; } >> 123 histo[i] = hf->createHistogram1D(idd, tittles[i], bins[i], xmin[i], xmax[i]); >> 124 } else { >> 125 histo[i] = 0; 116 } 126 } 117 } 127 } >> 128 delete hf; 118 // Creating a tuple factory, whose tuples wi 129 // Creating a tuple factory, whose tuples will be handled by the tree 119 if (fNtupleActive) { << 130 if(tupleList != "") { 120 fManager->CreateNtuple(fTupleName, fTupleT << 131 if(verbose>0) { 121 G4int i; << 132 G4cout<<"Histo books tuple factory for "<<tupleName <<G4endl; 122 G4int n = fNtupleI.size(); << 133 } 123 for (i = 0; i < n; ++i) { << 134 AIDA::ITupleFactory* tpf = af->createTupleFactory( *tree ); 124 if (fTupleI[i] == -1) { << 135 ntup = tpf->create(tupleId, tupleName, tupleList); 125 fTupleI[i] = fManager->CreateNtupleICo << 136 delete tpf; 126 } << 127 } << 128 n = fNtupleF.size(); << 129 for (i = 0; i < n; ++i) { << 130 if (fTupleF[i] == -1) { << 131 fTupleF[i] = fManager->CreateNtupleFCo << 132 } << 133 } << 134 n = fNtupleD.size(); << 135 for (i = 0; i < n; ++i) { << 136 if (fTupleD[i] == -1) { << 137 fTupleD[i] = fManager->CreateNtupleDCo << 138 } << 139 } << 140 } 137 } 141 } << 138 #endif >> 139 } 142 140 143 //....oooOO0OOooo........oooOO0OOooo........oo << 141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 144 142 145 void Histo::Save() << 143 void Histo::save() 146 { 144 { 147 if (!(fHistoActive || fNtupleActive)) { << 145 #ifdef G4ANALYSIS_USE 148 return; << 149 } << 150 << 151 // Creating a tree mapped to a new hbook fil << 152 G4String nam = fHistName + "." + fHistType; << 153 << 154 // Write histogram file 146 // Write histogram file 155 if (!fManager->Write()) { << 147 tree->commit(); 156 G4ExceptionDescription message; << 148 G4cout << "Closing the tree..." << G4endl; 157 message << "Histo::Save: FATAL ERROR writi << 149 tree->close(); 158 G4Exception("Histo::Save()", "Hadr02Code00 << 150 G4cout << "Histograms and Ntuples are saved" << G4endl; 159 } << 151 delete tree; 160 if (fVerbose > 0) { << 152 tree = 0; 161 G4cout << "### Histo::Save: Histograms and << 153 #endif 162 } << 163 if (fManager->CloseFile() && fVerbose > 0) { << 164 G4cout << " File is closed << 165 } << 166 fManager->Clear(); << 167 } 154 } 168 155 169 //....oooOO0OOooo........oooOO0OOooo........oo << 156 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 170 << 171 void Histo::Add1D(const G4String& id, const G4 << 172 G4double u) << 173 { << 174 if (fVerbose > 0) { << 175 G4cout << "Histo::Add1D: New histogram wil << 176 << " " << x1 << " " << x2 << " " << 177 } << 178 ++fNHisto; << 179 x1 /= u; << 180 x2 /= u; << 181 fActive.push_back(fDefaultAct); << 182 fBins.push_back(nb); << 183 fXmin.push_back(x1); << 184 fXmax.push_back(x2); << 185 fUnit.push_back(u); << 186 fIds.push_back(id); << 187 fTitles.push_back(name); << 188 fHisto.push_back(-1); << 189 } << 190 << 191 //....oooOO0OOooo........oooOO0OOooo........oo << 192 << 193 void Histo::SetHisto1D(G4int i, G4int nb, G4do << 194 { << 195 if (i >= 0 && i < fNHisto) { << 196 if (fVerbose > 0) { << 197 G4cout << "Histo::SetHisto1D: #" << i << << 198 << G4endl; << 199 } << 200 fBins[i] = nb; << 201 fXmin[i] = x1; << 202 fXmax[i] = x2; << 203 fUnit[i] = u; << 204 fActive[i] = true; << 205 fHistoActive = true; << 206 } << 207 else { << 208 G4cout << "Histo::SetHisto1D: WARNING! wro << 209 } << 210 } << 211 << 212 //....oooOO0OOooo........oooOO0OOooo........oo << 213 157 214 void Histo::Activate(G4int i, G4bool val) << 158 void Histo::reset() 215 { 159 { 216 if (fVerbose > 1) { << 160 #ifdef G4ANALYSIS_USE 217 G4cout << "Histo::Activate: Histogram: #" << 161 delete tree; 218 } << 162 tree = 0; 219 if (i >= 0 && i < fNHisto) { << 163 #endif 220 fActive[i] = val; << 221 if (val) { << 222 fHistoActive = true; << 223 } << 224 } << 225 } 164 } 226 165 227 //....oooOO0OOooo........oooOO0OOooo........oo << 166 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 228 167 229 void Histo::Fill(G4int i, G4double x, G4double << 168 void Histo::setFileType(const G4String& nam) 230 { 169 { 231 if (!fHistoActive) { << 170 if(nam == "hbook" || nam == "root" || nam == "aida") histType = nam; 232 return; << 171 else if(nam == "XML" || nam == "xml") histType = "aida"; 233 } << 234 if (fVerbose > 1) { << 235 G4cout << "Histo::Fill: Histogram: #" << i << 236 } << 237 if (i >= 0 && i < fNHisto) { << 238 if (fActive[i]) { << 239 fManager->FillH1(fHisto[i], x / fUnit[i] << 240 } << 241 } << 242 else { << 243 G4cout << "Histo::Fill: WARNING! wrong his << 244 } << 245 } 172 } 246 173 247 //....oooOO0OOooo........oooOO0OOooo........oo << 174 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 248 175 249 void Histo::ScaleH1(G4int i, G4double x) << 176 void Histo::add1D(const G4String& id, const G4String& name, G4int nb, >> 177 G4double x1, G4double x2, G4double u) 250 { 178 { 251 if (!fHistoActive) { << 179 if(nHisto > 0) { 252 return; << 180 for(G4int i=0; i<nHisto; i++) { 253 } << 181 if(ids[i] == id) return; 254 if (fVerbose > 0) { << 255 G4cout << "Histo::Scale: Histogram: #" << << 256 } << 257 if (i >= 0 && i < fNHisto) { << 258 if (fActive[i]) { << 259 if (fHistType == "root") << 260 dynamic_cast<G4RootAnalysisManager*>(f << 261 if (fHistType == "xml") << 262 dynamic_cast<G4XmlAnalysisManager*>(fM << 263 } 182 } 264 } 183 } 265 else { << 266 G4cout << "Histo::Scale: WARNING! wrong hi << 267 } << 268 } << 269 << 270 //....oooOO0OOooo........oooOO0OOooo........oo << 271 << 272 void Histo::AddTuple(const G4String& w1) << 273 { << 274 fTupleTitle = w1; << 275 } << 276 184 277 //....oooOO0OOooo........oooOO0OOooo........oo << 185 if(verbose > 0) >> 186 G4cout << "New histogram will be booked: #" << id << " <" << name >> 187 << " " << nb << " " << x1 << " " << x2 << " " << u >> 188 << G4endl; 278 189 279 void Histo::AddTupleI(const G4String& w1) << 190 nHisto++; 280 { << 191 x1 /= u; 281 fNtupleActive = true; << 192 x2 /= u; 282 fNtupleI.push_back(w1); << 193 active.push_back(defaultAct); 283 fTupleI.push_back(-1); << 194 bins.push_back(nb); 284 } << 195 xmin.push_back(x1); 285 << 196 xmax.push_back(x2); 286 //....oooOO0OOooo........oooOO0OOooo........oo << 197 unit.push_back(u); 287 << 198 ids.push_back(id); 288 void Histo::AddTupleF(const G4String& w1) << 199 tittles.push_back(name); 289 { << 200 histo.push_back(0); 290 fNtupleActive = true; << 291 fNtupleF.push_back(w1); << 292 fTupleF.push_back(-1); << 293 } 201 } 294 202 295 //....oooOO0OOooo........oooOO0OOooo........oo << 203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 296 204 297 void Histo::AddTupleD(const G4String& w1) << 205 void Histo::setHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u) 298 { 206 { 299 fNtupleActive = true; << 207 if(i>=0 && i<nHisto) { 300 fNtupleD.push_back(w1); << 208 if(verbose > 0) 301 fTupleD.push_back(-1); << 209 { >> 210 G4cout << "Update histogram: #" << i >> 211 << " " << nb << " " << x1 << " " << x2 << " " << u >> 212 << G4endl; >> 213 } >> 214 bins[i] = nb; >> 215 xmin[i] = x1; >> 216 xmax[i] = x2; >> 217 unit[i] = u; >> 218 } else { >> 219 G4cout << "Histo::setHisto1D: WARNING! wrong histogram index " << i << G4endl; >> 220 G4cout << "nHisto = " << nHisto << G4endl; >> 221 } 302 } 222 } 303 223 304 //....oooOO0OOooo........oooOO0OOooo........oo << 224 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 305 225 306 void Histo::FillTupleI(G4int i, G4int x) << 226 void Histo::fill(G4int i, G4double x, G4double w) 307 { 227 { 308 if (!fNtupleActive) { << 228 if(verbose > 1) { 309 return; << 229 G4cout << "fill histogram: #" << i << " at x= " << x 310 } << 230 << " weight= " << w << " unit= " << unit[i] 311 G4int n = fNtupleI.size(); << 231 << G4endl; 312 if (i >= 0 && i < n) { << 232 } 313 if (fVerbose > 1) { << 233 #ifdef G4ANALYSIS_USE 314 G4cout << "Histo::FillTupleI: i= " << i << 234 if(i>=0 && i<nHisto) { 315 << "> = " << x << G4endl; << 235 histo[i]->fill(x/unit[i], w); 316 } << 236 } else { 317 fManager->FillNtupleIColumn(fTupleI[i], x) << 237 G4cout << "Histo::fill: WARNING! wrong histogram index " << i << G4endl; 318 } << 319 else { << 320 G4cout << "Histo::FillTupleI: WARNING! wro << 321 } 238 } >> 239 #endif 322 } 240 } 323 241 324 //....oooOO0OOooo........oooOO0OOooo........oo << 242 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 325 243 326 void Histo::FillTupleF(G4int i, G4float x) << 244 void Histo::scale(G4int i, G4double x) 327 { 245 { 328 if (!fNtupleActive) { << 246 if(verbose > 0) { 329 return; << 247 G4cout << "Scale histogram: #" << i << " by factor " << x << G4endl; 330 } << 331 G4int n = fNtupleF.size(); << 332 if (i >= 0 && i < n) { << 333 if (fVerbose > 1) { << 334 G4cout << "Histo::FillTupleF: i= " << i << 335 << "> = " << x << G4endl; << 336 } << 337 fManager->FillNtupleFColumn(fTupleF[i], x) << 338 } 248 } 339 else { << 249 #ifdef G4ANALYSIS_USE 340 G4cout << "Histo::FillTupleF: WARNING! wro << 250 if(i>=0 && i<nHisto) { >> 251 histo[i]->scale(x); >> 252 } else { >> 253 G4cout << "Histo::scale: WARNING! wrong histogram index " << i << G4endl; 341 } 254 } >> 255 #endif 342 } 256 } 343 257 344 //....oooOO0OOooo........oooOO0OOooo........oo << 258 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 345 259 346 void Histo::FillTupleD(G4int i, G4double x) << 260 void Histo::addTuple(const G4String& w1, const G4String& w2, const G4String& w3) 347 { 261 { 348 if (!fNtupleActive) { << 262 tupleId = w1; 349 return; << 263 tupleName = w2; 350 } << 264 tupleList = w3; 351 G4int n = fNtupleD.size(); << 265 352 if (i >= 0 && i < n) { << 266 G4cout<<" addTuple: Id "<<w1<<" Name "<<w2<<" List "<<w3<<G4endl; 353 if (fVerbose > 1) { << 354 G4cout << "Histo::FillTupleD: i= " << i << 355 << "> = " << x << G4endl; << 356 } << 357 fManager->FillNtupleDColumn(fTupleD[i], x) << 358 } << 359 else { << 360 G4cout << "Histo::FillTupleD: WARNING! wro << 361 } << 362 } 267 } 363 268 364 //....oooOO0OOooo........oooOO0OOooo........oo << 269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 365 270 366 void Histo::AddRow() << 271 void Histo::fillTuple(const G4String& parname, G4double x) 367 { 272 { 368 if (!fNtupleActive) { << 273 if(verbose > 1) { 369 return; << 274 G4cout << "fill tuple by parameter <" << parname << "> = " << x << G4endl; 370 } 275 } 371 fManager->AddNtupleRow(); << 276 #ifdef G4ANALYSIS_USE >> 277 if(ntup) ntup->fill(ntup->findColumn(parname), (float)x); >> 278 #endif 372 } 279 } 373 280 374 //....oooOO0OOooo........oooOO0OOooo........oo << 281 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 375 282 376 void Histo::SetFileName(const G4String& nam) << 283 void Histo::addRow() 377 { 284 { 378 fHistName = nam; << 285 #ifdef G4ANALYSIS_USE 379 fHistoActive = true; << 286 if(ntup) ntup->addRow(); 380 } << 287 #endif 381 << 288 } 382 //....oooOO0OOooo........oooOO0OOooo........oo << 383 289 384 void Histo::SetFileType(const G4String& nam) << 290 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... >> 291 void Histo::print(G4int i) 385 { 292 { 386 if (nam == "root" || nam == "ROOT") { << 293 G4cout<<"### Histogram "<<i<<" ###"<<G4endl; 387 fHistType = "root"; << 294 #ifdef G4ANALYSIS_USE 388 } << 295 if(i>=0 && i<nHisto) { 389 else if (nam == "xml" || nam == "XML") { << 296 G4double step = (xmax[i] - xmin[i])/G4double( bins[i]); 390 fHistType = "xml"; << 297 G4double x = xmin[i] - step*0.5; 391 } << 298 G4double y, maxX=0, maxY=0; 392 else { << 299 G4int maxJ=0; 393 G4cout << "Histo::SetFileType: Sorry, form << 300 394 << " not supported. Use \"root\" or << 301 for(G4int j=0; j<bins[i]; j++) { >> 302 x += step; >> 303 y = histo[i]->binHeight(j); >> 304 if(maxY < y) {maxY = y; maxX = x; maxJ = j;} >> 305 >> 306 G4cout<<x<<" "<<y<<G4endl; >> 307 } >> 308 G4cout<<" maxJ "<<maxJ<<" maxX "<<maxX<<" maxY "<<maxY<<G4endl; 395 } 309 } >> 310 #endif 396 } 311 } >> 312 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... 397 313 398 //....oooOO0OOooo........oooOO0OOooo........oo << 399 314