10 string hd_msg =
" >> GDML Factory: >> ";
12 map<string, detector> dets;
25 string dname = it->first;
26 string fname = dname +
".gdml";
29 cout << hd_msg <<
" Importing Detector: " << dname <<
" with " <<
factoryType <<
" factory. " << endl;
32 QFile gdet(fname.c_str());
36 cout << hd_msg <<
" Failed to open geometry file " << fname <<
" for system: " << dname <<
". Maybe the filename doesn't exist? Exiting." << endl;
40 QDomDocument domDocument;
42 if(!domDocument.setContent(&gdet))
45 cout << hd_msg <<
" Failed to open geometry file " << fname <<
" for system: " << dname <<
". Wrong XML syntax. Exiting." << endl;
52 map<string, double> gconstantMap;
53 map<string, gposition> gpositionsMap;
54 map<string, grotation> grotationsMap;
55 map<string, glogicV> glogicVMap;
56 map<string, gsolid> gsolidsMap;
57 map<string, gphysV> gphysVMap;
58 map<string, goperation> operationMap;
59 map<string, string> auxiliaryMap;
60 map<string, gposition> widthOffsetMap;
63 gpositionsMap[
"unity"] =
gposition(0, 0, 0,
"mm");
64 grotationsMap[
"unity"] =
grotation(
"0",
"0",
"0",
"deg");
70 QDomElement docElem = domDocument.documentElement();
71 QDomNode n = docElem.firstChild();
76 QDomElement e = n.toElement();
80 if(!e.isNull() && e.tagName().toStdString() ==
"define")
83 QDomNode nn= e.firstChild();
87 QDomElement ee = nn.toElement();
90 if(ee.tagName().toStdString() ==
"constant")
97 gconstantMap[constant_name] =
resolve_strings(constant_name, v, gconstantMap);
98 if(
verbosity>0) cout <<
"map value of " << constant_name <<
" " << gconstantMap[constant_name] << endl;
103 if(ee.tagName().toStdString() ==
"position")
108 if(
verbosity>0) cout <<
"!!!!!!!!!!!!!! position name : " << position_name <<
" !!!!!!!!!!!!!!!!! " << endl;
113 if(
verbosity>0) cout <<
"x_p is obtained " <<
"x_p = " << x_p << endl;
116 if(
verbosity>0) cout <<
"y_p is obtained " <<
"y_p = " << y_p << endl;
119 if(
verbosity>0) cout <<
"z_p is obtained " <<
"z_p = " << z_p << endl;
123 gpositionsMap[position_name] =
gposition(x_p, y_p, z_p, lunit);
127 if(ee.tagName().toStdString() ==
"rotation")
132 if(
verbosity>0) cout <<
"!!!!!!!!!! rotation name : " << rotation_name <<
"!!!!!!!!!!!!!!!!! " << endl;
136 if(
verbosity>0) cout <<
"a_r is obtained " << a_r << endl;
139 if(
verbosity>0) cout <<
"b_r is obtained " << b_r << endl;
142 if(
verbosity>0) cout <<
"c_r is obtained " << c_r << endl;
150 grotationsMap[rotation_name] =
grotation(a_r, b_r, c_r, aunit);
166 if(!e.isNull() && e.tagName().toStdString() ==
"solids")
169 QDomNode nn= e.firstChild();
170 vector<string> seconds;
171 while( !nn.isNull() )
173 QDomElement ee = nn.toElement();
175 string type =
togType(ee.tagName().toStdString());
178 if(type ==
"union" || type ==
"subtraction")
180 string first, second;
182 string pos =
"unity";
183 string rot =
"unity";
184 string optype, renamed_second;
188 QDomNode nnn= ee.firstChild();
189 while( !nnn.isNull() )
191 QDomElement eee = nnn.toElement();
193 if(eee.tagName().toStdString() ==
"first")
196 if(eee.tagName().toStdString() ==
"second")
200 seconds.push_back(second);
203 for(
unsigned int i=0; i<seconds.size(); i++)
206 if(seconds[i] == second)
211 renamed_second = second +
"_" +
stringify(reused);
213 renamed_second = second;
218 if(eee.tagName().toStdString() ==
"positionref")
221 if(eee.tagName().toStdString() ==
"rotationref")
224 nnn=nnn.nextSibling();
228 optype =
"Operation: " + first +
"+" + second;
230 if(type ==
"subtraction")
231 optype =
"Operation: " + first +
"-" + second;
234 gsolidsMap[opname] =
gsolid(optype,
"0");
236 operationMap[opname] =
goperation(first, second, renamed_second, pos, rot);
246 if(
verbosity>0) cout <<
"-------solids name : " << name <<
" solids type : " <<type <<
"--------" << endl;
257 if(!e.isNull() && e.tagName().toStdString() ==
"structure")
259 QDomNode nn= e.firstChild();
260 while( !nn.isNull() )
262 QDomElement ee = nn.toElement();
265 if(!ee.isNull() && ee.tagName().toStdString() ==
"volume")
270 QDomNode nnn= ee.firstChild();
273 string auxtype, auxvalue;
275 while( !nnn.isNull() )
277 QDomElement eee = nnn.toElement();
284 if(eee.tagName().toStdString() ==
"physvol")
286 string posref =
"unity";
287 string rotref =
"unity";
290 QDomNode nnnn= eee.firstChild();
291 while( !nnnn.isNull() )
293 QDomElement eeee = nnnn.toElement();
295 if(eeee.tagName().toStdString() ==
"volumeref")
296 physvol_name=eeee.attributeNode(
"ref").value().toStdString();
298 if(eeee.tagName().toStdString() ==
"positionref")
301 if(eeee.tagName().toStdString() ==
"rotationref")
304 nnnn=nnnn.nextSibling();
307 gphysVMap[physvol_name] =
gphysV(posref, rotref);
311 if(eee.tagName().toStdString() ==
"materialref")
315 if(eee.tagName().toStdString() ==
"solidref")
319 if(eee.tagName().toStdString() ==
"replicavol")
323 double j = gconstantMap[replica_num];
326 QDomNode nnnn= eee.firstChild();
328 while( !nnnn.isNull() )
331 QDomElement eeee = nnnn.toElement();
333 if(eeee.tagName().toStdString() ==
"volumeref")
334 volume_ref=eeee.attributeNode(
"ref").value().toStdString();
336 if(eeee.tagName().toStdString() ==
"replicate_along_axis")
338 QDomNode n_5= eeee.firstChild();
340 string x_s, y_s, z_s, direction;
343 string width_ref, width_unit, offset_ref, offset_unit;
345 while( !n_5.isNull() )
348 QDomElement e_5 = n_5.toElement();
349 if(e_5.tagName().toStdString() ==
"direction")
355 if(e_5.tagName().toStdString() ==
"width")
359 width = gconstantMap[width_ref];
361 if(e_5.tagName().toStdString() ==
"offset")
365 offset = gconstantMap[offset_ref];
367 n_5 = n_5.nextSibling();
371 if(x_s ==
"1") direction =
"1";
372 if(y_s ==
"1") direction =
"2";
373 if(z_s ==
"1") direction =
"3";
376 string replica_type =
"Replica "+ volume_ref +
"," +
"root" +
","+ direction+
"," +
stringify(j) +
"," +
stringify(width)+
"*"+width_unit +
"," +
stringify(offset)+
"*"+offset_unit;
378 gsolidsMap[solidref].type = replica_type;
382 nnnn=nnnn.nextSibling();
386 if(eee.tagName().toStdString() ==
"auxiliary")
392 nnn=nnn.nextSibling();
396 glogicVMap[lvol_name] =
glogicV(matref, solidref);
397 auxiliaryMap[auxtype] = auxvalue;
411 for(map<string, glogicV>::iterator itt=glogicVMap.begin(); itt != glogicVMap.end(); itt++)
416 string b =
"no description";
428 if(gphysVMap.find(itt->first) == gphysVMap.end())
430 if(
verbosity>0)cout <<
" !! Attention: position ref doesn't exist. Defaulting to (0,0,0)" << endl;
436 string posref_name = gphysVMap[itt->first].positionRef;
438 if(gpositionsMap.find(posref_name) == gpositionsMap.end())
440 if(
verbosity>0)cout <<
" !! Attention: position ref for " << posref_name <<
" not found. Defaulting to (0,0,0)" << endl;
446 if(
verbosity>0)cout <<
"position get_dimensions() : " << gpositionsMap[posref_name].get_dimensions()<< endl;
451 if(gphysVMap.find(itt->first) == gphysVMap.end())
453 if(
verbosity>0)cout <<
" !! Attention: rotation ref doesn't exist. Defaulting to (0,0,0)" << endl;
458 string rotref_name = gphysVMap[itt->first].rotationRef;
460 if(grotationsMap.find(rotref_name) == grotationsMap.end())
462 if(
verbosity>0)cout <<
" !! Attention: rotation ref for " << rotref_name <<
" not found. Defaulting to (0,0,0)" << endl;
469 if(
verbosity>0)cout <<
"rotation get_dimensions() : " << grotationsMap[rotref_name].get_dimensions() << endl;
473 string solid_type = gsolidsMap[itt->second.solidRef].type;
476 string solid_dimensions = gsolidsMap[itt->second.solidRef].dimension;
479 gt.
add_data(itt->second.materialRef);
484 if (itt->first ==
"World")
505 for(map<string, goperation>::iterator itt=operationMap.begin(); itt != operationMap.end(); itt++)
509 for(
unsigned int i=0; i<2; i++)
514 string b =
"no description";
519 string g =
"Component";
529 if(
verbosity>0)cout <<
"-------------------first--------------------"<< endl;
533 gt.
add_data(itt->second.renamed_second);
535 if(
verbosity>0)cout <<
"--------------------second----------------------" << endl;
548 else if(fs ==
"second")
550 posref_name = itt->second.pos;
552 if(gpositionsMap.find(posref_name) == gpositionsMap.end())
554 if(
verbosity>0)cout <<
" !! Attention: position ref for " << posref_name <<
" not found. Defaulting to (0,0,0)" << endl;
560 if(
verbosity>0)cout <<
"position ref " << posref_name <<
" position " << gpositionsMap[posref_name].get_dimensions() << endl;
567 else if(fs ==
"second")
569 rotref_name = itt->second.rot;
571 if(grotationsMap.find(rotref_name) == grotationsMap.end())
573 if(
verbosity>0)cout <<
" !! Attention: position ref for " << rotref_name <<
" not found. Defaulting to (0,0,0)" << endl;
581 if(
verbosity>0)cout <<
"rotation ref " << rotref_name <<
" rotation " << grotationsMap[rotref_name].get_dimensions() << endl;
592 lvol_name = itt->second.first;
593 else if(fs ==
"second")
594 lvol_name = itt->second.second;
596 string solid_type = gsolidsMap[lvol_name].type;
600 string solid_dimensions = gsolidsMap[lvol_name].dimension;
635 ostream &operator<<(ostream &stream, map<string, glogicV> glogicVMap)
637 for(map<string, glogicV>::iterator it = glogicVMap.begin(); it != glogicVMap.end(); it++)
638 cout <<
" Logic Volume :" << it->first <<
" solidRef: " << it->second.solidRef <<
", materialRef: " << it->second.materialRef << endl;
644 ostream &operator<<(ostream &stream, map<string, gposition> gpositionsMap)
646 for(map<string, gposition>::iterator it = gpositionsMap.begin(); it != gpositionsMap.end(); it++)
647 cout <<
" Position " << it->first <<
": x: " << it->second.x <<
", y: " << it->second.y <<
", z: " << it->second.z <<
" lunit: " << it->second.unit << endl;
653 ostream &operator<<(ostream &stream,map<string, grotation> grotationsMap )
655 for(map<string,grotation>::iterator it = grotationsMap.begin(); it != grotationsMap.end(); it++)
656 cout <<
" Rotation " << it->first <<
": x: " << it->second.x <<
", y: " << it->second.y <<
", z: " << it->second.z <<
" aunit: " << it->second.unit << endl;
662 ostream &operator<<(ostream &stream,map<string, gsolid> gsolidsMap )
664 for(map<string, gsolid>::iterator it = gsolidsMap.begin(); it != gsolidsMap.end(); it++)
665 cout <<
" Solid :" << it->first <<
" type: " << it->second.type <<
", dimension: " << it->second.dimension << endl;
671 ostream &operator<<(ostream &stream,map<string, gphysV> gphysVMap )
673 for(map<string, gphysV>::iterator it = gphysVMap.begin(); it != gphysVMap.end(); it++)
674 cout <<
" Physical Volume :" << it->first <<
" positionRef: " << it->second.positionRef <<
", rotationRef: " << it->second.rotationRef << endl;
683 if(type ==
"para")
return "Parallelepiped";
684 if(type ==
"cone")
return "Cons";
685 if(type ==
"box")
return "Box";
686 if(type ==
"sphere")
return "Sphere";
687 if(type ==
"ellipsoid")
return "Ellipsoid";
688 if(type ==
"paraboloid")
return "Paroboloid";
689 if(type ==
"hype")
return "Hype";
690 if(type ==
"tube")
return "Tube";
691 if(type ==
"eltube")
return "EllipticalTube";
692 if(type ==
"torus")
return "Torus";
693 if(type ==
"trd")
return "Trd";
694 if(type ==
"orb")
return "Orb";
695 if(type ==
"tet")
return "Tet";
700 double resolve_strings(
string constant_name,
string v, map<string, double> gconstantMap)
703 double constantResult;
717 vector<string> operation;
718 vector<double> constant;
719 vector<string> constantName;
723 for(map<string, double>::iterator it=gconstantMap.begin(); it != gconstantMap.end(); it++)
724 constantName.push_back(it->first);
726 if (v ==
"0" || v ==
"0.0")
727 constantResult = 0.0;
729 else if ( atof(v.c_str())== 0 && atoi(v.c_str()) == 0)
731 if(
verbosity>0)cout <<
"----------constant calculation---------- "<< endl;
733 int size = v.length();
734 char constantChar[size];
735 strcpy(constantChar, v.c_str());
738 pch = strtok (constantChar,
" \n");
746 if(token ==
"+" || token ==
"-" || token ==
"*" || token ==
"/")
747 operation.push_back(token);
750 else if (atof(token.c_str()) != 0)
751 constant.push_back(atof(token.c_str()));
754 constant.push_back(gconstantMap[token]);
757 if(
verbosity>0) cout <<
"constant " << token << endl;
759 pch = strtok (NULL,
" \n");
764 if (operation.size() == 0)
765 constantResult = constant[0];
771 unsigned int calSize = constant.size()*2-1;
773 vector<string> calculation;
774 calculation.resize(calSize);
777 for(
unsigned int a=0;a<constant.size();a++)
778 calculation[2*a] =
stringify(constant[a]);
779 for(
unsigned int a=0;a<constant.size()-1;a++)
780 calculation[2*a+1] = operation[a];
785 cout <<
"calculation" << endl;
788 for(
unsigned int a=0;a<calSize;a++)
789 cout << calculation[a] << endl;
795 if(
verbosity>0) cout <<
"!!!!!calculated value!!!!! " << calculation[0] << endl;
796 constantResult = atof(calculation[0].c_str());
802 constantResult = atof(v.c_str());
804 return constantResult;
823 string cosine(
"cos");
834 if(constString.size()>=sine.size())
836 if(constString.size()>=cosine.size())
839 if(
verbosity>0)cout <<
"constString after replacing : " << constString << endl;
842 vector<string> calculation;
844 vector<string> operation;
845 vector<double> constant;
848 if (constString ==
"0" || constString ==
"0.0")
851 else if ( atof(constString.c_str())== 0 && atoi(constString.c_str()) == 0)
853 int size = constString.length();
854 char constantChar[size];
855 strcpy(constantChar, constString.c_str());
857 pch = strtok (constantChar,
" \n");
866 if(token ==
"+" || token ==
"-" ||token ==
"*" ||token ==
"/" || token ==
"(" || token ==
")" || token ==
"sin" || token ==
"cos" )
867 calculation.push_back(token);
869 else if(atof(token.c_str()) != 0)
870 calculation.push_back(token);
873 else if(token ==
"pi")
874 calculation.push_back(
"3.14");
877 calculation.push_back(
stringify(gconstantMap[token]));
879 pch = strtok (NULL,
" \n");
882 for(
unsigned int i=0; i<calculation.size(); i++)
883 if(
verbosity>0)cout <<
"calculation[" << i <<
"] = " << calculation[i] << endl;
885 if(calculation.size() == 1)
886 result = atof(calculation[0].c_str());
891 vector<unsigned int> a;
892 vector<unsigned int> b;
893 unsigned int element_a, element_b;
895 for(
unsigned int i=0; i<calculation.size();i++)
897 if(calculation[i] ==
"(")
899 if(calculation[i] ==
")")
902 unsigned int size_a = a.size();
908 vector<unsigned int> c;
910 for(
unsigned int i=0; i<a.size()-1; i++)
919 unsigned int size_c = c.size();
922 if(size_a>0 && doublep_count == 0)
929 vector<string> paren_calculation;
931 for(
unsigned int i=element_a+1; i<element_b; i++)
932 paren_calculation.push_back(calculation[i]);
937 for(
unsigned int i = element_b+1; i<calculation.size(); i++)
938 calculation[i-element_b+element_a] = calculation[i];
939 for(
unsigned int i = 0; i<element_b-element_a; i++)
940 calculation.pop_back();
943 for(
unsigned int i=0; i<calculation.size();i++)
945 if(calculation[i] ==
"(")
947 if(calculation[i] ==
")")
950 for(
unsigned int j=0; j<calculation.size(); j++)
951 if(
verbosity>0)cout <<
"calculation[" << j <<
"] = " << calculation[j] << endl;
956 else if(doublep_count>0)
958 unsigned int m = c[0];
963 vector<string> paren_calculation;
965 for(
unsigned int n=a[m]+1; n<b[m-1]; n++)
967 paren_calculation.push_back(calculation[n]);
969 for(
unsigned int h=0; h<paren_calculation.size(); h++)
970 if(
verbosity>0)cout <<
"double_paren_calculation[i] = " << paren_calculation[h] << endl;
974 for(
unsigned int n=b[m-1]+1; n<calculation.size(); n++)
975 calculation[n-b[m-1]+a[m]] = calculation[n];
976 for(
unsigned int n=0; n<b[m-1]-a[m]; n++)
977 calculation.pop_back();
982 vector<unsigned int>aa;
983 vector<unsigned int>bb;
985 for(
unsigned int n=0;n<calculation.size();n++)
987 if(calculation[n] ==
"(")
989 if(calculation[n] ==
")")
992 for(
unsigned int n=0;n<calculation.size()-1;n++)
1002 vector<unsigned int> d;
1003 vector<unsigned int> e;
1004 unsigned int element_d, element_e;
1005 for(
unsigned int i=0; i<calculation.size(); i++)
1007 if(calculation[i] ==
"(")
1009 if(calculation[i] ==
")")
1014 unsigned int size_d = d.size();
1017 vector<string>paren_calculation;
1018 for(
unsigned int i=element_d+1; i<element_e; i++)
1019 paren_calculation.push_back(calculation[i]);
1020 for(
unsigned int h=0; h<paren_calculation.size(); h++)
1021 if(
verbosity>0)cout <<
"__paren_calculation[i] = " << paren_calculation[h] << endl;
1024 for(
unsigned int i=element_e+1; i<calculation.size(); i++)
1025 calculation[i-element_e+element_d] = calculation[i];
1026 for(
unsigned int i=0; i<element_e-element_d; i++)
1027 calculation.pop_back();
1029 for(
unsigned int i=0; i<calculation.size();i++)
1031 if(calculation[i] ==
"(")
1033 if(calculation[i] ==
")")
1041 if(
verbosity>0)cout <<
"There is no parenthesis in calculation[]" << endl;
1043 for(
unsigned int i=0; i<calculation.size();i++)
1046 if(calculation[i] ==
"sin")
1048 calculation[i] =
stringify(sin(atof(calculation[i+1].c_str())));
1049 for(
unsigned int j=i+2; j<calculation.size();j++)
1050 calculation[j]=calculation[j-1];
1051 calculation.pop_back();
1054 if(calculation[i] ==
"cos")
1056 calculation[i] =
stringify(cos(atof(calculation[i+1].c_str())));
1057 for(
unsigned int j=i+2; j<calculation.size();j++)
1058 calculation[j]=calculation[j-1];
1059 calculation.pop_back();
1064 if(
verbosity>0)cout <<
"return value result = " << result << endl;
1071 result = atof(constString.c_str());
1081 string paren_result = paren_calculation[0];
1083 for(
unsigned int m=0; m<paren_calculation.size(); m++)
1084 if(
verbosity>0)cout <<
"calculation in p_calculation : " << paren_calculation[m] << endl;
1087 if(paren_calculation[0] ==
"-")
1090 paren_calculation[1] =
stringify(-1*atof(paren_calculation[1].c_str()));
1091 if(
verbosity>0)cout <<
"calculation[1] after multiplying -1 " << paren_calculation[1] << endl;
1092 for(
unsigned int i =1;i<paren_calculation.size();i++)
1093 paren_calculation[i-1]=paren_calculation[i];
1094 paren_calculation.pop_back();
1098 unsigned int paren_calSize = paren_calculation.size();
1101 for(
unsigned int j=0;j<(paren_calSize-1)/2;j++)
1103 if(paren_calculation[2*j+1] ==
"*" || paren_calculation[2*j+1] ==
"/")
1109 for(
unsigned int j=0;j<(paren_calculation.size()-1)/2;j++)
1112 if(paren_calculation[2*j+1] ==
"*")
1115 paren_calculation[2*j] =
stringify(atof(paren_calculation[2*j].c_str())*atof(paren_calculation[2*j+2].c_str()));
1117 for(
unsigned int l=2*j+3;l<paren_calculation.size();l++)
1118 paren_calculation[l-2]=paren_calculation[l];
1120 paren_calculation.pop_back();
1121 paren_calculation.pop_back();
1125 else if(paren_calculation[2*j+1] ==
"/")
1127 paren_calculation[2*j] =
stringify(atof(paren_calculation[2*j].c_str())/atof(paren_calculation[2*j+2].c_str()));
1129 for(
unsigned int l=2*j+3;l<paren_calculation.size();l++)
1130 paren_calculation[l-2]=paren_calculation[l];
1132 paren_calculation.pop_back();
1133 paren_calculation.pop_back();
1141 while(paren_calculation.size()>1)
1144 if(paren_calculation[2*m+1] ==
"+")
1146 paren_calculation[2*m] =
stringify(atof(paren_calculation[2*m].c_str())+atof(paren_calculation[2*m+2].c_str()));
1147 for(
unsigned int l=2*m+3;l<paren_calculation.size();l++)
1148 paren_calculation[l-2]=paren_calculation[l];
1150 paren_calculation.pop_back();
1151 paren_calculation.pop_back();
1153 if(paren_calculation[2*m+1] ==
"-")
1155 paren_calculation[2*m] =
stringify(atof(paren_calculation[2*m].c_str())-atof(paren_calculation[2*m+2].c_str()));
1156 for(
unsigned int l=2*m+3;l<paren_calculation.size();l++)
1157 paren_calculation[l-2]=paren_calculation[l];
1159 paren_calculation.pop_back();
1160 paren_calculation.pop_back();
1166 paren_result = paren_calculation[0];
1168 return paren_result;
int check_if_factory_is_needed(map< string, detectorCondition > dcon, string factory)
string rad_to_deg(string radValue)
string get_dimensions(QDomNode solidNode, map< string, double > gconstantMap)
map< string, detector > loadDetectors()
string replaceCharsWithChars(string input, string x, string y)
void add_data(QVariant input)
string replaceCharWithChars(string input, string x, string y)
map< string, detectorCondition > detectorConditionsMap
string assignAttribute(QDomElement e, string attribute, string defaultValue)
string stringify(double x)
string rad_to_deg_u(string radValue, string radUnit)
converts the unit radian to degree if there is radian value.
double resolve_strings(string constant_name, string v, map< string, double > gconstantMap)
map< string, aopt > optMap
Options map.
string togType(string type)
double resolve_string(string constString, map< string, double > gconstantMap)
string p_calculation(vector< string > paren_calculation)
detector get_detector(gtable gt, goptions go, runConditions RC)