Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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$
27 
28 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr)
29 
30 #include "G4VAnalysisManager.hh"
31 #include "G4AnalysisMessenger.hh"
32 #include "G4UnitsTable.hh"
33 
34 #include <iostream>
35 
36 //_____________________________________________________________________________
38  : fVerboseLevel(0),
39  fActivation(false),
40  fFirstHistoId(0),
41  fFirstNtupleColumnId(0),
42  fFileName(""),
43  fHistoDirectoryName(""),
44  fNtupleDirectoryName(""),
45  fLockFirstHistoId(false),
46  fLockFirstNtupleColumnId(false),
47  fLockFileName(false),
48  fLockHistoDirectoryName(false),
49  fLockNtupleDirectoryName(false),
50  fVerboseL1(type,1),
51  fVerboseL2(type,2),
52  fVerboseL3(type,3),
53  fVerboseL4(type,4),
54  fpVerboseL1(0),
55  fpVerboseL2(0),
56  fpVerboseL3(0),
57  fpVerboseL4(0),
58  fMessenger(0),
59  fNofActiveObjects(0),
60  fNofAsciiObjects(0),
61  fH1Informations(),
62  fH2Informations()
63 {
64  fMessenger = new G4AnalysisMessenger(this);
65 }
66 
67 //_____________________________________________________________________________
69 {
70  delete fMessenger;
71  // add delete G4HnInformation objects
72 }
73 
74 //
75 // protected methods
76 //
77 
78 //_____________________________________________________________________________
80  const G4String& unitName,
81  const G4String& fcnName,
82  G4double unit, G4Fcn fcn)
83 {
84  fH1Informations.push_back(
85  new G4HnInformation(name, unitName, unitName, fcnName, fcnName,
86  unit, unit, fcn, fcn));
87  ++fNofActiveObjects;
88 }
89 
90 //_____________________________________________________________________________
92  const G4String& xunitName,
93  const G4String& yunitName,
94  const G4String& xfcnName,
95  const G4String& yfcnName,
96  G4double xunit, G4double yunit,
97  G4Fcn xfcn, G4Fcn yfcn)
98 {
99  fH2Informations.push_back(
100  new G4HnInformation(name, xunitName, yunitName, xfcnName, yfcnName,
101  xunit, yunit, xfcn, yfcn));
102  ++fNofActiveObjects;
103 }
104 
105 //_____________________________________________________________________________
107 {
108  // Replace or add file extension .ascii
110  if ( name.find(".") != std::string::npos ) {
111  name.erase(name.find("."), name.length());
112  }
113  name.append(".ascii");
114 
115 #ifdef G4VERBOSE
116  if ( fpVerboseL3 )
117  fpVerboseL3->Message("write ASCII", "file", name);
118 #endif
119 
120  std::ofstream output(name, std::ios::out);
121  if ( ! output ) {
122  G4ExceptionDescription description;
123  description
124  << "Cannot open file. File name is not defined.";
125  G4Exception("G4VAnalysisManager::WriteAscii()",
126  "Analysis_W009", JustWarning, description);
127  return false;
128  }
129  output.setf( std::ios::scientific, std::ios::floatfield );
130 
131  G4bool result = WriteOnAscii(output);
132 
133 #ifdef G4VERBOSE
134  if ( fpVerboseL1 )
135  fpVerboseL1->Message("write ASCII", "file", name, result);
136 #endif
137 
138  return result;
139 }
140 
141 //_____________________________________________________________________________
143 {
144  G4HnInformation* info = GetInformation(type, id);
145 
146  if ( ! info ) return "";
147 
148  return info->fName;
149 }
150 
151 //_____________________________________________________________________________
153 {
154  G4HnInformation* info = GetInformation(type, id);
155 
156  if ( ! info ) return 1.0;
157 
158  return info->fXUnit;
159 }
160 
161 //_____________________________________________________________________________
163 {
164  G4HnInformation* info = GetInformation(type, id);
165 
166  if ( ! info ) return 1.0;
167 
168  return info->fYUnit;
169 }
170 
171 //_____________________________________________________________________________
173 {
174  G4HnInformation* info = GetInformation(type, id);
175 
176  if ( ! info ) return true;
177 
178  return info->fActivation;
179 }
180 
181 //_____________________________________________________________________________
183 {
184  G4HnInformation* info = GetInformation(type, id);
185 
186  if ( ! info ) return false;
187 
188  return info->fAscii;
189 }
190 
191 //_____________________________________________________________________________
193 {
194  G4double value = 1.;
195  if ( unit != "none" ) {
196  value = G4UnitDefinition::GetValueOf(unit);
197  if ( value == 0. ) value = 1.;
198  }
199  return value;
200 }
201 
202 //_____________________________________________________________________________
204 {
205  G4Fcn fcn = G4FcnIdentity;
206  if ( fcnName != "none" ) {
207  if ( fcnName == "log" ) fcn = std::log;
208  else if ( fcnName == "log10") fcn = std::log10;
209  else if ( fcnName == "exp" ) fcn = std::exp;
210  else {
211  G4ExceptionDescription description;
212  description
213  << " \"" << fcnName << "\" function is not supported." << G4endl
214  << " " << "No function will be applied to h1 values.";
215  G4Exception("G4AnalysisMessenger::GetFunction",
216  "Analysis_W013", JustWarning, description);
217  }
218  }
219  return fcn;
220 }
221 
222 //
223 // public methods
224 //
225 
226 //_____________________________________________________________________________
228 {
229  if ( verboseLevel == fVerboseLevel || verboseLevel < 0 ) return;
230 
231  fVerboseLevel = verboseLevel;
232 
233  if ( verboseLevel == 0 ) {
234  fpVerboseL1 = 0;
235  fpVerboseL2 = 0;
236  fpVerboseL3 = 0;
237  fpVerboseL4 = 0;
238  }
239  else if ( verboseLevel == 1 ) {
241  fpVerboseL2 = 0;
242  fpVerboseL3 = 0;
243  fpVerboseL4 = 0;
244  }
245  else if ( verboseLevel == 2 ) {
248  fpVerboseL3 = 0;
249  fpVerboseL4 = 0;
250  }
251  else if ( verboseLevel == 3 ) {
255  fpVerboseL4 = 0;
256  }
257  else {
262  }
263 }
264 
265 //_____________________________________________________________________________
267 {
268  if ( fFileName == "" ) {
269  G4ExceptionDescription description;
270  description
271  << "Cannot open file. File name is not defined.";
272  G4Exception("G4VAnalysisManager::OpenFile()",
273  "Analysis_W009", JustWarning, description);
274  return false;
275  }
276 
277  return OpenFile(fFileName);
278 }
279 
280 //_____________________________________________________________________________
282 {
283  if ( fLockFileName ) {
284  G4ExceptionDescription description;
285  description
286  << "Cannot set File name as its value was already used.";
287  G4Exception("G4VAnalysisManager::SetFileName()",
288  "Analysis_W009", JustWarning, description);
289  return false;
290  }
291 
292  fFileName = fileName;
293  return true;
294 }
295 
296 //_____________________________________________________________________________
298 {
299  if ( fLockHistoDirectoryName ) {
300  G4ExceptionDescription description;
301  description
302  << "Cannot set Histo directory name as its value was already used.";
303  G4Exception("G4VAnalysisManager::SetHistoDirectoryName()",
304  "Analysis_W009", JustWarning, description);
305  return false;
306  }
307 
308  fHistoDirectoryName = dirName;
309  return true;
310 }
311 
312 //_____________________________________________________________________________
314 {
315  if ( fLockNtupleDirectoryName ) {
316  G4ExceptionDescription description;
317  description
318  << "Cannot set Ntuple directory name as its value was already used.";
319  G4Exception("G4VAnalysisManager::SetNtupleDirectoryName()",
320  "Analysis_W010", JustWarning, description);
321  return false;
322  }
323 
324  fNtupleDirectoryName = dirName;
325  return true;
326 }
327 
328 //_____________________________________________________________________________
330 {
332  if ( name.find(".") == std::string::npos ) {
333  name.append(".");
334  name.append(GetFileType());
335  }
336 
337  return name;
338 }
339 
340 //_____________________________________________________________________________
342 {
343  if ( fLockFirstHistoId ) {
344  G4ExceptionDescription description;
345  description
346  << "Cannot set FirstHistoId as its value was already used.";
347  G4Exception("G4VAnalysisManager::SetFirstHistoId()",
348  "Analysis_W009", JustWarning, description);
349  return false;
350  }
351 
352  fFirstHistoId = firstId;
353  return true;
354 }
355 
356 //_____________________________________________________________________________
358 {
359  if ( fLockFirstNtupleColumnId ) {
360  G4ExceptionDescription description;
361  description
362  << "Cannot set FirstNtupleColumnId as its value was already used.";
363  G4Exception("G4VAnalysisManager::SetFirstHistoId()",
364  "Analysis_W010", JustWarning, description);
365  return false;
366  }
367 
368  fFirstNtupleColumnId = firstId;
369  return true;
370 }
371 
372 //_____________________________________________________________________________
374 {
375  if ( ! fActivation ) return true;
376 
377  return ( fNofActiveObjects > 0 );
378 }
379 
380 //_____________________________________________________________________________
382 {
383  return ( fNofAsciiObjects > 0 );
384 }
385 
386 //_____________________________________________________________________________
388 {
389  G4int index = id - fFirstHistoId;
390  if ( index < 0 || index >= GetNofH1s() ) {
391  G4ExceptionDescription description;
392  description << " " << "histo " << id << " does not exist.";
393  G4Exception("G4VAnalysisManager::GetH1Information()",
394  "Analysis_W007", JustWarning, description);
395  return 0;
396  }
397  return fH1Informations[index];
398 }
399 
400 //_____________________________________________________________________________
402 {
403  G4int index = id - fFirstHistoId;
404  if ( index < 0 || index >= GetNofH2s() ) {
405  G4ExceptionDescription description;
406  description << " " << "histo " << id << " does not exist.";
407  G4Exception("G4VAnalysisManager::GetH2Information()",
408  "Analysis_W007", JustWarning, description);
409  return 0;
410  }
411  return fH2Informations[index];
412 }
413 
414 //_____________________________________________________________________________
416 {
417  switch ( objType ) {
418  case kH1:
419  return GetH1Information(id);
420  break;
421 
422  case kH2:
423  return GetH2Information(id);
424  break;
425 
426  case kNtuple:
427  default:
428  return 0;
429  break;
430  }
431 
432  // Cannot reach this line
433  G4ExceptionDescription description;
434  description << "Wrong object type.";
435  G4Exception("G4VAnalysisManager::SetFirstHistoId()",
436  "Analysis_W010", FatalException, description);
437  return 0;
438 }
439 
440 //_____________________________________________________________________________
442  G4bool activation)
443 {
444 // Set activation to a given object
445 
446  G4HnInformation* info = GetInformation(type, id);
447 
448  if ( ! info ) return;
449 
450  // Do nothing if activation does not change
451  if ( info->fActivation == activation ) return;
452 
453  // Change activation and account it in fNofActiveObjects
454  info->fActivation = activation;
455  if ( activation )
456  fNofActiveObjects++;
457  else
458  fNofActiveObjects--;
459 }
460 
461 //_____________________________________________________________________________
463 {
464 // Set activation to all objects of the given type
465 
466  std::vector<G4HnInformation*>* informations;
467  if ( type == kH1 )
468  informations = &fH1Informations;
469  else if ( type == kH2 )
470  informations = &fH2Informations;
471  else if ( type == kNtuple ) {
472  return;
473  }
474  else {
475  G4ExceptionDescription description;
476  description << "Wrong object type.";
477  G4Exception("G4VAnalysisManager::SetActivation()",
478  "Analysis_W010", FatalException, description);
479  return;
480  }
481 
482  std::vector<G4HnInformation*>::iterator it;
483  for ( it = informations->begin(); it != informations->end(); it++ ) {
484  G4HnInformation* info = *it;
485 
486  // Do nothing if activation does not change
487  if ( info->fActivation == activation ) continue;
488 
489  // Change activation and account it in fNofActiveObjects
490  info->fActivation = activation;
491  if ( activation )
492  fNofActiveObjects++;
493  else
494  fNofActiveObjects--;
495  }
496 }
497 
498 //_____________________________________________________________________________
500 {
501  G4HnInformation* info = GetInformation(type, id);
502 
503  if ( ! info ) return;
504 
505  // Do nothing if ascii does not change
506  if ( info->fAscii == ascii ) return;
507 
508  // Change ascii and account it in fNofAsciiObjects
509  info->fAscii = ascii;
510  if ( ascii )
511  fNofAsciiObjects++;
512  else
513  fNofAsciiObjects--;
514 }
515 
516 //_____________________________________________________________________________
518 {
519  G4String fileType = fVerboseL1.GetType();
520  fileType.toLower();
521  return fileType;
522 }