Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Luter 345 Experiments
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Adjusting Variables at Run-Time
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Get shortened URL
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Variables Class== The variables class is the class that allows use of an input file and other classes to use those variables. In this class, all desired variables (ones that will be used throughout the simulation, often in different classes) should be defined with appropriate getters and setters, as well as a read function. This read function is the crux of the whole input file process, and thus will be focused upon heavily here. Below are a few example sections of a variables class, and explanations of the code are provided after. //this example requires the use of a string stream, a c++ class #include <sstream> //the following are definitions of some functions to read in the variables from the file #define READBOOL(myx) \ if (buf == #myx) \ { \ ss >> wert; \ myx = ReadBoolean(wert); \ } #define READDOUBLE(x) \ if (buf == #x) \ { \ ss >> doublewert; \ x = doublewert; \ } #define READINT(x) \ if (buf == #x) \ { \ ss >> intwert; \ x = intwert; \ } #define READSTRING(x) \ if (buf == #x) \ { \ ss >> x; \ } #define READVECTOR(v) \ if (buf == #v) \ { \ G4double x,y,z; \ if (ss >> x >> y >> z) \ v = G4ThreeVector(x, y, z); \ } //this is the default constructor S1Variables::S1Variables() { //Any defaults should be set in this space in case loadFromFile() fails } G4bool S1Variables::loadFromFile(G4String FileName) { //reads from some input file(S1.ini), which should list all variables in appropriate order ifstream infile; G4String line; infile.open(FileName); if (infile) { G4String outFileBase; G4String outFileTag; while (!infile.eof()) { if (getline(infile,line)) { G4String buf; G4String wert; G4double doublewert; G4int intwert; stringstream ss(line); ss >> buf; READDOUBLE(scintX); READDOUBLE(scintY); READDOUBLE(scintZ); READDOUBLE(bottomScintPos); READDOUBLE(topScintPos); READINT(blockThickness); READSTRING(blockMaterial); READSTRING(outFileBase); READSTRING(outFileTag); READINT(runNum); } } As stated earlier, all the variable names are already defined in the associated header file but are not initialized until the readfile function is executed. If this function fails, as occasionally happens when performing file io, it is always a good idea to initialize with some defaults in the constructor so the program can will not fail altogether. The defined functions at the top are crucial to the function as well as the string stream class, and explanations of these features will not be discussed here, as better and more in depth discussions can be found on any number of c++ reference pages. From the piece of the example presented, it is clear how this function works: An io stream is opened and that stream passes lines to the string stream, which is then parsed by the string stream and the corresponding data is read and saved by the predefined functions at the top. For this to work properly it is of great importance that the input file being read adhere to strict structure rules, and the example input file found in another section above can be used as reference. Once the above code is executed the variables have been read and can now be used throughout the simulation. In some cases the variables may require further manipulation. One common example of this is an output file name. In the code presented above, there are two variables read in called "outFileBase" and "outFileTag" that were used, along with some other variables found above, to create a file name that would later hold the nTuple produced by the simulation. Below is how this process was coded, albeit in a round-about way. Again for other methods of concatenation use your favorite c++ reference site. stringstream holdRunNum(""); stringstream holdThick(""); holdRunNum << runNum; holdThick << blockThickness; outFile = (outFileBase + holdThick.str() + outFileTag + holdRunNum.str() + ".root"); Here "outFile" is the actual variable held by the variables class, but there is no convenient way to craft the complete file name prior to reading the variables in. If running in batch mode this is a simple trick to automate the file naming process according to a convention, which makes further use of those files simple (i.e. looping through files in a ROOT macro). I will admit this string stream method is a poor method to use for concatenation so it may be better to find a new method when crafting your own simulation; However, it does work so if in a bind, feel free to utilize.
Summary:
Please note that all contributions to Luter 345 Experiments may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Luter 345 Experiments:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)