Geant4-11
G4TScoreNtupleWriter.icc
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, 30/10/2018 (ivana@ipno.in2p3.fr)
28// ---------------------------------------------------------------------
29
30#include "G4TScoreNtupleWriterMessenger.hh"
31#include "G4THitsMap.hh"
32#include "G4Threading.hh"
33
34//_____________________________________________________________________________
35//
36// ctor, dtor
37//
38
39//_____________________________________________________________________________
40template <typename T>
41G4TScoreNtupleWriter<T>::G4TScoreNtupleWriter()
42 : G4VScoreNtupleWriter()
43 , fMessenger(nullptr)
44 , fHCIds()
45 , fAnalysisManager(nullptr)
46 , fDefaultFileType("root")
47 , fFileName("scoring")
48 , fVerboseLevel(1)
49 , fHasAnalysisFile(false)
50 , fIsBooked(false)
51 , fIsInitialized(false)
52 , fFirstNtupleId(0)
53{
54 if(G4Threading::IsMasterThread())
55 {
56 fMessenger = new G4TScoreNtupleWriterMessenger<T>(this);
57 }
58}
59
60//_____________________________________________________________________________
61template <typename T>
62G4TScoreNtupleWriter<T>::~G4TScoreNtupleWriter()
63{
64 delete fMessenger;
65}
66
67//
68// private methods
69//
70
71//_____________________________________________________________________________
72template <typename T>
73void G4TScoreNtupleWriter<T>::CreateAnalysisManager()
74{
75 if(fAnalysisManager)
76 return;
77
78 // create/get analysis manager
79 fAnalysisManager = T::Instance();
80
81 fAnalysisManager->SetDefaultFileType(fDefaultFileType);
82 fAnalysisManager->SetVerboseLevel(fVerboseLevel);
83}
84
85//
86// protected methods
87//
88
89//_____________________________________________________________________________
90template <typename T>
91G4VScoreNtupleWriter* G4TScoreNtupleWriter<T>::CreateInstance() const
92{
93 auto instance = new G4TScoreNtupleWriter();
94 instance->SetFileName(fFileName);
95 instance->SetVerboseLevel(fVerboseLevel);
96
97 return instance;
98}
99
100//
101// public methods
102//
103
104//_____________________________________________________________________________
105template <typename T>
106G4bool G4TScoreNtupleWriter<T>::Book(G4HCofThisEvent* hce)
107{
108#ifdef G4VERBOSE
109 if(fVerboseLevel > 1)
110 {
111 G4cout << "--- G4TScoreNtupleWriter<T>::Book" << G4endl;
112 }
113#endif
114 if(!hce)
115 return false;
116
117 // book collection ID if DoubleHitsMap
118 if(fHCIds.size() == 0)
119 {
120#ifdef G4VERBOSE
121 if(fVerboseLevel > 1)
122 {
123 G4cout << "--- going to fill fHCIds " << G4endl;
124 }
125#endif
126 for(G4int i = 0; i < hce->GetNumberOfCollections(); ++i)
127 {
128 auto hitsMap = dynamic_cast<G4THitsMap<G4double>*>(hce->GetHC(i));
129
130 if(hitsMap)
131 {
132#ifdef G4VERBOSE
133 if(fVerboseLevel > 1)
134 {
135 G4cout << "--- adding hcID = " << i << G4endl;
136 }
137#endif
138 fHCIds.push_back(i);
139 }
140 }
141 }
142
143 // Create analysis manager
144 if(fHCIds.size())
145 {
146 CreateAnalysisManager();
147 }
148
149 // Create ntuples (only once)
150 if(!fIsInitialized)
151 {
152#ifdef G4VERBOSE
153 if(fVerboseLevel > 1)
154 {
155 G4cout << "-- going to create ntuples " << G4endl;
156 }
157#endif
158 auto first = true;
159 for(auto id : fHCIds)
160 {
161 auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
162
163 // Create ntuple for this primitive
164 G4String ntupleName(hitsMap->GetSDname());
165 ntupleName.append("_");
166 ntupleName.append(hitsMap->GetName());
167
168 G4int ntupleId = fAnalysisManager->CreateNtuple(ntupleName, ntupleName);
169 if(first)
170 {
171 fFirstNtupleId = ntupleId;
172#ifdef G4VERBOSE
173 if(fVerboseLevel > 1)
174 {
175 G4cout << "-- set first ntuple Id " << fFirstNtupleId << G4endl;
176 }
177#endif
178 first = false;
179 }
180
181 G4String colName(ntupleName);
182 colName.append("_eventId");
183 fAnalysisManager->CreateNtupleIColumn(colName);
184 colName = ntupleName;
185 colName.append("_cell");
186 fAnalysisManager->CreateNtupleIColumn(colName);
187 colName = ntupleName;
188 colName.append("_score");
189 fAnalysisManager->CreateNtupleDColumn(colName);
190 fAnalysisManager->FinishNtuple();
191 fIsBooked = true;
192 }
193 fIsInitialized = true;
194 }
195
196 return fIsBooked;
197}
198
199//_____________________________________________________________________________
200template <typename T>
201void G4TScoreNtupleWriter<T>::OpenFile()
202{
203#ifdef G4VERBOSE
204 if(fVerboseLevel > 1)
205 {
206 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile" << G4endl;
207 }
208#endif
209
210 if(fAnalysisManager->IsOpenFile())
211 return;
212
213#ifdef G4VERBOSE
214 if(fVerboseLevel > 1)
215 {
216 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile executing" << G4endl;
217 }
218#endif
219
220 if(fAnalysisManager->GetFileName() == "")
221 {
222 fAnalysisManager->SetFileName(fFileName);
223 }
224 fAnalysisManager->OpenFile();
225
226#ifdef G4VERBOSE
227 if(fVerboseLevel > 1)
228 {
229 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile isOpenFile: "
230 << fAnalysisManager->IsOpenFile() << G4endl;
231 }
232#endif
233
234 fHasAnalysisFile = fAnalysisManager->IsOpenFile();
235}
236
237//_____________________________________________________________________________
238template <typename T>
239void G4TScoreNtupleWriter<T>::Fill(G4HCofThisEvent* hce, G4int eventNumber)
240{
241#ifdef G4VERBOSE
242 if(fVerboseLevel > 1)
243 {
244 G4cout << "--- start G4TScoreNtupleWriter<T>::Fill" << G4endl;
245 }
246#endif
247
248 auto counter = 0;
249 for(auto id : fHCIds)
250 {
251#ifdef G4VERBOSE
252 if(fVerboseLevel > 1)
253 {
254 G4cout << "in loop over fHCIds, counter " << counter << G4endl;
255 }
256#endif
257 auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
258
259 // G4cout << eventNumber << ".. go to fill ntuple " << counter +
260 // fFirstNtupleId << G4endl;
261
262 // fill hits in ntuple
263 std::map<G4int, G4double*>::iterator it;
264 for(it = hitsMap->GetMap()->begin(); it != hitsMap->GetMap()->end(); it++)
265 {
266 fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 0,
267 eventNumber);
268 fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 1,
269 it->first);
270 fAnalysisManager->FillNtupleDColumn(counter + fFirstNtupleId, 2,
271 *(it->second));
272 fAnalysisManager->AddNtupleRow(counter + fFirstNtupleId);
273 }
274 counter++;
275 }
276
277#ifdef G4VERBOSE
278 if(fVerboseLevel > 1)
279 {
280 G4cout << "--- done G4TScoreNtupleWriter<T>::Fill" << G4endl;
281 }
282#endif
283}
284
285//_____________________________________________________________________________
286template <typename T>
287void G4TScoreNtupleWriter<T>::Write()
288{
289#ifdef G4VERBOSE
290 if(fVerboseLevel > 1)
291 {
292 G4cout << "--- start G4TScoreNtupleWriter<T>::Write" << G4endl;
293 }
294#endif
295
296 if(fHasAnalysisFile)
297 {
298#ifdef G4VERBOSE
299 if(fVerboseLevel > 1)
300 {
301 G4cout << "--- G4TScoreNtupleWriter<T>::Write - has file" << G4endl;
302 }
303#endif
304 fAnalysisManager->Write();
305 fAnalysisManager->CloseFile();
306 fAnalysisManager->SetFileName("");
307 }
308
309#ifdef G4VERBOSE
310 if(fVerboseLevel > 1)
311 {
312 G4cout << "--- done G4TScoreNtupleWriter<T>::Write" << G4endl;
313 }
314#endif
315}
316
317//_____________________________________________________________________________
318template <typename T>
319void G4TScoreNtupleWriter<T>::SetDefaultFileType(const G4String& value)
320{
321 fDefaultFileType = value;
322 if(fAnalysisManager)
323 {
324 fAnalysisManager->SetDefaultFileType(value);
325 }
326}
327
328//_____________________________________________________________________________
329template <typename T>
330void G4TScoreNtupleWriter<T>::SetFileName(const G4String& fileName)
331{
332 fFileName = fileName;
333}
334
335//_____________________________________________________________________________
336template <typename T>
337void G4TScoreNtupleWriter<T>::SetVerboseLevel(G4int value)
338{
339 fVerboseLevel = value;
340 if(fAnalysisManager)
341 {
342 fAnalysisManager->SetVerboseLevel(value);
343 }
344}