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

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

class Analysis finished and its compile

File size: 51.4 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   
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//--------------------------------------------------------------------------------------------------------
814void Analysis::PlotScurve(
815                Int_t    kDrawCh=31,
816                TString  fname=kFname,
817                TString  fname_ped=kFnamePed, //ASIC_D ped_file
818                Double_t dac_half_spe_fix = -1,
819                TString  fname_gain="gain-org.txt",
820                Int_t    fit_min=kFitMin,
821                Int_t    fit_max=kFitMax,
822                Int_t    fit_min_scurve=kFitMinS,
823                Int_t    fit_max_scurve=kFitMaxS,
824                Bool_t   kBoard=0, //0:ASIC_C, 1:ASIC_D
825                Int_t    kChRef=kRefCh,//ASIC_C
826                Bool_t   checkAve=0 //0:use ref pix, 1:check average*/
827                )
828{
829
830  // reset all variables
831  Init();
832
833  //OpenGainFile(fname_gain);
834
835  FillHistoName();
836
837  ReadGainFile(fname_gain,kBoard);
838
839// Open the pedestal file and the S-Curve files for each channel and test if the file can be open
840
841  trigger=0;
842  trigger_fit=0;
843  trigger_mean=0;
844  trigger_sfit=0;
845
846  // Big loop on all the channels : second loop
847  //--------------------------------------------
848  // build the input data file name for each channel fname-chXX.dat
849  for(ch=0;ch<kNch;ch++)
850    {
851      ProcessSingleChannel(kDrawCh,fname,fname_ped,dac_half_spe_fix,fname_gain,
852                           fit_min,fit_max,fit_min_scurve,fit_max_scurve,kBoard,kChRef);
853      }
854
855
856 
857  ComputeDACAverage(dac_half_spe_fix ,kChRef,checkAve);
858  OutputGain(kFnameOut);
859
860
861
862}
863//--------------------------------------------------------------------------------------------------------
864
865
866//--------------------------------------------------------------------------------------------------------
867// Initialize the program by reseting counters or variables and create objects
868//--------------------------------------------------------------------------------------------------------
869void Analysis::Init()
870{
871
872  if(input!=0) delete input;
873  if(input_ped!=0) delete input_ped;
874
875  // recreate the name
876  input             = new TString[kNch];
877  input_ped         = new TString[kNch];
878
879  // Init sums
880  dac_half_sum      = 0;
881  dac_ped_sum       = 0;
882  dac_spe_sum       = 0;
883  dac_sfit_sum      = 0;
884  diff_sum          = 0;
885  mean_sum          = 0;
886  maxBinSum         = 0;
887
888
889  // create canvas
890
891  c1                = new TCanvas(kCanvasName1,kCanvasTitle1,200,20,1100,600); //smoothed S-curves
892  c1->Divide(11,6);
893  c2                = new TCanvas(kCanvasName2,kCanvasTitle2,300,50,1100,600); //differentiated histograms of smoothed s-curves
894  c2->Divide(11,6);
895  c5                = new TCanvas(kCanvasName5,kCanvasTitle5,400,70,1100,600);
896  c5->Divide(11,6);
897  c3                = new TCanvas(kCanvasName3,kCanvasTitle3,500,100,1200,300);
898  c3->Divide(4,1);
899  c4                = new TCanvas(kCanvasName4,kCanvasTitle4,900,400,300,300);
900
901
902  // create histos and function
903  hist_scurve       = new TH1D[kNch]; // smoothed S-curves
904  hist_scurve_ped   = new TH1D[kNch]; // S-curves for pedestal runs
905  hist_diff         = new TH1D[kNch]; // differentiated histograms of smoothed s-curves
906  hist_diff_fit     = new TH1D[kNch]; // differentiated histograms of fitted s-curves
907  fit_sdiff         = new TF1[kNch];  // function fitted on differentiated histograms of fitted s-curves
908
909
910 for(ch=0;ch<kNch;ch++){
911    dac_half[ch]=0;
912    dac_ped[ch]=0;
913    dac_spe[ch]=0;
914    dac_sfit[ch]=0;
915    gain_org[ch]=0;
916    mean_diff[ch]=0;
917    sfit_chiS[ch]=0;
918    flag_fit[ch]=0;
919 }
920
921
922 
923}
924//--------------------------------------------------------------------------------------------------------
925
926//--------------------------------------------------------------------------------------------------------
927// Initialize the program by reseting counters or variables and create objects
928//--------------------------------------------------------------------------------------------------------
929void Analysis::OpenGainFile(const TString fname_gain)
930{
931  TString input_gain(fname_gain);
932  ifstream in_gain(input_gain.Data());
933  if(!in_gain.good()){
934    cerr << "File " << input_gain << " open failed!" << endl;
935    exit(-1);
936  }
937  in_gain.close();
938  in_gain.clear();
939
940}
941//--------------------------------------------------------------------------------------------------------
942
943//--------------------------------------------------------------------------------------------------------
944// Fill Histogram name
945//--------------------------------------------------------------------------------------------------------
946void Analysis::FillHistoName()
947{
948 
949// first loop on channels to fill histonames
950  //----------------------
951  for(ch=0;ch<kNch;ch++){
952   
953    name_hist[ch]           ="hist";
954    name_hist[ch]          +=ch;
955    name_hist_ped[ch]       ="hist_ped";
956    name_hist_ped[ch]      +=ch;
957    name_hist_diff[ch]      ="hist_diff";
958    name_hist_diff[ch]     +=ch;
959    name_hist_diff_fit[ch]  ="hist_diff_fit";
960    name_hist_diff_fit[ch] +=ch;
961    name_fit_diff[ch]       ="fit_diff";
962    name_fit_diff[ch]      +=ch;
963  }
964
965}
966//--------------------------------------------------------------------------------------------------------
967
968
969//--------------------------------------------------------------------------------------------------------
970// Read the gain
971// input : the gain filename
972// output the gain_org[ch] array
973//--------------------------------------------------------------------------------------------------------
974
975void Analysis::ReadGainFile(const TString fname_gain,Bool_t kBoard)
976{
977  TString input_gain(fname_gain);
978  ifstream in_gain(input_gain.Data());
979  if(!in_gain.good()){
980    cerr << "File " << input_gain << " open failed!" << endl;
981    exit(-1);
982  }
983 // read gain
984  Double_t da;
985  Int_t count=0;
986  ch=0;
987  while(in_gain >> da){
988    if(!kBoard && (count>=128 && count<192)){
989      gain_org[ch]=da;
990      cerr << "!kBoard, count, gain_org[" << ch << "] "
991           << !kBoard << " " << count << " " << gain_org[ch] << endl;
992      ch++;
993    }
994    if(kBoard && (count>=192 && count<256)){
995      gain_org[ch]=da;
996      cerr << "kBoard, count, gain_org[" << ch << "] "
997           << kBoard << " " << count << " " << gain_org[ch] << endl;
998      ch++;
999    }
1000    count++;
1001  }// end if first loop on all channels     
1002  in_gain.close();
1003  in_gain.clear();
1004 
1005
1006}
1007//--------------------------------------------------------------------------------------------------------
1008
1009
1010
1011//--------------------------------------------------------------------------------------------------------
1012// ProcessSingleChannel
1013//--------------------------------------------------------------------------------------------------------
1014void Analysis::ProcessSingleChannel(
1015                Int_t    kDrawCh,
1016                TString  fname,
1017                TString  fname_ped, //ASIC_D ped_file
1018                Double_t dac_half_spe_fix,
1019                TString  fname_gain,
1020                Int_t    fit_min,
1021                Int_t    fit_max,
1022                Int_t    fit_min_scurve,
1023                Int_t    fit_max_scurve,
1024                Bool_t   kBoard, //0:ASIC_C, 1:ASIC_D
1025                Int_t    kChRef//ASIC_C
1026                //      Bool_t   checkAve //0:use ref pix, 1:check average)
1027                                    )
1028{
1029    input[ch] += fname;
1030    input[ch] += "-ch";
1031    input[ch] += ch;
1032    input[ch] += ".dat";
1033    ifstream in(input[ch].Data());
1034    if(!in.good()){
1035      cerr << "File " << input[ch] << " open failed!" << endl;
1036      return;
1037    }
1038
1039    // build the input data file name for each channel fname_ped-chXX.dat
1040    input_ped[ch] += fname_ped;
1041    input_ped[ch] += "-ch";
1042    input_ped[ch] += ch;
1043    input_ped[ch] += ".dat";
1044    ifstream in_ped(input_ped[ch].Data());
1045    if(!in_ped.good()){
1046      cerr << "File " << input_ped[ch] << " open failed!" << endl;
1047      return;
1048    }   
1049
1050
1051   
1052    //10DAC~10mV
1053    // determine the DAC range in dac_min, dac_max, step_dac from file input_ped
1054    // and count the number of events
1055    Double_t da, dat, dac_min, dac_max, step_dac;
1056    Int_t count = 0;
1057    while (in >> da >> dat ){
1058      if(count==0) dac_min  = da;
1059      if(count==1) step_dac = da - dac_min;
1060      dac_max=da;
1061      count++;
1062    }     
1063    in.close();
1064    in.clear();
1065
1066    /*   
1067    if(ch==43 || ch==47){//for T1 ASIC_C
1068      cerr << "CHECK " << ch << " " << fit_min_scurve << endl;
1069      Int_t fit_min_scurve=140;
1070    } else {
1071      Int_t fit_min_scurve=kFitMinS;
1072    }
1073    */
1074
1075    // correct the scale for DAC range
1076    if(kXmin<dac_min)kXmin=dac_min;
1077    const Int_t kNdata    = count;
1078    count = (fit_max_scurve-fit_min_scurve)/step_dac;
1079    const Int_t kNdataFit = count;
1080    if(ch==0){
1081      cerr << "kNdata, dac_min, dac_max, step_dac: " 
1082           << kNdata << " " << dac_min << " " << dac_max << " " 
1083           << step_dac << endl;
1084    }
1085   
1086    Double_t dac_min_ped, dac_max_ped, step_dac_ped;
1087    count = 0;
1088    while (in_ped >> da >> dat ){
1089      if(count==0) dac_min_ped  = da;
1090      if(count==1) step_dac_ped = da - dac_min_ped;
1091      dac_max_ped=da;
1092      count++;
1093    }
1094     
1095    in_ped.close();
1096    in_ped.clear();
1097   
1098
1099    const Int_t kNdataPed = count;
1100    if(ch==0){
1101      cerr << "kNdataPed, dac_min_ped, dac_max_ped, step_dac_ped: " 
1102           << kNdataPed << " " << dac_min_ped << " " << dac_max_ped
1103           << " " << step_dac_ped << endl;
1104    }
1105   
1106    // book histograms
1107    // SDC: 27/09/2013 hist_scurve is a pointer on an array of THD, thus the syntax is correct (no new operator)
1108   
1109
1110    hist_scurve[ch]      = TH1D(name_hist[ch],name_hist[ch],kNdata-1,
1111                                dac_min,dac_max);
1112    hist_scurve_ped[ch]  = TH1D(name_hist_ped[ch],name_hist_ped[ch],
1113                                kNdataPed-1,dac_min_ped,dac_max_ped);
1114    hist_diff[ch]        = TH1D(name_hist_diff[ch],name_hist_diff[ch],
1115                                kNdata-2,dac_min,dac_max);
1116    hist_diff_fit[ch]    = TH1D(name_hist_diff_fit[ch],name_hist_diff_fit[ch],
1117                                kNdataFit,fit_min_scurve,fit_max_scurve);
1118
1119   
1120
1121
1122
1123    // read the data for that channel for normal data
1124    in.open(input[ch].Data());
1125    Double_t       data[kNdata], dac[kNdata], dac_fit[kNdataFit], data_fit[kNdataFit];
1126    while (in >> da >> dat ){
1127      hist_scurve[ch].Fill(da,dat); // fill the histograms of the s-curve
1128    }
1129    in.close();
1130    in.clear();
1131
1132    // read the data for pedestal data
1133    in_ped.open(input_ped[ch].Data());
1134    while (in_ped >> da >> dat ){
1135      hist_scurve_ped[ch].Fill(da,dat); // fill the histogram for pedestal data
1136    }
1137    in_ped.close();
1138    in_ped.clear();
1139
1140    //dac_ped[ch]  = hist_scurve_ped[ch].GetMaximumBin()+dac_min_ped; //absolute value
1141    dac_ped[ch]  = hist_scurve_ped[ch].GetBinCenter(hist_scurve_ped[ch].GetMaximumBin()); //absolute value
1142    dac_ped_sum += dac_ped[ch]; //absolute value
1143    //cerr << "dac_ped[" << ch << "] dac_ped_sum " << dac_ped[ch]
1144    //<< " " << dac_ped_sum << endl;
1145   
1146
1147    hist_scurve[ch].Smooth(3); // Smooth the S curve
1148    Int_t binx=0;
1149    //hist_scurve[ch].GetBinWithContent(kHalfMax,binx,kXmin-dac_min+1,
1150    //                                  dac_max-dac_min+1,kHalfMax/2.);
1151
1152    // ??????
1153    hist_scurve[ch].GetBinWithContent(kHalfMax,binx,kXmin-dac_min+1,
1154                                      dac_max-dac_min+1,2000);
1155    dac_half[ch] = hist_scurve[ch].GetBinCenter(binx); //absolute value
1156    if(dac_half[ch]<kXmin)dac_half[ch]=-1;
1157   
1158    // put the histograms of the smoothed S-Curves into two arrays (dac, data)
1159    //-------------------------------------------------------------------------
1160    Int_t i=0;
1161    for(i=0;i<kNdata;i++){
1162      dac[i]  = hist_scurve[ch].GetBinCenter(i+1);
1163      data[i] = hist_scurve[ch].GetBinContent(i+1);
1164    }
1165   
1166    /*** compute derive diff on S-Curve ***/
1167    Double_t diff[kNdata-1], dac_diff[kNdata-1];
1168    Int_t trigger_diff=0;
1169    for(i=0;i<kNdata-1;i++){
1170      diff[i]     = (data[i]-data[i+1])/step_dac;
1171      dac_diff[i] = dac[i]+step_dac/2;
1172      hist_diff[ch].Fill(dac_diff[i],diff[i]); // fills differentiated histograms of smoothed s-curves which is the single photoelectron spectrum
1173      if(dac_diff[i]>=fit_min){
1174        diff_sum += diff[i]*dac_diff[i];
1175        //cerr << "diff[" << i << "], dac_diff[" << i << "] "
1176        //<< diff[i] << " " << dac_diff[i] << endl;
1177        trigger_diff+=diff[i];
1178      }
1179    }
1180    //mean_diff[ch] = diff_sum/trigger_diff;
1181    hist_diff[ch].GetXaxis()->SetRangeUser(fit_min,fit_max);
1182    mean_diff[ch] = hist_diff[ch].GetMean();
1183    //cerr << "trigger_diff, mean_diff[" << ch << "] "
1184    //<< trigger_diff << " " << mean_diff[ch] << endl;
1185   
1186   
1187    /******************* GRAPHICS *****************/
1188
1189
1190    // Now start the analysis which consist in finding the peak in the differentiated spectrum
1191    c1->cd(ch+1);   
1192    /*** fit scurves  ***/
1193    cerr << "ch " << ch;
1194
1195    //    SDC 24/09/2013 :: Need to declare outside this variable in a compiled program
1196    TF1 *fit_pol ;
1197
1198
1199    // Fit a polynomial curve in a given range on the s-curve directly. This is a kind of DEBUG procedure.
1200
1201    //if(ch==43 || ch==47){ // SDC: 25/09/2013 Why a special channel : the fit pol4 is performed for another DAC range
1202 /*   if(ch==170){
1203      fit_pol      = new TF1("fit_pol","pol4",150,fit_max_scurve);
1204      hist_scurve[ch].Fit(fit_pol,"","",150,fit_max_scurve);
1205    }else{ */
1206      fit_pol      = new TF1("fit_pol","pol4",fit_min_scurve,fit_max_scurve);
1207      hist_scurve[ch].Fit(fit_pol,"","",fit_min_scurve,fit_max_scurve);
1208   // }
1209
1210   
1211
1212    Double_t fit_par_pol4[5]; // container for fit parameter results
1213    fit_pol->GetParameters(fit_par_pol4);
1214    sfit_chiS[ch] =  fit_pol->GetChisquare(); // chi squared
1215    TF1 *func               = hist_scurve[ch].GetFunction("fit_pol");
1216    Double_t data_diff_fit;
1217    for(i=0;i<kNdataFit;i++){
1218      //if(ch==43 || ch==47){
1219      if(ch==170){
1220        dac_fit[i]  = hist_scurve[ch].GetBinCenter(i)+150-dac_min;
1221        data_fit[i] = func->Eval(hist_scurve[ch].GetBinCenter(i)+150-dac_min); // get the value of the fitted function
1222      }else{
1223        dac_fit[i]  = hist_scurve[ch].GetBinCenter(i)+fit_min_scurve-dac_min;
1224        data_fit[i] = func->Eval(hist_scurve[ch].GetBinCenter(i)+fit_min_scurve-dac_min); // get the value of the fitted function
1225        //cerr << "dac_fit[" << i << "], data_fit[" << i << "] "
1226        //<< dac_fit[i] << " " << data_fit[i] << endl;
1227      }
1228    }
1229
1230    // derive the fitted function on the S-curve to obtain the SPE
1231    Double_t dac_diff_fit;
1232    for(i=0;i<kNdataFit-1;i++){
1233      data_diff_fit = (data_fit[i]-data_fit[i+1])/step_dac;
1234      hist_diff_fit[ch].Fill(dac_fit[i],data_diff_fit); // Fill differentiated fitted S-curve
1235    }
1236
1237    TLine *lhalf_max   = new TLine(dac[0],kHalfMax,dac[kNdata-1],kHalfMax);
1238    TLine *ldac_half   = new TLine(dac_half[ch],0,dac_half[ch],
1239                                   (Double_t)kDrawMax);
1240    hist_scurve[ch].SetMaximum(kDrawMax);
1241    hist_scurve[ch].GetXaxis()->SetRangeUser(kDrawDacMin,kDrawDacMax);
1242    hist_scurve[ch].Draw();
1243    lhalf_max->Draw("same"); 
1244    ldac_half->Draw("same");
1245    if(dac_half[ch]>0){
1246      dac_half_sum += dac_half[ch]-dac_ped[ch]; //relative absolute value
1247      trigger++;
1248    }
1249
1250    /*** Differential plot from scurve fitting function  ***/
1251    c5->cd(ch+1);
1252    hist_diff_fit[ch].GetXaxis()->SetRangeUser(fit_min_scurve,fit_max_scurve-2*step_dac);
1253    Double_t dac_sfit_max=hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
1254    //if(dac_sfit_max<=fit_min_scurve+4*step_dac){
1255    if(dac_sfit_max<=fit_min_scurve+2*step_dac){
1256      hist_diff_fit[ch].GetXaxis()->SetRangeUser(fit_min_scurve+kFitSadj,fit_max_scurve-2*step_dac);
1257      dac_sfit[ch]  =  hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
1258      //if(dac_sfit[ch]<=fit_min_scurve+kFitSadj+4*step_dac)dac_sfit[ch]=-1;
1259      if(dac_sfit[ch]<fit_min_scurve+kFitSadj+step_dac){
1260        dac_sfit[ch]=-1;
1261        //dac_sfit[ch]=dac_half[ch];
1262        flag_fit[ch] = -1;
1263        cerr << "flag_fit[" << ch << "] " << flag_fit[ch]  << endl;
1264      }
1265    }else {
1266      dac_sfit[ch]  =  hist_diff_fit[ch].GetBinCenter(hist_diff_fit[ch].GetMaximumBin());
1267    }
1268
1269    if(dac_sfit[ch]>fit_min_scurve+5){
1270      dac_sfit_sum += dac_sfit[ch]-dac_ped[ch];
1271      trigger_sfit++;
1272    }else{
1273      //dac_sfit[ch] = fit_min_scurve+10;
1274      dac_sfit[ch] = -1;
1275    }
1276    //cerr << "dac_sfit[" << ch << "] " << dac_sfit[ch] << endl;
1277    TLine *ldac_sfit      = new TLine(dac_sfit[ch],0,dac_sfit[ch],kDrawMaxFit);
1278    TLine *ldac_sfit_copy = new TLine(dac_sfit[ch],0,dac_sfit[ch],kDrawMax);
1279    ldac_sfit->SetLineColor(kMagenta);
1280    ldac_sfit_copy->SetLineColor(kMagenta);
1281    hist_diff_fit[ch].SetMaximum(kDrawMaxFit);
1282    hist_diff_fit[ch].SetMinimum(0);
1283    hist_diff_fit[ch].Draw();
1284    ldac_sfit->Draw("same");
1285   
1286    /**** differential plot ****/
1287    c2->cd(ch+1);
1288    //hist_diff[ch].Rebin(8);
1289    hist_diff[ch].Rebin(3);
1290    fit_sdiff     = new TF1("fit_sdiff","gaus",dac_min,dac_max);
1291    //fit_sdiff->SetLineWidth(1);
1292    //if(ch==8 || ch==18 || ch==44 || ch==56)
1293    mean_sum += mean_diff[ch]-dac_ped[ch];
1294    trigger_mean++;
1295
1296    // fit the derivated polynom fit
1297    Double_t fit_par[3];
1298    //hist_diff[ch].Fit(fit_sdiff,"RQ0","",fit_min,fit_max);
1299    hist_diff[ch].Fit(fit_sdiff,"","",fit_min,fit_max);
1300    hist_diff[ch].GetXaxis()->SetRangeUser(kDrawDacMin,kDrawDacMax);
1301    hist_diff[ch].SetMinimum(0);
1302    hist_diff[ch].SetMaximum(kDrawMaxDiff);
1303    //hist_diff[ch].Rebin();
1304    hist_diff[ch].Draw();
1305    fit_sdiff->GetParameters(fit_par); // get the parameters for this fit
1306    dac_spe[ch] = fit_par[1];
1307    if(dac_spe[ch]>fit_min){
1308      dac_spe_sum += dac_spe[ch]-dac_ped[ch];
1309      //cerr << "dac_spe[" << ch << "] " << dac_spe[ch] << endl;
1310      trigger_fit++;
1311    }else{
1312      dac_spe[ch] = fit_min+10;
1313    }
1314    //cerr << "dac_spe[" << ch << "] " << dac_spe[ch] << endl;
1315    TLine *ldac_spe    = new TLine(dac_spe[ch],0,dac_spe[ch],
1316                                 (Double_t)kDrawMaxDiff);
1317    ldac_spe->SetLineColor(kRed);
1318    TLine *lmean_diff  = new TLine(mean_diff[ch],0,mean_diff[ch],
1319                                   (Double_t)kDrawMaxDiff);
1320    lmean_diff->SetLineColor(kCyan);
1321    TLine *lfit_min    = new TLine(fit_min,0,fit_min,kDrawMaxDiff);
1322    lfit_min->SetLineColor(kGray);
1323
1324    ldac_spe->Draw();
1325    lmean_diff->Draw("same");
1326    lfit_min->Draw("same");
1327   
1328    c3->cd(1);
1329    if(ch==0){
1330      hist_scurve[ch].SetMaximum(kDrawMax);
1331      hist_scurve[ch].Draw();
1332    }else{
1333    /*if(ch==8 || ch==18 || ch==44 || ch==56)*/
1334      hist_scurve[ch].Draw("same");
1335    }
1336    if(ch==kDrawCh){
1337      c3->cd(2);
1338      hist_scurve[ch].SetMaximum(kDrawMax);
1339      hist_scurve[kDrawCh].Draw();
1340      lhalf_max->Draw("same"); 
1341      ldac_half->Draw("same");
1342      ldac_sfit_copy->Draw("same");
1343      c3->cd(3);
1344      hist_diff[kDrawCh].Draw();     
1345      ldac_spe->Draw();
1346      lmean_diff->Draw("same");
1347      lfit_min->Draw("same");
1348      c3->cd(4);
1349      hist_diff_fit[kDrawCh].Draw();
1350       ldac_sfit->Draw("same");
1351      }
1352    /*
1353    if(ch<32){
1354      c6->cd(ch/4+1);
1355      hist_scurve[ch].SetMaximum(kDrawMax);
1356      if(ch%4==0){
1357        hist_scurve[ch].Draw();
1358      }else{
1359        hist_scurve[ch].Draw("same");
1360      }
1361      ldac_sfit_copy->Draw("same");
1362    }else{
1363      c7->cd(ch/4-7);
1364      hist_scurve[ch].SetMaximum(kDrawMax);
1365      if(ch%4==0){
1366        hist_scurve[ch].Draw();
1367      }else{
1368        hist_scurve[ch].Draw("same");
1369      }
1370      ldac_sfit_copy->Draw("same");
1371      }
1372    */
1373   
1374    c4->cd();
1375    if(ch==0){
1376      hist_scurve_ped[ch].SetMaximum(kDrawMaxPed);
1377      hist_scurve_ped[ch].Draw("l");
1378    }else{
1379      hist_scurve_ped[ch].Draw("lsame");
1380    }
1381   
1382  }// end of process single  channels
1383
1384//---------------------------------------------------------------------------------------------------
1385
1386
1387//---------------------------------------------------------------------------------------------------
1388
1389//---------------------------------------------------------------------------------------------------
1390void Analysis::ComputeDACAverage(Double_t dac_half_spe_fix ,Int_t    kChRef, Bool_t   checkAve )//0:use ref pix, 1:check average
1391{
1392
1393 // SDC 24/09/2013 :: In a compiled program, I have to declare the following variables outside the parenthesis
1394
1395 
1396  mean_diff_ave = 252.5; //dac_sfit of ch31 of ASIC_C with the 180 for lower gain pixels
1397 
1398  if(!checkAve){
1399    dac_ped_ave   = dac_ped[kChRef]; //absolute value
1400    dac_half_ave  = dac_half[kChRef];
1401    dac_spe_ave   = dac_spe[kChRef];
1402    dac_sfit_ave  = dac_sfit[kChRef];
1403    //Float_t dac_sfit_ave  = 252.5; //dac_sfit of ch31 of ASIC_C with the 180 for lower gain pixels
1404    mean_diff_ave = mean_diff[kChRef];
1405  }else{
1406    dac_ped_ave  = dac_ped_sum/(Float_t)kNch; //absolute value
1407    dac_half_ave = dac_half_sum/(Float_t)trigger+dac_ped_ave;
1408    dac_spe_ave  = dac_spe_sum/(Float_t)trigger_fit+dac_ped_ave;
1409    mean_diff_ave = mean_sum/(Float_t)trigger_mean+dac_ped_ave; 
1410    dac_sfit_ave = dac_sfit_sum/(Float_t)trigger_sfit+dac_ped_ave; 
1411  }
1412
1413
1414// SDC 24/09/2013 :: In a compiled program, I have to declare the following variables outside the parenthesis
1415
1416
1417  //trigger_mean=4;
1418  if(dac_half_spe_fix!=-1) {
1419    dac_half_spe = dac_half_spe_fix;
1420  } else {
1421    //Double_t dac_half_spe = dac_half_ave;
1422    dac_half_spe = dac_spe_ave;
1423  }
1424
1425  TLine *ldac_ped_ave   = new TLine(dac_ped_ave,0,dac_ped_ave,
1426                                   (Double_t)kDrawMax);
1427  TLine *ldac_ped_ave_copy = new TLine(dac_ped_ave,0,dac_ped_ave,
1428                                   (Double_t)kDrawMaxDiff);
1429  TLine *ldac_half_ave  = new TLine(dac_half_ave,0,dac_half_ave,
1430                                   (Double_t)kDrawMax);
1431  TLine *ldac_half_spe  = new TLine(dac_half_spe,0,dac_half_spe,
1432                                   (Double_t)kDrawMax);
1433  TLine *lmean_diff_ave = new TLine(mean_diff_ave,0,mean_diff_ave,
1434                                   (Double_t)kDrawMax);
1435  TLine *ldac_sfit_ave  = new TLine(dac_sfit_ave,0,dac_sfit_ave,
1436                                   (Double_t)kDrawMax);
1437  ldac_ped_ave->SetLineColor(kOrange);
1438  ldac_half_ave->SetLineColor(kRed);
1439  lmean_diff_ave->SetLineColor(kCyan);
1440  //ldac_half_spe->SetLineColor(kGreen);
1441  ldac_sfit_ave->SetLineColor(kMagenta); 
1442
1443  c3->cd(1);
1444  ldac_ped_ave->Draw("same");
1445  ldac_half_ave->Draw("same");
1446  // lhalf_max->Draw("same");  SDC (24/09/2013) :: Not declared
1447  ldac_half_spe->Draw("same");
1448  lmean_diff_ave->Draw("same");
1449  ldac_sfit_ave->Draw("same");
1450
1451  c3->cd(3);
1452  ldac_ped_ave_copy->SetLineColor(kOrange);
1453  ldac_ped_ave_copy->Draw("same");
1454
1455 
1456
1457}
1458
1459//---------------------------------------------------------------------------------------------------
1460
1461//---------------------------------------------------------------------------------------------------
1462void Analysis::OutputGain(const TString fname_out)
1463{
1464
1465 ofstream out(fname_out.Data()); 
1466
1467 Int_t    gain_asic_diff, gain_asic_dacHalf, gain_asic_diff_mean, gain_asic_sfit;
1468  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" 
1469       << endl;
1470
1471
1472  // loop 3 on all channels to make final computations on the gain
1473  for(ch=0;ch<kNch;ch++){
1474    /*
1475    if(ch==8 || ch==44 || ch==56 || ch==18){
1476      gain_asic_diff_mean=(Int_t)((mean_diff_ave-dac_ped_ave)/
1477                                  (mean_diff[ch]-dac_ped[ch])*gain_org[ch]);
1478    }else{
1479      gain_asic_diff_mean=0;
1480    }
1481    */
1482    if(dac_half[ch]<0){
1483      gain_asic_dacHalf   = 0;
1484      gain_asic_diff      = 0;
1485      gain_asic_diff_mean = 0;
1486      gain_asic_sfit      = 0;
1487    }else{
1488      gain_asic_dacHalf  =(Int_t)((dac_half_ave-dac_ped_ave)/
1489                                (dac_half[ch]-dac_ped[ch])*gain_org[ch]);
1490      gain_asic_diff     =(Int_t)((dac_half_spe-dac_ped_ave)/
1491                             (dac_spe[ch]-dac_ped[ch])*gain_org[ch]);
1492      gain_asic_diff_mean=(Int_t)((mean_diff_ave-dac_ped_ave)/
1493                                  (mean_diff[ch]-dac_ped[ch])*gain_org[ch]);
1494      gain_asic_sfit     =(Int_t)((dac_sfit_ave-dac_ped_ave)/
1495                             (dac_sfit[ch]-dac_ped[ch])*gain_org[ch]);
1496      //cerr << ch << " " << gain_org[ch] << endl;
1497    }
1498
1499   
1500
1501    out << "OUT_TABLE    "
1502         << ch << "    "
1503         << gain_asic_dacHalf   << "    "
1504         << gain_asic_diff      << "    "
1505         << gain_asic_diff_mean << "    "
1506         << gain_asic_sfit      << "    "
1507         << dac_half[ch]        << "    "
1508         << dac_ped[ch]         << "    "
1509         << (Int_t)dac_spe[ch]  << "    "
1510         << dac_half_ave        << "    "
1511         << dac_half_spe        << "    "
1512         << dac_ped_ave         << "    "
1513         << dac_sfit_ave        << "    "
1514         << sfit_chiS[ch]       << "    "
1515         << ch                  << "    "
1516         << flag_fit[ch]       
1517         << endl;
1518      std::cout << "channel " << ch << " old gain " << gain_org[ch] << " new gain " << gain_asic_sfit << std::endl;
1519  }
1520  out.close();
1521
1522}
Note: See TracBrowser for help on using the repository browser.