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