Geant4-11
G4ToolsAnalysisManager.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
28
30#include "G4H1ToolsManager.hh"
31#include "G4H2ToolsManager.hh"
32#include "G4H3ToolsManager.hh"
33#include "G4P1ToolsManager.hh"
34#include "G4P2ToolsManager.hh"
35#include "G4PlotManager.hh"
36#include "G4MPIToolsManager.hh"
38#include "G4AutoLock.hh"
39#include "G4Threading.hh"
40
41using namespace G4Analysis;
42
43namespace {
44 //Mutex to lock master manager when merging histograms
46}
47
48//_____________________________________________________________________________
50{
51 return fgToolsInstance;
52}
53
54//_____________________________________________________________________________
56{
57 return ( fgToolsInstance != nullptr );
58}
59
60//_____________________________________________________________________________
62 : G4VAnalysisManager(type)
63{
64 // Set instance pointer
66 fgToolsInstance = this;
67
68 // Create managers
74 // The managers will be deleted by the base class
75
76 // Set managers to base class which takes then their ownership
82
83 // Plot manager
84 SetPlotManager(std::make_shared<G4PlotManager>(fState));
85
86 // Messenger
87 fMessenger = std::make_unique<G4ToolsAnalysisMessenger>(this);
88}
89
90//_____________________________________________________________________________
92{
93 if ( fState.GetIsMaster() ) fgMasterToolsInstance = nullptr;
94 fgToolsInstance = nullptr;
95}
96
97//
98// protected methods
99//
100
101//_____________________________________________________________________________
103{
104
105 // Only master thread performs plotting
106 if ( G4Threading::IsWorkerThread() ) return true;
107
108 auto result = true;
109
110 // Open output file
111 fPlotManager->OpenFile(fVFileManager->GetPlotFileName());
112
113 // H1
114 result
115 &= fPlotManager->PlotAndWrite<tools::histo::h1d>(fH1Manager->GetH1Vector(),
117
118 // H2
119 result
120 &= fPlotManager->PlotAndWrite<tools::histo::h2d>(fH2Manager->GetH2Vector(),
122
123 // H3
124 // not yet available in tools
125
126 // P1
127 result
128 &= fPlotManager->PlotAndWrite<tools::histo::p1d>(fP1Manager->GetP1Vector(),
130
131 // P2
132 // not yet available in tools
133
134 // Close output file
135 result &= fPlotManager->CloseFile();
136
137 return result;
138}
139
140//_____________________________________________________________________________
142{
143
144 // if ( G4Threading::IsWorkerThread() ) return true;
145
146 if ( ! hmpi ) return false;
147
148 auto result = true;
149
150 // Create MPI manager
151 G4MPIToolsManager mpiToolsManager(fState, hmpi);
152
153 // H1
154 result
155 &= mpiToolsManager.Merge<tools::histo::h1d>(fH1Manager->GetH1Vector(),
157
158 // H2
159 result
160 &= mpiToolsManager.Merge<tools::histo::h2d>(fH2Manager->GetH2Vector(),
162
163 // H3
164 result
165 &= mpiToolsManager.Merge<tools::histo::h3d>(fH3Manager->GetH3Vector(),
167
168 // P1
169 result
170 &= mpiToolsManager.Merge<tools::histo::p1d>(fP1Manager->GetP1Vector(),
172
173 // P2
174 result
175 &= mpiToolsManager.Merge<tools::histo::p2d>(fP2Manager->GetP2Vector(),
177
178 return result;
179}
180
181//_____________________________________________________________________________
183{
184 // Nothing to be done on worker
185 if ( G4Threading::IsWorkerThread() ) return false;
186
187 auto result = true;
188
189 // Write all histograms/profile on master
195
196 return result;
197}
198
199//_____________________________________________________________________________
201{
202// Reset histograms and profiles
203
204 auto result = true;
205
206 result &= fH1Manager->Reset();
207 result &= fH2Manager->Reset();
208 result &= fH3Manager->Reset();
209 result &= fP1Manager->Reset();
210 result &= fP2Manager->Reset();
211
212 return result;
213}
214
215//_____________________________________________________________________________
217{
218// Reset histograms and profiles
219
225}
226
227//_____________________________________________________________________________
229{
230 // Nothing to be done on master
231 if ( ! G4Threading::IsWorkerThread() ) return false;
232
233 if ( ! fgMasterToolsInstance ) {
234 if (! IsEmpty() ) {
235 Warn("No master G4AnalysisManager instance exists.\n"
236 "Histogram/profile data will not be merged.",
237 fkClass, "Merge");
238 return false;
239 }
240 return true;
241 }
242
243 Message(kVL4, "merge on worker", "histograms");
244
245 // The worker manager just adds its histograms to the master
251
252 Message(kVL3, "merge on worker", "histograms");
253
254 return true;
255}
256
257//_____________________________________________________________________________
259{
260 return fH1Manager->IsEmpty() && fH2Manager->IsEmpty() && fH3Manager->IsEmpty() &&
262}
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
bool G4bool
Definition: G4Types.hh:86
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::h1d * > & GetH1Vector() const
const std::vector< tools::histo::h2d * > & GetH2Vector() const
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::h3d * > & GetH3Vector() const
G4bool Merge(const std::vector< HT * > &htVector, const std::vector< G4HnInformation * > &hnVector)
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::p1d * > & GetP1Vector() const
const std::vector< G4HnInformation * > & GetHnVector() const
const std::vector< tools::histo::p2d * > & GetP2Vector() const
void Merge(G4Mutex &mergeMutex, G4THnManager< HT > *masterInstance)
G4bool Reset()
void ClearData()
G4bool IsEmpty() const
virtual G4bool ResetImpl() override
virtual G4bool PlotImpl() final
std::unique_ptr< G4ToolsAnalysisMessenger > fMessenger
static G4ToolsAnalysisManager * Instance()
static G4ThreadLocal G4ToolsAnalysisManager * fgToolsInstance
virtual G4bool WriteImpl() override
static constexpr std::string_view fkClass
G4ToolsAnalysisManager(const G4String &type)
virtual G4bool MergeImpl(tools::histo::hmpi *hmpi) final
G4bool WriteT(const std::vector< HT * > &htVector, const std::vector< G4HnInformation * > &hnVector)
static G4ToolsAnalysisManager * fgMasterToolsInstance
virtual void ClearImpl() override
std::shared_ptr< G4PlotManager > fPlotManager
void SetH1Manager(G4VH1Manager *h1Manager)
void SetH2Manager(G4VH2Manager *h2Manager)
void SetPlotManager(std::shared_ptr< G4PlotManager > plotManager)
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4AnalysisManagerState fState
void SetP2Manager(G4VP2Manager *p2Manager)
void SetP1Manager(G4VP1Manager *p1Manager)
void SetH3Manager(G4VH3Manager *h3Manager)
std::shared_ptr< G4VFileManager > fVFileManager
constexpr G4int kVL3
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool IsWorkerThread()
Definition: G4Threading.cc:123