-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLSQFit.cpp
More file actions
110 lines (84 loc) · 2.62 KB
/
Copy pathLSQFit.cpp
File metadata and controls
110 lines (84 loc) · 2.62 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
#include "TRandom2.h"
#include "TGraphErrors.h"
#include "TMath.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TH2F.h"
#include "TH1F.h"
#include "TGClient.h"
#include "TStyle.h"
#include <iostream>
using namespace std;
using TMath::Log;
//parms
const double xmin=1;
const double xmax=20;
const int npoints=12;
const double sigma=0.2;
double f(double x){
const double a=0.5;
const double b=1.3;
const double c=0.5;
return a+b*Log(x)+c*Log(x)*Log(x);
}
void getX(double *x){
double step=(xmax-xmin)/npoints;
for (int i=0; i<npoints; i++){
x[i]=xmin+i*step;
}
}
void getY(const double *x, double *y, double *ey){
static TRandom2 tr(0);
for (int i=0; i<npoints; i++){
y[i]=f(x[i])+tr.Gaus(0,sigma);
ey[i]=sigma;
}
}
void leastsq(){
double x[npoints];
double y[npoints];
double ey[npoints];
getX(x);
getY(x,y,ey);
auto tg = new TGraphErrors(npoints,x,y,0,ey);
tg->Draw("alp");
}
int main(int argc, char **argv){
TApplication theApp("App", &argc, argv); // init ROOT App for displays
// ******************************************************************************
// ** this block is useful for supporting both high and std resolution screens **
UInt_t dh = gClient->GetDisplayHeight()/2; // fix plot to 1/2 screen height
//UInt_t dw = gClient->GetDisplayWidth();
UInt_t dw = 1.1*dh;
// ******************************************************************************
gStyle->SetOptStat(0); // turn off histogram stats box
TCanvas *tc = new TCanvas("c1","Sample dataset",dw,dh);
double lx[npoints];
double ly[npoints];
double ley[npoints];
getX(lx);
getY(lx,ly,ley);
auto tgl = new TGraphErrors(npoints,lx,ly,0,ley);
tgl->SetTitle("Pseudoexperiment;x;y");
// An example of one pseudo experiment
tgl->Draw("alp");
tc->Draw();
// *** modify and add your code here ***
TH2F *h1 = new TH2F("h1","Parameter b vs a;a;b",100,0,1,100,0,1);
TH2F *h2 = new TH2F("h2","Parameter c vs a;a;c",100,0,1,100,0,1);
TH2F *h3 = new TH2F("h3","Parameter c vs b;b;c",100,0,1,100,0,1);
TH1F *h4 = new TH1F("h4","reduced chi^2;;frequency",100,0,1);
// perform many least squares fits on different pseudo experiments here
// fill histograms w/ required data
TCanvas *tc2 = new TCanvas("c2","my study results",200,200,dw,dh);
tc2->Divide(2,2);
tc2->cd(1); h1->Draw("colz");
tc2->cd(2); h2->Draw("colz");
tc2->cd(3); h3->Draw("colz");
tc2->cd(4); h4->Draw();
tc2->Draw();
// **************************************
cout << "Press ^c to exit" << endl;
theApp.SetIdleTimer(30,".q"); // set up a failsafe timer to end the program
theApp.Run();
}