Geant4  10.00.p02
G4VAnalysisManager.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 // $Id: G4VAnalysisManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 09/07/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4VAnalysisManager.hh"
31 #include "G4AnalysisMessenger.hh"
32 #include "G4AnalysisUtilities.hh"
33 #include "G4HnManager.hh"
34 #include "G4VH1Manager.hh"
35 #include "G4VH2Manager.hh"
36 #include "G4VNtupleManager.hh"
37 #include "G4VFileManager.hh"
38 
39 #include <iostream>
40 
41 using namespace G4Analysis;
42 
43 //_____________________________________________________________________________
45  : fState(type, isMaster),
46  fMessenger(0),
47  fH1HnManager(0),
48  fH2HnManager(0),
49  fVH1Manager(0),
50  fVH2Manager(0),
51  fVNtupleManager(0),
52  fVFileManager(0)
53 {
54  fMessenger = new G4AnalysisMessenger(this);
55 }
56 
57 //_____________________________________________________________________________
59 {
60  delete fMessenger;
61  delete fVH1Manager;
62  delete fVH2Manager;
63  delete fVNtupleManager;
64  delete fVFileManager;
65 }
66 
67 //
68 // protected methods
69 //
70 
71 //_____________________________________________________________________________
73 {
74  fVH1Manager = h1Manager;
75  fH1HnManager = h1Manager->fHnManager;
77 }
78 
79 //_____________________________________________________________________________
81 {
82  fVH2Manager = h2Manager;
83  fH2HnManager = h2Manager->fHnManager;
85 }
86 
87 //_____________________________________________________________________________
89 {
90  fVNtupleManager = ntupleManager;
91 }
92 
93 //_____________________________________________________________________________
95 {
96  fVFileManager = fileManager;
97 }
98 
99 //_____________________________________________________________________________
101 {
102  G4bool finalResult = true;
103 
104  // Replace or add file extension .ascii
105  G4String name(fileName);
106  if ( name.find(".") != std::string::npos ) {
107  name.erase(name.find("."), name.length());
108  }
109  name.append(".ascii");
110 
111 #ifdef G4VERBOSE
112  if ( fState.GetVerboseL3() )
113  fState.GetVerboseL3()->Message("write ASCII", "file", name);
114 #endif
115 
116  std::ofstream output(name, std::ios::out);
117  if ( ! output ) {
118  G4ExceptionDescription description;
119  description
120  << "Cannot open file. File name is not defined.";
121  G4Exception("G4VAnalysisManager::WriteAscii()",
122  "Analysis_W009", JustWarning, description);
123  return false;
124  }
125  output.setf( std::ios::scientific, std::ios::floatfield );
126 
127  G4bool result = fVH1Manager->WriteOnAscii(output);
128  finalResult = finalResult && result;
129 
130  result = fVH2Manager->WriteOnAscii(output);
131  finalResult = finalResult && result;
132 
133 #ifdef G4VERBOSE
134  if ( fState.GetVerboseL1() )
135  fState.GetVerboseL1()->Message("write ASCII", "file", name, result);
136 #endif
137 
138  return finalResult;
139 }
140 
141 //
142 // public methods
143 //
144 
145 //_____________________________________________________________________________
147 {
148  if ( fVFileManager->GetFileName() == "" ) {
149  G4ExceptionDescription description;
150  description
151  << "Cannot open file. File name is not defined.";
152  G4Exception("G4VFileManager::OpenFile()",
153  "Analysis_W009", JustWarning, description);
154  return false;
155  }
156 
158 }
159 
160 //_____________________________________________________________________________
162 {
163  return OpenFileImpl(fileName);
164 }
165 
166 //_____________________________________________________________________________
168 {
169  return WriteImpl();
170 }
171 
172 //_____________________________________________________________________________
174 {
175  return CloseFileImpl();
176 }
177 
178 //_____________________________________________________________________________
180 {
181  return fVFileManager->SetFileName(fileName);
182 }
183 
184 //_____________________________________________________________________________
186 {
187  return fVFileManager->SetHistoDirectoryName(dirName);
188 }
189 
190 //_____________________________________________________________________________
192 {
193  return fVFileManager->SetNtupleDirectoryName(dirName);
194 }
195 
196 //_____________________________________________________________________________
198 {
199  return fVFileManager->GetFileName();
200 }
201 
202 //_____________________________________________________________________________
204 {
206 }
207 
208 //_____________________________________________________________________________
210 {
212 }
213 
214 //_____________________________________________________________________________
216  G4int nbins, G4double xmin, G4double xmax,
217  const G4String& unitName, const G4String& fcnName,
218  const G4String& binSchemeName)
219 {
220  if ( ! CheckName(name, "H1") ) return kInvalidId;
221  if ( ! CheckNbins(nbins) ) return kInvalidId;
222  if ( ! CheckMinMax(xmin, xmax, binSchemeName) ) return kInvalidId;
223 
224  return fVH1Manager->CreateH1(name, title, nbins, xmin, xmax,
225  unitName, fcnName, binSchemeName);
226 }
227 
228 //_____________________________________________________________________________
230  const std::vector<G4double>& edges,
231  const G4String& unitName, const G4String& fcnName)
232 {
233  if ( ! CheckName(name, "H1") ) return kInvalidId;
234  if ( ! CheckEdges(edges) ) return kInvalidId;
235 
236  return fVH1Manager->CreateH1(name, title, edges, unitName, fcnName);
237 }
238 
239 //_____________________________________________________________________________
241  G4int nxbins, G4double xmin, G4double xmax,
242  G4int nybins, G4double ymin, G4double ymax,
243  const G4String& xunitName, const G4String& yunitName,
244  const G4String& xfcnName, const G4String& yfcnName,
245  const G4String& xbinSchemeName,
246  const G4String& ybinSchemeName)
247 
248 {
249  if ( ! CheckName(name, "H2") ) return kInvalidId;
250 
251  if ( ! CheckNbins(nxbins) ) return kInvalidId;
252  if ( ! CheckMinMax(xmin, xmax, xbinSchemeName) ) return kInvalidId;
253 
254  if ( ! CheckNbins(nybins) ) return kInvalidId;
255  if ( ! CheckMinMax(ymin, ymax, ybinSchemeName) ) return kInvalidId;
256 
257  return fVH2Manager->CreateH2(name, title,
258  nxbins, xmin, xmax, nybins, ymin, ymax,
259  xunitName, yunitName, xfcnName, yfcnName,
260  xbinSchemeName, ybinSchemeName);
261 }
262 
263 //_____________________________________________________________________________
265  const std::vector<G4double>& xedges,
266  const std::vector<G4double>& yedges,
267  const G4String& xunitName, const G4String& yunitName,
268  const G4String& xfcnName, const G4String& yfcnName)
269 
270 {
271  if ( ! CheckName(name, "H2") ) return kInvalidId;
272 
273  if ( ! CheckEdges(xedges) ) return kInvalidId;
274  if ( ! CheckEdges(yedges) ) return kInvalidId;
275 
276  return fVH2Manager->CreateH2(name, title,
277  xedges, yedges,
278  xunitName, yunitName, xfcnName, yfcnName);
279 }
280 
281 //_____________________________________________________________________________
283  G4int nbins, G4double xmin, G4double xmax,
284  const G4String& unitName, const G4String& fcnName,
285  const G4String& binSchemeName)
286 {
287  if ( ! CheckNbins(nbins) ) return kInvalidId;
288  if ( ! CheckMinMax(xmin, xmax, binSchemeName) ) return kInvalidId;
289 
290  return fVH1Manager->SetH1(id, nbins, xmin, xmax, unitName, fcnName, binSchemeName);
291 }
292 
293 //_____________________________________________________________________________
295  const std::vector<G4double>& edges,
296  const G4String& unitName, const G4String& fcnName)
297 {
298  if ( ! CheckEdges(edges) ) return kInvalidId;
299 
300  return fVH1Manager->SetH1(id, edges, unitName, fcnName);
301 }
302 
303 //_____________________________________________________________________________
305  G4int nxbins, G4double xmin, G4double xmax,
306  G4int nybins, G4double ymin, G4double ymax,
307  const G4String& xunitName, const G4String& yunitName,
308  const G4String& xfcnName, const G4String& yfcnName,
309  const G4String& xbinSchemeName,
310  const G4String& ybinSchemeName)
311 {
312  if ( ! CheckNbins(nxbins) ) return kInvalidId;
313  if ( ! CheckMinMax(xmin, xmax, xbinSchemeName) ) return kInvalidId;
314 
315  if ( ! CheckNbins(nybins) ) return kInvalidId;
316  if ( ! CheckMinMax(ymin, ymax, ybinSchemeName) ) return kInvalidId;
317 
318  return fVH2Manager->SetH2(id, nxbins, xmin, xmax, nybins, ymin, ymax,
319  xunitName, yunitName, xfcnName, yfcnName,
320  xbinSchemeName, ybinSchemeName);
321 }
322 
323 //_____________________________________________________________________________
325  const std::vector<G4double>& xedges,
326  const std::vector<G4double>& yedges,
327  const G4String& xunitName, const G4String& yunitName,
328  const G4String& xfcnName, const G4String& yfcnName)
329 {
330  if ( ! CheckEdges(xedges) ) return kInvalidId;
331  if ( ! CheckEdges(yedges) ) return kInvalidId;
332 
333  return fVH2Manager->SetH2(id, xedges, yedges,
334  xunitName, yunitName, xfcnName, yfcnName);
335 }
336 
337 //_____________________________________________________________________________
339 {
340  return fVH1Manager->ScaleH1(id, factor);
341 }
342 
343 //_____________________________________________________________________________
345 {
346  return fVH2Manager->ScaleH2(id, factor);
347 }
348 
349 //_____________________________________________________________________________
351  const G4String& title)
352 {
353  if ( ! CheckName(name, "Ntuple") ) return kInvalidId;
354 
355  return fVNtupleManager->CreateNtuple(name, title);
356 }
357 
358 //_____________________________________________________________________________
360 {
361  if ( ! CheckName(name, "NtupleIColumn") ) return kInvalidId;
362 
363  return fVNtupleManager->CreateNtupleIColumn(name);
364 }
365 
366 //_____________________________________________________________________________
368 {
369  if ( ! CheckName(name, "NtupleFColumn") ) return kInvalidId;
370 
371  return fVNtupleManager->CreateNtupleFColumn(name);
372 }
373 
374 //_____________________________________________________________________________
376 {
377  if ( ! CheckName(name, "NtupleDColumn") ) return kInvalidId;
378 
379  return fVNtupleManager->CreateNtupleDColumn(name);
380 }
381 
382 //_____________________________________________________________________________
384 {
385  return fVNtupleManager->FinishNtuple();
386 }
387 
388 //_____________________________________________________________________________
390  const G4String& name)
391 {
392  if ( ! CheckName(name, "NtupleIColumn") ) return kInvalidId;
393 
394  return fVNtupleManager->CreateNtupleIColumn(ntupleId, name);
395 }
396 
397 //_____________________________________________________________________________
399 {
400  if ( ! CheckName(name, "NtupleFColumn") ) return kInvalidId;
401 
402  return fVNtupleManager->CreateNtupleFColumn(ntupleId, name);
403 }
404 
405 
406 //_____________________________________________________________________________
408 {
409  if ( ! CheckName(name, "NtupleDColumn") ) return kInvalidId;
410 
411  return fVNtupleManager->CreateNtupleDColumn(ntupleId, name);
412 }
413 
414 //_____________________________________________________________________________
416 {
417  return fVNtupleManager->FinishNtuple(ntupleId);
418 }
419 
420 //_____________________________________________________________________________
422 {
423  G4bool finalResult = true;
424  G4bool result = fVH1Manager->SetFirstId(firstId);
425  finalResult = finalResult && result;
426 
427  result = fH1HnManager->SetFirstId(firstId);
428  finalResult = finalResult && result;
429 
430  result = fVH2Manager->SetFirstId(firstId);
431  finalResult = finalResult && result;
432 
433  result = fH2HnManager->SetFirstId(firstId);
434  finalResult = finalResult && result;
435 
436  return finalResult;
437 }
438 
439 //_____________________________________________________________________________
441 {
442  G4bool finalResult = true;
443  G4bool result = fVH1Manager->SetFirstId(firstId);
444  finalResult = finalResult && result;
445 
446  result = fH1HnManager->SetFirstId(firstId);
447  finalResult = finalResult && result;
448 
449  return finalResult;
450 }
451 
452 //_____________________________________________________________________________
454 {
455  G4bool finalResult = true;
456  G4bool result = fVH2Manager->SetFirstId(firstId);
457  finalResult = finalResult && result;
458 
459  result = fH2HnManager->SetFirstId(firstId);
460  finalResult = finalResult && result;
461 
462  return finalResult;
463 }
464 
465 //_____________________________________________________________________________
467 {
468  return fVNtupleManager->SetFirstId(firstId);
469 }
470 
471 //_____________________________________________________________________________
473 {
474  return fVNtupleManager->SetFirstNtupleColumnId(firstId);
475 }
476 
477 // Fill methods in .icc
478 
479 //_____________________________________________________________________________
481 {
482  fState.SetIsActivation(activation);
483 }
484 
485 // GetActivation() in .icc
486 
487 //_____________________________________________________________________________
489 {
490 // Return true if activation option is selected and any of managers has
491 // an activated object.
492 
493  return fState.GetIsActivation() &&
495 }
496 
497 
498 //_____________________________________________________________________________
500 {
501 // Return true any of managers has an object with activated ASCII option.
502 
503  return ( fH1HnManager->IsAscii() || fH2HnManager->IsAscii() );
504 }
505 
506 //_____________________________________________________________________________
508 {
509  return fH1HnManager->GetNofHns();
510 }
511 
512 //_____________________________________________________________________________
514 {
515  return fH2HnManager->GetNofHns();
516 }
517 
518 //_____________________________________________________________________________
520 {
521  return fVNtupleManager->GetNofNtuples();
522 }
523 
524 // GetH1Id(), GetH2Id in .icc
525 
526 //_____________________________________________________________________________
528 {
529 // Set activation to a given H1 object
530 
531  fH1HnManager->SetActivation(id, activation);
532 }
533 
534 //_____________________________________________________________________________
536 {
537 // Set activation to all H1 objects
538 
539  fH1HnManager->SetActivation(activation);
540 }
541 
542 //_____________________________________________________________________________
544 {
545  fH1HnManager->SetAscii(id, ascii);
546 }
547 
548 //_____________________________________________________________________________
550 {
551 // Set activation to a given H2 object
552 
553  fH2HnManager->SetActivation(id, activation);
554 }
555 
556 //_____________________________________________________________________________
558 {
559 // Set activation to all H2 objects
560 
561  fH2HnManager->SetActivation(activation);
562 }
563 
564 //_____________________________________________________________________________
566 {
567  fH2HnManager->SetAscii(id, ascii);
568 }
569 
570 // Access methods in .icc
571 
572 //_____________________________________________________________________________
574 {
575  fState.SetVerboseLevel(verboseLevel);
576 }
577 
578 // GetVerboseLevel() in .icc
579 
G4bool SetHistoDirectoryName(const G4String &dirName)
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4bool IsAscii() const
Definition: G4HnManager.cc:114
G4HnManager * fHnManager
void SetH2Ascii(G4int id, G4bool ascii)
G4bool SetNtupleDirectoryName(const G4String &dirName)
G4String GetNtupleDirectoryName() const
G4AnalysisMessenger * fMessenger
G4int CreateNtupleIColumn(const G4String &name)
G4bool SetFirstHistoId(G4int firstId)
G4VH2Manager * fVH2Manager
virtual G4int GetNofNtuples() const =0
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
virtual G4int CreateNtupleIColumn(const G4String &name)=0
G4bool ScaleH1(G4int id, G4double factor)
G4bool ScaleH2(G4int id, G4double factor)
void SetH2HnManager(G4HnManager *h2HnManager)
void SetH1Manager(G4VH1Manager *h1Manager)
G4String name
Definition: TRTMaterials.hh:40
virtual G4bool WriteImpl()=0
void SetVerboseLevel(G4int verboseLevel)
G4bool SetFirstId(G4int firstId)
G4bool IsAscii() const
void SetH2Activation(G4bool activation)
G4int CreateNtuple(const G4String &name, const G4String &title)
G4VFileManager * fVFileManager
G4VNtupleManager * fVNtupleManager
G4bool SetFileName(const G4String &fileName)
G4bool SetNtupleDirectoryName(const G4String &dirName)
int G4int
Definition: G4Types.hh:78
virtual G4bool ScaleH2(G4int id, G4double factor)=0
void SetH1HnManager(G4HnManager *h1HnManager)
virtual G4bool WriteOnAscii(std::ofstream &output)=0
G4int GetNofH1s() const
G4bool SetFirstH1Id(G4int firstId)
G4String GetNtupleDirectoryName() const
G4VAnalysisManager(const G4String &type, G4bool isMaster)
virtual G4int CreateNtuple(const G4String &name, const G4String &title)=0
const G4AnalysisVerbose * GetVerboseL3() const
void SetH1Ascii(G4int id, G4bool ascii)
G4bool SetFirstNtupleColumnId(G4int firstId)
G4bool CheckName(const G4String &name, const G4String &objectType)
virtual void FinishNtuple()=0
G4int GetNofNtuples() const
G4String GetFileName() const
G4String GetHistoDirectoryName() const
G4VH1Manager * fVH1Manager
bool G4bool
Definition: G4Types.hh:79
virtual G4bool OpenFileImpl(const G4String &fileName)=0
void SetIsActivation(G4bool isActivation)
virtual G4int CreateNtupleFColumn(const G4String &name)=0
void SetAscii(G4int id, G4bool ascii)
Definition: G4HnManager.cc:161
G4bool SetH1(G4int id, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")
virtual G4int CreateH2(const G4String &name, const G4String &title, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none", const G4String &xbinScheme="linear", const G4String &ybinScheme="linear")=0
virtual G4bool SetH2(G4int id, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none", const G4String &xbinScheme="linear", const G4String &ybinScheme="linear")=0
virtual G4int CreateNtupleDColumn(const G4String &name)=0
G4bool CheckMinMax(G4double xmin, G4double xmax, const G4String &binSchemeName)
G4String GetFileName() const
G4bool IsActive() const
void SetFileManager(G4VFileManager *fileManager)
G4String GetHistoDirectoryName() const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool SetH2(G4int id, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none", const G4String &xbinSchemeName="linear", const G4String &ybinSchemeName="linear")
void SetH2Manager(G4VH2Manager *h2Manager)
G4HnManager * fH2HnManager
G4bool SetFirstNtupleId(G4int firstId)
G4HnManager * fHnManager
G4String & append(const G4String &)
void SetH1Activation(G4bool activation)
void SetActivation(G4bool activation)
G4int CreateNtupleFColumn(const G4String &name)
G4HnManager * fH1HnManager
G4bool SetFileName(const G4String &fileName)
G4bool CheckNbins(G4int nbins)
G4bool SetFirstNtupleColumnId(G4int firstId)
virtual G4bool SetH1(G4int id, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")=0
G4bool SetFirstH2Id(G4int firstId)
G4int CreateNtupleDColumn(const G4String &name)
G4bool SetHistoDirectoryName(const G4String &dirName)
virtual G4bool ScaleH1(G4int id, G4double factor)=0
double G4double
Definition: G4Types.hh:76
G4bool IsActive() const
Definition: G4HnManager.cc:108
G4AnalysisManagerState fState
virtual G4bool WriteOnAscii(std::ofstream &output)=0
void SetVerboseLevel(G4int verboseLevel)
const G4int kInvalidId
virtual G4bool CloseFileImpl()=0
const G4AnalysisVerbose * GetVerboseL1() const
void SetActivation(G4bool activation)
Definition: G4HnManager.cc:140
void SetNtupleManager(G4VNtupleManager *ntupleManager)
G4int GetNofH2s() const
G4bool CheckEdges(const std::vector< G4double > &edges)
G4int GetNofHns() const
Definition: G4HnManager.hh:111
G4bool WriteAscii(const G4String &fileName)
virtual G4int CreateH1(const G4String &name, const G4String &title, G4int nbins, G4double xmin, G4double xmax, const G4String &unitName="none", const G4String &fcnName="none", const G4String &binSchemeName="linear")=0
G4int CreateH2(const G4String &name, const G4String &title, G4int nxbins, G4double xmin, G4double xmax, G4int nybins, G4double ymin, G4double ymax, const G4String &xunitName="none", const G4String &yunitName="none", const G4String &xfcnName="none", const G4String &yfcnName="none", const G4String &xbinScheme="linear", const G4String &ybinScheme="linear")