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/TestEm17/src/HistoMa 26 /// \file electromagnetic/TestEm17/src/HistoManager.cc 27 /// \brief Implementation of the HistoManager 27 /// \brief Implementation of the HistoManager class 28 // 28 // 29 // 29 // 30 // << 30 // $Id$ 31 //....oooOO0OOooo........oooOO0OOooo........oo << 31 // 32 //....oooOO0OOooo........oooOO0OOooo........oo 32 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 33 34 34 #include "HistoManager.hh" 35 #include "HistoManager.hh" 35 << 36 #include "HistoMessenger.hh" 36 #include "HistoMessenger.hh" 37 << 38 #include "G4PhysicalConstants.hh" << 39 #include "G4SystemOfUnits.hh" << 40 #include "G4UnitsTable.hh" 37 #include "G4UnitsTable.hh" 41 38 42 //....oooOO0OOooo........oooOO0OOooo........oo 39 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 43 40 44 HistoManager::HistoManager() : fHistoMessenger << 41 HistoManager::HistoManager() 45 { 42 { 46 fileName[0] = "testem17"; << 43 fileName[0] = "testem17"; 47 factoryOn = false; 44 factoryOn = false; 48 fNbHist = 0; << 45 fNbHist = 0; 49 46 50 // histograms 47 // histograms 51 for (G4int k = 0; k < kMaxHisto; k++) { << 48 for (G4int k=0; k<MaxHisto; k++) { 52 fHistId[k] = 0; 49 fHistId[k] = 0; 53 fHistPt[k] = 0; << 50 fHistPt[k] = 0; 54 fExist[k] = false; 51 fExist[k] = false; 55 fUnit[k] = 1.0; << 52 fUnit[k] = 1.0; 56 fWidth[k] = 1.0; 53 fWidth[k] = 1.0; 57 fAscii[k] = false; << 54 fAscii[k] = false; 58 } 55 } 59 56 60 fHistoMessenger = new HistoMessenger(this); 57 fHistoMessenger = new HistoMessenger(this); 61 } 58 } 62 59 63 //....oooOO0OOooo........oooOO0OOooo........oo 60 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 64 61 65 HistoManager::~HistoManager() 62 HistoManager::~HistoManager() 66 { 63 { 67 delete fHistoMessenger; 64 delete fHistoMessenger; 68 } 65 } 69 66 70 //....oooOO0OOooo........oooOO0OOooo........oo 67 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 71 68 72 void HistoManager::Book() << 69 void HistoManager::book() 73 { 70 { 74 // if no histos, do nothing 71 // if no histos, do nothing 75 if (fNbHist == 0) return; 72 if (fNbHist == 0) return; 76 << 73 77 // Create or get analysis manager 74 // Create or get analysis manager 78 // The choice of analysis technology is done 75 // The choice of analysis technology is done via selection of a namespace 79 // in HistoManager.hh 76 // in HistoManager.hh 80 G4AnalysisManager* analysisManager = G4Analy 77 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 81 analysisManager->SetDefaultFileType("root"); << 82 analysisManager->SetVerboseLevel(0); 78 analysisManager->SetVerboseLevel(0); 83 G4String extension = analysisManager->GetFil 79 G4String extension = analysisManager->GetFileType(); 84 fileName[1] = fileName[0] + "." + extension; 80 fileName[1] = fileName[0] + "." + extension; 85 << 81 86 // Open an output file 82 // Open an output file 87 // 83 // 88 G4bool fileOpen = analysisManager->OpenFile( 84 G4bool fileOpen = analysisManager->OpenFile(fileName[0]); 89 if (!fileOpen) { 85 if (!fileOpen) { 90 G4cout << "\n---> HistoManager::book(): ca << 86 G4cout << "\n---> HistoManager::book(): cannot open " << fileName[1] >> 87 << G4endl; 91 return; 88 return; 92 } << 89 } 93 90 94 // create selected histograms 91 // create selected histograms 95 // 92 // 96 analysisManager->SetFirstHistoId(1); 93 analysisManager->SetFirstHistoId(1); 97 << 94 98 for (G4int k = 0; k < kMaxHisto; k++) { << 95 for (G4int k=0; k<MaxHisto; k++) { 99 if (fExist[k]) { 96 if (fExist[k]) { 100 fHistId[k] = analysisManager->CreateH1(f << 97 fHistId[k] = analysisManager->CreateH1( fLabel[k], fTitle[k], >> 98 fNbins[k], fVmin[k], fVmax[k]); 101 fHistPt[k] = analysisManager->GetH1(fHis 99 fHistPt[k] = analysisManager->GetH1(fHistId[k]); 102 factoryOn = true; 100 factoryOn = true; 103 } 101 } 104 } 102 } 105 103 106 if (factoryOn) G4cout << "\n----> Histogram << 104 if (factoryOn) >> 105 G4cout << "\n----> Histogram file is opened in " << fileName[1] << G4endl; 107 } 106 } 108 107 109 //....oooOO0OOooo........oooOO0OOooo........oo 108 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 110 109 111 void HistoManager::Save() << 110 void HistoManager::save() 112 { 111 { 113 if (factoryOn) { 112 if (factoryOn) { 114 G4cout << "\n----> HistoManager::save " << << 113 G4AnalysisManager* analysisManager = G4AnalysisManager::Instance(); 115 G4AnalysisManager* analysisManager = G4Ana << 116 analysisManager->Write(); 114 analysisManager->Write(); 117 analysisManager->CloseFile(); 115 analysisManager->CloseFile(); 118 SaveAscii(); // Write fAscii file, if any << 116 saveAscii(); // Write fAscii file, if any 119 G4cout << "\n----> Histograms are saved in 117 G4cout << "\n----> Histograms are saved in " << fileName[1] << G4endl; 120 << 118 121 analysisManager->Clear(); << 119 delete G4AnalysisManager::Instance(); 122 factoryOn = false; 120 factoryOn = false; 123 } << 121 } 124 } 122 } 125 123 126 //....oooOO0OOooo........oooOO0OOooo........oo 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 127 125 128 void HistoManager::FillHisto(G4int ih, G4doubl 126 void HistoManager::FillHisto(G4int ih, G4double e, G4double weight) 129 { 127 { 130 if (ih > kMaxHisto) { << 128 if (ih > MaxHisto) { 131 G4cout << "---> warning from HistoManager: 129 G4cout << "---> warning from HistoManager::FillHisto() : histo " << ih 132 << "does not fExist; e= " << e << " 130 << "does not fExist; e= " << e << " w= " << weight << G4endl; 133 return; 131 return; 134 } 132 } 135 133 136 if (fHistPt[ih]) fHistPt[ih]->fill(e / fUnit << 134 if (fHistPt[ih]) fHistPt[ih]->fill(e/fUnit[ih], weight); 137 } 135 } 138 136 139 //....oooOO0OOooo........oooOO0OOooo........oo 137 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 140 138 141 void HistoManager::SetHisto(G4int ih, G4int nb << 139 void HistoManager::SetHisto(G4int ih, 142 const G4String& un << 140 G4int nbins, G4double vmin, G4double vmax, const G4String& unit) 143 { 141 { 144 if (ih > kMaxHisto) { << 142 if (ih > MaxHisto) { 145 G4cout << "---> warning from HistoManager: << 143 G4cout << "---> warning from HistoManager::SetHisto() : histo " << ih 146 << G4endl; << 144 << "does not fExist" << G4endl; 147 return; 145 return; 148 } 146 } 149 << 147 150 const G4String id[] = {"0", "1", "2", "3" << 148 const G4String id[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 151 "10", "11", "12", "13 << 149 const G4String title[] = 152 "20", "21", "22", "23 << 150 { "dummy", //0 153 const G4String title[] = { << 151 "log10(Etransfert/Emu) muIonization", //1 154 "dummy", // 0 << 152 "log10(Etransfert/Emu) muPair", //2 155 "log10(Eloss/Emu) muIonization", // 1 << 153 "log10(Etransfert/Emu) muBrems", //3 156 "log10(Eloss/Emu) muPair", // 2 << 154 "log10(Etransfert/Emu) muNuclear", //4 157 "log10(Eloss/Emu) muBrems", // 3 << 155 "log10(Etransfert/Emu) hIonization", //5 158 "log10(Eloss/Emu) muNuclear", // 4 << 156 "log10(Etransfert/Emu) hPair", //6 159 "log10(Eloss/Emu) hIonization", // 5 << 157 "log10(Etransfert/Emu) hBrems", //7 160 "log10(Eloss/Emu) hPair", // 6 << 158 "dummy", //8 161 "log10(Eloss/Emu) hBrems", // 7 << 159 "dummy" //9 162 "log10(Eloss/Emu) muToMuonPair", // 8 << 160 }; 163 "dummy", // 9 << 164 "dummy", // 10 << 165 "log10(Eloss/Emu) muIonization", // 11 << 166 "log10(Eloss/Emu) muPair", // 12 << 167 "log10(Eloss/Emu) muBrems", // 13 << 168 "log10(Eloss/Emu) muNuclear", // 14 << 169 "dummy", // 15 << 170 "dummy", // 16 << 171 "dummy", // 17 << 172 "log10(Eloss/Emu) muToMuonPair", // 18 << 173 "dummy", // 19 << 174 "dummy", // 20 << 175 "CS(1/mm) vs ekin muIonisation", // 21 << 176 "CS(1/mm) vs ekin muPair", // 22 << 177 "CS(1/mm) vs ekin muBrems", // 23 << 178 "CS(1/mm) vs ekin muNuclear", // 24 << 179 "dummy", // 25 << 180 "dummy", // 26 << 181 "dummy", // 27 << 182 "CS(1/mm) vs ekin muToMuonPair", // 28 << 183 "dummy" // 29 << 184 }; << 185 161 186 G4String titl = title[ih]; 162 G4String titl = title[ih]; 187 fUnit[ih] = 1.; 163 fUnit[ih] = 1.; 188 164 189 if (unit != "none") { 165 if (unit != "none") { 190 titl = title[ih] + " (" + unit + ")"; 166 titl = title[ih] + " (" + unit + ")"; 191 fUnit[ih] = G4UnitDefinition::GetValueOf(u 167 fUnit[ih] = G4UnitDefinition::GetValueOf(unit); 192 } 168 } 193 169 194 fExist[ih] = true; 170 fExist[ih] = true; 195 fLabel[ih] = "h" + id[ih]; << 171 fLabel[ih] = id[ih]; 196 fTitle[ih] = titl; 172 fTitle[ih] = titl; 197 fNbins[ih] = nbins; 173 fNbins[ih] = nbins; 198 fVmin[ih] = vmin; << 174 fVmin[ih] = vmin; 199 fVmax[ih] = vmax; << 175 fVmax[ih] = vmax; 200 fWidth[ih] = fUnit[ih] * (vmax - vmin) / nbi << 176 fWidth[ih] = fUnit[ih]*(vmax-vmin)/nbins; 201 << 177 202 fNbHist++; 178 fNbHist++; 203 179 204 G4cout << "----> SetHisto " << ih << ": " << << 180 G4cout << "----> SetHisto " << ih << ": " << titl << "; " 205 << " " << unit << " to " << vmax << " << 181 << nbins << " bins from " >> 182 << vmin << " " << unit << " to " << vmax << " " << unit << G4endl; >> 183 206 } 184 } 207 185 208 //....oooOO0OOooo........oooOO0OOooo........oo 186 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 209 187 210 void HistoManager::Normalize(G4int ih, G4doubl 188 void HistoManager::Normalize(G4int ih, G4double fac) 211 { 189 { 212 if (ih >= kMaxHisto) { << 190 if (ih >= MaxHisto) { 213 G4cout << "---> warning from HistoManager: << 191 G4cout << "---> warning from HistoManager::Normalize() : histo " << ih 214 << G4endl; << 192 << " fac= " << fac << G4endl; 215 return; 193 return; 216 } 194 } 217 195 218 if (fHistPt[ih]) fHistPt[ih]->scale(fac); 196 if (fHistPt[ih]) fHistPt[ih]->scale(fac); 219 } 197 } 220 198 221 //....oooOO0OOooo........oooOO0OOooo........oo 199 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 222 200 223 void HistoManager::PrintHisto(G4int ih) 201 void HistoManager::PrintHisto(G4int ih) 224 { 202 { 225 if (ih < kMaxHisto) { << 203 if (ih < MaxHisto) { fAscii[ih] = true; fAscii[0] = true; } 226 fAscii[ih] = true; << 204 else 227 fAscii[0] = true; << 205 G4cout << "---> warning from HistoManager::PrintHisto() : histo " << ih 228 } << 206 << "does not exist" << G4endl; 229 else << 230 G4cout << "---> warning from HistoManager: << 231 << G4endl; << 232 } 207 } 233 208 234 //....oooOO0OOooo........oooOO0OOooo........oo 209 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 235 210 236 #include <fstream> 211 #include <fstream> 237 212 238 void HistoManager::SaveAscii() << 213 void HistoManager::saveAscii() 239 { 214 { 240 if (!fAscii[0]) return; << 215 if (!fAscii[0]) return; 241 << 216 242 G4String name = fileName[0] + ".ascii"; << 217 G4String name = fileName[0] + ".ascii"; 243 std::ofstream File(name, std::ios::out); << 218 std::ofstream File(name, std::ios::out); 244 if (!File) { << 219 if (!File) { 245 G4cout << "\n---> HistoManager::saveAscii( << 220 G4cout >> 221 << "\n---> HistoManager::saveAscii(): cannot open " << name << G4endl; 246 return; 222 return; 247 } << 223 } 248 << 249 File.setf(std::ios::scientific, std::ios::fl << 250 224 251 // write selected histograms << 225 File.setf( std::ios::scientific, std::ios::floatfield ); 252 for (G4int ih = 0; ih < kMaxHisto; ih++) { << 226 >> 227 //write selected histograms >> 228 for (G4int ih=0; ih<MaxHisto; ih++) { 253 if (fHistPt[ih] && fAscii[ih]) { 229 if (fHistPt[ih] && fAscii[ih]) { 254 File << "\n 1D histogram " << ih << ": << 230 255 << G4endl; << 231 File << "\n 1D histogram " << ih << ": " << fTitle[ih] 256 << 232 << "\n \n \t X \t\t Y" << G4endl; 257 for (G4int iBin = 0; iBin < fNbins[ih]; << 233 258 File << " " << iBin << "\t" << fHistP << 234 for (G4int iBin=0; iBin<fNbins[ih]; iBin++) { 259 << fHistPt[ih]->bin_height(iBin) << 235 File << " " << iBin << "\t" 260 } << 236 << fHistPt[ih]->axis().bin_center(iBin) << "\t" >> 237 << fHistPt[ih]->bin_height(iBin) >> 238 << G4endl; >> 239 } 261 } 240 } 262 } << 241 } 263 } 242 } 264 243 265 //....oooOO0OOooo........oooOO0OOooo........oo 244 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 245 266 246