forked from jnugent42/mcsframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCSUnfolding.cpp
220 lines (191 loc) · 7.14 KB
/
MCSUnfolding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "MCSAnalysis.h"
// xml parser
#include "libxml/tree.h"
#include "libxml/parser.h"
#include "libxml/xpath.h"
#include "libxml/xpathInternals.h"
#if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_SAX1_ENABLED)
struct specvals {
std::string outfile;
std::string outfilename;
std::string dataname;
std::string trainname;
std::string modelname;
std::string geometryname;
std::string model1;
std::string model2;
std::map<std::string, double> sys;
std::map<std::string, double> histlimits;
double TOF_ll;
double TOF_ul;
double fid_grad;
double fid_rad;
int mode;
};
static void print_element_names(xmlNode * a_node, specvals& spec)
{
xmlNode *cur_node = NULL;
for (cur_node= a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
const xmlChar* id = xmlCharStrdup("id");
const xmlChar* nm = xmlCharStrdup("name");
const xmlChar* vl = xmlCharStrdup("value");
char* val1 = (char*)xmlGetProp(cur_node, id);
char* val2 = (char*)xmlGetProp(cur_node, nm);
char* val3 = (char*)xmlGetProp(cur_node, vl);
printf("node type: Element, name: %s, with prop id: %s, prop name: %s, prop value: %s\n",
cur_node->name, val1, val2, val3);
if ( xmlStrEqual(cur_node->name, xmlCharStrdup("file")) ) {
if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("outfile")) ){
spec.outfile = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("datafile")) ){
spec.dataname = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("outfilename")) ){
spec.outfilename = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("trainfile")) ){
spec.trainname = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("modelfile")) ){
spec.modelname = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("model1")) ) {
spec.model1 = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("model2")) ) {
spec.model2 = (char*)xmlGetProp(cur_node, nm);
} else if ( xmlStrEqual(xmlGetProp(cur_node, id), xmlCharStrdup("geofile")) ) {
spec.geometryname = (char*)xmlGetProp(cur_node, nm);
}else {
std::cout<<"File designation not recognized. Will ignore "
<< xmlGetProp(cur_node, id)
<< std::endl;
}
}
if (xmlStrEqual(cur_node->name, xmlCharStrdup("cuts")) ) {
if ( xmlStrEqual(xmlGetProp(cur_node, nm), xmlCharStrdup("TOF_ll")) ){
spec.TOF_ll = std::atof((char*)xmlGetProp(cur_node, vl));
} else if ( xmlStrEqual(xmlGetProp(cur_node, nm), xmlCharStrdup("TOF_ul")) ){
spec.TOF_ul = std::atof((char*)xmlGetProp(cur_node, vl));
} else if ( xmlStrEqual(xmlGetProp(cur_node, nm), xmlCharStrdup("fid_grad")) ){
spec.fid_grad = std::atof((char*)xmlGetProp(cur_node, vl));
} else if ( xmlStrEqual(xmlGetProp(cur_node, nm), xmlCharStrdup("fid_rad")) ){
spec.fid_rad = std::atof((char*)xmlGetProp(cur_node, vl));
} else if ( xmlStrEqual(xmlGetProp(cur_node, nm), xmlCharStrdup("mode")) ){
spec.mode = std::atof((char*)xmlGetProp(cur_node, vl));
} else {
std::cout<<"Cut designation not recognized. Will ignore "
<< xmlGetProp(cur_node, nm)
<< std::endl;
}
}
if (xmlStrEqual(cur_node->name, xmlCharStrdup("sys")) ){
spec.sys[(char*)xmlGetProp(cur_node, nm)] =
std::atof((char*)xmlGetProp(cur_node, vl));
}
if (xmlStrEqual(cur_node->name, xmlCharStrdup("histlimits")) ){
spec.histlimits[(char*)xmlGetProp(cur_node, nm)] =
std::atof((char*)xmlGetProp(cur_node, vl));
}
}
print_element_names(cur_node->children, spec);
}
}
int main(int argc, char* argv[]) {
// char* mrd = getenv("MAUS_ROOT_DIR");
std::cout<<"Algorithm to analyse reduced MAUS root\n";
std::cout<<"trees for multiple scattering measurements\n";
std::cout<<"and to unfold the distribution\n";
std::cout<<"\nReceived "<<argc<<" arguements.\n";
if( argc != 9 && argc != 2){
std::cout<<"2 arguements are required:\n\n";
std::cout<<"${exec} ${spec file}\n";
return -1;
}
std::string mode_tree = "";
specvals spec;
spec.outfile = "";
spec.outfilename = "";
spec.dataname = "";
spec.trainname = "";
spec.modelname = "";
spec.geometryname = "";
spec.TOF_ll = 27.0;
spec.TOF_ul = 42.0;
spec.fid_grad = 0.01;
spec.fid_rad = 150.0;
spec.mode = 0;
if ( argc == 2 ){
xmlInitParser();
xmlDocPtr doc=NULL;
assert(argv[1]);
doc = xmlReadFile(argv[1], NULL, 0);
if (doc == NULL) {
printf("error: could not parse file %s\n", argv[1]);
}
xmlNode * root_element = xmlDocGetRootElement(doc);
print_element_names(root_element, spec);
xmlFreeDoc(doc);
}
else if ( argc == 10 ){
spec.outfile = argv[1];
spec.dataname = argv[2];
spec.trainname = argv[3];
spec.TOF_ll = std::atof(argv[4]);
spec.TOF_ul = std::atof(argv[5]);
spec.fid_rad = std::atof(argv[6]);
spec.modelname = argv[7];
spec.mode = std::atoi(argv[8]);
spec.outfilename = argv[9];
}
if (spec.mode > 0){
mode_tree = "reduced_tree";
} else {
mode_tree = "reduced_MCtree";
}
std::cout<<"Writing outfile to "<<spec.outfile<<std::endl;
std::cout<<"Reading outfilename at "<<spec.outfilename<<std::endl;
std::cout<<"Reading data from "<<spec.dataname<<std::endl;
std::cout<<"Reading reference data from "<<spec.trainname<<std::endl;
std::cout<<"Reading models from "<<spec.modelname<<std::endl;
std::cout<<"Use "<<spec.geometryname<<" for propagation\n";
std::cout<<"\n";
std::cout<<"TOF selection between "
<<spec.TOF_ll<<" and "<<spec.TOF_ul<<std::endl;
std::cout<<"Fiducial selection contained by radius "
<<spec.fid_rad<<" with scattering "<<spec.fid_grad<<std::endl;
std::cout<<"\n";
MCSAnalysis anal("reduced_tree", mode_tree, spec.outfile, spec.histlimits);
TFile* data = new TFile(spec.dataname.c_str());
if(data->IsZombie()){
std::cout<<"Data file is a zombie. Aborting."<<std::endl;
return -1;
}
TFile* training = new TFile(spec.trainname.c_str());
if(training->IsZombie()){
std::cout<<"Training file is a zombie. Aborting."<<std::endl;
return -1;
}
anal.SetTOFLowerLimit(spec.TOF_ll);
anal.SetTOFUpperLimit(spec.TOF_ul);
anal.SetRadialLimit(spec.fid_rad);
anal.SetGradientLimit(spec.fid_grad);
anal.SetModelFileName(spec.modelname.c_str());
anal.SetModelName1(spec.model1);
anal.SetModelName2(spec.model2);
anal.SetParentGeometryFile(spec.geometryname.c_str());
anal.SetFileName(spec.outfilename.c_str());
anal.GetMCTree()->Add(spec.trainname.c_str());
anal.GetTree()->Add(spec.dataname.c_str());
for(std::map<std::string, double>::iterator it=spec.sys.begin();
it != spec.sys.end(); ++it){
anal.SetSysOffset(it->first, it->second);
std::cout<<"Adding systematic effect "<<it->first
<<" with value "<<it->second<<std::endl;
}
anal.Execute(spec.mode);
anal.Write();
return 0;
}
#else
int main(void) {
fprintf(stderr, "XPath support not compiled in\n");
exit(1);
}
#endif