Geant4  10.02.p01
G4NtupleMessenger.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, 05/05/2015 (ivana@ipno.in2p3.fr)
29 
30 #include "G4NtupleMessenger.hh"
31 #include "G4VAnalysisManager.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include "G4UIcommand.hh"
35 #include "G4UIparameter.hh"
36 #include "G4UIcmdWithABool.hh"
37 
38 #include <iostream>
39 
40 using namespace G4Analysis;
41 
42 //_____________________________________________________________________________
44  : G4UImessenger(),
45  fManager(manager),
46  fSetActivationCmd(nullptr),
47  fSetActivationAllCmd(nullptr)
48 {
49  fNtupleDir = G4Analysis::make_unique<G4UIdirectory>("/analysis/ntuple/");
50  fNtupleDir->SetGuidance("ntuple control");
51 
54 }
55 
56 //_____________________________________________________________________________
58 {
59 }
60 
61 //
62 // public functions
63 //
64 
65 //_____________________________________________________________________________
67 {
68  auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
69  ntupleId->SetGuidance("Ntuple id");
70  ntupleId->SetParameterRange("NtupleId>=0");
71 
72  auto ntupleActivation = new G4UIparameter("NtupleActivation", 's', true);
73  ntupleActivation->SetGuidance("Ntuple activation");
74  ntupleActivation->SetDefaultValue("none");
75 
76  fSetActivationCmd = G4Analysis::make_unique<G4UIcommand>("/analysis/ntuple/setActivation", this);
77  G4String guidance("Set activation for the ntuple of given id");
78 
79  fSetActivationCmd->SetGuidance(guidance);
80  fSetActivationCmd->SetParameter(ntupleId);
81  fSetActivationCmd->SetParameter(ntupleActivation);
82  fSetActivationCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
83 }
84 
85 //_____________________________________________________________________________
87 {
89  = G4Analysis::make_unique<G4UIcmdWithABool>("/analysis/ntuple/setActivationToAll", this);
90  G4String guidance("Set activation to all ntuples");
91  fSetActivationAllCmd->SetGuidance(guidance);
92  fSetActivationAllCmd->SetParameterName("AllNtupleActivation",false);
93 }
94 
95 //
96 // public methods
97 //
98 
99 //_____________________________________________________________________________
101 {
102  if ( command == fSetActivationCmd.get() ) {
103  // tokenize parameters in a vector
104  std::vector<G4String> parameters;
105  G4Analysis::Tokenize(newValues, parameters);
106  // check consistency
107  if ( G4int(parameters.size()) == command->GetParameterEntries() ) {
108  auto counter = 0;
109  auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
110  auto activation = G4UIcommand::ConvertToBool(parameters[counter++]);
111  fManager->SetNtupleActivation(id, activation);
112  }
113  else {
114  // Should never happen but let's check anyway for consistency
115  G4ExceptionDescription description;
116  description
117  << "Got wrong number of \"" << command->GetCommandName()
118  << "\" parameters: " << parameters.size()
119  << " instead of " << command->GetParameterEntries()
120  << " expected" << G4endl;
121  G4Exception("G4NtupleMessenger::SetNewValue",
122  "Analysis_W013", JustWarning, description);
123  }
124  }
125  else if ( command == fSetActivationAllCmd.get() ) {
126  auto activation = fSetActivationAllCmd->GetNewBoolValue(newValues);
127  fManager->SetNtupleActivation(activation);
128  }
129 }
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4NtupleMessenger(G4VAnalysisManager *manager)
std::unique_ptr< G4UIdirectory > fNtupleDir
int G4int
Definition: G4Types.hh:78
std::unique_ptr< G4UIcmdWithABool > fSetActivationAllCmd
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:425
static G4int ConvertToInt(const char *st)
Definition: G4UIcommand.cc:435
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
virtual void SetNewValue(G4UIcommand *command, G4String value) final
void Tokenize(const G4String &line, std::vector< G4String > &tokens)
G4VAnalysisManager * fManager
Associated class.
#define G4endl
Definition: G4ios.hh:61
virtual ~G4NtupleMessenger()
void SetNtupleActivation(G4bool activation)
std::unique_ptr< G4UIcommand > fSetActivationCmd
G4int GetParameterEntries() const
Definition: G4UIcommand.hh:143