forked from jnugent42/mcsframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCSAnalysis.h
More file actions
303 lines (260 loc) · 8.46 KB
/
MCSAnalysis.h
File metadata and controls
303 lines (260 loc) · 8.46 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
// This is what we are here for
#include "RooUnfold.h"
#include "RooUnfoldResponse.h"
#include "RooUnfoldBayes.h"
// Standard ROOT stuff
#include "TCanvas.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TF1.h"
#include "TFile.h"
#include "TTree.h"
#include "TChain.h"
#include "TGraphErrors.h"
#include "TSpectrum.h"
#include "Fit/Fitter.h"
#include "Math/Functor.h"
#include "Math/RootFinder.h"
// Read directly from the MAUS data structure.
#include "src/common_cpp/DataStructure/TOFEvent.hh"
#include "src/common_cpp/DataStructure/SciFiEvent.hh"
#include "src/common_cpp/DataStructure/SciFiTrack.hh"
#include "src/common_cpp/DataStructure/SciFiTrackPoint.hh"
#include "src/common_cpp/DataStructure/SciFiSpacePoint.hh"
#include "src/common_cpp/DataStructure/KLEvent.hh"
#include "src/common_cpp/DataStructure/CkovEvent.hh"
#include "src/common_cpp/DataStructure/EMREvent.hh"
#include "src/common_cpp/DataStructure/MCEvent.hh"
#include "src/common_cpp/DataStructure/VirtualHit.hh"
#include "src/common_cpp/DataStructure/Primary.hh"
#include "src/common_cpp/DataStructure/Spill.hh"
#include "src/common_cpp/DataStructure/Data.hh"
// Borrowed from Chris Rogers script
#include "src/legacy/BeamTools/BTFieldConstructor.hh"
#include "src/common_cpp/Utils/JsonWrapper.hh"
#include "src/common_cpp/Utils/Globals.hh"
#include "src/common_cpp/Globals/GlobalsManager.hh"
#include "src/common_cpp/Recon/Global/GlobalTools.hh"
// #include "src/common_cpp/JsonCppStreamer/IRStream.hh"
// And my own home brew object collection.
#include "Collection.h"
class MCSAnalysis {
public:
MCSAnalysis(std::string tree, std::string mctree, std::string outname, std::map<std::string, double> histlimits);
~MCSAnalysis();
void Write();
TChain* GetTree(){ return chain; }
TChain* GetMCTree(){ return mcchain; }
void Execute(int mode);
void dataSelection();
void referenceSelection();
void generateMCSResponse();
void ConvolveWithInputDistribution(std::string distname);
void DoUnfolding();
void DoDeconvolution(std::string model, int n_sel);
void DoFFTDeconvolution();
void UpdateRunInfo();
void PlotRunInfo();
void FitGaussian(std::string outfilename);
void CalculateChi2(std::string outfilename, std::string distname);
double myfunc(double pz, double s1, double s2, double E, double delta);
double myfunc_deriv(double pz, double s1, double s2, double E, double delta);
double myfunc1(double x);
double myfunc_deriv1(double x);
double operator()(double x) const { return x*x; }
double Eval(double x) const { return x+x; }
double Derivative(double x) const { return 2*x; }
void SetTOFUpperLimit(double a){ TOF_upper_limit=a; }
void SetTOFLowerLimit(double a){ TOF_lower_limit=a; }
void SetRadialLimit(double a){ meanp=a; }
void SetGradientLimit(double a){ sigmap=a; }
void SetModelFileName(std::string a){ modelfile=a; }
void SetModelName1(std::string a) {modelname1=a; }
void SetModelName2(std::string a) {modelname2=a; }
void SetParentGeometryFile(std::string a) {geometryfile=a; }
void SetFFTBinLimit(int a) { binlimit=a; }
void SetFileName(std::string a) {outfilename=a; }
void SetSysOffset(std::string eff, double val) { _sys[eff] = val; }
private:
int jUS, jDS, kUS, kDS;
double TOF_lower_limit;
double TOF_upper_limit;
double meanp;
double sigmap;
int binlimit;
std::string modelfile;
std::string modelname1;
std::string modelname2;
std::string geometryfile;
double USrefplaneZ;
double DSrefplaneZ;
int USrefplaneI;
int DSrefplaneI;
Collection _USset;
Collection _DSset;
Collection _USAbsset;
Collection _DSAbsset;
Collection _USMCset;
Collection _DSMCset;
Collection _UStmpset;
RooUnfoldResponse resp_thetaX;
RooUnfoldResponse resp_thetaY;
RooUnfoldResponse resp_thetaScatt;
RooUnfoldResponse resp_theta2Scatt;
RooUnfoldResponse tresp_thetaX;
RooUnfoldResponse tresp_thetaY;
RooUnfoldResponse tresp_thetaScatt;
RooUnfoldResponse tresp_theta2Scatt;
int USabsPlaneI;
int DSabsPlaneI;
std::map<std::string, double> _histlimits;
std::map<std::string, double> _sys;
// Chain containing only the data of interest (not necessarily MC).
TChain* chain;
// Training tree containing the response to the volume of interest
TChain* mcchain;
int runnumber, LastRunNumber;
int SpillNumber;
MAUS::TOFEvent* tofevent;
MAUS::SciFiEvent* scifievent;
MAUS::KLEvent* klevent;
MAUS::CkovEvent* ckovevent;
MAUS::EMREvent* emrevent;
MAUS::MCEvent* mcevent;
MAUS::Primary* primary;
MAUS::SpecialVirtualHitArray* sphitarray;
TFile* outfile;
std::string outfilename;
TH1D* tof10;
TH1D* tof10_sel;
TH1D* tof21;
TH1D* tof21_sel;
TH1D* calc_mom;
TH2D* t_cor;
TH1D* cor_mom;
TH1D* mctof10;
TH1D* mctof10_sel;
TH1D* mctof21;
TH1D* mctof21_sel;
TH1D* mccalc_mom;
TH1D* mctrue_mom;
TH1D* cuts_accept;
TH1D* mccuts_accept;
TH1D* scattering_pos_x;
TH1D* scattering_pos_y;
TH1D* theta_true_x_graph;
TH1D* theta_true_x_bin;
TH1D* theta_true_y_graph;
TH2D* scattering_proj_x_R;
TH2D* scattering_proj_y_R;
TH2D* scattering_proj_x_resp;
TH2D* scattering_proj_y_resp;
TH1D* theta_meas_y_graph;
TH1D* theta_meas_x_graph;
std::vector<int> _run;
std::vector<double> _emit;
std::vector<double> _emitError;
std::vector<double> _meanX;
std::vector<double> _meanXerror;
std::vector<double> _meanY;
std::vector<double> _meanYerror;
std::vector<double> _meandXdz;
std::vector<double> _meandXdzError;
std::vector<double> _meandYdz;
std::vector<double> _meandYdzError;
std::vector<double> path_length;
bool MatchUSDS();
bool PIDSelection(bool isdata);
bool RadialSelection(double pz);
std::vector<double> DefineProjectionAngles(Vars US, Vars DS);
double MomentumFromTOF(bool isdata);
double BetheBloch(double pz);
double TimeofFlight();
std::vector<double> CalculatePathLength(double pz);
double CorMomFromTOF(double pz);
bool findVirtualPlanes();
void FillMuScattResponse(bool event_ok, Vars& US, Vars& DS, Vars& USMC, Vars& DSMC);
void FillMCSResponse(bool event_ok, Vars& US, Vars& DS, Vars& USMC, Vars& DSMC);
void FillVarsVirtual(Vars& tmpvar, int j);
void FillCollectionSciFi(Collection& Set, int j, int k, double pz, int isDS, bool project=false);
void FillVarsSciFi(Vars& tmpvar, int j, int k, double pz, int isDS);
void make_beam_histograms(Collection Set, std::string desc, std::string suffix);
void make_acceptance_histograms(Collection USset, Collection DSset,
std::string desc, std::string suffix);
void make_scattering_acceptance_histograms(Collection USset,
Collection DSset,
Collection DSrefset,
std::string desc,
std::string suffix);
Json::Value SetupConfig(int verbose_level);
Vars PropagateVarsMu(Vars event, double z0);
/*
static TVectorD p_vec;
static TVectorD res;
static TVectorD pStart_vec;
static TVectorD pStart_vec_y;
static TVectorD theta_true_x;
static TVectorD theta_true_y;
*/
double pStart[19];
// double p_vec[19];
int counter;
int nSize_true;
int nSize_true_y;
double scattering_proj_x_entry;
double scattering_proj_x_new;
double scattering_proj_x_centre;
double scattering_proj_y_entry;
double scattering_proj_y_new;
double scattering_proj_y_centre;
struct SumDistance2 {
TVectorD D_x_func;
TMatrixD e_x_func;
TMatrixD R_x_func;
SumDistance2(TVectorD D, TMatrixD e, TMatrixD R) :
D_x_func(D), e_x_func(e), R_x_func(R) {}
double operator() (const double * par) {
double sum = 0;
TVectorD p_vec(19);
int z = 1;
for (int j=0; j<19; j++)
p_vec[j] = par[j];
for (int i=10; i<19; i++){
p_vec[i] = par[i-z];
z += 2;
}
TVectorD res = R_x_func * e_x_func * p_vec;
for (int i = 0; i<30; i++) {
if (D_x_func(i) != 0) {
double d = pow(D_x_func(i)-res(i),2) / D_x_func(i);
sum += d;
}
/*
if (first) {
std::cout << "point " << i << "\t"
<< D_x_func(i) << "\t"
<< res(i) << "\t" << std::endl;
}
}
if (first) {
std::cout << "Total Initial distance square = " << sum << std::endl;
first = false;
*/
}
return sum;
}
};
};
class MyFunction1D {
public:
double E;
double delta;
double s1;
double s2;
double operator()(double pz) const { return s1*sqrt(pow(pz+delta,2)+pow(105.65,2))/(pz+delta)+s2*sqrt(pow(pz-delta,2)+pow(105.65,2))/(pz-delta); }
double Derivative(double pz) const { return -(s1 - s2) * delta * pow(105.65,2)/ (pow(pz,2)*E); }
};