reconstructAndPlot.cc 16.4 KB
Newer Older
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

/////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//  Author: Jonas Nuber, jonas.nuber@psi.ch
//  Date:    November 2020
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

//// compile with g++ -std=c++11 -o reconstructAndPlot reconstructAndPlot.cc `root-config --cflags --glibs`


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <map>
#include <vector>
#include <random>
#include <algorithm>

//ROOT includes

#include "TFile.h"
#include "TSystem.h"

#include "TROOT.h"
#include "TString.h"
#include "TNtuple.h"
#include "TMath.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TH2.h"
#include "TColor.h"
#include "TF1.h"
#include "TF2.h"
#include "TLegend.h"
#include "TGraph.h"
#include "TGraphErrors.h"
#include "TVector.h"
#include "TList.h"
#include "TLatex.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TGaxis.h"
#include "TFolder.h"



struct hitInfo
{
    float EventID;
    float x;
    float y;
    float t;
    float qmax;

    hitInfo():EventID(-1),x(0),y(0),t(-1),qmax(0) { }
};


// the format to store the sources

void Analyze(int argc, char *argv[])
{

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///// Plotting settings
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////    

    // this is important to load trees
    gSystem->Load("libTree");

    gStyle->SetPaperSize(26, 20);
    gStyle->SetOptStat(1);
    gStyle->SetTitleXOffset(1.15);
    gStyle->SetTitleYOffset(1.15);
    gStyle->SetTitleBorderSize(0);
    gStyle->SetLegendBorderSize(0);
    gStyle->SetPadBottomMargin(0.12);
    gStyle->SetPadLeftMargin(0.12);
    gStyle->SetPadTopMargin(0.12);
    //gStyle->SetPadRightMargin(0.05);
    gStyle->SetCanvasColor(10);
    gStyle->SetPadColor(0);
    gStyle->SetTitleFillColor(0);
    gStyle->SetCanvasColor(0);
    //gStyle->SetPalette(1,0);
    //gStyle->SetOptTitle(kFALSE);
    gStyle->SetLabelSize(0.045,"XYZ");
    gStyle->SetTitleSize(0.05,"XYZ");

    const Int_t NRGBs = 5;
    const Int_t NCont = 255;
    // Define Color palette for 2D plots
    Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
    Double_t red[NRGBs]   = { 0.00, 0.00, 0.87, 1.00, 0.51 };
    Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
    Double_t blue[NRGBs]  = { 0.51, 1.00, 0.12, 0.00, 0.00 };
    TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
    gStyle->SetNumberContours(NCont);


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///// Define variables and initalize
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    // define variables to extract data from the g4bl ntuples
    Float_t x,y,t,EventID, qmax;

    if (argc<3)
    {
        std::cout << "Usage: ./executable [inputFileName] [outpuFileName]." << std::endl;
        return;
    }

    // get parameters from command line arguments
    std::string inputFileName = argv[1];
    std::string outputFileName = argv[2];

    std::vector<std::string> MM_Names;
    MM_Names.push_back("MM1");
    MM_Names.push_back("MM2");

positron cool guys's avatar
positron cool guys committed
125
126
    // distance between two detectors, exactly the same as Jonas run, mechanics was fixed already
    float dist_MM12 =  66.5; //mm
127
    // distance of MM1C from center plane
positron cool guys's avatar
positron cool guys committed
128
    float dist_MM1 = 75.; //modify for the setup, preliminary
129
130
131
132

    float size_MM = 80.0;  // size in mm

    // cut angle in degrees
positron cool guys's avatar
positron cool guys committed
133
    std::vector<float> maxAngle_degree = {20.0,17.0,13,10.0};
134
    std::vector<float> maxAngle_rad;
positron cool guys's avatar
positron cool guys committed
135
    for (int i=0;i<maxAngle_degree.size();i++){maxAngle_rad.push_back(maxAngle_degree[i]/180.*TMath::Pi()); std::cout << "angle in [rad]:" << maxAngle_degree[i]/180.*TMath::Pi() << "\n";}
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

    std::cout << " position 1\n";

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////  read in data from file
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // BG file has two additional ntuples   
    TFile* inFile = new TFile(TString(inputFileName)); 

    std::cout << " position 1.1\n";

    std::vector<TNtuple*> theMMtuples;

    for (int i=0;i<MM_Names.size();i++)
    {
        theMMtuples.push_back((TNtuple*)inFile->Get(TString(MM_Names[i])));
        theMMtuples[i]->SetBranchAddress("EventID",&EventID);
        theMMtuples[i]->SetBranchAddress("x",&x);
        theMMtuples[i]->SetBranchAddress("y",&y);
        theMMtuples[i]->SetBranchAddress("t_rise",&t);
	theMMtuples[i]->SetBranchAddress("q_max",&qmax);
    }

    std::cout << " position 2 after reading in\n";

positron cool guys's avatar
positron cool guys committed
162
    TFile* newFile = new TFile(TString(outputFileName),"RECREATE");
163
164
165
166
167
168
169
    // prepare a canvas for the plots
    TCanvas* c1 = new TCanvas;

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///// initialize histograms
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

positron cool guys's avatar
positron cool guys committed
170
    std::vector<TH2D*> projectedMaps;
171
172

    /// concidences detected
positron cool guys's avatar
positron cool guys committed
173
174
175
176
177
178
179
180
    TH2D* projectedHeatMap_20deg  = new TH2D("projectedHeatMap_20deg","reconstructed Map, angular cut 10 degrees; x [mm] ; z [mm] ",160,-43,43,160,-43,43);
    TH2D* projectedHeatMap_17deg  = new TH2D("projectedHeatMap_17deg","reconstructed Map, angular cut 8 degrees; x [mm] ; z [mm] ",160,-43,43,160,-43,43);
    TH2D* projectedHeatMap_13deg  = new TH2D("projectedHeatMap_13deg","reconstructed Map, angular cut 6 degrees; x [mm] ; z [mm] ",160,-43,43,160,-43,43);
    TH2D* projectedHeatMap_10deg  = new TH2D("projectedHeatMap_10deg","reconstructed Map, angular cut 4 degrees; x [mm] ; z [mm] ",160,-43,43,160,-43,43);

    projectedMaps.push_back(projectedHeatMap_20deg);
    projectedMaps.push_back(projectedHeatMap_17deg);
    projectedMaps.push_back(projectedHeatMap_13deg);
181
182
    projectedMaps.push_back(projectedHeatMap_10deg);

positron cool guys's avatar
positron cool guys committed
183
184
    TH2D* projectedZoom_20deg  = new TH2D("projectedZoom_20deg","reconstructed Map, angular cut 10 degrees; x [mm] ; z [mm] ",110,-14,14,125,-11,20);
    TH2D* projectedZoom_17deg  = new TH2D("projectedZoom_17deg","reconstructed Map, angular cut 8 degrees; x [mm] ; z [mm] ",110,-14,14,125,-11,20);
185

positron cool guys's avatar
positron cool guys committed
186
187
    TH2D* singleHeatMap_1  = new TH2D("singleHeatMap_1","histogram for MM2; x [mm] ; z [mm] ",80,-43,43,80,-43,43);
    TH2D* singleHeatMap_2  = new TH2D("singleHeatMap_2","histogram for MM1; x [mm] ; z [mm] ",80,-43,43,80,-43,43);
188

positron cool guys's avatar
positron cool guys committed
189
190
191
    TH2D* qmaxMap_1  = new TH2D("qmaxMap_1","qmax distribution for MM2; x [mm] ; z [mm] ",80,-43,43,80,-43,43);
    TH2D* qmaxMap_2  = new TH2D("qmaxMap_2","qmax distribution for MM1; x [mm] ; z [mm] ",80,-43,43,80,-43,43);
    TH2D* qmaxMap_projected_13deg  = new TH2D("qmaxMap_projected_13deg","average qmax for projected, cut at 6 degrees; x [mm] ; z [mm] ",80,-43,43,80,-43,43);
192

positron cool guys's avatar
positron cool guys committed
193
194
195
    TH1D* decayPositions_z  = new TH1D("decayPositions_z","reconstructed decay positions in z direction; z [mm] ",100,-32,32);
    TH1D* decayPositions_z_target  = new TH1D("decayPositions_z_target","reconstructed decay positions in z direction for -10<x1,x2,x3<10; z [mm] ",100,-32,32);
    TH1D* decayPositions_z_20deg  = new TH1D("decayPositions_z_20deg","reconstructed decay positions in z direction for -10<x<10, angle<20deg; z [mm] ",120,-32,32);
196

positron cool guys's avatar
positron cool guys committed
197
198
199
    TH1D* qMax_All  = new TH1D("qMax_All","distribution of (average) qmax; qmax ",200,0,1.4e6);
    TH1D* qMax_double  = new TH1D("qMax_double","distribution of average qmax (only double hits); qmax ",200,0,1.4e6);
    TH1D* qMax_13deg  = new TH1D("qMax_13deg","average qmax passed 6 degree cuts; qmax",200,0,1.4e6);
200

positron cool guys's avatar
positron cool guys committed
201
202
203
    TH1D* qMax_double_small  = new TH1D("qMax_double_small","distribution of average qmax (only double hits); qmax ",200,0,1.e5);
    TH1D* qMax_13deg_small  = new TH1D("qMax_13deg_small","average qmax passed 6 degree cuts; qmax",200,0,1.e5);
    TH1D* angle  = new TH1D("angle","angle to vertical; angle [rad]",1000,0,1.);
204
205


positron cool guys's avatar
positron cool guys committed
206
    //TH2D* timeVSz = new TH2D("timeVSz","reconstructed decay positions over time; timeBin; z [mm]",27,-0.5,26.5,40,-20,20);
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

    std::cout << " position 3 defined histograms\n";


    ////////////////////////////////////////////////////////////////////////////////////
    //// do reconstruction and histogram filling
    ////////////////////////////////////////////////////////////////////////////////////
    

    // analysis for one detector pair

    int n_Entries_MM1 = theMMtuples[0]->GetEntries();
    int n_Entries_MM2 = theMMtuples[1]->GetEntries();

    std::cout << "n_Entries_MM1="<<n_Entries_MM1<<" n_Entries_MM2="<<n_Entries_MM2<<std::endl;

    // get highest EventIds from the two ntuples
    theMMtuples[0]->GetEntry(n_Entries_MM1-1);
    float EventID_MM1 = EventID;

    theMMtuples[1]->GetEntry(n_Entries_MM2-1);
    float EventID_MM2 = EventID;

    // the ntuple with the highest EventID sets the number of Events
    int numberOfEvents_MM1 = std::max(EventID_MM1,EventID_MM2);

    std::cout << "numberOfEvents_MM1="<<numberOfEvents_MM1<<std::endl;

    std::vector <hitInfo> hitsVector_MM1;
    std::vector <hitInfo> hitsVector_MM2;

    for (int i=0;i<numberOfEvents_MM1;i++)
    {
        hitsVector_MM1.push_back(hitInfo());
        hitsVector_MM2.push_back(hitInfo());
    }

    /// prepare vectors with entries according to the detected events
    
    for (int n=0;n<theMMtuples[0]->GetEntries();n++)
    {
        theMMtuples[0]->GetEntry(n);
        hitsVector_MM1[int(EventID)].EventID = EventID;
        hitsVector_MM1[int(EventID)].x = x;
        hitsVector_MM1[int(EventID)].y = y;
        hitsVector_MM1[int(EventID)].t = t;
	hitsVector_MM1[int(EventID)].qmax = qmax;
    }

    for (int n=0;n<theMMtuples[1]->GetEntries();n++)
    {
        theMMtuples[1]->GetEntry(n);
        hitsVector_MM2[int(EventID)].EventID = EventID;
        hitsVector_MM2[int(EventID)].x = x;
        hitsVector_MM2[int(EventID)].y = y;
        hitsVector_MM2[int(EventID)].t = t;
	hitsVector_MM2[int(EventID)].qmax = qmax;
    }

    std::cout << "hitsVector_MM1.size(0)="<< hitsVector_MM1.size() << "  hitsVector_MM2.size(0)="  << hitsVector_MM2.size() << std::endl;


    int count_Coincidences = 0;

    // now go throug vectors and reproduce position on center plane
    for (int i=0;i<numberOfEvents_MM1;i++)
    {
	double average_qMax = 0;

        if (hitsVector_MM2[i].EventID>=0)
        {
            singleHeatMap_1->Fill(hitsVector_MM2[i].x,hitsVector_MM2[i].y);
	    qmaxMap_1->Fill(hitsVector_MM2[i].x,hitsVector_MM2[i].y,hitsVector_MM2[i].qmax);
            average_qMax += hitsVector_MM2[i].qmax;
        }
        if (hitsVector_MM1[i].EventID>=0)
        {
            singleHeatMap_2->Fill(hitsVector_MM1[i].x,hitsVector_MM1[i].y);
	    qmaxMap_2->Fill(hitsVector_MM1[i].x,hitsVector_MM1[i].y,hitsVector_MM1[i].qmax);
	    average_qMax += hitsVector_MM1[i].qmax;
        }
        // check that both MM had hits
        if (hitsVector_MM1[i].EventID>=0 && hitsVector_MM2[i].EventID>=0 && hitsVector_MM1[i].EventID==hitsVector_MM2[i].EventID)
        {
	    average_qMax /= 2.;
            qMax_double->Fill(average_qMax);
            qMax_double_small->Fill(average_qMax);

            count_Coincidences++;
            float Delta_x = hitsVector_MM1[i].x-(hitsVector_MM2[i].x);
            float Delta_y = hitsVector_MM1[i].y-(hitsVector_MM2[i].y);

            // reconstruct: linear extrapolation
            float reconstructed_x = hitsVector_MM2[i].x + (Delta_x)/dist_MM12*(dist_MM12+dist_MM1);
            float reconstructed_y = hitsVector_MM2[i].y + (Delta_y)/dist_MM12*(dist_MM12+dist_MM1);

            // average for time
            int reconstructed_Time = (hitsVector_MM1[i].t+hitsVector_MM2[i].t)/2.;
positron cool guys's avatar
positron cool guys committed
305
	    
306
            float angle_to_Vertical = TMath::ATan( TMath::Sqrt(TMath::Power(Delta_x,2)+TMath::Power(Delta_y,2)) / dist_MM12);
positron cool guys's avatar
positron cool guys committed
307
308
	    //std::cout << angle_to_Vertical << "\n";
	    angle->Fill(angle_to_Vertical);
309
310
311
312
313
314
315
316
317
            // angular cut for 2D histo

	    for (int l=0;l<maxAngle_rad.size();l++)
	    {
	       if (angle_to_Vertical<maxAngle_rad[l])
	       {
	         projectedMaps[l]->Fill(reconstructed_x,reconstructed_y);
		 if (l==0)
		 {
positron cool guys's avatar
positron cool guys committed
318
	           projectedZoom_20deg->Fill(reconstructed_x,reconstructed_y);
319
320
321
		 }
		 else if (l==1)
		 {
positron cool guys's avatar
positron cool guys committed
322
		   projectedZoom_17deg->Fill(reconstructed_x,reconstructed_y);
323
324
325
		 }
		 else if (l==2)
		 {
positron cool guys's avatar
positron cool guys committed
326
327
328
		   qmaxMap_projected_13deg->Fill(reconstructed_x,reconstructed_y,average_qMax);
                   qMax_13deg->Fill(average_qMax);
                   qMax_13deg_small->Fill(average_qMax);
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
		 }
               }
	    }
            // now prepare z distributions. Cross check: is this the "correct" z dimension?

            //float z_C = TMath::Sqrt(TMath::Power(hitsVector_MM1[i].x,2)+TMath::Power(hitsVector_MM1[i].y,2));
            //float z_F = TMath::Sqrt(TMath::Power(hitsVector_MM2[i].x,2)+TMath::Power(hitsVector_MM2[i].y,2));
        
            if (true || TMath::Abs(Delta_y)/dist_MM12 < TMath::Tan(maxAngle_rad[0]))
            {
                //float reconstructed_z = (z_C - z_F)/dist_MM2*(dist_MM2+dist_MM1);

                // accepotance of vertical line is largest in center of micromegas - balance this (only very rough approximation)
                float acceptance_Factor = 1-TMath::Sqrt(2.)/size_MM*TMath::Abs(reconstructed_y);
                decayPositions_z->Fill(reconstructed_y,acceptance_Factor);
                if (std::abs(reconstructed_x)<10 && std::abs(hitsVector_MM2[i].x)<10 && std::abs(hitsVector_MM1[i].x)<10) {decayPositions_z_target->Fill(reconstructed_y);}
                //timeVSz->Fill(reconstructed_Time,reconstructed_y,acceptance_Factor);

            }
	    if (TMath::Abs(Delta_y)/dist_MM12 < TMath::Tan(maxAngle_rad[0]))
	    {
positron cool guys's avatar
positron cool guys committed
350
	        if (std::abs(reconstructed_x)<10) decayPositions_z_20deg->Fill(reconstructed_y);
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
            }
        }
        else
        {
            //std::cout << "no coinc: EventID_C=" << hitsVector_MM1[i].EventID << "  EventID_F=" << hitsVector_MM2[i].EventID << std::endl;
        }
	qMax_All->Fill(average_qMax);

    }

    std::cout << "fraction of coincidences relative to triggered events:" << count_Coincidences/float(numberOfEvents_MM1) << std::endl; 
   

    for (int nBinsX=0;nBinsX<=singleHeatMap_1->GetNbinsX()+1;nBinsX++)
    {
      for (int nBinsY=0;nBinsY<=singleHeatMap_1->GetNbinsY()+1;nBinsY++)
      {
	if (singleHeatMap_1->GetBinContent(nBinsX,nBinsY)>0)
          { qmaxMap_1->SetBinContent(nBinsX,nBinsY,qmaxMap_1->GetBinContent(nBinsX,nBinsY)/singleHeatMap_1->GetBinContent(nBinsX,nBinsY));}
	if (singleHeatMap_2->GetBinContent(nBinsX,nBinsY)>0)
          { qmaxMap_2->SetBinContent(nBinsX,nBinsY,qmaxMap_2->GetBinContent(nBinsX,nBinsY)/singleHeatMap_2->GetBinContent(nBinsX,nBinsY));}

positron cool guys's avatar
positron cool guys committed
373
374
	if (projectedHeatMap_13deg->GetBinContent(nBinsX,nBinsY)>0)
	  { qmaxMap_projected_13deg->SetBinContent(nBinsX,nBinsY,qmaxMap_projected_13deg->GetBinContent(nBinsX,nBinsY)/projectedHeatMap_13deg->GetBinContent(nBinsX,nBinsY));}
375
376
377
378
379
380
381
382
383
      }
    }




    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // plot histograms
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
positron cool guys's avatar
positron cool guys committed
384
    
385
    newFile->cd();
positron cool guys's avatar
positron cool guys committed
386
387
    newFile->Write();
    newFile->Close();
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

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///// plot the histograms
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //std::cout << " position 8 Plot\n";


    //c1->Divide(2,2);

    //TCanvas* c2 = new TCanvas;
    //c2->Divide(2,2);

    //TCanvas* c3 = new TCanvas;
    //c3->Divide(2,2);



    //c1->cd(1);
    //Sum_coinc_1->SetTitle("A1-B1 coincidence");
    //Sum_coinc_1->SetStats(kFALSE);

    //Sum_coinc_1->SetLineColor(kBlue);
    //Sum_coinc_1->DrawCopy();
    //BG_coinc_1->SetLineColor(kRed);
    //BG_coinc_1->DrawCopy("same");


}


# ifndef __CINT__  // the following code will be invisible for the root interpreter
int main(int argc, char *argv[])
{
    Analyze(argc, argv);
 
 
    std::cout << "Done." << std::endl;
    return 0;
}



#endif