source: JEM-EUSO/equalization_gain/trunk/src/PlotScurvesAll-diffFit.cc @ 201

Last change on this file since 201 was 201, checked in by dagoret, 11 years ago

class Analysis finished and its compile but need to be debuged

File size: 51.8 KB
Line 
1#include <fstream>
2#include <iostream>
3#include "TROOT.h"
4#include "TString.h"
5#include "TH1D.h"
6#include "TH2D.h"
7#include "TCanvas.h"
8#include "TF1.h"
9#include "TPad.h"
10#include "TGraphErrors.h"
11#include "TGraph.h"
12#include "TLine.h"
13#include "TROOT.h"
14#include "TString.h"
15#include <TStyle.h>
16
17#include "PlotScurvesAll-diffFitConst.h"
18#include "PlotScurvesAll-diffFit.h"
19
20//#if defined(__CINT__) && !defined(__MAKECINT__)
21//#include "PlotScurveAll-diffFit.h"
22//#else
23//#include "PlotScurvesAll-diffFit.h"
24//#endif
25
26
27
28// for format of EC_ASIC Scurves_all_ASIC data
29// ASIC A,B,C,.... : i=0,1,2,... ch 0,1,...,63 : j=0,1,...,63
30// % cat scurves_PC_inj_run5.lvm |awk '{i=2;j=37;print($1,$(i*64+j+1+1))}'
31//   > scurves_PC_inj_run5-C-ch37.data
32// => % cd $datadir
33// => % sh $srcdir/makeDataFile.sh with assigining proper fname
34// PlotScurvesAll-diffFit-v6.6.C: dac_sfit[ch]=-1; in the case of fitting failure.
35
36using namespace std;
37Int_t          kXmin       = 0;
38
39//--------------------------------------------------------------------------------------------------------
40void Help()
41{
42
43  cout << "This is the Help of the PlotScurveAll-diffFit" <<endl;
44  cout << " Version 1.0 frol September 24 2013 " <<endl;
45  cout << " Main author Hiroko Miyamoto " << endl;
46  cout << " ==> Please in ROOT interpreter, issue the command .L PlotScurvesAll-diffFit.cc " <<endl;
47  cout << " ==> Then play with PlotScurve() function " << endl;
48  cout << " ==> If you need more information, please type Menu()" << endl;
49   
50}
51//--------------------------------------------------------------------------------------------------------
52
53//--------------------------------------------------------------------------------------------------------
54void Menu()
55{
56 cout << "This is the Detailed menu information for running the PlotScurveAll-diffFit" <<endl;
57
58 cout << " void PlotScurve( " << endl;
59 cout << " \t " <<  " Int_t    kDrawCh=31, " << endl;
60 cout << " \t " <<  " TString  fname=kFname, " << endl;
61 cout << " \t " <<  " TString  fname_ped=kFnamePed, //ASIC_D ped_file " << endl;
62 cout << " \t " <<  " Double_t dac_half_spe_fix = -1," << endl;
63 cout << " \t " <<  " TString  fname_gain=\"gain-org.txt\"," << endl;
64 cout << " \t " <<  " Int_t    fit_min=kFitMin," << endl;
65 cout << " \t " <<  " Int_t    fit_max=kFitMax," << endl;
66 cout << " \t " <<  " Int_t    fit_min_scurve=kFitMinS," << endl;
67 cout << " \t " <<  " Int_t    fit_max_scurve=kFitMaxS," << endl;
68 cout << " \t " <<  " Bool_t   kBoard=1, //0:ASIC_C, 1:ASIC_D " << endl;
69 cout << " \t " <<  " Int_t    kChRef=kRefCh,//ASIC_C " << endl;
70 cout << " \t " <<  " Bool_t   checkAve=0 //0:use ref pix, 1:check average " << endl;
71 cout << " ) " << endl;         
72 cout << "Set the parameters on the top of the program depending on your analysis:  " << endl;
73 cout << "General policy: parameters starting with a \"k\" at the beginning is fixed gloval parameter and usually set in the header file, never change through the program. " << endl;
74 cout << " : " << endl;
75 cout << " - kDrawCh: channel just you want to check on display. doens't effect the analysis. " << endl;
76 
77 cout << " - fname, fname_ped (kFname, kFnamePed): \"YOUR DATA FILE\" without .data"  << endl;
78 cout << " See examples in the header file. " << endl;
79 cout << " - dac_half_spe_fix: set DAC value only when you want to adjust the gain to particular pulse height (DAC value) instead of either average of the pulse height of reference pixel. Usually just leave it -1. " << endl;
80 cout << " - fit_min, fit_max (kFitMin, kFitMax): leave them as default, it is not currently used for the analysis."  << endl;
81 cout << " - fit_min_scurve, fit_max_scurve (kFitMinS, kFitMaxS): define the range of s-curve fitting. search proper values by yourself with running root program a few times."  << endl;
82 cout << " - kBoard: don't forget to change 0(C) or 1(D) depending on which ASIC you're analysing "  << endl;
83 cout << " - kChRef (kRefCh): a pixel whose gain remains 64 thorough the gain adjustment. " << endl;
84 cout << " - checkAve: for the first run, set this parameter 1, and find a pixel which remains 64. In the case of parameter 1, all the gains are adjusted to the average pulse height. " << endl;
85   cout <<  " Once you choose the reference pixel, set the parameter 0, as well as set kRefCh the pixel number. (better to keep in the header file for the record of each PMTs.) " << endl;
86
87   cout << " Actual configuration " << endl;
88   cout << " \t " <<  "TString kFname = " << kFname << endl;
89   cout << " \t " <<  "TString  kFnamePed = " << kFnamePed  << endl;
90   cout << " \t " <<  "Int_t kFitMin = " << kFitMin << endl; 
91   cout << " \t " <<  "Int_t kFitMax = " << kFitMax << endl;
92
93}
94//--------------------------------------------------------------------------------------------------------
95
96//-------------------------------------------------------------------
97void InitRoot()
98{
99
100  gStyle->SetTitleFillColor(kWhite);
101  gStyle->SetTitleColor(kBlue,"xyz");
102  gStyle->SetTitleSize(0.03,"xyz");
103  gStyle->SetLabelSize(0.03,"xyz");
104  gStyle->SetLabelColor(kBlue,"xyz");
105  gStyle->SetAxisColor(kMagenta);
106  gStyle->SetPalette(1,0);
107  gStyle->SetOptFit(101);
108  gStyle->SetOptStat(0);
109 
110  //  gStyle->SetOptStat("emr");
111  gStyle->SetOptStat("");
112}
113//-------------------------------------------------------------------
114
115
116//--------------------------------------------------------------------------------------------------------
117/*
118
119The declaration of this function is now put inside the h file with the default values to be used
120
121void PlotScurve(
122                Int_t    kDrawCh=31,
123                TString  fname=kFname,
124                TString  fname_ped=kFnamePed, //ASIC_D ped_file
125                Double_t dac_half_spe_fix = -1,
126                TString  fname_gain="gain-org.txt",
127                Int_t    fit_min=kFitMin,
128                Int_t    fit_max=kFitMax,
129                Int_t    fit_min_scurve=kFitMinS,
130                Int_t    fit_max_scurve=kFitMaxS,
131                Bool_t   kBoard=1, //0:ASIC_C, 1:ASIC_D
132                Int_t    kChRef=kRefCh,//ASIC_C
133                Bool_t   checkAve=0 //0:use ref pix, 1:check average
134                )
135*/
136
137// Definition of the function  PlotScurve
138void PlotScurve(
139                Int_t    kDrawCh,
140                TString  fname,
141                TString  fname_ped, //ASIC_D ped_file
142                Double_t dac_half_spe_fix,
143                TString  fname_gain,
144                Int_t    fit_min,
145                Int_t    fit_max,
146                Int_t    fit_min_scurve,
147                Int_t    fit_max_scurve,
148                Bool_t   kBoard, //0:ASIC_C, 1:ASIC_D
149                Int_t    kChRef,//ASIC_C
150                Bool_t   checkAve //0:use ref pix, 1:check average
151                )
152
153
154{
155                //NOTE   
156                //Double_t dac_half_spe_fix = 177; //for EC_ASICb N101(asic N158).
157                //check DAC value corresponds to voltage to 160fC input
158                //(depends on DAC linearity)
159                //Double_t dac_half_spe_fix = -1 //set gain to DAC average (default)
160
161  /******************* Readout data file *********************/
162 
163  /*********************** SIGNAL RUN ********************/
164  TString *input             = new TString[kNch];
165  TString *input_ped         = new TString[kNch];
166  Int_t    ch;
167  Float_t  dac_half_sum      = 0;
168  Float_t  dac_ped_sum       = 0;
169  Float_t  dac_spe_sum       = 0;
170  Float_t  dac_sfit_sum      = 0;
171  Float_t  diff_sum          = 0;
172  Float_t  mean_sum          = 0;
173  Float_t  maxBinSum         = 0;
174  Double_t dac_half[kNch];
175  Double_t dac_ped[kNch];
176  Double_t dac_spe[kNch];
177  Double_t dac_sfit[kNch];
178  Double_t mean_diff[kNch];
179  Double_t gain_org[kNch];
180  Double_t sfit_chiS[kNch];
181  Double_t flag_fit[kNch];
182
183
184  InitRoot();
185
186  TCanvas *c1                = new TCanvas(kCanvasName1,kCanvasTitle1,200,20,1100,600); //smoothed S-curves
187  c1->Divide(11,6);
188  TCanvas *c2                = new TCanvas(kCanvasName2,kCanvasTitle2,300,50,1100,600); //differentiated histograms of smoothed s-curves
189  c2->Divide(11,6);
190  TCanvas *c5                = new TCanvas(kCanvasName5,kCanvasTitle5,400,70,1100,600);
191  c5->Divide(11,6);
192  TCanvas *c3                = new TCanvas(kCanvasName3,kCanvasTitle3,500,100,1200,300);
193  c3->Divide(4,1);
194  /*TCanvas *c6                = new TCanvas("c6","c6",450,150,1000,500);
195  c6->Divide(4,2);
196  TCanvas *c7                = new TCanvas("c7","c7",470,200,1000,500);
197  c7->Divide(4,2);*/
198  TCanvas *c4                = new TCanvas(kCanvasName4,kCanvasTitle4,900,400,300,300);
199
200  TString  name_hist[kNch];
201  TString  name_hist_ped[kNch];
202  TString  name_hist_diff[kNch];
203  TString  name_hist_diff_fit[kNch];
204  TString  name_fit_diff[kNch];
205  TH1D    *hist_scurve       = new TH1D[kNch]; // smoothed S-curves
206  TH1D    *hist_scurve_ped   = new TH1D[kNch]; // S-curves for pedestal runs
207  TH1D    *hist_diff         = new TH1D[kNch]; // differentiated histograms of smoothed s-curves
208  TH1D    *hist_diff_fit     = new TH1D[kNch]; // differentiated histograms of fitted s-curves
209  TF1     *fit_sdiff         = new TF1[kNch];  // function fitted on differentiated histograms of fitted s-curves
210
211
212  // Open the ASIC Gain file
213  //-------------------------
214
215  TString input_gain(fname_gain);
216  ifstream in_gain(input_gain.Data());
217  if(!in_gain.good()){
218    cerr << "File " << input_gain << " open failed!" << endl;
219    return;
220  }
221 
222 
223
224  // first loop on channels to fill histonames
225  //----------------------
226  for(ch=0;ch<kNch;ch++){
227    dac_half[ch]=0;
228    dac_ped[ch]=0;
229    dac_spe[ch]=0;
230    dac_sfit[ch]=0;
231    gain_org[ch]=0;
232    mean_diff[ch]=0;
233    sfit_chiS[ch]=0;
234    flag_fit[ch]=0;
235    name_hist[ch]           ="hist";
236    name_hist[ch]          +=ch;
237    name_hist_ped[ch]       ="hist_ped";
238    name_hist_ped[ch]      +=ch;
239    name_hist_diff[ch]      ="hist_diff";
240    name_hist_diff[ch]     +=ch;
241    name_hist_diff_fit[ch]  ="hist_diff_fit";
242    name_hist_diff_fit[ch] +=ch;
243    name_fit_diff[ch]       ="fit_diff";
244    name_fit_diff[ch]      +=ch;
245  }
246 
247
248  // read gain
249  Double_t da;
250  Int_t count=0;
251  ch=0;
252  while(in_gain >> da){
253    if(!kBoard && (count>=128 && count<192)){
254      gain_org[ch]=da;
255      cerr << "!kBoard, count, gain_org[" << ch << "] "
256           << !kBoard << " " << count << " " << gain_org[ch] << endl;
257      ch++;
258    }
259    if(kBoard && (count>=192 && count<256)){
260      gain_org[ch]=da;
261      cerr << "kBoard, count, gain_org[" << ch << "] "
262           << kBoard << " " << count << " " << gain_org[ch] << endl;
263      ch++;
264    }
265    count++;
266  }// end if first loop on all channels     
267  in_gain.close();
268  in_gain.clear();
269 
270  //----
271 
272  // Open the pedestal file and the S-Curve files for each channel and test if the file can be open
273
274  Int_t trigger=0, trigger_fit=0, trigger_mean=0, trigger_sfit=0;
275  // Big loop on all the channels : second loop
276  //--------------------------------------------
277  // build the input data file name for each channel fname-chXX.dat
278  for(ch=0;ch<kNch;ch++){
279    input[ch] += fname;
280    input[ch] += "-ch";
281    input[ch] += ch;
282    input[ch] += ".dat";
283    ifstream in(input[ch].Data());
284    if(!in.good()){
285      cerr << "File " << input[ch] << " open failed!" << endl;
286      return;
287    }
288
289    // build the input data file name for each channel fname_ped-chXX.dat
290    input_ped[ch] += fname_ped;
291    input_ped[ch] += "-ch";
292    input_ped[ch] += ch;
293    input_ped[ch] += ".dat";
294    ifstream in_ped(input_ped[ch].Data());
295    if(!in_ped.good()){
296      cerr << "File " << input_ped[ch] << " open failed!" << endl;
297      return;
298    }   
299
300
301   
302    //10DAC~10mV
303    // determine the DAC range in dac_min, dac_max, step_dac from file input_ped
304    // and count the number of events
305    Double_t da, dat, dac_min, dac_max, step_dac;
306    count = 0;
307    while (in >> da >> dat ){
308      if(count==0) dac_min  = da;
309      if(count==1) step_dac = da - dac_min;
310      dac_max=da;
311      count++;
312    }     
313    in.close();
314    in.clear();
315
316    /*   
317    if(ch==43 || ch==47){//for T1 ASIC_C
318      cerr << "CHECK " << ch << " " << fit_min_scurve << endl;
319      Int_t fit_min_scurve=140;
320    } else {
321      Int_t fit_min_scurve=kFitMinS;
322    }
323    */
324
325    // correct the scale for DAC range
326    if(kXmin<dac_min)kXmin=dac_min;
327    const Int_t kNdata    = count;
328    count = (fit_max_scurve-fit_min_scurve)/step_dac;
329    const Int_t kNdataFit = count;
330    if(ch==0){
331      cerr << "kNdata, dac_min, dac_max, step_dac: " 
332           << kNdata << " " << dac_min << " " << dac_max << " " 
333           << step_dac << endl;
334    }
335   
336    Double_t dac_min_ped, dac_max_ped, step_dac_ped;
337    count = 0;
338    while (in_ped >> da >> dat ){
339      if(count==0) dac_min_ped  = da;
340      if(count==1) step_dac_ped = da - dac_min_ped;
341      dac_max_ped=da;
342      count++;
343    }
344     
345    in_ped.close();
346    in_ped.clear();
347   
348
349    const Int_t kNdataPed = count;
350    if(ch==0){
351      cerr << "kNdataPed, dac_min_ped, dac_max_ped, step_dac_ped: " 
352           << kNdataPed << " " << dac_min_ped << " " << dac_max_ped
353           << " " << step_dac_ped << endl;
354    }
355   
356    // book histograms
357    // SDC: 27/09/2013 hist_scurve is a pointer on an array of THD, thus the syntax is correct (no new operator)
358   
359
360    hist_scurve[ch]      = TH1D(name_hist[ch],name_hist[ch],kNdata-1,
361                                dac_min,dac_max);
362    hist_scurve_ped[ch]  = TH1D(name_hist_ped[ch],name_hist_ped[ch],
363                                kNdataPed-1,dac_min_ped,dac_max_ped);
364    hist_diff[ch]        = TH1D(name_hist_diff[ch],name_hist_diff[ch],
365                                kNdata-2,dac_min,dac_max);
366    hist_diff_fit[ch]    = TH1D(name_hist_diff_fit[ch],name_hist_diff_fit[ch],
367                                kNdataFit,fit_min_scurve,fit_max_scurve);
368
369   
370
371
372
373    // read the data for that channel for normal data
374    in.open(input[ch].Data());
375    Double_t       data[kNdata], dac[kNdata], dac_fit[kNdataFit], data_fit[kNdataFit];
376    while (in >> da >> dat ){
377      hist_scurve[ch].Fill(da,dat); // fill the histograms of the s-curve
378    }
379    in.close();
380    in.clear();
381
382    // read the data for pedestal data
383    in_ped.open(input_ped[ch].Data());
384    while (in_ped >> da >> dat ){
385      hist_scurve_ped[ch].Fill(da,dat); // fill the histogram for pedestal data
386    }
387    in_ped.close();
388    in_ped.clear();
389
390    //dac_ped[ch]  = hist_scurve_ped[ch].GetMaximumBin()+dac_min_ped; //absolute value
391    dac_ped[ch]  = hist_scurve_ped[ch].GetBinCenter(hist_scurve_ped[ch].GetMaximumBin()); //absolute value
392    dac_ped_sum += dac_ped[ch]; //absolute value
393    //cerr << "dac_ped[" << ch << "] dac_ped_sum " << dac_ped[ch]
394    //<< " " << dac_ped_sum << endl;
395   
396
397    hist_scurve[ch].Smooth(3); // Smooth the S curve
398    Int_t binx=0;
399    //hist_scurve[ch].GetBinWithContent(kHalfMax,binx,kXmin-dac_min+1,
400    //                                  dac_max-dac_min+1,kHalfMax/2.);
401
402    // ??????
403    hist_scurve[ch].GetBinWithContent(kHalfMax,binx,kXmin-dac_min+1,
404                                      dac_max-dac_min+1,2000);
405    dac_half[ch] = hist_scurve[ch].GetBinCenter(binx); //absolute value
406    if(dac_half[ch]<kXmin)dac_half[ch]=-1;
407   
408    // put the histograms of the smoothed S-Curves into two arrays (dac, data)
409    //-------------------------------------------------------------------------
410    Int_t i=0;
411    for(i=0;i<kNdata;i++){
412      dac[i]  = hist_scurve[ch].GetBinCenter(i+1);
413      data[i] = hist_scurve[ch].GetBinContent(i+1);
414    }
415   
416    /*** compute derive diff on S-Curve ***/
417    Double_t diff[kNdata-1], dac_diff[kNdata-1];
418    Int_t trigger_diff=0;
419    for(i=0;i<kNdata-1;i++){
420      diff[i]     = (data[i]-data[i+1])/step_dac;
421      dac_diff[i] = dac[i]+step_dac/2;
422      hist_diff[ch].Fill(dac_diff[i],diff[i]); // fills differentiated histograms of smoothed s-curves which is the single photoelectron spectrum
423      if(dac_diff[i]>=fit_min){
424        diff_sum += diff[i]*dac_diff[i];
425        //cerr << "diff[" << i << "], dac_diff[" << i << "] "
426        //<< diff[i] << " " << dac_diff[i] << endl;
427        trigger_diff+=diff[i];
428      }
429    }
430    //mean_diff[ch] = diff_sum/trigger_diff;
431    hist_diff[ch].GetXaxis()->SetRangeUser(fit_min,fit_max);
432    mean_diff[ch] = hist_diff[ch].GetMean();
433    //cerr << "trigger_diff, mean_diff[" << ch << "] "
434    //<< trigger_diff << " " << mean_diff[ch] << endl;
435   
436   
437    /******************* GRAPHICS *****************/
438
439
440    // Now start the analysis which consist in finding the peak in the differentiated spectrum
441    c1->cd(ch+1);   
442    /*** fit scurves  ***/
443    cerr << "ch " << ch;
444
445    //    SDC 24/09/2013 :: Need to declare outside this variable in a compiled program
446    TF1 *fit_pol ;
447
448
449    // Fit a polynomial curve in a given range on the s-curve directly. This is a kind of DEBUG procedure.
450
451    //if(ch==43 || ch==47){ // SDC: 25/09/2013 Why a special channel : the fit pol4 is performed for another DAC range
452 /*   if(ch==170){
453      fit_pol      = new TF1("fit_pol","pol4",150,fit_max_scurve);
454      hist_scurve[ch].Fit(fit_pol,"","",150,fit_max_scurve);
455    }else{ */
456      fit_pol      = new TF1("fit_pol","pol4",fit_min_scurve,fit_max_scurve);
457      hist_scurve[ch].Fit(fit_pol,"","",fit_min_scurve,fit_max_scurve);
458   // }
459
460   
461
462    Double_t fit_par_pol4[5]; // container for fit parameter results
463    fit_pol->GetParameters(fit_par_pol4);
464    sfit_chiS[ch] =  fit_pol->GetChisquare(); // chi squared
465    TF1 *func               = hist_scurve[ch].GetFunction("fit_pol");
466    Double_t data_diff_fit;
467    for(i=0;i<kNdataFit;i++){
468      //if(ch==43 || ch==47){
469      if(ch==170){
470        dac_fit[i]  = hist_scurve[ch].GetBinCenter(i)+150-dac_min;
471        data_fit[i] = func->Eval(hist_scurve[ch].GetBinCenter(i)+150-dac_min); // get the value of the fitted function
472      }else{
473        dac_fit[i]  = hist_scurve[ch].GetBinCenter(i)+fit_min_scurve-dac_min;
474        data_fit[i] = func->Eval(hist_scurve[ch].GetBinCenter(i)+fit_min_scurve-dac_min); // get the value of the fitted function
475        //cerr << "dac_fit[" << i << "], data_fit[" << i << "] "
476        //<< dac_fit[i] << " " << data_fit[i] << endl;
477      }
478    }
479
480    // derive the fitted function on the S-curve to obtain the SPE
481    Double_t dac_diff_fit;
482    for(i=0;i<kNdataFit-1;i++){
483      data_diff_fit = (data_fit[i]-data_fit[i+1])/step_dac;
484      hist_diff_fit[ch].Fill(dac_fit[i],data_diff_fit); // Fill differentiated fitted S-curve
485    }
486
487    TLine *lhalf_max   = new TLine(dac[0],kHalfMax,dac[kNdata-1],kHalfMax);
488    TLine *ldac_half   = new TLine(dac_half[ch],0,dac_half[ch],
489                                   (Double_t)kDrawMax);
490    hist_scurve[ch].SetMaximum(kDrawMax);
491    hist_scurve[ch].GetXaxis()->SetRangeUser(kDrawDacMin,kDrawDacMax);
492    hist_scurve[ch].Draw();
493    lhalf_max->Draw("same"); 
494    ldac_half->Draw("same");
495    if(dac_half[ch]>0){
496      dac_half_sum += dac_half[ch]-dac_ped[ch]; //relative absolute value
497      trigger++;
498    }
499
500    /*** Differential plot from scurve fitting function  ***/
501    c5->cd(ch+1);
502    hist_diff_fit[ch].GetXaxis()->SetRangeUser(fit_min_scurve,fit_max_scurve-2*step_dac);
503    Double_t dac_sfit_max=hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
504    //if(dac_sfit_max<=fit_min_scurve+4*step_dac){
505    if(dac_sfit_max<=fit_min_scurve+2*step_dac){
506      hist_diff_fit[ch].GetXaxis()->SetRangeUser(fit_min_scurve+kFitSadj,fit_max_scurve-2*step_dac);
507      dac_sfit[ch]  =  hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
508      //if(dac_sfit[ch]<=fit_min_scurve+kFitSadj+4*step_dac)dac_sfit[ch]=-1;
509      if(dac_sfit[ch]<fit_min_scurve+kFitSadj+step_dac){
510        dac_sfit[ch]=-1;
511        //dac_sfit[ch]=dac_half[ch];
512        flag_fit[ch] = -1;
513        cerr << "flag_fit[" << ch << "] " << flag_fit[ch]  << endl;
514      }
515    }else {
516      dac_sfit[ch]  =  hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
517    }
518
519    if(dac_sfit[ch]>fit_min_scurve+5){
520      dac_sfit_sum += dac_sfit[ch]-dac_ped[ch];
521      trigger_sfit++;
522    }else{
523      //dac_sfit[ch] = fit_min_scurve+10;
524      dac_sfit[ch] = -1;
525    }
526    //cerr << "dac_sfit[" << ch << "] " << dac_sfit[ch] << endl;
527    TLine *ldac_sfit      = new TLine(dac_sfit[ch],0,dac_sfit[ch],kDrawMaxFit);
528    TLine *ldac_sfit_copy = new TLine(dac_sfit[ch],0,dac_sfit[ch],kDrawMax);
529    ldac_sfit->SetLineColor(kMagenta);
530    ldac_sfit_copy->SetLineColor(kMagenta);
531    hist_diff_fit[ch].SetMaximum(kDrawMaxFit);
532    hist_diff_fit[ch].SetMinimum(0);
533    hist_diff_fit[ch].Draw();
534    ldac_sfit->Draw("same");
535   
536    /**** differential plot ****/
537    c2->cd(ch+1);
538    //hist_diff[ch].Rebin(8);
539    hist_diff[ch].Rebin(3);
540    fit_sdiff     = new TF1("fit_sdiff","gaus",dac_min,dac_max);
541    //fit_sdiff->SetLineWidth(1);
542    //if(ch==8 || ch==18 || ch==44 || ch==56)
543    mean_sum += mean_diff[ch]-dac_ped[ch];
544    trigger_mean++;
545
546    // fit the derivated polynom fit
547    Double_t fit_par[3];
548    //hist_diff[ch].Fit(fit_sdiff,"RQ0","",fit_min,fit_max);
549    hist_diff[ch].Fit(fit_sdiff,"","",fit_min,fit_max);
550    hist_diff[ch].GetXaxis()->SetRangeUser(kDrawDacMin,kDrawDacMax);
551    hist_diff[ch].SetMinimum(0);
552    hist_diff[ch].SetMaximum(kDrawMaxDiff);
553    //hist_diff[ch].Rebin();
554    hist_diff[ch].Draw();
555    fit_sdiff->GetParameters(fit_par); // get the parameters for this fit
556    dac_spe[ch] = fit_par[1];
557    if(dac_spe[ch]>fit_min){
558      dac_spe_sum += dac_spe[ch]-dac_ped[ch];
559      //cerr << "dac_spe[" << ch << "] " << dac_spe[ch] << endl;
560      trigger_fit++;
561    }else{
562      dac_spe[ch] = fit_min+10;
563    }
564    //cerr << "dac_spe[" << ch << "] " << dac_spe[ch] << endl;
565    TLine *ldac_spe    = new TLine(dac_spe[ch],0,dac_spe[ch],
566                                 (Double_t)kDrawMaxDiff);
567    ldac_spe->SetLineColor(kRed);
568    TLine *lmean_diff  = new TLine(mean_diff[ch],0,mean_diff[ch],
569                                   (Double_t)kDrawMaxDiff);
570    lmean_diff->SetLineColor(kCyan);
571    TLine *lfit_min    = new TLine(fit_min,0,fit_min,kDrawMaxDiff);
572    lfit_min->SetLineColor(kGray);
573
574    ldac_spe->Draw();
575    lmean_diff->Draw("same");
576    lfit_min->Draw("same");
577   
578    c3->cd(1);
579    if(ch==0){
580      hist_scurve[ch].SetMaximum(kDrawMax);
581      hist_scurve[ch].Draw();
582    }else{
583    /*if(ch==8 || ch==18 || ch==44 || ch==56)*/
584      hist_scurve[ch].Draw("same");
585    }
586    if(ch==kDrawCh){
587      c3->cd(2);
588      hist_scurve[ch].SetMaximum(kDrawMax);
589      hist_scurve[kDrawCh].Draw();
590      lhalf_max->Draw("same"); 
591      ldac_half->Draw("same");
592      ldac_sfit_copy->Draw("same");
593      c3->cd(3);
594      hist_diff[kDrawCh].Draw();     
595      ldac_spe->Draw();
596      lmean_diff->Draw("same");
597      lfit_min->Draw("same");
598      c3->cd(4);
599      hist_diff_fit[kDrawCh].Draw();
600       ldac_sfit->Draw("same");
601      }
602    /*
603    if(ch<32){
604      c6->cd(ch/4+1);
605      hist_scurve[ch].SetMaximum(kDrawMax);
606      if(ch%4==0){
607        hist_scurve[ch].Draw();
608      }else{
609        hist_scurve[ch].Draw("same");
610      }
611      ldac_sfit_copy->Draw("same");
612    }else{
613      c7->cd(ch/4-7);
614      hist_scurve[ch].SetMaximum(kDrawMax);
615      if(ch%4==0){
616        hist_scurve[ch].Draw();
617      }else{
618        hist_scurve[ch].Draw("same");
619      }
620      ldac_sfit_copy->Draw("same");
621      }
622    */
623   
624    c4->cd();
625    if(ch==0){
626      hist_scurve_ped[ch].SetMaximum(kDrawMaxPed);
627      hist_scurve_ped[ch].Draw("l");
628    }else{
629      hist_scurve_ped[ch].Draw("lsame");
630    }
631   
632  }// end of second loop on all the channels
633
634  // SDC 24/09/2013 :: In a compiled program, I have to declare the following variables outside the parenthesis
635
636  Float_t dac_ped_ave  ; //absolute value
637  Float_t dac_half_ave  ;
638  Float_t dac_spe_ave   ;
639  Float_t dac_sfit_ave;
640  Float_t mean_diff_ave = 252.5; //dac_sfit of ch31 of ASIC_C with the 180 for lower gain pixels
641 
642  if(!checkAve){
643    dac_ped_ave   = dac_ped[kChRef]; //absolute value
644    dac_half_ave  = dac_half[kChRef];
645    dac_spe_ave   = dac_spe[kChRef];
646    dac_sfit_ave  = dac_sfit[kChRef];
647    //Float_t dac_sfit_ave  = 252.5; //dac_sfit of ch31 of ASIC_C with the 180 for lower gain pixels
648    mean_diff_ave = mean_diff[kChRef];
649  }else{
650    dac_ped_ave  = dac_ped_sum/(Float_t)kNch; //absolute value
651    dac_half_ave = dac_half_sum/(Float_t)trigger+dac_ped_ave;
652    dac_spe_ave  = dac_spe_sum/(Float_t)trigger_fit+dac_ped_ave;
653    mean_diff_ave = mean_sum/(Float_t)trigger_mean+dac_ped_ave; 
654    dac_sfit_ave = dac_sfit_sum/(Float_t)trigger_sfit+dac_ped_ave; 
655  }
656
657
658// SDC 24/09/2013 :: In a compiled program, I have to declare the following variables outside the parenthesis
659
660  Double_t dac_half_spe;
661
662  //trigger_mean=4;
663  if(dac_half_spe_fix!=-1) {
664    dac_half_spe = dac_half_spe_fix;
665  } else {
666    //Double_t dac_half_spe = dac_half_ave;
667    dac_half_spe = dac_spe_ave;
668  }
669
670  TLine *ldac_ped_ave   = new TLine(dac_ped_ave,0,dac_ped_ave,
671                                   (Double_t)kDrawMax);
672  TLine *ldac_ped_ave_copy = new TLine(dac_ped_ave,0,dac_ped_ave,
673                                   (Double_t)kDrawMaxDiff);
674  TLine *ldac_half_ave  = new TLine(dac_half_ave,0,dac_half_ave,
675                                   (Double_t)kDrawMax);
676  TLine *ldac_half_spe  = new TLine(dac_half_spe,0,dac_half_spe,
677                                   (Double_t)kDrawMax);
678  TLine *lmean_diff_ave = new TLine(mean_diff_ave,0,mean_diff_ave,
679                                   (Double_t)kDrawMax);
680  TLine *ldac_sfit_ave  = new TLine(dac_sfit_ave,0,dac_sfit_ave,
681                                   (Double_t)kDrawMax);
682  ldac_ped_ave->SetLineColor(kOrange);
683  ldac_half_ave->SetLineColor(kRed);
684  lmean_diff_ave->SetLineColor(kCyan);
685  //ldac_half_spe->SetLineColor(kGreen);
686  ldac_sfit_ave->SetLineColor(kMagenta); 
687
688  c3->cd(1);
689  ldac_ped_ave->Draw("same");
690  ldac_half_ave->Draw("same");
691  // lhalf_max->Draw("same");  SDC (24/09/2013) :: Not declared
692  ldac_half_spe->Draw("same");
693  lmean_diff_ave->Draw("same");
694  ldac_sfit_ave->Draw("same");
695
696  c3->cd(3);
697  ldac_ped_ave_copy->SetLineColor(kOrange);
698  ldac_ped_ave_copy->Draw("same");
699
700  ofstream out(kFnameOut.Data()); 
701
702  Int_t    gain_asic_diff, gain_asic_dacHalf, gain_asic_diff_mean, gain_asic_sfit;
703  out << "title  ch  g_asic_dacHalf  g_asic_diff  g_asic_diff_mean  g_asic_sfit  dac_half[ch]  dac_ped[ch]  dac_spe[ch]  dac_half_ave dac_half_spe dac_ped_ave  dac_sfit_ave" 
704       << endl;
705
706
707  // loop 3 on all channels to make final computations on the gain
708  for(ch=0;ch<kNch;ch++){
709    /*
710    if(ch==8 || ch==44 || ch==56 || ch==18){
711      gain_asic_diff_mean=(Int_t)((mean_diff_ave-dac_ped_ave)/
712                                  (mean_diff[ch]-dac_ped[ch])*gain_org[ch]);
713    }else{
714      gain_asic_diff_mean=0;
715    }
716    */
717    if(dac_half[ch]<0){
718      gain_asic_dacHalf   = 0;
719      gain_asic_diff      = 0;
720      gain_asic_diff_mean = 0;
721      gain_asic_sfit      = 0;
722    }else{
723      gain_asic_dacHalf  =(Int_t)((dac_half_ave-dac_ped_ave)/
724                                (dac_half[ch]-dac_ped[ch])*gain_org[ch]);
725      gain_asic_diff     =(Int_t)((dac_half_spe-dac_ped_ave)/
726                             (dac_spe[ch]-dac_ped[ch])*gain_org[ch]);
727      gain_asic_diff_mean=(Int_t)((mean_diff_ave-dac_ped_ave)/
728                                  (mean_diff[ch]-dac_ped[ch])*gain_org[ch]);
729      gain_asic_sfit     =(Int_t)((dac_sfit_ave-dac_ped_ave)/
730                             (dac_sfit[ch]-dac_ped[ch])*gain_org[ch]);
731      //cerr << ch << " " << gain_org[ch] << endl;
732    }
733
734   
735
736    out << "OUT_TABLE    "
737         << ch << "    "
738         << gain_asic_dacHalf   << "    "
739         << gain_asic_diff      << "    "
740         << gain_asic_diff_mean << "    "
741         << gain_asic_sfit      << "    "
742         << dac_half[ch]        << "    "
743         << dac_ped[ch]         << "    "
744         << (Int_t)dac_spe[ch]  << "    "
745         << dac_half_ave        << "    "
746         << dac_half_spe        << "    "
747         << dac_ped_ave         << "    "
748         << dac_sfit_ave        << "    "
749         << sfit_chiS[ch]       << "    "
750         << ch                  << "    "
751         << flag_fit[ch]       
752         << endl;
753      std::cout << "channel " << ch << " old gain " << gain_org[ch] << " new gain " << gain_asic_sfit << std::endl;
754  }
755
756
757
758  /*
759    for(ch=kNch-1;ch>=0;ch--){
760    cout << "OUT_TABLE_INV    "
761    << (Int_t)(dac_half_ave/(dac_half[ch]-dac_ped)*64)
762      << endl;
763      }
764  */
765 
766 
767}
768
769//--------------------------------------------------------------------------------------------------------
770
771//--------------------------------------------------------------------------------------------------------
772// Analysis Constructor
773//--------------------------------------------------------------------------------------------------------
774Analysis::Analysis():ch(0),dac_half_sum(0),dac_spe_sum(0),dac_sfit_sum(0),diff_sum(0),mean_sum(0),maxBinSum(0) 
775{
776 
777  cout << " analysis initialisation " << endl;
778
779 /*********************** SIGNAL RUN ********************/
780  input             = new TString[kNch];
781  input_ped         = new TString[kNch];
782
783  // Init sums
784  dac_half_sum      = 0;
785  dac_ped_sum       = 0;
786  dac_spe_sum       = 0;
787  dac_sfit_sum      = 0;
788  diff_sum          = 0;
789  mean_sum          = 0;
790  maxBinSum         = 0;
791
792
793}
794//--------------------------------------------------------------------------------------------------------
795
796
797//--------------------------------------------------------------------------------------------------------
798// Analysis destructor
799//--------------------------------------------------------------------------------------------------------
800Analysis::~Analysis()
801{
802 
803  if(input!=0) delete input;
804  if(input_ped!=0) delete input_ped;
805
806
807}
808//--------------------------------------------------------------------------------------------------------
809
810
811//--------------------------------------------------------------------------------------------------------
812// Main public function of Analysis
813//--------------------------------------------------------------------------------------------------------
814/*
815void Analysis::PlotScurve(
816                Int_t    kDrawCh=31,
817                TString  fname=kFname,
818                TString  fname_ped=kFnamePed, //ASIC_D ped_file
819                Double_t dac_half_spe_fix = -1,
820                TString  fname_gain="gain-org.txt",
821                Int_t    fit_min=kFitMin,
822                Int_t    fit_max=kFitMax,
823                Int_t    fit_min_scurve=kFitMinS,
824                Int_t    fit_max_scurve=kFitMaxS,
825                Bool_t   kBoard=0, //0:ASIC_C, 1:ASIC_D
826                Int_t    kChRef=kRefCh,//ASIC_C
827                Bool_t   checkAve=0 //0:use ref pix, 1:check average
828                )
829*/
830void Analysis:: PlotScurve(
831                Int_t    kDrawCh,
832                TString  fname,
833                TString  fname_ped, //ASIC_D ped_file
834                Double_t dac_half_spe_fix,
835                TString  fname_gain,
836                Int_t    fit_min,
837                Int_t    fit_max,
838                Int_t    fit_min_scurve,
839                Int_t    fit_max_scurve,
840                Bool_t   kBoard, //0:ASIC_C, 1:ASIC_D
841                Int_t    kChRef,//ASIC_C
842                Bool_t   checkAve //0:use ref pix, 1:check average
843                )
844
845
846
847{
848
849  // reset all variables
850  Init();
851
852  //OpenGainFile(fname_gain);
853
854  FillHistoName();
855
856  ReadGainFile(fname_gain,kBoard);
857
858// Open the pedestal file and the S-Curve files for each channel and test if the file can be open
859
860  trigger=0;
861  trigger_fit=0;
862  trigger_mean=0;
863  trigger_sfit=0;
864
865  // Big loop on all the channels : second loop
866  //--------------------------------------------
867  // build the input data file name for each channel fname-chXX.dat
868  for(ch=0;ch<kNch;ch++)
869    {
870      ProcessSingleChannel(kDrawCh,fname,fname_ped,dac_half_spe_fix,fname_gain,
871                           fit_min,fit_max,fit_min_scurve,fit_max_scurve,kBoard,kChRef);
872      }
873
874
875 
876  ComputeDACAverage(dac_half_spe_fix ,kChRef,checkAve);
877  OutputGain(kFnameOut);
878
879
880
881}
882//--------------------------------------------------------------------------------------------------------
883
884
885//--------------------------------------------------------------------------------------------------------
886// Initialize the program by reseting counters or variables and create objects
887//--------------------------------------------------------------------------------------------------------
888void Analysis::Init()
889{
890
891  if(input!=0) delete input;
892  if(input_ped!=0) delete input_ped;
893
894  // recreate the name
895  input             = new TString[kNch];
896  input_ped         = new TString[kNch];
897
898  // Init sums
899  dac_half_sum      = 0;
900  dac_ped_sum       = 0;
901  dac_spe_sum       = 0;
902  dac_sfit_sum      = 0;
903  diff_sum          = 0;
904  mean_sum          = 0;
905  maxBinSum         = 0;
906
907
908  // create canvas
909
910  c1                = new TCanvas(kCanvasName1,kCanvasTitle1,200,20,1100,600); //smoothed S-curves
911  c1->Divide(11,6);
912  c2                = new TCanvas(kCanvasName2,kCanvasTitle2,300,50,1100,600); //differentiated histograms of smoothed s-curves
913  c2->Divide(11,6);
914  c5                = new TCanvas(kCanvasName5,kCanvasTitle5,400,70,1100,600);
915  c5->Divide(11,6);
916  c3                = new TCanvas(kCanvasName3,kCanvasTitle3,500,100,1200,300);
917  c3->Divide(4,1);
918  c4                = new TCanvas(kCanvasName4,kCanvasTitle4,900,400,300,300);
919
920
921  // create histos and function
922  hist_scurve       = new TH1D[kNch]; // smoothed S-curves
923  hist_scurve_ped   = new TH1D[kNch]; // S-curves for pedestal runs
924  hist_diff         = new TH1D[kNch]; // differentiated histograms of smoothed s-curves
925  hist_diff_fit     = new TH1D[kNch]; // differentiated histograms of fitted s-curves
926  fit_sdiff         = new TF1[kNch];  // function fitted on differentiated histograms of fitted s-curves
927
928
929 for(ch=0;ch<kNch;ch++){
930    dac_half[ch]=0;
931    dac_ped[ch]=0;
932    dac_spe[ch]=0;
933    dac_sfit[ch]=0;
934    gain_org[ch]=0;
935    mean_diff[ch]=0;
936    sfit_chiS[ch]=0;
937    flag_fit[ch]=0;
938 }
939
940
941 
942}
943//--------------------------------------------------------------------------------------------------------
944
945//--------------------------------------------------------------------------------------------------------
946// Initialize the program by reseting counters or variables and create objects
947//--------------------------------------------------------------------------------------------------------
948void Analysis::OpenGainFile(const TString fname_gain)
949{
950  TString input_gain(fname_gain);
951  ifstream in_gain(input_gain.Data());
952  if(!in_gain.good()){
953    cerr << "File " << input_gain << " open failed!" << endl;
954    exit(-1);
955  }
956  in_gain.close();
957  in_gain.clear();
958
959}
960//--------------------------------------------------------------------------------------------------------
961
962//--------------------------------------------------------------------------------------------------------
963// Fill Histogram name
964//--------------------------------------------------------------------------------------------------------
965void Analysis::FillHistoName()
966{
967 
968// first loop on channels to fill histonames
969  //----------------------
970  for(ch=0;ch<kNch;ch++){
971   
972    name_hist[ch]           ="hist";
973    name_hist[ch]          +=ch;
974    name_hist_ped[ch]       ="hist_ped";
975    name_hist_ped[ch]      +=ch;
976    name_hist_diff[ch]      ="hist_diff";
977    name_hist_diff[ch]     +=ch;
978    name_hist_diff_fit[ch]  ="hist_diff_fit";
979    name_hist_diff_fit[ch] +=ch;
980    name_fit_diff[ch]       ="fit_diff";
981    name_fit_diff[ch]      +=ch;
982  }
983
984}
985//--------------------------------------------------------------------------------------------------------
986
987
988//--------------------------------------------------------------------------------------------------------
989// Read the gain
990// input : the gain filename
991// output the gain_org[ch] array
992//--------------------------------------------------------------------------------------------------------
993
994void Analysis::ReadGainFile(const TString fname_gain,Bool_t kBoard)
995{
996  TString input_gain(fname_gain);
997  ifstream in_gain(input_gain.Data());
998  if(!in_gain.good()){
999    cerr << "File " << input_gain << " open failed!" << endl;
1000    exit(-1);
1001  }
1002 // read gain
1003  Double_t da;
1004  Int_t count=0;
1005  ch=0;
1006  while(in_gain >> da){
1007    if(!kBoard && (count>=128 && count<192)){
1008      gain_org[ch]=da;
1009      cerr << "!kBoard, count, gain_org[" << ch << "] "
1010           << !kBoard << " " << count << " " << gain_org[ch] << endl;
1011      ch++;
1012    }
1013    if(kBoard && (count>=192 && count<256)){
1014      gain_org[ch]=da;
1015      cerr << "kBoard, count, gain_org[" << ch << "] "
1016           << kBoard << " " << count << " " << gain_org[ch] << endl;
1017      ch++;
1018    }
1019    count++;
1020  }// end if first loop on all channels     
1021  in_gain.close();
1022  in_gain.clear();
1023 
1024
1025}
1026//--------------------------------------------------------------------------------------------------------
1027
1028
1029
1030//--------------------------------------------------------------------------------------------------------
1031// ProcessSingleChannel
1032//--------------------------------------------------------------------------------------------------------
1033void Analysis::ProcessSingleChannel(
1034                Int_t    kDrawCh,
1035                TString  fname,
1036                TString  fname_ped, //ASIC_D ped_file
1037                Double_t dac_half_spe_fix,
1038                TString  fname_gain,
1039                Int_t    fit_min,
1040                Int_t    fit_max,
1041                Int_t    fit_min_scurve,
1042                Int_t    fit_max_scurve,
1043                Bool_t   kBoard, //0:ASIC_C, 1:ASIC_D
1044                Int_t    kChRef//ASIC_C
1045                //      Bool_t   checkAve //0:use ref pix, 1:check average)
1046                                    )
1047{
1048    input[ch] += fname;
1049    input[ch] += "-ch";
1050    input[ch] += ch;
1051    input[ch] += ".dat";
1052    ifstream in(input[ch].Data());
1053    if(!in.good()){
1054      cerr << "File " << input[ch] << " open failed!" << endl;
1055      return;
1056    }
1057
1058    // build the input data file name for each channel fname_ped-chXX.dat
1059    input_ped[ch] += fname_ped;
1060    input_ped[ch] += "-ch";
1061    input_ped[ch] += ch;
1062    input_ped[ch] += ".dat";
1063    ifstream in_ped(input_ped[ch].Data());
1064    if(!in_ped.good()){
1065      cerr << "File " << input_ped[ch] << " open failed!" << endl;
1066      return;
1067    }   
1068
1069
1070   
1071    //10DAC~10mV
1072    // determine the DAC range in dac_min, dac_max, step_dac from file input_ped
1073    // and count the number of events
1074    Double_t da, dat, dac_min, dac_max, step_dac;
1075    Int_t count = 0;
1076    while (in >> da >> dat ){
1077      if(count==0) dac_min  = da;
1078      if(count==1) step_dac = da - dac_min;
1079      dac_max=da;
1080      count++;
1081    }     
1082    in.close();
1083    in.clear();
1084
1085    /*   
1086    if(ch==43 || ch==47){//for T1 ASIC_C
1087      cerr << "CHECK " << ch << " " << fit_min_scurve << endl;
1088      Int_t fit_min_scurve=140;
1089    } else {
1090      Int_t fit_min_scurve=kFitMinS;
1091    }
1092    */
1093
1094    // correct the scale for DAC range
1095    if(kXmin<dac_min)kXmin=dac_min;
1096    const Int_t kNdata    = count;
1097    count = (fit_max_scurve-fit_min_scurve)/step_dac;
1098    const Int_t kNdataFit = count;
1099    if(ch==0){
1100      cerr << "kNdata, dac_min, dac_max, step_dac: " 
1101           << kNdata << " " << dac_min << " " << dac_max << " " 
1102           << step_dac << endl;
1103    }
1104   
1105    Double_t dac_min_ped, dac_max_ped, step_dac_ped;
1106    count = 0;
1107    while (in_ped >> da >> dat ){
1108      if(count==0) dac_min_ped  = da;
1109      if(count==1) step_dac_ped = da - dac_min_ped;
1110      dac_max_ped=da;
1111      count++;
1112    }
1113     
1114    in_ped.close();
1115    in_ped.clear();
1116   
1117
1118    const Int_t kNdataPed = count;
1119    if(ch==0){
1120      cerr << "kNdataPed, dac_min_ped, dac_max_ped, step_dac_ped: " 
1121           << kNdataPed << " " << dac_min_ped << " " << dac_max_ped
1122           << " " << step_dac_ped << endl;
1123    }
1124   
1125    // book histograms
1126    // SDC: 27/09/2013 hist_scurve is a pointer on an array of THD, thus the syntax is correct (no new operator)
1127   
1128
1129    hist_scurve[ch]      = TH1D(name_hist[ch],name_hist[ch],kNdata-1,
1130                                dac_min,dac_max);
1131    hist_scurve_ped[ch]  = TH1D(name_hist_ped[ch],name_hist_ped[ch],
1132                                kNdataPed-1,dac_min_ped,dac_max_ped);
1133    hist_diff[ch]        = TH1D(name_hist_diff[ch],name_hist_diff[ch],
1134                                kNdata-2,dac_min,dac_max);
1135    hist_diff_fit[ch]    = TH1D(name_hist_diff_fit[ch],name_hist_diff_fit[ch],
1136                                kNdataFit,fit_min_scurve,fit_max_scurve);
1137
1138   
1139
1140
1141
1142    // read the data for that channel for normal data
1143    in.open(input[ch].Data());
1144    Double_t       data[kNdata], dac[kNdata], dac_fit[kNdataFit], data_fit[kNdataFit];
1145    while (in >> da >> dat ){
1146      hist_scurve[ch].Fill(da,dat); // fill the histograms of the s-curve
1147    }
1148    in.close();
1149    in.clear();
1150
1151    // read the data for pedestal data
1152    in_ped.open(input_ped[ch].Data());
1153    while (in_ped >> da >> dat ){
1154      hist_scurve_ped[ch].Fill(da,dat); // fill the histogram for pedestal data
1155    }
1156    in_ped.close();
1157    in_ped.clear();
1158
1159    //dac_ped[ch]  = hist_scurve_ped[ch].GetMaximumBin()+dac_min_ped; //absolute value
1160    dac_ped[ch]  = hist_scurve_ped[ch].GetBinCenter(hist_scurve_ped[ch].GetMaximumBin()); //absolute value
1161    dac_ped_sum += dac_ped[ch]; //absolute value
1162    //cerr << "dac_ped[" << ch << "] dac_ped_sum " << dac_ped[ch]
1163    //<< " " << dac_ped_sum << endl;
1164   
1165
1166    hist_scurve[ch].Smooth(3); // Smooth the S curve
1167    Int_t binx=0;
1168    //hist_scurve[ch].GetBinWithContent(kHalfMax,binx,kXmin-dac_min+1,
1169    //                                  dac_max-dac_min+1,kHalfMax/2.);
1170
1171    // ??????
1172    hist_scurve[ch].GetBinWithContent(kHalfMax,binx,kXmin-dac_min+1,
1173                                      dac_max-dac_min+1,2000);
1174    dac_half[ch] = hist_scurve[ch].GetBinCenter(binx); //absolute value
1175    if(dac_half[ch]<kXmin)dac_half[ch]=-1;
1176   
1177    // put the histograms of the smoothed S-Curves into two arrays (dac, data)
1178    //-------------------------------------------------------------------------
1179    Int_t i=0;
1180    for(i=0;i<kNdata;i++){
1181      dac[i]  = hist_scurve[ch].GetBinCenter(i+1);
1182      data[i] = hist_scurve[ch].GetBinContent(i+1);
1183    }
1184   
1185    /*** compute derive diff on S-Curve ***/
1186    Double_t diff[kNdata-1], dac_diff[kNdata-1];
1187    Int_t trigger_diff=0;
1188    for(i=0;i<kNdata-1;i++){
1189      diff[i]     = (data[i]-data[i+1])/step_dac;
1190      dac_diff[i] = dac[i]+step_dac/2;
1191      hist_diff[ch].Fill(dac_diff[i],diff[i]); // fills differentiated histograms of smoothed s-curves which is the single photoelectron spectrum
1192      if(dac_diff[i]>=fit_min){
1193        diff_sum += diff[i]*dac_diff[i];
1194        //cerr << "diff[" << i << "], dac_diff[" << i << "] "
1195        //<< diff[i] << " " << dac_diff[i] << endl;
1196        trigger_diff+=diff[i];
1197      }
1198    }
1199    //mean_diff[ch] = diff_sum/trigger_diff;
1200    hist_diff[ch].GetXaxis()->SetRangeUser(fit_min,fit_max);
1201    mean_diff[ch] = hist_diff[ch].GetMean();
1202    //cerr << "trigger_diff, mean_diff[" << ch << "] "
1203    //<< trigger_diff << " " << mean_diff[ch] << endl;
1204   
1205   
1206    /******************* GRAPHICS *****************/
1207
1208
1209    // Now start the analysis which consist in finding the peak in the differentiated spectrum
1210    c1->cd(ch+1);   
1211    /*** fit scurves  ***/
1212    cerr << "ch " << ch;
1213
1214    //    SDC 24/09/2013 :: Need to declare outside this variable in a compiled program
1215    TF1 *fit_pol ;
1216
1217
1218    // Fit a polynomial curve in a given range on the s-curve directly. This is a kind of DEBUG procedure.
1219
1220    //if(ch==43 || ch==47){ // SDC: 25/09/2013 Why a special channel : the fit pol4 is performed for another DAC range
1221 /*   if(ch==170){
1222      fit_pol      = new TF1("fit_pol","pol4",150,fit_max_scurve);
1223      hist_scurve[ch].Fit(fit_pol,"","",150,fit_max_scurve);
1224    }else{ */
1225      fit_pol      = new TF1("fit_pol","pol4",fit_min_scurve,fit_max_scurve);
1226      hist_scurve[ch].Fit(fit_pol,"","",fit_min_scurve,fit_max_scurve);
1227   // }
1228
1229   
1230
1231    Double_t fit_par_pol4[5]; // container for fit parameter results
1232    fit_pol->GetParameters(fit_par_pol4);
1233    sfit_chiS[ch] =  fit_pol->GetChisquare(); // chi squared
1234    TF1 *func               = hist_scurve[ch].GetFunction("fit_pol");
1235    Double_t data_diff_fit;
1236    for(i=0;i<kNdataFit;i++){
1237      //if(ch==43 || ch==47){
1238      if(ch==170){
1239        dac_fit[i]  = hist_scurve[ch].GetBinCenter(i)+150-dac_min;
1240        data_fit[i] = func->Eval(hist_scurve[ch].GetBinCenter(i)+150-dac_min); // get the value of the fitted function
1241      }else{
1242        dac_fit[i]  = hist_scurve[ch].GetBinCenter(i)+fit_min_scurve-dac_min;
1243        data_fit[i] = func->Eval(hist_scurve[ch].GetBinCenter(i)+fit_min_scurve-dac_min); // get the value of the fitted function
1244        //cerr << "dac_fit[" << i << "], data_fit[" << i << "] "
1245        //<< dac_fit[i] << " " << data_fit[i] << endl;
1246      }
1247    }
1248
1249    // derive the fitted function on the S-curve to obtain the SPE
1250    Double_t dac_diff_fit;
1251    for(i=0;i<kNdataFit-1;i++){
1252      data_diff_fit = (data_fit[i]-data_fit[i+1])/step_dac;
1253      hist_diff_fit[ch].Fill(dac_fit[i],data_diff_fit); // Fill differentiated fitted S-curve
1254    }
1255
1256    TLine *lhalf_max   = new TLine(dac[0],kHalfMax,dac[kNdata-1],kHalfMax);
1257    TLine *ldac_half   = new TLine(dac_half[ch],0,dac_half[ch],
1258                                   (Double_t)kDrawMax);
1259    hist_scurve[ch].SetMaximum(kDrawMax);
1260    hist_scurve[ch].GetXaxis()->SetRangeUser(kDrawDacMin,kDrawDacMax);
1261    hist_scurve[ch].Draw();
1262    lhalf_max->Draw("same"); 
1263    ldac_half->Draw("same");
1264    if(dac_half[ch]>0){
1265      dac_half_sum += dac_half[ch]-dac_ped[ch]; //relative absolute value
1266      trigger++;
1267    }
1268
1269    /*** Differential plot from scurve fitting function  ***/
1270    c5->cd(ch+1);
1271    hist_diff_fit[ch].GetXaxis()->SetRangeUser(fit_min_scurve,fit_max_scurve-2*step_dac);
1272    Double_t dac_sfit_max=hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
1273    //if(dac_sfit_max<=fit_min_scurve+4*step_dac){
1274    if(dac_sfit_max<=fit_min_scurve+2*step_dac){
1275      hist_diff_fit[ch].GetXaxis()->SetRangeUser(fit_min_scurve+kFitSadj,fit_max_scurve-2*step_dac);
1276      dac_sfit[ch]  =  hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
1277      //if(dac_sfit[ch]<=fit_min_scurve+kFitSadj+4*step_dac)dac_sfit[ch]=-1;
1278      if(dac_sfit[ch]<fit_min_scurve+kFitSadj+step_dac){
1279        dac_sfit[ch]=-1;
1280        //dac_sfit[ch]=dac_half[ch];
1281        flag_fit[ch] = -1;
1282        cerr << "flag_fit[" << ch << "] " << flag_fit[ch]  << endl;
1283      }
1284    }else {
1285      dac_sfit[ch]  =  hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
1286    }
1287
1288    if(dac_sfit[ch]>fit_min_scurve+5){
1289      dac_sfit_sum += dac_sfit[ch]-dac_ped[ch];
1290      trigger_sfit++;
1291    }else{
1292      //dac_sfit[ch] = fit_min_scurve+10;
1293      dac_sfit[ch] = -1;
1294    }
1295    //cerr << "dac_sfit[" << ch << "] " << dac_sfit[ch] << endl;
1296    TLine *ldac_sfit      = new TLine(dac_sfit[ch],0,dac_sfit[ch],kDrawMaxFit);
1297    TLine *ldac_sfit_copy = new TLine(dac_sfit[ch],0,dac_sfit[ch],kDrawMax);
1298    ldac_sfit->SetLineColor(kMagenta);
1299    ldac_sfit_copy->SetLineColor(kMagenta);
1300    hist_diff_fit[ch].SetMaximum(kDrawMaxFit);
1301    hist_diff_fit[ch].SetMinimum(0);
1302    hist_diff_fit[ch].Draw();
1303    ldac_sfit->Draw("same");
1304   
1305    /**** differential plot ****/
1306    c2->cd(ch+1);
1307    //hist_diff[ch].Rebin(8);
1308    hist_diff[ch].Rebin(3);
1309    fit_sdiff     = new TF1("fit_sdiff","gaus",dac_min,dac_max);
1310    //fit_sdiff->SetLineWidth(1);
1311    //if(ch==8 || ch==18 || ch==44 || ch==56)
1312    mean_sum += mean_diff[ch]-dac_ped[ch];
1313    trigger_mean++;
1314
1315    // fit the derivated polynom fit
1316    Double_t fit_par[3];
1317    //hist_diff[ch].Fit(fit_sdiff,"RQ0","",fit_min,fit_max);
1318    hist_diff[ch].Fit(fit_sdiff,"","",fit_min,fit_max);
1319    hist_diff[ch].GetXaxis()->SetRangeUser(kDrawDacMin,kDrawDacMax);
1320    hist_diff[ch].SetMinimum(0);
1321    hist_diff[ch].SetMaximum(kDrawMaxDiff);
1322    //hist_diff[ch].Rebin();
1323    hist_diff[ch].Draw();
1324    fit_sdiff->GetParameters(fit_par); // get the parameters for this fit
1325    dac_spe[ch] = fit_par[1];
1326    if(dac_spe[ch]>fit_min){
1327      dac_spe_sum += dac_spe[ch]-dac_ped[ch];
1328      //cerr << "dac_spe[" << ch << "] " << dac_spe[ch] << endl;
1329      trigger_fit++;
1330    }else{
1331      dac_spe[ch] = fit_min+10;
1332    }
1333    //cerr << "dac_spe[" << ch << "] " << dac_spe[ch] << endl;
1334    TLine *ldac_spe    = new TLine(dac_spe[ch],0,dac_spe[ch],
1335                                 (Double_t)kDrawMaxDiff);
1336    ldac_spe->SetLineColor(kRed);
1337    TLine *lmean_diff  = new TLine(mean_diff[ch],0,mean_diff[ch],
1338                                   (Double_t)kDrawMaxDiff);
1339    lmean_diff->SetLineColor(kCyan);
1340    TLine *lfit_min    = new TLine(fit_min,0,fit_min,kDrawMaxDiff);
1341    lfit_min->SetLineColor(kGray);
1342
1343    ldac_spe->Draw();
1344    lmean_diff->Draw("same");
1345    lfit_min->Draw("same");
1346   
1347    c3->cd(1);
1348    if(ch==0){
1349      hist_scurve[ch].SetMaximum(kDrawMax);
1350      hist_scurve[ch].Draw();
1351    }else{
1352    /*if(ch==8 || ch==18 || ch==44 || ch==56)*/
1353      hist_scurve[ch].Draw("same");
1354    }
1355    if(ch==kDrawCh){
1356      c3->cd(2);
1357      hist_scurve[ch].SetMaximum(kDrawMax);
1358      hist_scurve[kDrawCh].Draw();
1359      lhalf_max->Draw("same"); 
1360      ldac_half->Draw("same");
1361      ldac_sfit_copy->Draw("same");
1362      c3->cd(3);
1363      hist_diff[kDrawCh].Draw();     
1364      ldac_spe->Draw();
1365      lmean_diff->Draw("same");
1366      lfit_min->Draw("same");
1367      c3->cd(4);
1368      hist_diff_fit[kDrawCh].Draw();
1369       ldac_sfit->Draw("same");
1370      }
1371    /*
1372    if(ch<32){
1373      c6->cd(ch/4+1);
1374      hist_scurve[ch].SetMaximum(kDrawMax);
1375      if(ch%4==0){
1376        hist_scurve[ch].Draw();
1377      }else{
1378        hist_scurve[ch].Draw("same");
1379      }
1380      ldac_sfit_copy->Draw("same");
1381    }else{
1382      c7->cd(ch/4-7);
1383      hist_scurve[ch].SetMaximum(kDrawMax);
1384      if(ch%4==0){
1385        hist_scurve[ch].Draw();
1386      }else{
1387        hist_scurve[ch].Draw("same");
1388      }
1389      ldac_sfit_copy->Draw("same");
1390      }
1391    */
1392   
1393    c4->cd();
1394    if(ch==0){
1395      hist_scurve_ped[ch].SetMaximum(kDrawMaxPed);
1396      hist_scurve_ped[ch].Draw("l");
1397    }else{
1398      hist_scurve_ped[ch].Draw("lsame");
1399    }
1400   
1401  }// end of process single  channels
1402
1403//---------------------------------------------------------------------------------------------------
1404
1405
1406//---------------------------------------------------------------------------------------------------
1407
1408//---------------------------------------------------------------------------------------------------
1409void Analysis::ComputeDACAverage(Double_t dac_half_spe_fix ,Int_t    kChRef, Bool_t   checkAve )//0:use ref pix, 1:check average
1410{
1411
1412 // SDC 24/09/2013 :: In a compiled program, I have to declare the following variables outside the parenthesis
1413
1414 
1415  mean_diff_ave = 252.5; //dac_sfit of ch31 of ASIC_C with the 180 for lower gain pixels
1416 
1417  if(!checkAve){
1418    dac_ped_ave   = dac_ped[kChRef]; //absolute value
1419    dac_half_ave  = dac_half[kChRef];
1420    dac_spe_ave   = dac_spe[kChRef];
1421    dac_sfit_ave  = dac_sfit[kChRef];
1422    //Float_t dac_sfit_ave  = 252.5; //dac_sfit of ch31 of ASIC_C with the 180 for lower gain pixels
1423    mean_diff_ave = mean_diff[kChRef];
1424  }else{
1425    dac_ped_ave  = dac_ped_sum/(Float_t)kNch; //absolute value
1426    dac_half_ave = dac_half_sum/(Float_t)trigger+dac_ped_ave;
1427    dac_spe_ave  = dac_spe_sum/(Float_t)trigger_fit+dac_ped_ave;
1428    mean_diff_ave = mean_sum/(Float_t)trigger_mean+dac_ped_ave; 
1429    dac_sfit_ave = dac_sfit_sum/(Float_t)trigger_sfit+dac_ped_ave; 
1430  }
1431
1432
1433// SDC 24/09/2013 :: In a compiled program, I have to declare the following variables outside the parenthesis
1434
1435
1436  //trigger_mean=4;
1437  if(dac_half_spe_fix!=-1) {
1438    dac_half_spe = dac_half_spe_fix;
1439  } else {
1440    //Double_t dac_half_spe = dac_half_ave;
1441    dac_half_spe = dac_spe_ave;
1442  }
1443
1444  TLine *ldac_ped_ave   = new TLine(dac_ped_ave,0,dac_ped_ave,
1445                                   (Double_t)kDrawMax);
1446  TLine *ldac_ped_ave_copy = new TLine(dac_ped_ave,0,dac_ped_ave,
1447                                   (Double_t)kDrawMaxDiff);
1448  TLine *ldac_half_ave  = new TLine(dac_half_ave,0,dac_half_ave,
1449                                   (Double_t)kDrawMax);
1450  TLine *ldac_half_spe  = new TLine(dac_half_spe,0,dac_half_spe,
1451                                   (Double_t)kDrawMax);
1452  TLine *lmean_diff_ave = new TLine(mean_diff_ave,0,mean_diff_ave,
1453                                   (Double_t)kDrawMax);
1454  TLine *ldac_sfit_ave  = new TLine(dac_sfit_ave,0,dac_sfit_ave,
1455                                   (Double_t)kDrawMax);
1456  ldac_ped_ave->SetLineColor(kOrange);
1457  ldac_half_ave->SetLineColor(kRed);
1458  lmean_diff_ave->SetLineColor(kCyan);
1459  //ldac_half_spe->SetLineColor(kGreen);
1460  ldac_sfit_ave->SetLineColor(kMagenta); 
1461
1462  c3->cd(1);
1463  ldac_ped_ave->Draw("same");
1464  ldac_half_ave->Draw("same");
1465  // lhalf_max->Draw("same");  SDC (24/09/2013) :: Not declared
1466  ldac_half_spe->Draw("same");
1467  lmean_diff_ave->Draw("same");
1468  ldac_sfit_ave->Draw("same");
1469
1470  c3->cd(3);
1471  ldac_ped_ave_copy->SetLineColor(kOrange);
1472  ldac_ped_ave_copy->Draw("same");
1473
1474 
1475
1476}
1477
1478//---------------------------------------------------------------------------------------------------
1479
1480//---------------------------------------------------------------------------------------------------
1481void Analysis::OutputGain(const TString fname_out)
1482{
1483
1484 ofstream out(fname_out.Data()); 
1485
1486 Int_t    gain_asic_diff, gain_asic_dacHalf, gain_asic_diff_mean, gain_asic_sfit;
1487  out << "title  ch  g_asic_dacHalf  g_asic_diff  g_asic_diff_mean  g_asic_sfit  dac_half[ch]  dac_ped[ch]  dac_spe[ch]  dac_half_ave dac_half_spe dac_ped_ave  dac_sfit_ave" 
1488       << endl;
1489
1490
1491  // loop 3 on all channels to make final computations on the gain
1492  for(ch=0;ch<kNch;ch++){
1493    /*
1494    if(ch==8 || ch==44 || ch==56 || ch==18){
1495      gain_asic_diff_mean=(Int_t)((mean_diff_ave-dac_ped_ave)/
1496                                  (mean_diff[ch]-dac_ped[ch])*gain_org[ch]);
1497    }else{
1498      gain_asic_diff_mean=0;
1499    }
1500    */
1501    if(dac_half[ch]<0){
1502      gain_asic_dacHalf   = 0;
1503      gain_asic_diff      = 0;
1504      gain_asic_diff_mean = 0;
1505      gain_asic_sfit      = 0;
1506    }else{
1507      gain_asic_dacHalf  =(Int_t)((dac_half_ave-dac_ped_ave)/
1508                                (dac_half[ch]-dac_ped[ch])*gain_org[ch]);
1509      gain_asic_diff     =(Int_t)((dac_half_spe-dac_ped_ave)/
1510                             (dac_spe[ch]-dac_ped[ch])*gain_org[ch]);
1511      gain_asic_diff_mean=(Int_t)((mean_diff_ave-dac_ped_ave)/
1512                                  (mean_diff[ch]-dac_ped[ch])*gain_org[ch]);
1513      gain_asic_sfit     =(Int_t)((dac_sfit_ave-dac_ped_ave)/
1514                             (dac_sfit[ch]-dac_ped[ch])*gain_org[ch]);
1515      //cerr << ch << " " << gain_org[ch] << endl;
1516    }
1517
1518   
1519
1520    out << "OUT_TABLE    "
1521         << ch << "    "
1522         << gain_asic_dacHalf   << "    "
1523         << gain_asic_diff      << "    "
1524         << gain_asic_diff_mean << "    "
1525         << gain_asic_sfit      << "    "
1526         << dac_half[ch]        << "    "
1527         << dac_ped[ch]         << "    "
1528         << (Int_t)dac_spe[ch]  << "    "
1529         << dac_half_ave        << "    "
1530         << dac_half_spe        << "    "
1531         << dac_ped_ave         << "    "
1532         << dac_sfit_ave        << "    "
1533         << sfit_chiS[ch]       << "    "
1534         << ch                  << "    "
1535         << flag_fit[ch]       
1536         << endl;
1537      std::cout << "channel " << ch << " old gain " << gain_org[ch] << " new gain " << gain_asic_sfit << std::endl;
1538  }
1539  out.close();
1540
1541}
Note: See TracBrowser for help on using the repository browser.