Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:20:41

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file analysis/AnaEx01/src/HistoManager.cc
0027 /// \brief Implementation of the HistoManager class
0028 //
0029 //
0030 //
0031 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0032 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0033 
0034 #include "HistoManager.hh"
0035 
0036 #include "G4SystemOfUnits.hh"
0037 #include "G4UnitsTable.hh"
0038 
0039 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0040 
0041 HistoManager::HistoManager()
0042 {
0043   // Create or get analysis manager
0044   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0045   analysisManager->SetDefaultFileType("root");
0046   // the default file type can be overriden in run macro
0047 }
0048 
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0050 
0051 void HistoManager::Book()
0052 {
0053   // Create or get analysis manager
0054   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0055 
0056   if (!fFactoryOn) {
0057     //
0058     analysisManager->SetVerboseLevel(1);
0059     // Only merge in MT mode to avoid warning when running in Sequential mode
0060 #ifdef G4MULTITHREADED
0061     analysisManager->SetNtupleMerging(true);
0062 #endif
0063 
0064     // Create directories
0065     analysisManager->SetHistoDirectoryName("histo");
0066     analysisManager->SetNtupleDirectoryName("ntuple");
0067   }
0068 
0069   // Open an output file
0070   //
0071   G4bool fileOpen = analysisManager->OpenFile("AnaEx01");
0072   if (!fileOpen) {
0073     G4cerr << "\n---> HistoManager::Book(): cannot open " << analysisManager->GetFileName()
0074            << G4endl;
0075     return;
0076   }
0077 
0078   if (!fFactoryOn) {
0079     // Create histograms.
0080     // Histogram ids are generated automatically starting from 0.
0081     // The start value can be changed by:
0082     // analysisManager->SetFirstHistoId(1);
0083 
0084     // id = 0
0085     analysisManager->CreateH1("EAbs", "Edep in absorber (MeV)", 100, 0., 800 * MeV);
0086     // id = 1
0087     analysisManager->CreateH1("EGap", "Edep in gap (MeV)", 100, 0., 100 * MeV);
0088     // id = 2
0089     analysisManager->CreateH1("LAbs", "trackL in absorber (mm)", 100, 0., 1 * m);
0090     // id = 3
0091     analysisManager->CreateH1("LGap", "trackL in gap (mm)", 100, 0., 50 * cm);
0092 
0093     // Create ntuples.
0094     // Ntuples ids are generated automatically starting from 0.
0095     // The start value can be changed by:
0096     // analysisManager->SetFirstMtupleId(1);
0097 
0098     // Create 1st ntuple (id = 0)
0099     analysisManager->CreateNtuple("Ntuple1", "Edep");
0100     analysisManager->CreateNtupleDColumn("Eabs");  // column Id = 0
0101     analysisManager->CreateNtupleDColumn("Egap");  // column Id = 1
0102     analysisManager->FinishNtuple();
0103 
0104     // Create 2nd ntuple (id = 1)
0105     //
0106     analysisManager->CreateNtuple("Ntuple2", "TrackL");
0107     analysisManager->CreateNtupleDColumn("Labs");  // column Id = 0
0108     analysisManager->CreateNtupleDColumn("Lgap");  // column Id = 1
0109     analysisManager->FinishNtuple();
0110 
0111     fFactoryOn = true;
0112   }
0113 
0114   G4cout << "\n----> Output file is open in " << analysisManager->GetFileName() << "."
0115          << analysisManager->GetFileType() << G4endl;
0116 }
0117 
0118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0119 
0120 void HistoManager::Save()
0121 {
0122   if (!fFactoryOn) {
0123     return;
0124   }
0125 
0126   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0127   analysisManager->Write();
0128   analysisManager->CloseFile();
0129 
0130   G4cout << "\n----> Histograms and ntuples are saved\n" << G4endl;
0131 }
0132 
0133 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0134 
0135 void HistoManager::FillHisto(G4int ih, G4double xbin, G4double weight)
0136 {
0137   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0138   analysisManager->FillH1(ih, xbin, weight);
0139 }
0140 
0141 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0142 
0143 void HistoManager::Normalize(G4int ih, G4double fac)
0144 {
0145   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0146   auto h1 = analysisManager->GetH1(ih);
0147   if (h1 != nullptr) {
0148     h1->scale(fac);
0149   }
0150 }
0151 
0152 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0153 
0154 void HistoManager::FillNtuple(G4double energyAbs, G4double energyGap, G4double trackLAbs,
0155                               G4double trackLGap)
0156 {
0157   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0158   // Fill 1st ntuple ( id = 0)
0159   analysisManager->FillNtupleDColumn(0, 0, energyAbs);
0160   analysisManager->FillNtupleDColumn(0, 1, energyGap);
0161   analysisManager->AddNtupleRow(0);
0162   // Fill 2nd ntuple ( id = 1)
0163   analysisManager->FillNtupleDColumn(1, 0, trackLAbs);
0164   analysisManager->FillNtupleDColumn(1, 1, trackLGap);
0165   analysisManager->AddNtupleRow(1);
0166 }
0167 
0168 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0169 
0170 void HistoManager::PrintStatistic()
0171 {
0172   if (!fFactoryOn) {
0173     return;
0174   }
0175 
0176   G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
0177 
0178   G4cout << "\n ----> print histograms statistic \n" << G4endl;
0179   for (G4int i = 0; i < analysisManager->GetNofH1s(); ++i) {
0180     G4String name = analysisManager->GetH1Name(i);
0181     auto h1 = analysisManager->GetH1(i);
0182 
0183     G4String unitCategory;
0184     if (name[0U] == 'E') {
0185       unitCategory = "Energy";
0186     }
0187     if (name[0U] == 'L') {
0188       unitCategory = "Length";
0189     }
0190     // we use an explicit unsigned int type for operator [] argument
0191     // to avoid problems with windows compiler
0192 
0193     G4cout << name << ": mean = " << G4BestUnit(h1->mean(), unitCategory)
0194            << " rms = " << G4BestUnit(h1->rms(), unitCategory) << G4endl;
0195   }
0196 }
0197 
0198 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......