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 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

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