Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timewalk_fits.C
Go to the documentation of this file.
1 void GetData(TString fname, double *y, double *dy, double &min, double &max) {
2  ifstream fin(fname);
3  const int N = 41; // pulse-height bins
4  for (int i = 0; i < N; i++) {
5  char sep; int n; double stats, t, dt, sigma;
6  fin >> n >> sep >> n >> sep >> stats >> sep >> t >> sep >> dt >> sep >> sigma;
7  if (dt < 0.01) dt = 0.01;
8  y[i] = t; dy[i] = dt;
9  if (t > max) max = t;
10  if (t < min) min = t;
11  if (sigma < 0.06) {y[i] = 0.0; dy[i] = 0.0;} // remove bad Gaussian-fits
12  if (i < 5 && t < 0.1) {y[i] = 0.0; dy[i] = 0.0;} // ignore outliers
13  }
14  fin.close();
15 }
16 double func(double *x, double *par) {
17  if (x[0] < par[3] && x[0] >= 200.0)
18  return par[0]+par[1]/pow(x[0],par[2]);
19  else if (x[0] >= par[3])
20  return par[0]+par[1]*(1.0+par[2])/pow(par[3],par[2])-x[0]*par[1]*par[2]/pow(par[3],par[2]+1.0);
21  else return 999.0;
22 }
23 void WriteNA(ofstream &fout, ofstream &fout_ccdb, int counter) {
24  double na = 0.0; TString sep = ",";
25  fout << counter << sep << na << sep << na << sep << na;
26  fout << sep << na << sep << na << sep << na;
27  fout << sep << na << sep << na << sep << na << sep << na << endl; sep = " ";
28  fout_ccdb << counter << sep << 0.0 << sep << 0.0 << sep << 0.0 << sep << 0.0 << endl;
29 }
30 void WriteTimewalkFitResults(ofstream &fout, ofstream &fout_ccdb, int counter, double *y, double *dy, double min, double max) {
31  if (min == 0.0 && max == 0.0) {WriteNA(fout,fout_ccdb,counter); return;}
32  TString sep = ",";
33  stringstream ss; ss << counter;
34  TCanvas c("c","c",800,500);
35  const int N = 41; // pulse-height bins
36  double x[N]; double dx[N];
37  for (int i = 0; i < N; i++) {
38  x[i] = 50.0 + i*100.0;
39  dx[i] = 0.0;
40  }
41  TGraphErrors gr(N,x,y,dx,dy);
42  TF1 f("f",func,200.0,4000.0,4);
43  f.SetParameter(0,min); f.SetParLimits(0,min-10.0,0.0);
44  f.SetParameter(1,25.0); f.SetParLimits(1,1.0,90.0);
45  f.SetParameter(2,0.5); f.SetParLimits(2,0.02,0.85);
46  f.SetParameter(3,2250.0); f.SetParLimits(3,1400.0,4000.0);
47  gr.SetTitle("TAGH counter "+TString(ss.str()));
48  gr.GetXaxis()->SetTitle("pulse-height [fADC counts]");
49  gr.GetYaxis()->SetTitle("Gaussian mean of time(TDC) - time(RF) [ns]");
50  gr.Fit("f","eir");
51  gr.GetYaxis()->SetRangeUser(min-0.2,max+0.2);
52  gr.Draw("AP");
53  if (f.GetNDF() == 0 || f.GetChisquare()/f.GetNDF() > 15.0) {
54  WriteNA(fout,fout_ccdb,counter);
55  return;
56  }
57  system("mkdir -p fits_timewalk");
58  c.Print("fits_timewalk/counter_"+TString(ss.str())+".gif");
59  c.Clear();
60  fout << counter << sep << f.GetNDF() << sep << f.GetChisquare() << sep << f.GetParameter(0);
61  fout << sep << f.GetParameter(1) << sep << f.GetParameter(2) << sep << f.GetParameter(3);
62  fout << sep << f.GetParError(0) << sep << f.GetParError(1) << sep << f.GetParError(2) << sep << f.GetParError(3) << endl; sep = " ";
63  fout_ccdb << counter << sep << f.GetParameter(0) << sep << f.GetParameter(1) << sep << f.GetParameter(2) << sep << f.GetParameter(3) << endl;
64 }
65 void PrintHistogram(TH1D h) {
66  gStyle->SetOptStat("eimr");
67  TCanvas c("c","c",800,500);
68  h.Draw();
69  system("mkdir -p parms_timewalk");
70  c.Print("parms_timewalk/"+TString(h.GetName())+".gif");
71 }
72 void PrintHistograms(TString fname) {
73  TH1D *h[4]; TH1D *he[4];
74  double min[] = {-8.0,1.0,0.0,1400.0}; double max[] = {2.0,91.0,1.0,4000.0};
75  double emin[] = {0.0,0.0,0.0,0.0}; double emax[] = {0.5,10.0,0.05,100.0};
76  for (int i = 0; i < 4; i++) {
77  stringstream ss; ss << i; TString name = "c" + TString(ss.str());
78  h[i] = new TH1D(name,"TAGH timewalk: "+name+";"+name+";counters",100,min[i],max[i]);
79  name = "e_c" + TString(ss.str());
80  he[i] = new TH1D(name,"TAGH timewalk: "+name+";"+name+";counters"+";"+name+";counters",100,emin[i],emax[i]);
81  }
82  TH1D hChisq("Chisq","TAGH timewalk: chi-square / Ndf;chi-square / Ndf;counters",150,0.0,15.0);
83  ifstream fin(fname);
84  for (int i = 0; i < 274; i++) { // counters
85  char sep; int n, ndf; double chisq, c[4], dc[4];
86  fin >> n >> sep >> ndf >> sep >> chisq;
87  for (int j = 0; j < 4; j++) fin >> sep >> c[j];
88  for (int j = 0; j < 4; j++) fin >> sep >> dc[j];
89  if (chisq == 0.0) continue;
90  for (int j = 0; j < 4; j++) h[j]->Fill(c[j]);
91  for (int j = 0; j < 4; j++) he[j]->Fill(dc[j]);
92  hChisq.Fill(chisq/ndf);
93  }
94  fin.close();
95  for (int i = 0; i < 4; i++) {
96  PrintHistogram(*h[i]); PrintHistogram(*he[i]);
97  delete h[i]; delete he[i];
98  }
99  PrintHistogram(hChisq);
100 }
101 int timewalk_fits(TString dir) {
102  gStyle->SetOptFit(111);
103  TString fname = "timewalk-fits.txt";
104  ofstream fout; fout.open(fname);
105  ofstream fout_ccdb; fout_ccdb.open("tdc_timewalk.txt");
106  for (int i = 1; i <= 274; i++) { // counters
107  stringstream ss; ss << i;
108  const int N = 41; // pulse-height bins
109  double y[N]; double dy[N];
110  double max = -10.0; double min = 10.0;
111  GetData(dir+"/counter_"+TString(ss.str())+".txt",y,dy,min,max);
112  WriteTimewalkFitResults(fout,fout_ccdb,i,y,dy,min,max);
113  }
114  fout.close(); fout_ccdb.close();
115  PrintHistograms(fname);
116  return 0;
117 }
int timewalk_fits(TString dir)
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
void PrintHistogram(TH1D h)
Definition: timewalk_fits.C:65
axes Fill(100, 100)
#define c
#define y
TGraph * gr[NCHANNELS]
Definition: st_tw_resols.C:43
TF1 * f
Definition: FitGains.C:21
double counter
Definition: FitGains.C:151
void WriteTimewalkFitResults(ofstream &fout, ofstream &fout_ccdb, int counter, double *y, double *dy, double min, double max)
Definition: timewalk_fits.C:30
Double_t sigma[NCHANNELS]
Definition: st_tw_resols.C:37
void GetData(TString fname, double *y, double &mean, const int N)
void PrintHistograms(TString fname)
Definition: timewalk_fits.C:72
void WriteNA(ofstream &fout, ofstream &fout_ccdb, int counter)
Definition: timewalk_fits.C:23
double func(double *x, double *par)
Definition: timewalk_fits.C:16
TDirectory * dir
Definition: bcal_hist_eff.C:31