19 int prec = std::numeric_limits<double>::max_digits10;
39 int pos1 = key.Index(
"<!--");
40 int pos2 = key.Index(
"-->");
42 TString s1=
"X", s2=
"X";
43 if(pos1>=0 && pos2>pos1){
47 s2.Replace(0,pos2+3,
"");
49 for(
int i=0; i<s1.Length(); i++)
if(s1[i]==
char(9)) {s1.Replace(i,1,
" ");}
50 for(
int i=0; i<s2.Length(); i++)
if(s2[i]==
char(9)) {s2.Replace(i,1,
" ");}
51 k = s1.IsWhitespace() && s2.IsWhitespace();
57 Variables_t::const_iterator it;
62 stream.precision(
prec);
63 for (it=variables.begin(); it!=variables.end(); ++it) {
64 TString
const& varName = it->first;
65 double value = it->second;
66 if (!key.Contains(
"=") || !key.Contains(varName) )
continue;
69 std::cout <<
"REPLACING\t'" << varName <<
"'\t";
74 if(0) { stream << scientific <<
value;}
else {stream <<
value;}
83 i = key.Index(varName);
88 key.Replace(0,i+varName.Length(),
"");
98 int pos1 = key.Index(word);
100 int pos2 = word.Length();
101 TString delimiter1 = key[pos1-1];
102 TString delimiter2 = key[pos1+pos2];
103 if(delimiter1==
"\"" && delimiter2==
"\"") key.Replace(pos1,pos2,keyword);
114 int pos = key.First(
"\"")+1;
115 key.Replace(0,pos,
"");
116 pos = key.First(
"\"");
117 TString varName = key;
119 pos = key.First(
"\"")+1;
120 key.Replace(0,pos,
"");
121 pos = key.First(
"\"")+1;
122 key.Replace(0,pos,
"");
123 pos = key.First(
"\"");
124 TString varValue = key;
125 varValue.Remove(pos);
127 TFormula formula(
"formula",varValue);
129 double value = formula.Eval(0);
133 std::cout <<
"VARIABLE\t'" << name <<
"'=\"" << varValue <<
"\" => " << value << std::endl;
135 variables[name.Data()]=
value;
145 TString lookFor[
n]={
"x=\"",
"y=\"",
"z=\"",
"r=\"",
"rmax=\"",
"rmin=\"",
"rmax1=\"",
"rmin1=\"",
"rmax2=\"",
"rmin2=\"",
146 "startphi=\"",
"deltaphi=\"",
"starttheta=\"",
"deltatheta=\"",
"ax=\"",
"by=\"",
"cz=\"",
"dx=\"",
"dy=\"",
"dz=\"",
147 "zcut1=\"",
"zcut2=\"",
"rlo=\"",
"rhi=\"",
"alpha=\"",
"theta=\"",
"phi=\"",
"numsides=\"",
"x1=\"",
"x2=\"",
"y1=\"",
"y2=\"",
"x3=\"",
"x4=\"",
148 "alpha1=\"",
"alpha2=\"",
"inst=\"",
"outst=\"",
"lowX=\"",
"lowY=\"",
"lowZ=\"",
"highX=\"",
"highY=\"",
"highZ=\"",
"zOrder=\"",
"zPosition=\"",
149 "xOffset=\"",
"yOffset=\"",
"scalingFactor=\"",
"v1x=\"",
"v1y=\"",
"v2x=\"",
"v2y=\"",
"v3x=\"",
"v3y=\"",
"v4x=\"",
"v4y=\"",
"v5x=\"",
"v5y=\"",
150 "v6x=\"",
"v6y=\"",
"v7x=\"",
"v7y=\"",
"v8x=\"",
"v8y=\"",
"dz=\""};
154 for(
int i=0; i<
n; i++)
155 if(key.Contains(lookFor[i])){
157 pos1 = key.Index(lookFor[i])+lookFor[i].Length();
158 key.Replace(0,pos1,
"");
159 pos2 = key.First(
"\"");
160 TString varFormula = key;
161 varFormula.Remove(pos2);
162 TFormula formula(
"",varFormula);
163 double value = formula.Eval(0);
166 std::cout <<
"FML " << lookFor[i] <<
"='" << varFormula <<
"' => " << value << std::endl;
170 stream.precision(
prec);
171 if(
sci) { stream << scientific <<
value;}
else {stream <<
value;}
175 key.Replace(pos1,varFormula.Length()+1,varValue);
182 int pos = key.Index(
"for")+3;
183 key.Replace(0,pos,
"");
184 pos = key.First(
"\"")+1;
185 key.Replace(0,pos,
"");
186 pos = key.First(
"\"");
190 pos = key.Index(
"to")+2;
191 key.Replace(0,pos,
"");
192 pos = key.First(
"\"")+1;
193 key.Replace(0,pos,
"");
194 pos = key.First(
"\"");
198 pos = key.Index(
"step")+1;
199 key.Replace(0,pos,
"");
200 pos = key.First(
"\"")+1;
201 key.Replace(0,pos,
"");
202 pos = key.First(
"\"");
207 theLoop.
varTo = value.Atof();
211 void getFileNames(TString inName, TString &outName1, TString &outName2){
212 int pos = inName.Index(
"_base");
213 TString temp = inName;
216 if (outName1.IsNull()) outName1 = temp+
".gdml";
217 if (outName2.IsNull()) outName2 = temp+
"_nowires.gdml";
222 variables[
"degree"] = M_PI / 180.0;
230 pos = key.Index(
"name=");
236 s.Replace(pos,s.Length()-pos,
"");
240 pos = key.Index(
"version=\"");
246 s.Replace(pos,s.Length()-pos,
"");
250 pos = key.Index(
"ref=\"");
251 if(pos>=0 && key.Contains(
"world")){
256 s.Replace(pos,s.Length()-pos,
"");
262 int preparse(TString inName=
"sbnd_base.gdml", TString outName1=
"sbnd.gdml", TString outName2=
""){
264 std::cout <<
"Preparsing '" << inName <<
"'"
265 "\n => '" << outName1 <<
"' (complete description)";
266 if (!outName2.IsNull())
267 std::cout <<
"\n => '" << outName2 <<
"' (without TPC wires)";
272 bool noWiresFiles = 1;
273 if(outName2!=
"") noWiresFiles = 0;
275 ofstream output1, output2;
276 output1.open(outName1);
277 if(!noWiresFiles) output2.open(outName2);
280 ifstream input(inName);
285 std::vector<std::string> loopLine;
288 std::vector<aSetup> stpList;
291 TString
name, loopkey;
296 uint64_t lineCounter = 0;
299 key.ReadLine(input,0);
300 if(
debug>3)
cout << lineCounter <<
"\t::\t" << key <<
"\n";
301 if ((key.Contains(
"<variable") && !key.Contains(
"<!--")) || (key.Contains(
"<constant") && !key.Contains(
"<!--"))) {
303 }
else if (key.Contains(
"<loop")) {
305 if(key.Contains(
"#wire")) isWire=1;
307 }
else if (key.Contains(
"</loop")) {
309 for (std::vector<std::string>::iterator it = loopLine.begin() ; it != loopLine.end(); ++it){
312 if(loopkey.Contains(
"--")) {
314 std::cout <<
"checking double minus sign " << std::endl;
317 loopkey.ReplaceAll(
"--",
"+");
321 output1 << loopkey << endl;
322 if(!isWire && !noWiresFiles) output2 << loopkey << endl;
326 else {keepLoop = (variables[theLoop.
varName] >= theLoop.
varTo);}
332 loopLine.push_back(key.Data());
334 if( !(
isComment(key) || key.IsWhitespace() ) ){
335 if(key.Contains(
"<setup")){
340 if(key.Contains(
"</setup")) {
341 stpList.push_back(theSetup);
349 output1 << key << endl;
350 if(!noWiresFiles) output2 << key << endl;
354 }
while(!input.eof());
358 if(!noWiresFiles) output2.close();
368 ver.Replace(0,pos+1,
"");
373 for (std::vector<aSetup>::iterator itx = stpList.begin() ; itx != stpList.end(); ++itx){
377 else {goAhead =
false;}
381 cout <<
"ERROR: Setup " <<
mySetup <<
" or its version not found." << endl;
384 TString inName1, inName2, comm;
385 inName1 = outName1+
"~";
386 inName2 = outName2+
"~";
387 TString inNameVec[]={inName1,inName2};
388 TString outNameVec[]={outName1,outName2};
390 const int kmax = 1+(!noWiresFiles);
392 for(
int k=0;
k<kmax;
k++){
394 rename(outNameVec[
k], inNameVec[k]);
396 std::cerr <<
"Error renaming '" << outNameVec[
k] <<
"' into '" << inNameVec[
k] <<
"': "
397 << strerror(errno) << std::endl;
400 output1.open(outNameVec[k]);
401 ifstream input1(inNameVec[k]);
405 key.ReadLine(input1,0);
406 if((!key.Contains(
"<setup") && !inSetup) ){
411 output1 << key << endl;
413 }
while(!input1.eof());
415 output1 <<
"\t<setup name=\"Default\" version=\"1.0\">" << endl;
416 output1 <<
"\t\t<world ref=\"volWorld\" />" << endl;
417 output1 <<
"\t</setup>" << endl;
418 output1 <<
"</gdml_simple_extension>" << endl;
421 remove(inNameVec[
k]);
436 static const option longopts[] = {
437 {
"output", required_argument, NULL,
'o' },
438 {
"nowires", optional_argument, NULL,
'w' },
439 {
"sci", optional_argument, NULL,
's' },
440 {
"prec", required_argument, NULL,
'p' },
441 {
"setup", required_argument, NULL,
'S' },
442 {
"debug", optional_argument, NULL,
'd' },
443 {
"help", no_argument, NULL,
'h' },
448 std::string shortopts =
":";
449 for (
auto const& longopt: longopts) {
450 if (!longopt.name)
break;
451 if (longopt.val == 0)
continue;
452 shortopts += (char) longopt.val;
453 if (longopt.has_arg != no_argument) shortopts +=
':';
454 if (longopt.has_arg == optional_argument) shortopts +=
':';
462 ((ch = getopt_long(argc, argv, shortopts.c_str(), longopts, NULL)) != -1)
479 TString par = optarg;
481 throw std::runtime_error(
"Invalid precision in -s option.");
487 TString par = optarg;
489 throw std::runtime_error(
"Invalid debug level in -d option.");
496 TString par = optarg;
498 throw std::runtime_error(
"Invalid precision in -s option.");
510 if (optind < (
int) argc)
inFile = argv[optind++];
512 if (optind < (
int) argc) {
513 std::cerr <<
"Spurious arguments: '" << argv[optind] <<
"'";
514 if (optind + 1 < (
int) argc)
515 std::cerr <<
" and " << (argc - optind - 1) <<
" more";
517 throw std::runtime_error(
"Too many arguments on the command line!");
524 int main(
int argc,
char **argv)
530 std::string programName = argv[0];
531 if (programName.rfind(
'/') != std::string::npos) programName.erase(programName.rfind(
'/'));
534 "\nGDML Preparser v 1.0 (gustavo.valdiviesso@unifal-mg.edu.br)"
536 "\n " << programName <<
" [file_base.gdml] [flags]"
538 "\nGenerate file.gdml with the following configuration flags:"
539 "\n-w , --nowires[=nowirename]"
540 "\n Generate file_nowires.gdml, ignoring all <loop> with <!--wire--> comment."
541 "\n-p , --prec=precision"
542 "\n Set numerical output precision (current precision: " <<
prec <<
" digits)"
543 "\n-s , --sci[=precision]"
544 "\n Uses scientific notation for numbers (optionally alter numerical output precision)"
545 "\n-o , --output=outputname"
546 "\n Uses this file name as main output name"
547 "\n-S , --setup=setupname"
548 "\n Uses the specified setup name instead of default one"
550 "\n Enable debugging messages"
554 "\n " << programName <<
" geometry_base.gdml --nowires"
555 "\n Outputs geometry.gdml and geometry_nowires.gdml with standard numeric notation."
557 "\n " << programName <<
" geometry_base.gdml -setup Cryostat:1.0"
558 "\n Outputs geometry.gdml making setup Cryostat (version 1.0) the volWorld required by LArSoft."
565 if (!input.is_open()) {
566 std::cerr <<
"Error opening '" <<
inFile <<
"': " << strerror(errno) << std::endl;
578 int pos =
outFile.Index(
".gdml");
579 noWires.Replace(pos,5,
"_nowires.gdml");
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
then if[["$THISISATEST"==1]]
BEGIN_PROLOG could also be cerr
void makeLoop(TString &key, aLoop &theLoop)
std::map< std::string, double > Variables_t
void parseArguments(unsigned int argc, char **argv)
void getFileNames(TString inName, TString &outName1, TString &outName2)
void evalFormulas(TString &key)
bool isComment(TString key)
void getSetup(TString key, aSetup &theSetup)
void replaceKeyword(TString &key, TString word, TString keyword)
int preparse(TString inName="sbnd_base.gdml", TString outName1="sbnd.gdml", TString outName2="")
void SetDefaultVariables(Variables_t &variables)
std::string stpWorldVolume
then echo File list $list not found else cat $list while read file do echo $file sed s
void getVariable(TString key, Variables_t &variables)
int main(int argc, char **argv)
void replaceVariable(TString &key, Variables_t const &variables)
BEGIN_PROLOG could also be cout