[807] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | //--------------------------------------------------------------------------- |
---|
| 27 | // |
---|
| 28 | // ClassName: Histo - Generic histogram/ntuple manager class |
---|
| 29 | // |
---|
| 30 | // |
---|
| 31 | // Author: V.Ivanchenko 30.10.03 |
---|
| 32 | // |
---|
| 33 | //---------------------------------------------------------------------------- |
---|
| 34 | // |
---|
| 35 | |
---|
| 36 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 37 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 38 | |
---|
| 39 | #include "Histo.hh" |
---|
| 40 | |
---|
| 41 | #ifdef G4ANALYSIS_USE |
---|
| 42 | |
---|
| 43 | #include "AIDA/AIDA.h" |
---|
| 44 | #include "HistoMessenger.hh" |
---|
| 45 | |
---|
| 46 | #endif |
---|
| 47 | |
---|
| 48 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 49 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 50 | |
---|
| 51 | Histo::Histo() |
---|
| 52 | { |
---|
| 53 | verbose = 0; |
---|
| 54 | histName = "testem9"; |
---|
[1230] | 55 | histType = "root"; |
---|
[807] | 56 | nHisto = 0; |
---|
| 57 | defaultAct = 1; |
---|
| 58 | tupleName = "tuple9"; |
---|
| 59 | tupleId = "100"; |
---|
| 60 | tupleList = ""; |
---|
| 61 | ntup = 0; |
---|
| 62 | messenger = 0; |
---|
| 63 | |
---|
| 64 | #ifdef G4ANALYSIS_USE |
---|
| 65 | tree = 0; |
---|
| 66 | af = 0; |
---|
| 67 | messenger = new HistoMessenger(this); |
---|
| 68 | #endif |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 72 | |
---|
| 73 | Histo::~Histo() |
---|
| 74 | { |
---|
| 75 | #ifdef G4ANALYSIS_USE |
---|
| 76 | delete messenger; |
---|
| 77 | delete af; |
---|
| 78 | #endif |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 82 | |
---|
| 83 | void Histo::book() |
---|
| 84 | { |
---|
| 85 | #ifdef G4ANALYSIS_USE |
---|
| 86 | G4cout << "### Histo books " << nHisto << " histograms " << G4endl; |
---|
| 87 | // Creating the analysis factory |
---|
| 88 | if(!af) af = AIDA_createAnalysisFactory(); |
---|
| 89 | // Creating the tree factory |
---|
| 90 | AIDA::ITreeFactory* tf = af->createTreeFactory(); |
---|
| 91 | |
---|
| 92 | // Creating a tree mapped to a new hbook file. |
---|
| 93 | |
---|
| 94 | G4String nam = histName + "." + histType; |
---|
[1230] | 95 | G4String options = "--noErrors export=root uncompress"; |
---|
| 96 | |
---|
| 97 | tree = tf->create(nam,histType,false,true,options); |
---|
[807] | 98 | delete tf; |
---|
| 99 | if(tree) { |
---|
| 100 | G4cout << "Tree store : " << tree->storeName() << G4endl; |
---|
| 101 | } else { |
---|
| 102 | G4cout << "ERROR: Tree store " << histName << " is not created!" << G4endl; |
---|
| 103 | return; |
---|
| 104 | } |
---|
| 105 | // Creating a histogram factory, whose histograms will be handled by the tree |
---|
| 106 | AIDA::IHistogramFactory* hf = af->createHistogramFactory( *tree ); |
---|
| 107 | |
---|
| 108 | // Creating an 1-dimensional histograms in the root directory of the tree |
---|
| 109 | for(G4int i=0; i<nHisto; i++) { |
---|
[1230] | 110 | if(active[i]) { |
---|
| 111 | G4String ss = ids[i]; |
---|
| 112 | if(histType == "root") ss = "h" + ids[i]; |
---|
| 113 | histo[i] = hf->createHistogram1D(ss, titles[i], bins[i], xmin[i], xmax[i]); |
---|
| 114 | } |
---|
[807] | 115 | } |
---|
| 116 | delete hf; |
---|
| 117 | // Creating a tuple factory, whose tuples will be handled by the tree |
---|
| 118 | if(tupleList != "") { |
---|
| 119 | AIDA::ITupleFactory* tpf = af->createTupleFactory( *tree ); |
---|
| 120 | ntup = tpf->create(tupleId, tupleName, tupleList); |
---|
| 121 | delete tpf; |
---|
| 122 | } |
---|
| 123 | #endif |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 127 | |
---|
| 128 | void Histo::save() |
---|
| 129 | { |
---|
| 130 | #ifdef G4ANALYSIS_USE |
---|
| 131 | // Write histogram file |
---|
| 132 | if(tree) { |
---|
| 133 | tree->commit(); |
---|
| 134 | G4cout << "Closing the tree..." << G4endl; |
---|
| 135 | tree->close(); |
---|
| 136 | G4cout << "Histograms and Ntuples are saved" << G4endl; |
---|
| 137 | delete tree; |
---|
| 138 | tree = 0; |
---|
| 139 | } |
---|
| 140 | #endif |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 144 | |
---|
| 145 | void Histo::add1D(const G4String& id, const G4String& name, G4int nb, |
---|
| 146 | G4double x1, G4double x2, G4double u) |
---|
| 147 | { |
---|
| 148 | if(verbose > 0) { |
---|
| 149 | G4cout << "New histogram will be booked: #" << id << " <" << name |
---|
| 150 | << " " << nb << " " << x1 << " " << x2 << " " << u |
---|
| 151 | << G4endl; |
---|
| 152 | } |
---|
| 153 | nHisto++; |
---|
| 154 | x1 /= u; |
---|
| 155 | x2 /= u; |
---|
| 156 | active.push_back(defaultAct); |
---|
| 157 | bins.push_back(nb); |
---|
| 158 | xmin.push_back(x1); |
---|
| 159 | xmax.push_back(x2); |
---|
| 160 | unit.push_back(u); |
---|
| 161 | ids.push_back(id); |
---|
| 162 | titles.push_back(name); |
---|
| 163 | histo.push_back(0); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 167 | |
---|
| 168 | void Histo::setHisto1D(G4int i, G4int nb, G4double x1, G4double x2, G4double u) |
---|
| 169 | { |
---|
| 170 | if(i>=0 && i<nHisto) { |
---|
| 171 | if(verbose > 0) { |
---|
| 172 | G4cout << "Update histogram: #" << i |
---|
| 173 | << " " << nb << " " << x1 << " " << x2 << " " << u |
---|
| 174 | << G4endl; |
---|
| 175 | } |
---|
| 176 | bins[i] = nb; |
---|
| 177 | xmin[i] = x1; |
---|
| 178 | xmax[i] = x2; |
---|
| 179 | unit[i] = u; |
---|
| 180 | } else { |
---|
| 181 | G4cout << "Histo::setHisto1D: WARNING! wrong histogram index " << i << G4endl; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 186 | |
---|
| 187 | void Histo::fill(G4int i, G4double x, G4double w) |
---|
| 188 | { |
---|
| 189 | if(verbose > 1) { |
---|
| 190 | G4cout << "fill histogram: #" << i << " at x= " << x |
---|
| 191 | << " weight= " << w |
---|
| 192 | << G4endl; |
---|
| 193 | } |
---|
| 194 | #ifdef G4ANALYSIS_USE |
---|
| 195 | if(!tree) return; |
---|
| 196 | if(i>=0 && i<nHisto) { |
---|
| 197 | if(active[i]) histo[i]->fill((x/unit[i]), w); |
---|
| 198 | } else { |
---|
| 199 | G4cout << "Histo::fill: WARNING! wrong histogram index " << i << G4endl; |
---|
| 200 | } |
---|
| 201 | #endif |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 205 | |
---|
| 206 | void Histo::scale(G4int i, G4double x) |
---|
| 207 | { |
---|
| 208 | if(verbose > 0) { |
---|
| 209 | G4cout << "Scale histogram: #" << i << " by factor " << x << G4endl; |
---|
| 210 | } |
---|
| 211 | #ifdef G4ANALYSIS_USE |
---|
| 212 | if(!tree) return; |
---|
| 213 | if(i>=0 && i<nHisto) { |
---|
| 214 | histo[i]->scale(x); |
---|
| 215 | } else { |
---|
| 216 | G4cout << "Histo::scale: WARNING! wrong histogram index " << i << G4endl; |
---|
| 217 | } |
---|
| 218 | #endif |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 222 | |
---|
| 223 | void Histo::addTuple(const G4String& w1, const G4String& w2, const G4String& w3) |
---|
| 224 | { |
---|
| 225 | tupleId = w1; |
---|
| 226 | tupleName = w2; |
---|
| 227 | tupleList = w3; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 231 | |
---|
| 232 | void Histo::fillTuple(const G4String& parname, G4double x) |
---|
| 233 | { |
---|
| 234 | if(verbose > 1) { |
---|
| 235 | G4cout << "fill tuple by parameter <" << parname << "> = " << x << G4endl; |
---|
| 236 | } |
---|
| 237 | #ifdef G4ANALYSIS_USE |
---|
| 238 | if(ntup) ntup->fill(ntup->findColumn(parname), (float)x); |
---|
| 239 | #endif |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 243 | |
---|
| 244 | void Histo::addRow() |
---|
| 245 | { |
---|
| 246 | #ifdef G4ANALYSIS_USE |
---|
| 247 | if(ntup) ntup->addRow(); |
---|
| 248 | #endif |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 252 | |
---|
| 253 | void Histo::setFileName(const G4String& nam) |
---|
| 254 | { |
---|
| 255 | histName = nam; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 259 | |
---|
| 260 | void Histo::setFileType(const G4String& nam) |
---|
| 261 | { |
---|
| 262 | if(nam == "root" || nam == "hbook" || nam == "aida") histType = nam; |
---|
| 263 | else if(nam == "xml" || nam == "XML") histType = "aida"; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo.... |
---|
| 267 | |
---|