BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSMySQLTable.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSDebug.hh"
20#include "BDSMySQLTable.hh"
21#include "BDSMySQLVariable.hh"
22
23#include "G4String.hh"
24#include "G4Version.hh"
25
26#include <cstdlib>
27#include <string>
28#include <vector>
29
30BDSMySQLTable::BDSMySQLTable (G4String aTableName)
31{
32#ifdef BDSDEBUG
33 G4cout << __METHOD_NAME__ << " - creating new table named: " << aTableName << G4endl;
34#endif
35 SetValidVarTypes();
36 itsTableName = aTableName;
37 itsNVariables = 0;
38}
39
40void BDSMySQLTable::AddVariable(G4String aName, G4String aType)
41{
42 if(aName.empty())
43 {return;}
44
45 if(!isValidVarType(aType))
46 {return;}
47#ifdef BDSDEBUG
48 G4cout << __METHOD_NAME__ << " - adding variable: " << aName << " " << aType << " to table " << GetName() << G4endl;
49#endif
50 itsVar.push_back(new BDSMySQLVariable(aName,aType));
51 itsNVariables++;
52#ifdef BDSDEBUG
53 G4cout << __METHOD_NAME__ << " - nVariables = " << GetNVariables() << G4endl;
54#endif
55}
56
57void BDSMySQLTable::Print()
58{
59 G4cout << GetName() << G4endl;
60 for(int i=0; i<itsNVariables; i++)
61 {GetVariable(i)->Print();}
62}
63
64BDSMySQLVariable* BDSMySQLTable::GetVariable(G4String aVarName)
65{
66 for (G4int i=0; i<(G4int)itsVar.size(); i++)
67 {
68#if G4VERSION_NUMBER > 1099
69 if (G4StrUtil::icompare(itsVar[i]->GetName(), aVarName) == 0)
70#else
71 G4String::caseCompare cmpmode = G4String::ignoreCase;
72 if( (itsVar[i])->GetName().compareTo(aVarName,cmpmode)==0)
73#endif
74 {return itsVar[i];}
75 }
76 return nullptr; //if does not exist return null
77}
78
79BDSMySQLVariable* BDSMySQLTable::GetVariable(G4int aVarN)
80{
81 return itsVar[aVarN];
82}
83
84void BDSMySQLTable::SetValidVarTypes(){
85 _validVarTypes.push_back((G4String)"DOUBLE");
86 _validVarTypes.push_back((G4String)"INTEGER");
87 _validVarTypes.push_back((G4String)"STRING");
88
89}
90
91G4bool BDSMySQLTable::isValidVarType(G4String val){
92 G4bool result = false;
93 for(std::vector<G4String>::const_iterator iter = _validVarTypes.begin(); iter != _validVarTypes.end(); ++iter){
94 if((*iter) == val) result = true;
95 }
96 return result;
97}
98
99BDSMySQLTable::~BDSMySQLTable()
100{
101}
102
103
104void BDSMySQLVariable::Print(){
105 G4cout << __METHOD_NAME__ << G4endl;
106 std::string varType=GetVarType();
107 G4cout << GetName() << " " << GetVarType() << " " << G4endl;
108 for(int i=0; i<GetNVariables(); i++){
109 if(varType=="DOUBLE") G4cout << GetDblValue(i) << G4endl;
110 if(varType=="INTEGER")G4cout << GetIntValue(i) << G4endl;
111 if(varType=="STRING")G4cout << GetStrValue(i) << G4endl;
112 }
113}
114
A variable representation for mysql loader.
G4int GetNVariables() const
Accessor.
G4String GetName() const
Accessor.
G4double GetDblValue(G4int itemN) const
Accessor.
G4String GetVarType() const
Accessor.
G4int GetIntValue(G4int itemN) const
Accessor.
G4String GetStrValue(G4int itemN) const
Accessor.