targetPosition.c

realization of the described algorithm - Ivan Muzalevsky, 07/23/2020 09:38 AM

Download (3.61 KB)

 
1
 #include "TCanvas.h"
2
 #include "TRandom3.h"
3
 #include "TGraph.h"
4
 #include "TMath.h"
5
 #include "TArc.h"
6
 #include "Fit/Fitter.h"
7

    
8
void circleFit(Float_t *x,Float_t* y);
9
Float_t  x[4],y[4];
10

    
11
void targetPosition() {
12

    
13
   gStyle->SetOptStat(0);
14

    
15
   TChain *ch_be = new TChain("tree");
16
   ch_be->Add("/media/ivan/data/exp1906/be10/analysed/beamDiagnostics/eqTriggers/be10*");
17
   // ch_be->Add("/mnt/data/exp1904/analysed/selected/beamDiagnostics/h7*");
18

    
19

    
20
   TF2 *fitf = new TF2("fitf","bigaus");
21
   
22
   TCanvas *c1 = new TCanvas("c1","title",1000,1000);
23
   c1->Divide(2,2);
24

    
25
   c1->cd(1);
26
   ch_be->Draw("fYt:fXt>>p2(64,-20,20,64,-20,20)","trigger==2","col");
27
   TH2F *htemp = (TH2F*)gPad->GetPrimitive("p2");
28
   fitf->SetRange(-14.38,-6.25,-8.13,6.88);
29
   htemp->Fit(fitf,"R");
30

    
31
   x[0] = fitf->GetParameter("MeanX");
32
   y[0] = fitf->GetParameter("MeanY");
33

    
34
   c1->cd(2);
35
   ch_be->Draw("fYt:fXt>>p3(64,-20,20,64,-20,20)","trigger==3","col");
36
   htemp = (TH2F*)gPad->GetPrimitive("p3");
37
   fitf->SetRange(-8.13,-14.38,6.88,-9.38);
38
   htemp->Fit(fitf,"R+");
39

    
40
   x[1] = fitf->GetParameter("MeanX");
41
   y[1] = fitf->GetParameter("MeanY");   
42

    
43
   c1->cd(3);
44
   ch_be->Draw("fYt:fXt>>p4(64,-20,20,64,-20,20)","trigger==4","col");
45
   htemp = (TH2F*)gPad->GetPrimitive("p4");
46
   fitf->SetRange(8.75,-6.88,13.75,5.63);
47
   htemp->Fit(fitf,"R+");
48

    
49
   x[2] = fitf->GetParameter("MeanX");
50
   y[2] = fitf->GetParameter("MeanY"); 
51

    
52
   c1->cd(4);
53
   ch_be->Draw("fYt:fXt>>p5(64,-20,20,64,-20,20)","trigger==5","col");
54
   htemp = (TH2F*)gPad->GetPrimitive("p5");
55
   fitf->SetRange(-6.25,8.75,8.13,14.38);
56
   htemp->Fit(fitf,"R+");
57

    
58
   x[3] = fitf->GetParameter("MeanX");
59
   y[3] = fitf->GetParameter("MeanY");    
60

    
61
   c1->Update();
62
   c1->Print("/home/ivan/Desktop/sent/triggerPositions.png");
63

    
64
   for (Int_t i=0;i<4;i++) {
65
      cout << x[i] << " " << y[i] << endl;
66
   }
67

    
68
   circleFit(x,y);
69

    
70
}
71

    
72

    
73
void circleFit(Float_t *x,Float_t* y) {
74

    
75
  TGraph *g = new TGraph(4,x,y);
76
  g->SetMarkerStyle(20);
77

    
78
  auto chi2Function = [&](const Double_t *par) {
79
       //minimisation function computing the sum of squares of residuals
80
       // looping at the graph points
81
       Int_t np = g->GetN();
82
       Double_t f = 0;
83
       Double_t *x = g->GetX();
84
       Double_t *y = g->GetY();
85
       for (Int_t i=0;i<np;i++) {
86
          Double_t u = x[i] - par[0];
87
          Double_t v = y[i] - par[1];
88
          Double_t dr = par[2] - std::sqrt(u*u+v*v);
89
          // Double_t dr = 12.5*12.5 - std::sqrt(u*u+v*v);
90
          f += dr*dr;
91
       }
92
       return f;
93
    };
94

    
95
    // wrap chi2 funciton in a function object for the fit
96
    // 3 is the number of fit parameters (size of array par)
97
    ROOT::Math::Functor fcn(chi2Function,3);
98
    ROOT::Fit::Fitter  fitter;
99
 
100
 
101
    double pStart[3] = {0,0,12};
102
    fitter.SetFCN(fcn, pStart);
103
    fitter.Config().ParSettings(0).SetName("x0");
104
    fitter.Config().ParSettings(0).SetLimits(-5,5);
105

    
106
    fitter.Config().ParSettings(1).SetName("y0");
107
    fitter.Config().ParSettings(1).SetLimits(-5,5);
108

    
109
    fitter.Config().ParSettings(2).SetName("R");
110
    fitter.Config().ParSettings(2).SetLimits(5,20);
111
 
112
    // do the fit 
113
    bool ok = fitter.FitFCN();
114
    if (!ok) {
115
       Error("line3Dfit","Line3D Fit failed");
116
    }   
117
 
118
    const ROOT::Fit::FitResult & result = fitter.Result();
119
    result.Print(std::cout);
120
 
121
    //Draw the circle on top of the points
122

    
123
    TCanvas *c1 = new TCanvas("c1","fit",1000,1000);
124
    c1->cd();
125

    
126
    TArc *arc = new TArc(result.Parameter(0),result.Parameter(1),result.Parameter(2));
127
    arc->SetFillStyle(0); 
128
    arc->SetLineColor(kRed);
129
    arc->SetLineWidth(2);
130

    
131
    g->Draw();
132
    arc->Draw("same");
133
    c1->Print("/home/ivan/Desktop/sent/targetFit.png");
134
}