2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
27// Author: Ivana Hrivnacova, 30/10/2018 (ivana@ipno.in2p3.fr)
28// ---------------------------------------------------------------------
30#include "G4TScoreNtupleWriterMessenger.hh"
31#include "G4THitsMap.hh"
32#include "G4Threading.hh"
34//_____________________________________________________________________________
39//_____________________________________________________________________________
41G4TScoreNtupleWriter<T>::G4TScoreNtupleWriter()
42 : G4VScoreNtupleWriter()
45 , fAnalysisManager(nullptr)
46 , fDefaultFileType("root")
47 , fFileName("scoring")
49 , fHasAnalysisFile(false)
51 , fIsInitialized(false)
54 if(G4Threading::IsMasterThread())
56 fMessenger = new G4TScoreNtupleWriterMessenger<T>(this);
60//_____________________________________________________________________________
62G4TScoreNtupleWriter<T>::~G4TScoreNtupleWriter()
71//_____________________________________________________________________________
73void G4TScoreNtupleWriter<T>::CreateAnalysisManager()
78 // create/get analysis manager
79 fAnalysisManager = T::Instance();
81 fAnalysisManager->SetDefaultFileType(fDefaultFileType);
82 fAnalysisManager->SetVerboseLevel(fVerboseLevel);
89//_____________________________________________________________________________
91G4VScoreNtupleWriter* G4TScoreNtupleWriter<T>::CreateInstance() const
93 auto instance = new G4TScoreNtupleWriter();
94 instance->SetFileName(fFileName);
95 instance->SetVerboseLevel(fVerboseLevel);
104//_____________________________________________________________________________
106G4bool G4TScoreNtupleWriter<T>::Book(G4HCofThisEvent* hce)
109 if(fVerboseLevel > 1)
111 G4cout << "--- G4TScoreNtupleWriter<T>::Book" << G4endl;
117 // book collection ID if DoubleHitsMap
118 if(fHCIds.size() == 0)
121 if(fVerboseLevel > 1)
123 G4cout << "--- going to fill fHCIds " << G4endl;
126 for(G4int i = 0; i < hce->GetNumberOfCollections(); ++i)
128 auto hitsMap = dynamic_cast<G4THitsMap<G4double>*>(hce->GetHC(i));
133 if(fVerboseLevel > 1)
135 G4cout << "--- adding hcID = " << i << G4endl;
143 // Create analysis manager
146 CreateAnalysisManager();
149 // Create ntuples (only once)
153 if(fVerboseLevel > 1)
155 G4cout << "-- going to create ntuples " << G4endl;
159 for(auto id : fHCIds)
161 auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
163 // Create ntuple for this primitive
164 G4String ntupleName(hitsMap->GetSDname());
165 ntupleName.append("_");
166 ntupleName.append(hitsMap->GetName());
168 G4int ntupleId = fAnalysisManager->CreateNtuple(ntupleName, ntupleName);
171 fFirstNtupleId = ntupleId;
173 if(fVerboseLevel > 1)
175 G4cout << "-- set first ntuple Id " << fFirstNtupleId << G4endl;
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();
193 fIsInitialized = true;
199//_____________________________________________________________________________
201void G4TScoreNtupleWriter<T>::OpenFile()
204 if(fVerboseLevel > 1)
206 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile" << G4endl;
210 if(fAnalysisManager->IsOpenFile())
214 if(fVerboseLevel > 1)
216 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile executing" << G4endl;
220 if(fAnalysisManager->GetFileName() == "")
222 fAnalysisManager->SetFileName(fFileName);
224 fAnalysisManager->OpenFile();
227 if(fVerboseLevel > 1)
229 G4cout << "--- G4TScoreNtupleWriter<T>::OpenFile isOpenFile: "
230 << fAnalysisManager->IsOpenFile() << G4endl;
234 fHasAnalysisFile = fAnalysisManager->IsOpenFile();
237//_____________________________________________________________________________
239void G4TScoreNtupleWriter<T>::Fill(G4HCofThisEvent* hce, G4int eventNumber)
242 if(fVerboseLevel > 1)
244 G4cout << "--- start G4TScoreNtupleWriter<T>::Fill" << G4endl;
249 for(auto id : fHCIds)
252 if(fVerboseLevel > 1)
254 G4cout << "in loop over fHCIds, counter " << counter << G4endl;
257 auto hitsMap = static_cast<G4THitsMap<G4double>*>(hce->GetHC(id));
259 // G4cout << eventNumber << ".. go to fill ntuple " << counter +
260 // fFirstNtupleId << G4endl;
262 // fill hits in ntuple
263 std::map<G4int, G4double*>::iterator it;
264 for(it = hitsMap->GetMap()->begin(); it != hitsMap->GetMap()->end(); it++)
266 fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 0,
268 fAnalysisManager->FillNtupleIColumn(counter + fFirstNtupleId, 1,
270 fAnalysisManager->FillNtupleDColumn(counter + fFirstNtupleId, 2,
272 fAnalysisManager->AddNtupleRow(counter + fFirstNtupleId);
278 if(fVerboseLevel > 1)
280 G4cout << "--- done G4TScoreNtupleWriter<T>::Fill" << G4endl;
285//_____________________________________________________________________________
287void G4TScoreNtupleWriter<T>::Write()
290 if(fVerboseLevel > 1)
292 G4cout << "--- start G4TScoreNtupleWriter<T>::Write" << G4endl;
299 if(fVerboseLevel > 1)
301 G4cout << "--- G4TScoreNtupleWriter<T>::Write - has file" << G4endl;
304 fAnalysisManager->Write();
305 fAnalysisManager->CloseFile();
306 fAnalysisManager->SetFileName("");
310 if(fVerboseLevel > 1)
312 G4cout << "--- done G4TScoreNtupleWriter<T>::Write" << G4endl;
317//_____________________________________________________________________________
319void G4TScoreNtupleWriter<T>::SetDefaultFileType(const G4String& value)
321 fDefaultFileType = value;
324 fAnalysisManager->SetDefaultFileType(value);
328//_____________________________________________________________________________
330void G4TScoreNtupleWriter<T>::SetFileName(const G4String& fileName)
332 fFileName = fileName;
335//_____________________________________________________________________________
337void G4TScoreNtupleWriter<T>::SetVerboseLevel(G4int value)
339 fVerboseLevel = value;
342 fAnalysisManager->SetVerboseLevel(value);