Geant4  10.00.p01
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 ( ! CheckNbins(nbins) ) return kInvalidId;
221  if ( ! CheckMinMax(xmin, xmax, binSchemeName) ) return kInvalidId;
222 
223  return fVH1Manager->CreateH1(name, title, nbins, xmin, xmax,
224  unitName, fcnName, binSchemeName);
225 }
226 
227 //_____________________________________________________________________________
229  const std::vector<G4double>& edges,
230  const G4String& unitName, const G4String& fcnName)
231 {
232  if ( ! CheckEdges(edges) ) return kInvalidId;
233 
234  return fVH1Manager->CreateH1(name, title, edges, unitName, fcnName);
235 }
236 
237 //_____________________________________________________________________________
239  G4int nxbins, G4double xmin, G4double xmax,
240  G4int nybins, G4double ymin, G4double ymax,
241  const G4String& xunitName, const G4String& yunitName,
242  const G4String& xfcnName, const G4String& yfcnName,
243  const G4String& xbinSchemeName,
244  const G4String& ybinSchemeName)
245 
246 {
247  if ( ! CheckNbins(nxbins) ) return kInvalidId;
248  if ( ! CheckMinMax(xmin, xmax, xbinSchemeName) ) return kInvalidId;
249 
250  if ( ! CheckNbins(nybins) ) return kInvalidId;
251  if ( ! CheckMinMax(ymin, ymax, ybinSchemeName) ) return kInvalidId;
252 
253  return fVH2Manager->CreateH2(name, title,
254  nxbins, xmin, xmax, nybins, ymin, ymax,
255  xunitName, yunitName, xfcnName, yfcnName,
256  xbinSchemeName, ybinSchemeName);
257 }
258 
259 //_____________________________________________________________________________
261  const std::vector<G4double>& xedges,
262  const std::vector<G4double>& yedges,
263  const G4String& xunitName, const G4String& yunitName,
264  const G4String& xfcnName, const G4String& yfcnName)
265 
266 {
267  if ( ! CheckEdges(xedges) ) return kInvalidId;
268  if ( ! CheckEdges(yedges) ) return kInvalidId;
269 
270  return fVH2Manager->CreateH2(name, title,
271  xedges, yedges,
272  xunitName, yunitName, xfcnName, yfcnName);
273 }
274 
275 //_____________________________________________________________________________
277  G4int nbins, G4double xmin, G4double xmax,
278  const G4String& unitName, const G4String& fcnName,
279  const G4String& binSchemeName)
280 {
281  if ( ! CheckNbins(nbins) ) return kInvalidId;
282  if ( ! CheckMinMax(xmin, xmax, binSchemeName) ) return kInvalidId;
283 
284  return fVH1Manager->SetH1(id, nbins, xmin, xmax, unitName, fcnName, binSchemeName);
285 }
286 
287 //_____________________________________________________________________________
289  const std::vector<G4double>& edges,
290  const G4String& unitName, const G4String& fcnName)
291 {
292  if ( ! CheckEdges(edges) ) return kInvalidId;
293 
294  return fVH1Manager->SetH1(id, edges, unitName, fcnName);
295 }
296 
297 //_____________________________________________________________________________
299  G4int nxbins, G4double xmin, G4double xmax,
300  G4int nybins, G4double ymin, G4double ymax,
301  const G4String& xunitName, const G4String& yunitName,
302  const G4String& xfcnName, const G4String& yfcnName,
303  const G4String& xbinSchemeName,
304  const G4String& ybinSchemeName)
305 {
306  if ( ! CheckNbins(nxbins) ) return kInvalidId;
307  if ( ! CheckMinMax(xmin, xmax, xbinSchemeName) ) return kInvalidId;
308 
309  if ( ! CheckNbins(nybins) ) return kInvalidId;
310  if ( ! CheckMinMax(ymin, ymax, ybinSchemeName) ) return kInvalidId;
311 
312  return fVH2Manager->SetH2(id, nxbins, xmin, xmax, nybins, ymin, ymax,
313  xunitName, yunitName, xfcnName, yfcnName,
314  xbinSchemeName, ybinSchemeName);
315 }
316 
317 //_____________________________________________________________________________
319  const std::vector<G4double>& xedges,
320  const std::vector<G4double>& yedges,
321  const G4String& xunitName, const G4String& yunitName,
322  const G4String& xfcnName, const G4String& yfcnName)
323 {
324  if ( ! CheckEdges(xedges) ) return kInvalidId;
325  if ( ! CheckEdges(yedges) ) return kInvalidId;
326 
327  return fVH2Manager->SetH2(id, xedges, yedges,
328  xunitName, yunitName, xfcnName, yfcnName);
329 }
330 
331 //_____________________________________________________________________________
333 {
334  return fVH1Manager->ScaleH1(id, factor);
335 }
336 
337 //_____________________________________________________________________________
339 {
340  return fVH2Manager->ScaleH2(id, factor);
341 }
342 
343 //_____________________________________________________________________________
345  const G4String& title)
346 {
347  return fVNtupleManager->CreateNtuple(name, title);
348 }
349 
350 //_____________________________________________________________________________
352 {
353  return fVNtupleManager->CreateNtupleIColumn(name);
354 }
355 
356 //_____________________________________________________________________________
358 {
359  return fVNtupleManager->CreateNtupleFColumn(name);
360 }
361 
362 //_____________________________________________________________________________
364 {
365  return fVNtupleManager->CreateNtupleDColumn(name);
366 }
367 
368 //_____________________________________________________________________________
370 {
371  return fVNtupleManager->FinishNtuple();
372 }
373 
374 //_____________________________________________________________________________
376  const G4String& name)
377 {
378  return fVNtupleManager->CreateNtupleIColumn(ntupleId, name);
379 }
380 
381 //_____________________________________________________________________________
383 {
384  return fVNtupleManager->CreateNtupleFColumn(ntupleId, name);
385 }
386 
387 
388 //_____________________________________________________________________________
390 {
391  return fVNtupleManager->CreateNtupleDColumn(ntupleId, name);
392 }
393 
394 //_____________________________________________________________________________
396 {
397  return fVNtupleManager->FinishNtuple(ntupleId);
398 }
399 
400 //_____________________________________________________________________________
402 {
403  G4bool finalResult = true;
404  G4bool result = fVH1Manager->SetFirstId(firstId);
405  finalResult = finalResult && result;
406 
407  result = fH1HnManager->SetFirstId(firstId);
408  finalResult = finalResult && result;
409 
410  result = fVH2Manager->SetFirstId(firstId);
411  finalResult = finalResult && result;
412 
413  result = fH2HnManager->SetFirstId(firstId);
414  finalResult = finalResult && result;
415 
416  return finalResult;
417 }
418 
419 //_____________________________________________________________________________
421 {
422  G4bool finalResult = true;
423  G4bool result = fVH1Manager->SetFirstId(firstId);
424  finalResult = finalResult && result;
425 
426  result = fH1HnManager->SetFirstId(firstId);
427  finalResult = finalResult && result;
428 
429  return finalResult;
430 }
431 
432 //_____________________________________________________________________________
434 {
435  G4bool finalResult = true;
436  G4bool result = fVH2Manager->SetFirstId(firstId);
437  finalResult = finalResult && result;
438 
439  result = fH2HnManager->SetFirstId(firstId);
440  finalResult = finalResult && result;
441 
442  return finalResult;
443 }
444 
445 //_____________________________________________________________________________
447 {
448  return fVNtupleManager->SetFirstId(firstId);
449 }
450 
451 //_____________________________________________________________________________
453 {
454  return fVNtupleManager->SetFirstNtupleColumnId(firstId);
455 }
456 
457 // Fill methods in .icc
458 
459 //_____________________________________________________________________________
461 {
462  fState.SetIsActivation(activation);
463 }
464 
465 // GetActivation() in .icc
466 
467 //_____________________________________________________________________________
469 {
470 // Return true if activation option is selected and any of managers has
471 // an activated object.
472 
473  return fState.GetIsActivation() &&
475 }
476 
477 
478 //_____________________________________________________________________________
480 {
481 // Return true any of managers has an object with activated ASCII option.
482 
483  return ( fH1HnManager->IsAscii() || fH2HnManager->IsAscii() );
484 }
485 
486 //_____________________________________________________________________________
488 {
489  return fH1HnManager->GetNofHns();
490 }
491 
492 //_____________________________________________________________________________
494 {
495  return fH2HnManager->GetNofHns();
496 }
497 
498 //_____________________________________________________________________________
500 {
501  return fVNtupleManager->GetNofNtuples();
502 }
503 
504 // GetH1Id(), GetH2Id in .icc
505 
506 //_____________________________________________________________________________
508 {
509 // Set activation to a given H1 object
510 
511  fH1HnManager->SetActivation(id, activation);
512 }
513 
514 //_____________________________________________________________________________
516 {
517 // Set activation to all H1 objects
518 
519  fH1HnManager->SetActivation(activation);
520 }
521 
522 //_____________________________________________________________________________
524 {
525  fH1HnManager->SetAscii(id, ascii);
526 }
527 
528 //_____________________________________________________________________________
530 {
531 // Set activation to a given H2 object
532 
533  fH2HnManager->SetActivation(id, activation);
534 }
535 
536 //_____________________________________________________________________________
538 {
539 // Set activation to all H2 objects
540 
541  fH2HnManager->SetActivation(activation);
542 }
543 
544 //_____________________________________________________________________________
546 {
547  fH2HnManager->SetAscii(id, ascii);
548 }
549 
550 // Access methods in .icc
551 
552 //_____________________________________________________________________________
554 {
555  fState.SetVerboseLevel(verboseLevel);
556 }
557 
558 // GetVerboseLevel() in .icc
559 
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)
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")