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 electromagnetic/TestEm1/src/HistoMan << 26 // $Id$ 27 /// \brief Implementation of the HistoManager << 28 // << 29 // 27 // 30 //....oooOO0OOooo........oooOO0OOooo........oo 28 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 31 //....oooOO0OOooo........oooOO0OOooo........oo 29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 32 30 33 #include "HistoManager.hh" 31 #include "HistoManager.hh" >> 32 #include "HistoMessenger.hh" 34 #include "G4UnitsTable.hh" 33 #include "G4UnitsTable.hh" 35 34 >> 35 #ifdef G4ANALYSIS_USE >> 36 #include "AIDA/AIDA.h" >> 37 #endif >> 38 36 //....oooOO0OOooo........oooOO0OOooo........oo 39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 37 40 38 HistoManager::HistoManager() 41 HistoManager::HistoManager() 39 : fFileName("amsEcal") << 42 :factoryOn(false),af(0),tree(0) 40 { 43 { 41 Book(); << 44 #ifdef G4ANALYSIS_USE >> 45 // Creating the analysis factory >> 46 af = AIDA_createAnalysisFactory(); >> 47 if(!af) { >> 48 G4cout << " HistoManager::HistoManager :" >> 49 << " problem creating the AIDA analysis factory." >> 50 << G4endl; >> 51 } >> 52 #endif >> 53 >> 54 fileName[0] = "ecal"; >> 55 fileType = "root"; >> 56 fileOption = "export=root"; >> 57 >> 58 // histograms >> 59 for (G4int k=0; k<MaxHisto; k++) { >> 60 histo[k] = 0; >> 61 exist[k] = false; >> 62 Unit[k] = 1.0; >> 63 Width[k] = 1.0; >> 64 ascii[k] = false; >> 65 } >> 66 >> 67 // ntuples >> 68 for (G4int k=0; k<MaxNtupl; k++) { >> 69 ntupl[k] = 0; >> 70 existNt[k] = false; >> 71 } >> 72 >> 73 histoMessenger = new HistoMessenger(this); 42 } 74 } 43 75 44 //....oooOO0OOooo........oooOO0OOooo........oo 76 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 45 77 46 HistoManager::~HistoManager() 78 HistoManager::~HistoManager() >> 79 { >> 80 delete histoMessenger; >> 81 >> 82 #ifdef G4ANALYSIS_USE >> 83 delete af; >> 84 #endif >> 85 } >> 86 >> 87 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 88 >> 89 void HistoManager::book() >> 90 { >> 91 #ifdef G4ANALYSIS_USE >> 92 if(!af) return; >> 93 >> 94 // Creating a tree mapped to an histogram file. >> 95 fileName[1] = fileName[0] + "." + fileType; >> 96 G4bool readOnly = false; >> 97 G4bool createNew = true; >> 98 AIDA::ITreeFactory* tf = af->createTreeFactory(); >> 99 tree = tf->create(fileName[1], fileType, readOnly, createNew, fileOption); >> 100 delete tf; >> 101 if(!tree) { >> 102 G4cout << " HistoManager::book :" >> 103 << " problem creating the AIDA tree with " >> 104 << " storeName = " << fileName[1] >> 105 << " storeType = " << fileType >> 106 << " readOnly = " << readOnly >> 107 << " createNew = " << createNew >> 108 << " options = " << fileOption >> 109 << G4endl; >> 110 return; >> 111 } >> 112 >> 113 // Creating a histogram factory, whose histograms will be handled by the tree >> 114 AIDA::IHistogramFactory* hf = af->createHistogramFactory(*tree); >> 115 >> 116 // create selected histograms >> 117 for (G4int k=1; k<MaxHisto; k++) { >> 118 if (exist[k]) { >> 119 histo[k] = hf->createHistogram1D( Label[k], Title[k], >> 120 Nbins[k], Vmin[k], Vmax[k]); >> 121 factoryOn = true; >> 122 } >> 123 } >> 124 delete hf; >> 125 >> 126 // Creating a ntuple factory, handled by the tree >> 127 AIDA::ITupleFactory* ntf = af->createTupleFactory(*tree); >> 128 >> 129 // create selected ntuples >> 130 for (G4int k=1; k<MaxNtupl; k++) { >> 131 if (existNt[k]) { >> 132 ntupl[k] = ntf->create( LabelNt[k], TitleNt[k], ColumnNt[k]); >> 133 factoryOn = true; >> 134 } >> 135 } >> 136 >> 137 delete ntf; >> 138 >> 139 if (factoryOn) >> 140 G4cout << "\n----> Histogram Tree is opened in " << fileName[1] << G4endl; >> 141 #endif >> 142 } >> 143 >> 144 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 145 >> 146 void HistoManager::save() >> 147 { >> 148 #ifdef G4ANALYSIS_USE >> 149 if (factoryOn) { >> 150 saveAscii(); // Write ascii file, if any >> 151 tree->commit(); // Writing the histograms to the file >> 152 tree->close(); // and closing the tree (and the file) >> 153 G4cout << "\n----> Histogram Tree is saved in " << fileName[1] << G4endl; >> 154 >> 155 delete tree; >> 156 tree = 0; >> 157 factoryOn = false; >> 158 } >> 159 #endif >> 160 } >> 161 >> 162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 163 >> 164 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight) >> 165 { >> 166 if (ih >= MaxHisto) { >> 167 G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih >> 168 << " xbin= " << xbin << " weight= " << weight << G4endl; >> 169 return; >> 170 } >> 171 #ifdef G4ANALYSIS_USE >> 172 if(exist[ih]) histo[ih]->fill(xbin/Unit[ih], weight); >> 173 #endif >> 174 } >> 175 >> 176 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 177 >> 178 void HistoManager::SetHisto(G4int ih, >> 179 G4int nbins, G4double valmin, G4double valmax, const G4String& unit) >> 180 { >> 181 if (ih < 1 || ih >= MaxHisto) { >> 182 G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih >> 183 << "does not exist" << G4endl; >> 184 return; >> 185 } >> 186 >> 187 const G4String id[] = { "0", "1", "2", "3", "4", "5" }; >> 188 const G4String title[] = >> 189 { "dummy", //0 >> 190 "total Evis in Ecal", //1 >> 191 "total Edep in Ecal", //2 >> 192 "Evis profile", //3 >> 193 "Edep profile", //4 >> 194 "Nb of Radiation Length" //5 >> 195 }; >> 196 >> 197 G4String titl = title[ih]; >> 198 G4double vmin = valmin, vmax = valmax; >> 199 Unit[ih] = 1.; >> 200 >> 201 if (unit != "none") { >> 202 titl = title[ih] + " (" + unit + ")"; >> 203 Unit[ih] = G4UnitDefinition::GetValueOf(unit); >> 204 vmin = valmin/Unit[ih]; vmax = valmax/Unit[ih]; >> 205 } >> 206 >> 207 exist[ih] = true; >> 208 Label[ih] = id[ih]; >> 209 Title[ih] = titl; >> 210 Nbins[ih] = nbins; >> 211 Vmin[ih] = vmin; >> 212 Vmax[ih] = vmax; >> 213 Width[ih] = (valmax-valmin)/nbins; >> 214 >> 215 G4cout << "----> SetHisto " << ih << ": " << titl << "; " >> 216 << nbins << " bins from " >> 217 << vmin << " " << unit << " to " << vmax << " " << unit << G4endl; >> 218 >> 219 } >> 220 >> 221 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 222 >> 223 void HistoManager::Normalize(G4int ih, G4double fac) 47 { 224 { >> 225 if (ih >= MaxHisto) { >> 226 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih >> 227 << " fac= " << fac << G4endl; >> 228 return; >> 229 } >> 230 #ifdef G4ANALYSIS_USE >> 231 if(exist[ih]) histo[ih]->scale(fac); >> 232 #endif >> 233 } >> 234 >> 235 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 236 >> 237 void HistoManager::RemoveHisto(G4int ih) >> 238 { >> 239 if (ih >= MaxHisto) { >> 240 G4cout << "---> warning from HistoManager::RemoveHisto() : histo " << ih >> 241 << "does not exist" << G4endl; >> 242 return; >> 243 } >> 244 >> 245 histo[ih] = 0; exist[ih] = false; >> 246 } >> 247 >> 248 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 249 >> 250 void HistoManager::PrintHisto(G4int ih) >> 251 { >> 252 if (ih < MaxHisto) { ascii[ih] = true; ascii[0] = true; } >> 253 else >> 254 G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih >> 255 << "does not exist" << G4endl; 48 } 256 } 49 257 50 //....oooOO0OOooo........oooOO0OOooo........oo 258 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 51 259 52 void HistoManager::Book() << 260 #include <fstream> >> 261 >> 262 void HistoManager::saveAscii() 53 { 263 { 54 // Create or get analysis manager << 264 #ifdef G4ANALYSIS_USE 55 // The choice of analysis technology is done << 265 56 // in HistoManager.hh << 266 if (!ascii[0]) return; 57 G4AnalysisManager* analysisManager = G4Analy << 267 58 analysisManager->SetDefaultFileType("root"); << 268 G4String name = fileName[0] + ".ascii"; 59 analysisManager->SetFileName(fFileName); << 269 std::ofstream File(name, std::ios::out); 60 analysisManager->SetVerboseLevel(1); << 270 File.setf( std::ios::scientific, std::ios::floatfield ); 61 analysisManager->SetActivation(true); // << 271 62 << 272 //write selected histograms 63 // Define histograms start values << 273 for (G4int ih=0; ih<MaxHisto; ih++) { 64 const G4int kMaxHisto = 6; << 274 if (exist[ih] && ascii[ih]) { 65 const G4String id[] = {"0", "1", "2", "3" , << 275 File << "\n 1D histogram " << ih << ": " << Title[ih] >> 276 << "\n \n \t X \t\t Y" << G4endl; >> 277 >> 278 for (G4int iBin=0; iBin<Nbins[ih]; iBin++) { >> 279 File << " " << iBin << "\t" >> 280 << 0.5*(histo[ih]->axis().binLowerEdge(iBin) + >> 281 histo[ih]->axis().binUpperEdge(iBin)) << "\t" >> 282 << histo[ih]->binHeight(iBin) >> 283 << G4endl; >> 284 } >> 285 } >> 286 } >> 287 #endif >> 288 } >> 289 >> 290 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 291 >> 292 void HistoManager::SetNtuple(G4int nt) >> 293 { >> 294 if (nt < 1 || nt >= MaxNtupl) { >> 295 G4cout << "---> warning from HistoManager::SetNtuple() : Ntuple " << nt >> 296 << "does not exist" << G4endl; >> 297 return; >> 298 } >> 299 >> 300 const G4String id[] = { "100", "101" }; 66 const G4String title[] = 301 const G4String title[] = 67 { "dummy", << 302 { "dummy", //0 68 "total Etot in Ecal", //1 << 303 "Energy deposit in subModule" //1 69 "total Evis in Ecal", << 70 "Etot profile", << 71 "Evis profile", << 72 "Evis per fiber" << 73 }; 304 }; 74 << 305 75 // Default values (to be reset via /analysis << 306 G4String column[MaxNtupl]; 76 G4int nbins = 100; << 307 77 G4double vmin = 0.; << 308 column[0] = " int dum=0 "; //0 78 G4double vmax = 100.; << 309 79 << 310 column[1] = 80 // Create all histograms as inactivated << 311 "double Evis0, Evis1, Evis2, Evis3, Evis4, Evis5, Evis6, Evis7, Evis8, Evis9"; 81 // as we have not yet set nbins, vmin, vmax << 312 column[1] += 82 for (G4int k=0; k<kMaxHisto; k++) { << 313 ", Evis10, Evis11, Evis12, Evis13, Evis14, Evis15, Evis16, Evis17"; 83 G4int ih = analysisManager->CreateH1(id[k] << 314 column[1] += 84 analysisManager->SetH1Activation(ih, false << 315 ", Edep0, Edep1, Edep2, Edep3, Edep4, Edep5, Edep6, Edep7, Edep8, Edep9"; >> 316 column[1] += >> 317 ", Edep10, Edep11, Edep12, Edep13, Edep14, Edep15, Edep16, Edep17"; >> 318 >> 319 G4String titl = title[nt]; >> 320 >> 321 existNt[nt] = true; >> 322 LabelNt[nt] = id[nt]; >> 323 TitleNt[nt] = titl; >> 324 ColumnNt[nt] = column[nt]; >> 325 >> 326 G4cout << "----> SetNtuple " << nt << ": " << titl << "; " << G4endl; >> 327 >> 328 } >> 329 >> 330 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 331 >> 332 void HistoManager::FillNtuple(G4int nt, G4int column, G4double value) >> 333 { >> 334 if (nt >= MaxNtupl) { >> 335 G4cout << "---> warning from HistoManager::FillNtuple() : Ntuple " << nt >> 336 << " does not exist " << column << value << G4endl; >> 337 return; 85 } 338 } >> 339 #ifdef G4ANALYSIS_USE >> 340 if(existNt[nt]) ntupl[nt]->fill(column, value); >> 341 #endif 86 } 342 } 87 343 88 //....oooOO0OOooo........oooOO0OOooo........oo 344 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 345 >> 346 void HistoManager::AddRowNtuple(G4int nt) >> 347 { >> 348 if (nt >= MaxNtupl) { >> 349 G4cout << "---> warning from HistoManager::AddRowNtuple() : Ntuple " << nt >> 350 << " do not exist" << G4endl; >> 351 return; >> 352 } >> 353 #ifdef G4ANALYSIS_USE >> 354 if(existNt[nt]) ntupl[nt]->addRow(); >> 355 #endif >> 356 } >> 357 >> 358 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 359 >> 360 89 361