G4VAnalysisManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 
00028 // Author: Ivana Hrivnacova, 15/06/2011  (ivana@ipno.in2p3.fr)
00029 
00030 #include "G4VAnalysisManager.hh"
00031 #include "G4AnalysisMessenger.hh"
00032 #include "G4UnitsTable.hh"
00033 
00034 #include <iostream>
00035 
00036 //_____________________________________________________________________________
00037 G4VAnalysisManager::G4VAnalysisManager(const G4String& type)
00038   : fVerboseLevel(0),
00039     fActivation(false),
00040     fFirstHistoId(0),
00041     fFirstNtupleColumnId(0),
00042     fFileName(""), 
00043     fHistoDirectoryName(""), 
00044     fNtupleDirectoryName(""),
00045     fLockFirstHistoId(false),
00046     fLockFirstNtupleColumnId(false),
00047     fLockFileName(false),
00048     fLockHistoDirectoryName(false), 
00049     fLockNtupleDirectoryName(false),
00050     fVerboseL1(type,1),
00051     fVerboseL2(type,2),
00052     fVerboseL3(type,3),
00053     fVerboseL4(type,4),
00054     fpVerboseL1(0),
00055     fpVerboseL2(0),
00056     fpVerboseL3(0),
00057     fpVerboseL4(0),
00058     fMessenger(0),
00059     fNofActiveObjects(0),
00060     fNofAsciiObjects(0),
00061     fH1Informations(),
00062     fH2Informations()
00063 {
00064   fMessenger = new G4AnalysisMessenger(this);
00065 }
00066 
00067 //_____________________________________________________________________________
00068 G4VAnalysisManager::~G4VAnalysisManager()
00069 {
00070   delete fMessenger;
00071   // add delete G4HnInformation objects
00072 }
00073 
00074 // 
00075 // protected methods
00076 //
00077 
00078 //_____________________________________________________________________________
00079 void G4VAnalysisManager::AddH1Information(const G4String& name,
00080                                           const G4String& unitName,
00081                                           const G4String& fcnName,
00082                                           G4double unit, G4Fcn fcn)
00083 {
00084   fH1Informations.push_back(
00085     new G4HnInformation(name, unitName, unitName, fcnName, fcnName,
00086                         unit, unit, fcn, fcn));
00087   ++fNofActiveObjects;
00088 }  
00089 
00090 //_____________________________________________________________________________
00091 void  G4VAnalysisManager::AddH2Information(const G4String& name,
00092                                            const G4String& xunitName, 
00093                                            const G4String& yunitName,
00094                                            const G4String& xfcnName,
00095                                            const G4String& yfcnName,
00096                                            G4double xunit, G4double yunit,
00097                                            G4Fcn xfcn, G4Fcn yfcn) 
00098 {
00099   fH2Informations.push_back(
00100     new G4HnInformation(name, xunitName, yunitName, xfcnName, yfcnName,
00101                         xunit, yunit, xfcn, yfcn));
00102   ++fNofActiveObjects;
00103 }  
00104 
00105 //_____________________________________________________________________________
00106 G4bool G4VAnalysisManager::WriteAscii()
00107 {
00108   // Replace or add file extension .ascii
00109   G4String name(fFileName);
00110   if ( name.find(".") != std::string::npos ) { 
00111     name.erase(name.find("."), name.length()); 
00112   }
00113   name.append(".ascii");
00114 
00115 #ifdef G4VERBOSE
00116   if ( fpVerboseL3 ) 
00117     fpVerboseL3->Message("write ASCII", "file", name);
00118 #endif
00119      
00120   std::ofstream output(name, std::ios::out);
00121   if ( ! output ) {
00122     G4ExceptionDescription description;
00123     description 
00124       << "Cannot open file. File name is not defined.";
00125     G4Exception("G4VAnalysisManager::WriteAscii()",
00126                 "Analysis_W009", JustWarning, description);
00127     return false;
00128   }
00129   output.setf( std::ios::scientific, std::ios::floatfield );
00130 
00131   G4bool result = WriteOnAscii(output);
00132 
00133 #ifdef G4VERBOSE
00134     if ( fpVerboseL1 ) 
00135       fpVerboseL1->Message("write ASCII", "file",  name, result);
00136 #endif
00137   
00138   return result;
00139 }     
00140 
00141 //_____________________________________________________________________________
00142 G4String G4VAnalysisManager::GetName(ObjectType type, G4int id) const
00143 {
00144   G4HnInformation* info = GetInformation(type, id);
00145 
00146   if ( ! info ) return "";
00147     
00148   return info->fName;
00149 }    
00150 
00151 //_____________________________________________________________________________
00152 G4double G4VAnalysisManager::GetXUnit(ObjectType type, G4int id) const
00153 {
00154   G4HnInformation* info = GetInformation(type, id);
00155 
00156   if ( ! info ) return 1.0;
00157   
00158   return info->fXUnit;
00159 }    
00160 
00161 //_____________________________________________________________________________
00162 G4double G4VAnalysisManager::GetYUnit(ObjectType type, G4int id) const
00163 {
00164   G4HnInformation* info = GetInformation(type, id);
00165 
00166   if ( ! info ) return 1.0;
00167   
00168   return info->fYUnit;
00169 }    
00170 
00171 //_____________________________________________________________________________
00172 G4bool G4VAnalysisManager::GetActivation(ObjectType type, G4int id) const
00173 {
00174   G4HnInformation* info = GetInformation(type, id);
00175 
00176   if ( ! info ) return true;
00177   
00178   return info->fActivation;
00179 }    
00180 
00181 //_____________________________________________________________________________
00182 G4bool G4VAnalysisManager::GetAscii(ObjectType type, G4int id) const
00183 {
00184   G4HnInformation* info = GetInformation(type, id);
00185 
00186   if ( ! info ) return false;
00187   
00188   return info->fAscii;
00189 }    
00190 
00191 //_____________________________________________________________________________
00192 G4double G4VAnalysisManager::GetUnitValue(const G4String& unit) const
00193 {
00194    G4double value = 1.;
00195    if ( unit != "none" ) {
00196      value = G4UnitDefinition::GetValueOf(unit);
00197      if ( value == 0. ) value = 1.; 
00198    }  
00199    return value;
00200 }   
00201 
00202 //_____________________________________________________________________________
00203 G4Fcn G4VAnalysisManager::GetFunction(const G4String& fcnName) const
00204 {
00205   G4Fcn fcn = G4FcnIdentity;
00206    if ( fcnName != "none" ) {
00207     if      ( fcnName == "log" )  fcn = std::log;
00208     else if ( fcnName == "log10") fcn = std::log10;
00209     else if ( fcnName == "exp" )  fcn = std::exp;
00210     else {
00211       G4ExceptionDescription description;
00212       description 
00213         << "    \"" << fcnName << "\" function is not supported." << G4endl
00214         << "    " << "No function will be applied to h1 values.";
00215       G4Exception("G4AnalysisMessenger::GetFunction",
00216                 "Analysis_W013", JustWarning, description);
00217     }              
00218   }
00219   return fcn;            
00220 }
00221     
00222 // 
00223 // public methods
00224 //
00225 
00226 //_____________________________________________________________________________
00227 void G4VAnalysisManager::SetVerboseLevel(G4int verboseLevel) 
00228 {
00229   if ( verboseLevel == fVerboseLevel || verboseLevel < 0 ) return;
00230   
00231   fVerboseLevel = verboseLevel;
00232   
00233   if ( verboseLevel == 0 ) {
00234     fpVerboseL1 = 0;
00235     fpVerboseL2 = 0;
00236     fpVerboseL3 = 0;
00237     fpVerboseL4 = 0;
00238   }
00239   else if ( verboseLevel == 1 ) {  
00240     fpVerboseL1 = &fVerboseL1;
00241     fpVerboseL2 = 0;
00242     fpVerboseL3 = 0;
00243     fpVerboseL4 = 0;
00244   }
00245   else if ( verboseLevel == 2 ) {  
00246     fpVerboseL1 = &fVerboseL1;
00247     fpVerboseL2 = &fVerboseL2;
00248     fpVerboseL3 = 0;
00249     fpVerboseL4 = 0;
00250   }
00251   else if ( verboseLevel == 3 ) {  
00252     fpVerboseL1 = &fVerboseL1;
00253     fpVerboseL2 = &fVerboseL2;
00254     fpVerboseL3 = &fVerboseL3;
00255     fpVerboseL4 = 0;
00256   }
00257   else {
00258     fpVerboseL1 = &fVerboseL1;
00259     fpVerboseL2 = &fVerboseL2;
00260     fpVerboseL3 = &fVerboseL3;
00261     fpVerboseL4 = &fVerboseL4;
00262   }
00263 }  
00264 
00265 //_____________________________________________________________________________
00266 G4bool G4VAnalysisManager::OpenFile()
00267 {
00268   if ( fFileName == "" ) {
00269     G4ExceptionDescription description;
00270     description 
00271       << "Cannot open file. File name is not defined.";
00272     G4Exception("G4VAnalysisManager::OpenFile()",
00273                 "Analysis_W009", JustWarning, description);
00274     return false;
00275   }           
00276   
00277   return OpenFile(fFileName);
00278 }     
00279 
00280 //_____________________________________________________________________________
00281 G4bool G4VAnalysisManager::SetFileName(const G4String& fileName) 
00282 {
00283   if ( fLockFileName ) {
00284     G4ExceptionDescription description;
00285     description 
00286       << "Cannot set File name as its value was already used.";
00287     G4Exception("G4VAnalysisManager::SetFileName()",
00288                 "Analysis_W009", JustWarning, description);
00289     return false;
00290   }              
00291 
00292   fFileName = fileName;
00293   return true;
00294 }  
00295 
00296 //_____________________________________________________________________________
00297 G4bool G4VAnalysisManager::SetHistoDirectoryName(const G4String& dirName) 
00298 {
00299   if ( fLockHistoDirectoryName ) {
00300     G4ExceptionDescription description;
00301     description 
00302       << "Cannot set Histo directory name as its value was already used.";
00303     G4Exception("G4VAnalysisManager::SetHistoDirectoryName()",
00304                 "Analysis_W009", JustWarning, description);
00305     return false;
00306   }              
00307 
00308   fHistoDirectoryName = dirName;
00309   return true;
00310 }  
00311 
00312 //_____________________________________________________________________________
00313 G4bool G4VAnalysisManager::SetNtupleDirectoryName(const G4String& dirName) 
00314 {
00315   if ( fLockNtupleDirectoryName ) {
00316     G4ExceptionDescription description;
00317     description 
00318       << "Cannot set Ntuple directory name as its value was already used.";
00319     G4Exception("G4VAnalysisManager::SetNtupleDirectoryName()",
00320                 "Analysis_W010", JustWarning, description);
00321     return false;
00322   }              
00323 
00324   fNtupleDirectoryName = dirName;
00325   return true;
00326 }  
00327 
00328 //_____________________________________________________________________________
00329 G4String G4VAnalysisManager::GetFullFileName() const 
00330 {  
00331   G4String name(fFileName);
00332   if ( name.find(".") == std::string::npos ) { 
00333     name.append(".");
00334     name.append(GetFileType());
00335   }  
00336 
00337   return name;
00338 }  
00339 
00340 //_____________________________________________________________________________
00341 G4bool G4VAnalysisManager::SetFirstHistoId(G4int firstId) 
00342 {
00343   if ( fLockFirstHistoId ) {
00344     G4ExceptionDescription description;
00345     description 
00346       << "Cannot set FirstHistoId as its value was already used.";
00347     G4Exception("G4VAnalysisManager::SetFirstHistoId()",
00348                 "Analysis_W009", JustWarning, description);
00349     return false;
00350   }              
00351 
00352   fFirstHistoId = firstId;
00353   return true;
00354 }  
00355 
00356 //_____________________________________________________________________________
00357 G4bool G4VAnalysisManager::SetFirstNtupleColumnId(G4int firstId) 
00358 {
00359   if ( fLockFirstNtupleColumnId ) {
00360     G4ExceptionDescription description;
00361     description 
00362       << "Cannot set FirstNtupleColumnId as its value was already used.";
00363     G4Exception("G4VAnalysisManager::SetFirstHistoId()",
00364                 "Analysis_W010", JustWarning, description);
00365     return false;
00366   }              
00367 
00368   fFirstNtupleColumnId = firstId;
00369   return true;
00370 }
00371 
00372 //_____________________________________________________________________________
00373 G4bool G4VAnalysisManager::IsActive() const
00374 {
00375   if ( ! fActivation ) return true;
00376   
00377   return ( fNofActiveObjects > 0 );
00378 }  
00379 
00380 //_____________________________________________________________________________
00381 G4bool G4VAnalysisManager::IsAscii() const
00382 {
00383   return ( fNofAsciiObjects > 0 );
00384 }  
00385 
00386 //_____________________________________________________________________________
00387 G4HnInformation* G4VAnalysisManager::GetH1Information(G4int id) const
00388 {
00389   G4int index = id - fFirstHistoId;
00390   if ( index < 0 || index >= GetNofH1s() ) {
00391     G4ExceptionDescription description;
00392     description << "      " << "histo " << id << " does not exist.";
00393     G4Exception("G4VAnalysisManager::GetH1Information()",
00394                 "Analysis_W007", JustWarning, description);
00395     return 0;         
00396   }
00397   return fH1Informations[index];
00398 }    
00399 
00400 //_____________________________________________________________________________
00401 G4HnInformation* G4VAnalysisManager::GetH2Information(G4int id) const
00402 {
00403   G4int index = id - fFirstHistoId;
00404   if ( index < 0 || index >= GetNofH2s() ) {
00405     G4ExceptionDescription description;
00406     description << "      " << "histo " << id << " does not exist.";
00407     G4Exception("G4VAnalysisManager::GetH2Information()",
00408                 "Analysis_W007", JustWarning, description);
00409     return 0;         
00410   }
00411   return fH2Informations[index];
00412 }    
00413     
00414 //_____________________________________________________________________________
00415 G4HnInformation* G4VAnalysisManager::GetInformation(ObjectType objType, G4int id) const
00416 {
00417   switch ( objType ) {
00418     case kH1: 
00419       return GetH1Information(id);
00420       break;
00421       
00422     case kH2: 
00423       return GetH2Information(id);
00424       break;
00425       
00426     case kNtuple:
00427     default:
00428       return 0;
00429       break;
00430   }    
00431 
00432   // Cannot reach this line
00433   G4ExceptionDescription description;
00434   description << "Wrong object type.";
00435   G4Exception("G4VAnalysisManager::SetFirstHistoId()",
00436               "Analysis_W010", FatalException, description);
00437   return 0;
00438 }    
00439 
00440 //_____________________________________________________________________________
00441 void  G4VAnalysisManager::SetActivation(ObjectType type, G4int id, 
00442                                         G4bool activation)
00443 {
00444 // Set activation to a given object
00445 
00446   G4HnInformation* info = GetInformation(type, id);
00447 
00448   if ( ! info ) return;
00449 
00450   // Do nothing if activation does not change
00451   if ( info->fActivation == activation ) return;
00452   
00453   // Change activation and account it in fNofActiveObjects
00454   info->fActivation = activation;
00455   if ( activation ) 
00456     fNofActiveObjects++;
00457   else
00458     fNofActiveObjects--;   
00459 }    
00460 
00461 //_____________________________________________________________________________
00462 void  G4VAnalysisManager::SetActivation(ObjectType type, G4bool activation)
00463 {
00464 // Set activation to all objects of the given type
00465 
00466   std::vector<G4HnInformation*>* informations;
00467   if ( type == kH1 ) 
00468     informations = &fH1Informations;
00469   else if ( type == kH2 )
00470     informations = &fH2Informations;
00471   else  if ( type == kNtuple ) {
00472     return;
00473   }
00474   else {  
00475     G4ExceptionDescription description;
00476     description << "Wrong object type.";
00477     G4Exception("G4VAnalysisManager::SetActivation()",
00478               "Analysis_W010", FatalException, description);
00479     return;
00480   }   
00481   
00482   std::vector<G4HnInformation*>::iterator it;
00483   for ( it = informations->begin(); it != informations->end(); it++ ) {
00484     G4HnInformation* info = *it;
00485 
00486     // Do nothing if activation does not change
00487     if ( info->fActivation == activation ) continue;
00488   
00489     // Change activation and account it in fNofActiveObjects
00490     info->fActivation = activation;
00491     if ( activation ) 
00492       fNofActiveObjects++;
00493     else
00494       fNofActiveObjects--; 
00495   }     
00496 }    
00497 
00498 //_____________________________________________________________________________
00499 void  G4VAnalysisManager::SetAscii(ObjectType type, G4int id, G4bool ascii)
00500 {
00501   G4HnInformation* info = GetInformation(type, id);
00502 
00503   if ( ! info ) return;
00504 
00505   // Do nothing if ascii does not change
00506   if ( info->fAscii == ascii ) return;
00507   
00508   // Change ascii and account it in fNofAsciiObjects
00509   info->fAscii = ascii;
00510   if ( ascii ) 
00511     fNofAsciiObjects++;
00512   else
00513     fNofAsciiObjects--;   
00514 }    
00515 
00516 //_____________________________________________________________________________
00517 G4String G4VAnalysisManager::GetFileType() const 
00518 {
00519   G4String fileType = fVerboseL1.GetType();
00520   fileType.toLower();
00521   return fileType;
00522 }                 

Generated on Mon May 27 17:50:09 2013 for Geant4 by  doxygen 1.4.7