-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMCSAnalysis.h
572 lines (526 loc) · 17.5 KB
/
MCSAnalysis.h
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <typeinfo>
#include <iostream>
#include <sstream>
#include <list>
// 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"
#include "TLine.h"
#include "TPaveStats.h"
#include "TLegend.h"
#include "TProfile.h"
#include "TGraphAsymmErrors.h"
#include "TStyle.h"
#include "TFileInfo.h"
#include "TSystem.h"
#include "TString.h"
#include "TFrame.h"
#include "TMultiGraph.h"
#include "TEfficiency.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/Hit.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"
#include "libxml/tree.h"
#include "libxml/parser.h"
#include "libxml/xpath.h"
#include "libxml/xpathInternals.h"
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtold */
class MCSAnalysis {
public:
MCSAnalysis(std::string tree, std::string mctree, std::string reftree, std::string outname, std::map<std::string, double> histlimits);
~MCSAnalysis();
void Write();
TChain* GetTree(){ return chain; }
TChain* GetRefTree(){ return refchain; }
TChain* GetMCTree(){ return mcchain; }
TChain* GetMCEmptyTree(){ return mcemptychain; }
//TFileInfo* GetFileInfo(){ return fileinfo;}
void toffitting();
void Execute(int mode);
void dataevents(int mode);
void dataSelection(int mode);
void referenceSelection();
void generateMCSResponse();
void TruthData(int mode);
void ConvolveWithInputDistribution(std::string distname);
void ConvolveWithVirtualInputDistribution(std::string distname);
void DoUnfolding(Collection& _USsethold, Collection& _DSsethold, Collection& _USMCsethold, Collection& _DSMCsethold);
//void DoUnfolding();
void DoDeconvolution(std::string model, int mode, int n_sel);
void DoVirtualDeconvolution(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);
void print();
void complots(int mode);
std::vector<float> FillCrossCheck();
int counttof1sp1 = 0;
int counttof1sp0 = 0;
int counttofcoincidence = 0;
int counttof0sp1 = 0;
int counttof0sp0 = 0;
TH1D* thetaX_all;
TH1D* thetaX_tof;
TH1D* thetaX_diff;
TH1D* thetaX_chi;
TH1D* thetaX_proj;
TH1D* thetaY_all;
TH1D* thetaY_tof;
TH1D* thetaY_diff;
TH1D* thetaY_chi;
TH1D* thetaY_proj;
TH1D* trkreffix(TH1D* h1, bool muldiv);
TH1D* trkreffiy(TH1D* h1, bool muldiv);
TH1D* trkreffiscatt(TH1D* h1);
float dte01MC;
float dte12MC;
float dte01data;
float dte12data;
float dte01;
float dte12;
TH1D* trkreffi2scatt(TH1D* h1);
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; }
int list172[100] = {7764, 7826, 7766, 7827, 7767, 7831, 7782, 7832, 7783, 7861, 7785, 7863, 7786, 7864, 7787, 7865, 7799, 7866, 7800, 7768, 7806, 7833, 7822, 7823, 7824, 7825};
int list200[100] = {7726, 7807, 7729, 7834, 7735, 7835, 7736, 7836, 7754, 7838, 7770, 7841, 7771, 7842, 7772, 7843, 7773, 7777, 7778, 7837, 7784, 7788, 7789, 7797, 7798, 7804};
int list240[100] = {7727, 7817, 7733, 7818, 7737, 7819, 7738, 7844, 7775, 7847, 7776, 7848, 7790, 7849, 7794, 7741, 7795, 7852, 7796, 7853, 7805, 7854, 7808, 7855, 7809, 7856, 7813, 7858, 7814, 7859, 7816, 7860, 7845};
void Setangdef(double a){ angdef=a; }
void Setbeamtype(int a){ beamtype=a; }
void Setmode(double a){ mode=a; }
void Setoffset(double a){ offset=a; }
void SetisMC(double a){ isMC=a; }
void SetTOFsys(double a){ TOFsys=a; }
void Setoffset_needed(int a){ offset_needed=a; }
void Setrot_ang(double a){ rot_ang=a; }
void Setrot_ang_empty(double a){ rot_ang_empty=a; }
void Setrot_angX(double a){ rot_angX=a; }
void Setrot_ang_emptyX(double a){ rot_ang_emptyX=a; }
void Setaccpt(double a){ accpt=a; }
void SetTOFUpperLimit(long double a){ TOF_upper_limit=a; }
void SetTOFLowerLimit(long double a){ TOF_lower_limit=a; }
void SetPUpperLimit(double a){ P_upper_limit=a; }
void SetPLowerLimit(double a){ P_lower_limit=a; }
void SetTOFUpperLimitRef(long double a){ TOF_upper_limit_ref=a; }
void SetTOFLowerLimitRef(long double a){ TOF_lower_limit_ref=a; }
void SetRadialLimit(double a){ meanp=a; }
void SetEffCut(double a){ eff_cut=a; }
void SetGradientLimit(double a){ sigmap=a; }
void SetModelFileName(std::string a){ modelfile=a; }
void SetModelFileName2(std::string a){ modelfile2=a; }
void SetModelName1(std::string a) {modelname1=a; }
void SetModelName2(std::string a) {modelname2=a; }
void SetModelName3(std::string a) {modelname3=a; }
void SetMaterial(std::string a) {material=a; }
void SetTrkrEffiName(std::string a) {trkreffiname=a; }
void SetTrkrEffiEmptyName(std::string a) {trkreffiemptyname=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, lUS, lDS;
double percentTOF2;
double TOF1Hit = 0;
double TOF2Hit = 0;
double DSentries = 0;
double DSrefentries = 0;
double angdef;
int beamtype;
double isMC;
bool TOFsys;
int offset_needed;
bool isEmpty;
double rot_ang;
double rot_ang_empty;
double rot_angX;
double rot_ang_emptyX;
double accpt;
double DScor = 0;
double refDScor = 0;
double UScor = 0;
double refUScor = 0;
long double TOF_lower_limit;
long double TOF_upper_limit;
long double P_lower_limit;
long double P_upper_limit;
long double TOF_lower_limit_ref;
long double TOF_upper_limit_ref;
double semom;
double meanmom;
double rmsmom;
double errrmsmom;
double bwmom;
double empty_semom;
double empty_meanmom;
double empty_rmsmom;
double empty_errrmsmom;
double empty_bwmom;
double mode;
double offset;
double difradius;
double meanp;
double eff_cut;
double sigmap;
int binlimit;
double weight172;
double weight200;
double weight240;
double TOFtotal;
double tX_rG_int;
std::string modelfile;
std::string modelfile2;
std::string modelname1;
std::string modelname2;
std::string modelname3;
std::string material;
std::string trkreffiname;
std::string trkreffiemptyname;
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;
Collection USTruthSet;
Collection DSTruthSet;
Collection USTruthSetLiH;
Collection DSTruthSetLiH;
Collection mcreconUSTruthSet;
Collection mcreconDSTruthSet;
Collection eventMCUSAllTOF;
Collection eventMCDSAllTOF;
Collection USeventset;
Collection DSeventset;
RooUnfoldResponse resp_thetaX;
RooUnfoldResponse resp_thetaY;
RooUnfoldResponse resp_thetaScatt;
RooUnfoldResponse resp_theta2Scatt;
RooUnfoldResponse tresp_thetaX;
RooUnfoldResponse tresp_thetaY;
RooUnfoldResponse tresp_thetaScatt;
RooUnfoldResponse tresp_theta2Scatt;
RooUnfoldResponse mresp_thetaX;
RooUnfoldResponse mresp_thetaY;
RooUnfoldResponse mresp_thetaScatt;
RooUnfoldResponse mresp_theta2Scatt;
RooUnfoldResponse fresp_thetaX;
RooUnfoldResponse fresp_thetaY;
RooUnfoldResponse fresp_thetaScatt;
RooUnfoldResponse fresp_theta2Scatt;
int USabsPlaneI;
int DSabsPlaneI;
int centre;
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* refchain;
// Truth tree containing the response to the volume of interest
TChain* mcchain;
// Truth tree containing the response to the empty channel
TChain* mcemptychain;
int runnumber, LastRunNumber;
int SpillNumber;
int tracker0;
int tracker1;
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* diffradius;
TH1D* chi2pern;
TH1D* projradius;
TH1D* nocutprojradius;
TH2D* trackno;
TH2D* eventnovspz;
TH2D* tofhitno;
TH1D* pathlengthabs;
TH1D* tof10;
TH1D* tof10_sel;
TH1D* noweighttof10;
TH1D* noweighttof21;
TH1D* rawtime;
TH1D* rawtime12;
TH1D* uncor_mom;
TH1D* uncor_momsel;
TH1D* tof21;
TH1D* tof21_sel;
TH1D* calc_mom;
TH2D* t_cor;
TH1D* residual;
TH1D* residual01;
TH1D* residual12;
TH1D* residualUD;
TH1D* refresidual;
TH1D* refresidual01;
TH1D* refresidual12;
TH1D* refresidualUD;
TH1D* residual12p6;
TH1D* residual01j;
TH1D* residual01short;
TH1D* residual12p2;
TH1D* residual01p6;
TH1D* residualcobb;
TH1D* TOF0Energy;
TH1D* difEloss;
TH2D* energyloss;
TH1D* energylossproj;
TProfile* energylosspro;
TH1D* TOF1Energy;
TH1D* TOF2Energy;
TH2D* pzdEdx;
TH2D* residual_plot;
TH2D* TOF01vsTOF12;
TH2D* refTOF01vsTOF12;
TH2D* TOF01forvsTOF01absfor;
TH2D* TOFcom;
TH1D* MCTruth;
TH1D* refMCTruth;
TH2D* TOF01vsMCTruth;
TH2D* refTOFvsMCTruth;
TH2D* TOFvsMCTruth;
TH2D* TOF12Paul6thforvsMCTruth;
TH2D* TOF12longPaul6thforvsMCTruth;
TH2D* TOF12Paul2ndforvsMCTruth;
TH2D* TOF01Paul6thforvsMCTruth;
TH2D* TOF01shortPaul6thforvsMCTruth;
TH2D* TOF01fordownvsMCTruth;
TH2D* TOF12fordownvsMCTruth;
TH2D* TOF12cobbvsMCTruth;
TH2D* TOF01forupvsTOF01fordown;
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;
TH1D* theta_true_scat_graph;
TH1D* theta_true_scat2_graph;
TH1D* minustheta_true_x_graph;
TH1D* minustheta_true_y_graph;
TH1D* mctheta_true_x_graph;
TH1D* mctheta_true_y_graph;
TH1D* mctheta_true_scat_graph;
TH1D* sym_theta_true_x_graph;
TH1D* sym_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(const bool& isdata);
Vars RadialSelection(double& pz, double& pos, double& radius);
bool TruthMatchUSDS();
bool TruthTime(const bool& isdata);
bool TruthTime12(const bool& isdata);
bool TruthRadialSelection(double& pz, double& pos, double& radius, int& j);
std::vector<double> DefineProjectionAngles(Vars& US, Vars& DS);
std::vector<double> RotDefineProjectionAngles(const Vars& US, const Vars& DS, int l);
std::vector<double> mcRotDefineProjectionAngles(const Vars& US, const Vars& DS, int l);
TH1D *defineHist2(const char* name, const char* title, Double_t& nbinsx, Double_t& xlow, Double_t& xup);
double ReadOffset(bool file);
double WriteOffset(bool file);
double getoffset(xmlNode * a_node, bool file);
double MomentumFromTOF(const bool& isdata);
double BetheBloch(double& pz, double& Imat, double& Z, double& A, double& hw);
double MostProbBB(double& pz, double& Imat, double& Z, double& A, double& hw, double& R, double& z);
double TimeofFlight(const bool& raw);
double TimeofFlight12(const bool& raw);
std::vector<double> CalculatePathLength(double& pz);
std::vector<double> rCalculatePathLength(double& pz);
double PathLengthInLH2(double& pz);
double CorMomFromTOF(Vars& set, double& diff, const bool& isdata);
double MomCalc(double& TOF, const bool& isdata);
bool findVirtualPlanes();
void tophatl();
double_t tophatl_(double_t *x, double_t *par);
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 TruthGraph(Collection& USMC, Collection& DSMC, bool recon);
void FillVarsVirtual(Vars& tmpvar, int& j, int& centre);
void FillCollectionSciFi(Collection& Set, int& j, int& k, int& l, int& pid, double& pz, int& isDS, Vars& difproj, Vars& proj, std::vector<float> chi2nUS, int& beamtype, double& ptruth, const bool& difcut, const bool& fidcut, const bool& chicut, const bool& TOFcut);
void FillEventCollection(Collection& Set, Vars& tmpvar);
void FillVarsSciFi(Vars& tmpvar, int& j, int& k, int& l, int& pid, double& pz, int& isDS, Vars& proj, Vars& projdif, std::vector<float> chi2nUS, int& beamtype, double& ptruth, const bool& difcut, const bool& fidcut, const bool& chicut, const bool& TOFcut);
void make_beam_histograms(Collection& Set, std::string desc, std::string suffix);
void data_make_beam_histograms(Collection& Set, std::string desc, std::string suffix, Collection& SetMC);
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);
void trkr_eff(Collection& USTruthSethold,
Collection& DSTruthSethold,
Collection& _USsethold,
Collection& _DSsethold,
std::string desc,
std::string suffix);
Json::Value SetupConfig(int verbose_level);
Vars PropagateVarsMu(const Vars& event, const 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 a;
double b;
double cp;
double d;
double operator()(double x) const { return a*pow(x,6)+b*pow(x,3)+cp*pow(x,2)+d; }
double Derivative(double x) const { return 6*a*pow(x,5)+3*b*pow(x,2)+2*cp*x; }
};
class My2Function1D {
public:
double a;
double b;
double cp;
double d;
double operator()(double x) const { return a*pow(x,6)+b*pow(x,3)+cp*pow(x,2)+d; }
double Derivative(double x) const { return 6*a*pow(x,5)+3*b*pow(x,2)+2*cp*x; }
};
class My3Function1D {
public:
double a;
double b;
double cp;
double d;
double operator()(double x) const { return a*pow(x,6)+b*pow(x,4)+cp*pow(x,3)+d; }
double Derivative(double x) const { return 6*a*pow(x,5)+4*b*pow(x,3)+3*cp*pow(x,2); }
};