// $Id: BaseRunAction.cc 8495 2017-05-16 22:59:21Z d3m222@PNL.GOV $ // // Base class to define actions to be taken at beginning and end of a run (/run/beamOn) //#include "BaseAnalysis.hh" #include "BaseRunAction.hh" #include "G4AnalysisManager.hh" #include "Utils.hh" using namespace std; // Verbosity // 2: Initial particle info // 4: Detailed step information // 8: Energy Deposit Info // 16: write thrown particle tree // 32: Write method enter/leave comments //------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------- BaseRunAction::BaseRunAction() { // HistoManager=NULL; } //------------------------------------------------------------------------------------------------- /*** BaseRunAction::BaseRunAction(BaseHistoManager* histmanager, BasePrimaryGeneratorAction* pgen, BaseEventAction* pevent, BaseSteppingAction* pstep) : pGen(pgen), pEvent(pevent), pStepping(pstep), HistoManager(histmanager) { SetVerbosity(0); SetMilestone(10000); TreeName = pGen->GetAppName() + "Tree"; pMessenger = new BaseRunMessenger(this); } ***/ //------------------------------------------------------------------------------------------------- BaseRunAction::BaseRunAction(BasePrimaryGeneratorAction* pgen, BaseEventAction* pevent, BaseSteppingAction* pstep, G4int chns) : pGen(pgen), pEvent(pevent), pStepping(pstep), NumChannels(chns) { TotalEvents = 0; BatchMode = false; SetVerbosity(0); SetMilestone(10000); SetApplicationName(pGen->GetAppName()); SetIterationNumber(pGen->GetIterationNumber()); SetShortDescription(pGen->GetSourceLabel()); TreeName = ApplicationName + "Tree"; //TreeName = pGen->GetAppName() + "Tree"; pMessenger = new BaseRunMessenger(this); } //------------------------------------------------------------------------------------------------- BaseRunAction::~BaseRunAction() { delete G4AnalysisManager::Instance(); } //------------------------------------------------------------------------------------------------- void BaseRunAction::DefineAnalysis() { if (!WriteTree && !WriteHists) { G4cout << endl; G4cout << "Will not define analysis manager. No trees or histograms." << G4endl; G4cout << endl; return; } // Create analysis manager // The choice of analysis technology is done via selectin of a namespace // in Analysis.hh auto AnalysisManager = G4AnalysisManager::Instance(); G4cout << endl; G4cout << "Using " << AnalysisManager->GetType() << " analysis manager" << G4endl; G4cout << endl; SetOutputFilename(pGen->GetAppName(), pGen->GetIterationNumber(), pGen->GetSourceLabel(), AnalysisManager->GetType()); // Default settings AnalysisManager->SetVerboseLevel(1); AnalysisManager->SetNtupleMerging(true); if (WriteHists) DefineHistograms(); if (WriteTree) DefineTree(); AnalysisManager->OpenFile(OutputFilename); } //------------------------------------------------------------------------------------------------- void BaseRunAction::DefineHistograms() { auto AnalysisManager = G4AnalysisManager::Instance(); // Create 1D histograms for each channel AnalysisManager->CreateH1("hEInit","Thrown Energy",6000,0,6000); for (G4int i=0; iCreateH1(HistName,HistTitle,12000,0,6000); AnalysisManager->SetH1XAxisTitle(i,"Energy [keV]"); AnalysisManager->SetH1YAxisTitle(i,"Counts"); } } //------------------------------------------------------------------------------------------------- void BaseRunAction::DefineTree() { auto AnalysisManager = G4AnalysisManager::Instance(); // Creating ntuple AnalysisManager->CreateNtuple(TreeName,"G4 Results"); AnalysisManager->CreateNtupleIColumn("ID"); // column Id = 0 AnalysisManager->CreateNtupleFColumn("x0"); // column Id = 1 AnalysisManager->CreateNtupleFColumn("y0"); // column Id = 2 AnalysisManager->CreateNtupleFColumn("z0"); // column Id = 3 if (NumChannels>1) { AnalysisManager->CreateNtupleIColumn("nHits"); // column Id = 4 AnalysisManager->CreateNtupleFColumn("Etot"); // column Id = 5 } for (G4int i=0; iCreateNtupleFColumn(Label); } AnalysisManager->FinishNtuple(); } //------------------------------------------------------------------------------------------------- void BaseRunAction::BeginOfRunAction(const G4Run* aRun) { if (PrintDebugInfo) G4cout << "Enter BaseRunAction:BeginOfRunAction" << G4endl; pEvent->SetRunID(aRun->GetRunID()); pEvent->SetBatchMode(BatchMode); if (BatchMode && aRun->GetRunID()>0) return; // The type of source generator can be set through a macro, with the // information stored in the PrimaryGeneratorAction. At beginning // of run, copy to the SteppingAction G4cout << endl; G4cout << "==================================================================" << G4endl; G4cout << "### Starting Run " << aRun->GetRunID() << G4endl; G4cout << "==================================================================" << G4endl; DefineAnalysis(); pStepping->SetSrcGenerator(pGen->GetSrcGenerator()); pEvent ->SetSrcGenerator(pGen->GetSrcGenerator()); pEvent ->SetWriteThrownParticleInfo(WriteThrownParticleInfo); pEvent ->SetWriteTree(WriteTree); pEvent ->SetWriteHists(WriteHists); //if (WriteHists || WriteTree) { // G4AnalysisManager* AnalysisManager = G4AnalysisManager::Instance(); // AnalysisManager->OpenFile(OutputFilename); //} if (PrintDebugInfo) G4cout << "Leave BaseRunAction:BeginOfRunAction" << G4endl; } //------------------------------------------------------------------------------------------------- void BaseRunAction::EndOfRunAction(const G4Run* aRun) { G4int NumEvents = aRun->GetNumberOfEvent(); TotalEvents += NumEvents; if (NumEvents == 0) return; //if (HistoManager) HistoManager->Save(); G4cout << G4endl; G4cout << "======================== run summary ======================"; G4cout << G4endl; G4cout.precision(5); G4cout << "Number of events this run: " << NumEvents << G4endl; G4cout << "#Singles Hits Det 1: " << pEvent->GetNumSingleHitsDet1() << endl; G4cout << "#Singles Hits Det 2: " << pEvent->GetNumSingleHitsDet2() << endl; G4cout << "#Coin Hits: " << pEvent->GetNumCoinHits() << endl; if (BatchMode) G4cout << "Total number of events: " << TotalEvents << G4endl; G4cout << G4endl; if ((WriteTree || WriteHists) && !BatchMode) { auto AnalysisManager = G4AnalysisManager::Instance(); AnalysisManager->Write(); AnalysisManager->CloseFile(); G4cout << endl; } } //------------------------------------------------------------------------------------------------- void BaseRunAction::WriteFile() { auto AnalysisManager = G4AnalysisManager::Instance(); AnalysisManager->Write(); AnalysisManager->CloseFile(); } //------------------------------------------------------------------------------------------------- void BaseRunAction::SetVerbosity(G4int val) { Verbosity = val; PrintDebugInfo = val & 32; pGen->SetVerbosity(val); pEvent->SetVerbosity(val); pStepping->SetVerbosity(val); } //------------------------------------------------------------------------------------------------- void BaseRunAction::SetVerbosity(G4int bit, bool val ) { G4int NewVerbosity = Verbosity; G4int BitValue = pow(2.,bit); NewVerbosity += -(Verbosity & BitValue) + val*BitValue; SetVerbosity(NewVerbosity); } //------------------------------------------------------------------------------------------------- void BaseRunAction::SetMilestone(G4int val) { pGen->SetMilestone(val); } //------------------------------------------------------------------------------------------------- void BaseRunAction::SetWriteThrownParticleInfo(G4bool val) { WriteThrownParticleInfo = val; pGen -> SetWriteThrownParticleInfo(val); pEvent -> SetWriteThrownParticleInfo(val); pStepping -> SetWriteThrownParticleInfo(val); } //------------------------------------------------------------------------------------------------- void BaseRunAction::SetOutputFilename(G4String AppName, G4int IterNum, G4String SourceLabel, G4String AnlType) { OutputFilename = "data/" + AppName + "." + IntToString(IterNum) + "." + SourceLabel; if (AnlType == "Root") { OutputFilename += ".root"; } else { OutputFilename += ".dat"; } } //-------------------------------------------------------------------------------------------------