Subversion Repositories f9daq

Rev

Rev 47 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "TROOT.h"
#include "TFile.h"
#include "TBenchmark.h"
#include "TH1.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TH3F.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TPad.h"
#include "TF1.h"
#include "TGraph.h"
#include "TSpectrum.h"
#include "TAttFill.h"
#include "stdio.h"

#include "include/RTUtil.h"

//#include "include/RTUtil.h"

int tdc(char filename[256] = "test", int chX=0, int chY=0, double rangeLeft=87, double rangeRight=107, bool debug = false)
{
  
  int map[8][8]={{32,34,53,55,40,42,61,63},
                 {48,50,37,39,56,58,45,47},
                 {33,35,52,54,41,43,60,62},
                 {49,51,36,38,57,59,44,46},
                 {17,19,4,6,25,27,12,14},
                 {1,3,20,22,9,11,28,30},
                 {16,18,5,7,24,26,13,15},
                 {0,2,21,23,8,10,29,31}
                };
  
  char fnameroot[256];
  TFile* rootfile;
        sprintf(fnameroot, "root/%s.root", filename); 
        rootfile = (TFile *) gROOT->FindObject(filename); 
        if(rootfile==NULL) rootfile = new TFile(fnameroot);
        if(rootfile==NULL) {
          printf("Cannot open root file %s!!!\n",fnameroot); 
          return(0);
        }

        // set draw style
        /*
        gStyle->SetPalette(1, 0);
        
        gStyle->SetPaperSize(TStyle::kA4);
        gStyle->SetStatBorderSize(1);
        gStyle->SetFrameBorderMode(0);
        gStyle->SetFrameFillColor(0);
        gStyle->SetCanvasBorderMode(0);
        gStyle->SetPadBorderMode(0);
        gStyle->SetPadColor(0);
        gStyle->SetCanvasColor(0);
        gStyle->SetStatColor(0);
        gStyle->SetOptFit(1111);
        gStyle->SetOptStat("ne");
        gStyle->SetPadTopMargin(0.15);
        gStyle->SetPadBottomMargin(0.15);
        gStyle->SetPadRightMargin(0.15);
        gStyle->SetPadLeftMargin(0.15);
        //gStyle->SetTitleYOffset(1.4);*/
        RTSetStyle(gStyle);
         
    TCanvas* canvas1 = new TCanvas("canvas1","canvas1",1000,1000);
    TH2F* h_tdc = (TH2F*) rootfile->Get("htdc");
    canvas1->cd();
    h_tdc->Draw("colz");
    
    //TH3F* h_3D = (TH3F*) rootfile->Get("h_correctedTDC");
    TH2F* h_correctedTDC = (TH2F*) rootfile->Get("h_correctedTDC");
    TCanvas* canvas2 = new TCanvas("canvas2","canvas2",1000,1000);
    canvas2->cd();
    h_correctedTDC->Draw("colz");
    
    
    //TH2D* h_correctedTDC = (TH2D*) h_3D->Project3D("xz");
    //h_correctedTDC->SetTitle("; t [ns]; Channel");
    //h_correctedTDC->Draw("colz");
    TCanvas* canvas3 = new TCanvas("canvas3","canvas3",1600,1600);
    canvas3->cd();
    //TH1D* h_allTDCbins = h_correctedTDC->ProjectionX("", 1, 64);
    //h_allTDCbins->Draw();
    
    
    int binY = map[chX][chY];
    if (debug) printf("%d\n", binY);
    //TH1D* channelY = h_correctedTDC->ProjectionX("",binY+1,binY+1);
    TH1D* channelY = h_tdc->ProjectionX("",binY+1,binY+1);
    channelY->SetStats(1);
    
    char title[256];
    sprintf(title,";Time [ns];Events");
    channelY->SetTitle(title);
    //channelY->GetYaxis()->SetTitleOffset(1.7);
    //TAxis* xAxis = h_correctedTDC->GetXaxis();
    //int range = xAxis->GetBinUpEdge(xAxis->GetLast()+1);
    //channelY->GetXaxis()->SetRangeUser(-range, range);
    //gStyle->SetOptStat("nemr");
    //gStyle->SetOptFit(111);
    channelY->GetXaxis()->SetRangeUser(rangeLeft,rangeRight);
    channelY->Draw();
    
    // Fit function: linear for background c + k*x
    // Signal: norm*gaussian(mu,sigma)
    TF1* f_gaus1 = new TF1("f_gaus1","[0] +[1]*x + gaus(2)", rangeLeft, rangeRight);
    //TF1* f_gaus1 = new TF1("f_gaus1","[0] +[1]*x + gaus(2)");
    f_gaus1->SetParNames("Const","Linear","Norm","#mu","#sigma");
    //f_gaus1->SetLineColor(kAzure+1);
    f_gaus1->SetLineColor(kBlack);
    f_gaus1->SetLineWidth(4);
    
    f_gaus1->SetParameter(0, channelY->GetMinimum());
    f_gaus1->SetParameter(1, 0.0);
    f_gaus1->SetParameter(2, channelY->GetMaximum() - channelY->GetMinimum());
    f_gaus1->SetParameter(3, channelY->GetBinCenter(channelY->GetMaximumBin()));
    f_gaus1->SetParameter(4, 1.0);
    //channelY->Fit(f_gaus1,"qr");
    //channelY->Fit(f_gaus1,"r");
    
    //Fit in range +- 3 sigma
    //channelY->Fit(f_gaus1,"r","", f_gaus1->GetParameter(3)-3*f_gaus1->GetParameter(4),
    channelY->Fit(f_gaus1,"","");
                                  //f_gaus1->GetParameter(3)+3*f_gaus1->GetParameter(4));
                                  
    double N = channelY->GetEntries();
    double Nbkg = (rangeRight - rangeLeft)*f_gaus1->GetParameter(0);
    double Nsig = N - Nbkg;
    printf("\nN = %f, Nsig = %f, Nbkg = %f, SN = %f***\n\n", N, Nsig, Nbkg, Nsig/Nbkg);
   /* 
    //to draw a shaded area above and below an histogram range, we create
   //a TGraph object (here we shade bins 60 to 80).
   Int_t i;
   Int_t n = 2*(80-60);
   TGraph *gr = new TGraph(2*n);
   for (i=0;i<20;i++) {
      Float_t xlow = h1->GetBinLowEdge(60+i);
      Float_t xup  = h1->GetBinLowEdge(60+i+1);
      Float_t y    = h1->GetBinContent(60+i);
      Float_t yup  = 1.1*y;
      Float_t ydown= 0.9*y;
      gr->SetPoint(2*i,  xlow,yup);
      gr->SetPoint(2*i+1,xup, yup);
      gr->SetPoint(2*n-2*i-1,xlow, ydown);
      gr->SetPoint(2*n-2*i-2,xup, ydown);
   }
   gr->SetFillColor(2);
   gr->Draw("lf");*/

   TH1D* timeWindow = (TH1D*) channelY->Clone();
    timeWindow->GetXaxis()->SetRangeUser(92,102);
    //timeWindow->SetFillColor(kAzure-1);
    timeWindow->SetFillColor(kBlack);
    timeWindow->SetFillStyle(3004);
    timeWindow->Draw("same");
    
  
  char drawing[128];
  sprintf(drawing, "ps/%sTDC.pdf", filename);
  canvas3->SaveAs(drawing);
  
  gPad->Update();                          
  return (0);
  }