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