Geant4  10.02
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 
52 }
53 
54 //_____________________________________________________________________________
56 {
57 }
58 
59 //
60 // public functions
61 //
62 
63 //_____________________________________________________________________________
65 {
66  auto ntupleId = new G4UIparameter("NtupleId", 'i', false);
67  ntupleId->SetGuidance("Ntuple id");
68  ntupleId->SetParameterRange("NtupleId>=0");
69 
70  auto ntupleActivation = new G4UIparameter("NtupleActivation", 's', true);
71  ntupleActivation->SetGuidance("Ntuple activation");
72  ntupleActivation->SetDefaultValue("none");
73 
74  fSetActivationCmd = G4Analysis::make_unique<G4UIcommand>("/analysis/ntuple/setActivation", this);
75  G4String guidance("Set activation for the ntuple of given id");
76 
77  fSetActivationCmd->SetGuidance(guidance);
78  fSetActivationCmd->SetParameter(ntupleId);
79  fSetActivationCmd->SetParameter(ntupleActivation);
80  fSetActivationCmd->AvailableForStates(G4State_PreInit, G4State_Idle);
81 }
82 
83 //_____________________________________________________________________________
85 {
87  = G4Analysis::make_unique<G4UIcmdWithABool>("/analysis/ntuple/setActivationToAll", this);
88  G4String guidance("Set activation to all ntuples");
89  fSetActivationAllCmd->SetGuidance(guidance);
90  fSetActivationAllCmd->SetParameterName("AllNtupleActivation",false);
91 }
92 
93 //
94 // public methods
95 //
96 
97 //_____________________________________________________________________________
99 {
100  if ( command == fSetActivationCmd.get() ) {
101  // tokenize parameters in a vector
102  std::vector<G4String> parameters;
103  G4Analysis::Tokenize(newValues, parameters);
104  // check consistency
105  if ( G4int(parameters.size()) == command->GetParameterEntries() ) {
106  auto counter = 0;
107  auto id = G4UIcommand::ConvertToInt(parameters[counter++]);
108  auto activation = G4UIcommand::ConvertToBool(parameters[counter++]);
109  fManager->SetNtupleActivation(id, activation);
110  }
111  else {
112  // Should never happen but let's check anyway for consistency
113  G4ExceptionDescription description;
114  description
115  << "Got wrong number of \"" << command->GetCommandName()
116  << "\" parameters: " << parameters.size()
117  << " instead of " << command->GetParameterEntries()
118  << " expected" << G4endl;
119  G4Exception("G4NtupleMessenger::SetNewValue",
120  "Analysis_W013", JustWarning, description);
121  }
122  }
123  else if ( command == fSetActivationAllCmd.get() ) {
124  auto activation = fSetActivationAllCmd->GetNewBoolValue(newValues);
125  fManager->SetNtupleActivation(activation);
126  }
127 }
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4NtupleMessenger(G4VAnalysisManager *manager)
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