Subversion Repositories f9daq

Rev

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

  1. #include "TROOT.h"
  2. #include "TFile.h"
  3. #include "TBenchmark.h"
  4. #include "TH1F.h"
  5. #include "TH2F.h"
  6. #include "TH3F.h"
  7. #include "TCanvas.h"
  8. #include "TStyle.h"
  9. #include "TPad.h"
  10. #include "TF1.h"
  11. #include "TGraph.h"
  12. #include "TSpectrum.h"
  13. #include "stdio.h"
  14. #include "THStack.h"
  15. #include "TPaveText.h"
  16.  
  17. #include "include/RTUtil.h"
  18.  
  19. double getNoise(TH2F*, int, int);
  20.  
  21. int biasScan(char filename[256] = "test", char plopt[256]="th", int chXstart=0, int chXend=7, double par1=0.0, double par2=0.0, bool debug = false)
  22. {
  23.   const int c_nChannels = 64;
  24.   const double c_xOffset = 0; // mm
  25.   const double c_yOffset = 0;
  26.  
  27.   int map[8][8]={{32,34,53,55,40,42,61,63},
  28.                  {48,50,37,39,56,58,45,47},
  29.                  {33,35,52,54,41,43,60,62},
  30.                  {49,51,36,38,57,59,44,46},
  31.                  {17,19,4,6,25,27,12,14},
  32.                  {1,3,20,22,9,11,28,30},
  33.                  {16,18,5,7,24,26,13,15},
  34.                  {0,2,21,23,8,10,29,31}
  35.                 };
  36.                
  37.   // Set draw style              
  38.   RTSetStyle(gStyle);
  39.  
  40.   // open the file with histograms
  41.   char fnameroot[256];
  42.   TFile* rootfile;
  43.         sprintf(fnameroot, "root/%s.root", filename);
  44.         rootfile = (TFile *) gROOT->FindObject(filename);
  45.         if(rootfile==NULL) rootfile = new TFile(fnameroot);
  46.         if(rootfile==NULL) {
  47.           printf("Cannot open root file %s!!!\n",fnameroot);
  48.           return(0);
  49.         }
  50.  
  51.   if(strstr(plopt, "tdc") != NULL) {
  52.     TCanvas *canvas21 = new TCanvas("canvas21","canvas21",1600,800);
  53.     TH3F* h0 = (TH3F*) rootfile->Get("h_correctedTDC");
  54.           Int_t binsX = h0->GetXaxis()->GetNbins();
  55.     Int_t minX  = h0->GetXaxis()->GetFirst();
  56.     Int_t maxX  = h0->GetXaxis()->GetLast()+1;
  57.     Int_t binsY = h0->GetYaxis()->GetNbins();
  58.     Int_t minY  = h0->GetYaxis()->GetFirst();
  59.     Int_t maxY  = h0->GetYaxis()->GetLast()+1;
  60.     Int_t binsZ = h0->GetZaxis()->GetNbins();
  61.     Int_t minZ  = h0->GetZaxis()->GetFirst();
  62.     Int_t maxZ  = h0->GetZaxis()->GetLast()+1;
  63.     Double_t xLowUser  = h0->GetXaxis()->GetBinLowEdge(minX);
  64.     Double_t xUpUser = h0->GetXaxis()->GetBinUpEdge(maxX);
  65.     Double_t yLowUser  = h0->GetYaxis()->GetBinLowEdge(minY);
  66.     Double_t yUpUser = h0->GetYaxis()->GetBinUpEdge(maxY);
  67.    
  68.     canvas21->Divide(2);
  69.     //h0->Draw();
  70.     canvas21->cd(1);
  71.     //h0->Project3D("xy")->Draw("colz");
  72.     //canvas21->cd(2);
  73.     //h0->Project3D("yz")->Draw("colz");
  74.     //canvas21->cd(3);
  75.     TH2D* h_correctedTDC = (TH2D*) h0->Project3D("xz");
  76.     //h0->GetZaxis()->SetRangeUser(-5,5);
  77.     //h0->Project3D("xzo")->Draw("colz");
  78.     //h_correctedTDC->SetTitle("; t [ns]; Channel");
  79.     h_correctedTDC->Draw("colz");
  80.     /*
  81.     TH1F* tdc1 = new TH1F("tdc1",";TDC [ns];Events",binsZ, minZ, maxZ);
  82.     //for(int j=minY; j<maxY; j++) {
  83.       for(int k=minZ; k<maxZ; k++) {
  84.         double signal = h0->GetBinContent(1,1,k);
  85.         tdc1->Fill(k,signal);
  86.         //}
  87.         }
  88.     tdc1->Draw();
  89.     */
  90.    
  91.     TH2D* h_timeWalk = (TH2D*) h0->Project3D("zy");
  92.     canvas21->cd(2);
  93.     //h_timeWalk->SetTitle(";Threshold [V]; t [ns]");
  94.     h_timeWalk->Draw("colz");
  95.   }
  96.  
  97.   if (strstr(plopt, "bias") != NULL) {
  98.     TCanvas* canvas01 = new TCanvas("canvas01","",800, 800);
  99.     canvas01->cd();
  100.     TH2F* h_bias = (TH2F*) rootfile->Get("h_bias");
  101.     h_bias->Draw("colz");
  102.   }
  103.  
  104.   if (strstr(plopt, "th") != NULL) {
  105.    TCanvas* canvas1 = new TCanvas("canvas1","",800,800);
  106.    canvas1->cd();
  107.    //gPad->SetLogz();
  108.    //canvas1->SetLogz();
  109.    TH2F* h_threshold = (TH2F*) rootfile->Get("h_threshold");
  110.    h_threshold->SetTitleOffset(1.5,"y");
  111.    //h_threshold->GetZaxis()->SetRangeUser(0,1000);
  112.    h_threshold->SetContour(20);
  113.    
  114.    h_threshold->Draw("colz");
  115.    
  116.    TCanvas* canvas2 = new TCanvas("canvas2","",1600,800);
  117.    canvas2->Divide(2);
  118.    TH1D* h_projection1 = h_threshold->ProjectionY("Ch 37",38,38);
  119.    canvas2->cd(1);
  120.    //h_projection1->GetYaxis()->SetRangeUser(0,1000);
  121.    //gPad->SetLogy();
  122.    h_projection1->Draw();
  123.    
  124.    TH1D* h_projection2 = h_threshold->ProjectionY("Ch 38",39,39);
  125.    canvas2->cd(2);
  126.    //gPad->SetLogy();
  127.    h_projection2->Draw();
  128.   }
  129.  
  130.   if(strstr(plopt,"16") != NULL)
  131.   {
  132.   TH2F* h_threshold = (TH2F*) rootfile->Get("h_bias");
  133.   TCanvas* canvas = new TCanvas("canvas","",1600,1600);
  134.   TCanvas* canvas11 = new TCanvas("canvas11","",800,800);
  135.   canvas->cd();
  136.   TVirtualPad *main = new TPad("main","main",0,0,1,1,10,1);
  137.   main->Draw();
  138.   main->cd();
  139.   main->Divide(4,4);
  140.   for(int i=chXstart; i<chXend; i++) {
  141.     for(int j=(int)par1; j<(int)par2; j++) {
  142.       int channel = map[i][j];
  143.       TH1D* h_projection = h_threshold->ProjectionY("",channel+1,channel+1);
  144.       int canvasPosition = i-chXend+4*((int)par2-j)+1;
  145.       printf(" %d ", canvasPosition);
  146.       main->cd(canvasPosition);
  147.       gPad->SetMargin(0.08, 0.08, 0.08, 0.08);
  148.       char name[128];
  149.       sprintf(name,"Channel %d",channel);
  150.       h_projection->SetTitle(name);
  151.       h_projection->GetYaxis()->SetRangeUser(0,10000);
  152.       h_projection->DrawCopy("pe1x0");
  153.       canvas11->cd();
  154.       if (canvasPosition == 1) {
  155.         h_projection->SetMarkerStyle(kFullDotMedium);
  156.         h_projection->GetYaxis()->SetRangeUser(0,10000);
  157.         h_projection->DrawCopy("Pl");
  158.         }
  159.       else {
  160.         h_projection->SetMarkerStyle(kFullDotMedium);
  161.         h_projection->SetLineColor(canvasPosition);
  162.         h_projection->SetMarkerColor(canvasPosition);
  163.         h_projection->SetTitle("");
  164.         h_projection->DrawCopy("plsame");
  165.         }
  166.     }
  167.  
  168.   }
  169.   printf("\n");
  170.   }
  171.  
  172.   if(strstr(plopt, "1ch") != NULL)
  173.   {
  174.     TH2F* h_bias = (TH2F*) rootfile->Get("h_bias");
  175.     TCanvas* canvas = new TCanvas("canvas","",800,800);
  176.     canvas->cd();
  177.     int channel = map[chXstart][chXend];
  178.     TH1D* h_projection = h_bias->ProjectionY("",channel+1,channel+1);
  179.     char name[128];
  180.     sprintf(name,"Channel %d",channel);
  181.     h_projection->SetTitle(name);
  182.     h_projection->SetMarkerStyle(kFullDotMedium);
  183.     //h_projection->SetMarkerSize(8);
  184.     gPad->SetMargin(0.1,0.1,0.1,0.1);
  185.    
  186.    
  187.     TF1* f_linear = new TF1("f_linear", "[0] + [1]*x", par1, par2);
  188.     f_linear->SetParameter(0, -1);
  189.     f_linear->SetParameter(1, 0.1);
  190.     f_linear->SetParNames("p0", "p1");
  191.     h_projection->Fit(f_linear,"q");
  192.     h_projection->Fit(f_linear,"r");
  193.     h_projection->Draw("pe1x0");
  194.    
  195.     double p0 = f_linear->GetParameter(0);
  196.     double p1 = f_linear->GetParameter(1);
  197.     printf("p0 %f p1 %f V_b = %f [V]\n", p0, p1, -p0/p1);
  198.     TPaveText *text = new TPaveText(0.15,0.5,0.35,0.56);
  199.     text->SetFillColor(0);
  200.     text->SetBorderSize(0.1);
  201.     text->SetTextSize(0.030);
  202.     char string[128];
  203.     sprintf(string, "V_b = %2.2f", -p0/p1);
  204.     text->AddText(string);
  205.     text->Draw("same");
  206.     text->Paint();
  207.   }
  208.  
  209.   if( strstr(plopt, "all") != NULL ) {
  210.     TCanvas *canvas2 = new TCanvas("canvas2","Hits x;;",2000,2000);
  211.     TCanvas *canvas3 = new TCanvas("canvas3","Hits y;;",2000,2000);
  212.     canvas2->Divide(8,8);
  213.     canvas3->Divide(8,8);
  214.     TH1F* h_hitsx;
  215.     TH1F* h_hitsy;
  216.     for(int i=0; i<c_nChannels; i++) {
  217.       canvas2->cd(i+1);
  218.       char hname[128];
  219.       sprintf(hname, "hnhitsx%d", i);
  220.       h_hitsx = (TH1F*)rootfile->Get(hname);
  221.       h_hitsx->Draw();
  222.       canvas3->cd(i+1);
  223.       sprintf(hname, "hnhitsy%d", i);
  224.       h_hitsy = (TH1F*)rootfile->Get(hname);
  225.       h_hitsy->Draw();  
  226.     }
  227.   }
  228.  
  229.   if( strstr(plopt, "x") != NULL ) {
  230.     TCanvas *canvas10 = new TCanvas("canvas10","Ch x;;",500,500);
  231.     TH1F* h_hitsx;
  232.     canvas10->cd();
  233.     char hname[128];
  234.     sprintf(hname, "hnhitsx%d", chXstart);
  235.     h_hitsx = (TH1F*)rootfile->Get(hname);
  236.     h_hitsx->Draw("colz");
  237.   }
  238.  
  239.   if( strstr(plopt, "y") != NULL ) {
  240.     TCanvas *canvas11 = new TCanvas("canvas11","Ch x;;",500,500);
  241.     TH1F* h_hitsy;
  242.     canvas11->cd();
  243.     char hname[128];
  244.     sprintf(hname, "hnhitsy%d", chXstart);
  245.     h_hitsy = (TH1F*)rootfile->Get(hname);
  246.     h_hitsy->Draw();
  247.   }
  248.  
  249.  
  250.   /** Draws the sum of the channels
  251.     * Each channel is a 2d plot
  252.     * Intended for the study of 1 channel
  253.     */
  254.   if (strstr(plopt, "2d") != NULL) {
  255.    
  256.     int nX = chXend - chXstart + 1;
  257.     int nY = (int)par2 - (int)par1 + 1;
  258.     TCanvas* canvas7 = new TCanvas("canvas7","canvas7", nX*400,nY*400);
  259.     printf("nx %d ny %d\n",nX,nY);
  260.     canvas7->Divide(nX,nY);
  261.     for(int i=(int)par1; i<=(int)par2; i++) {
  262.       for(int j=chXstart; j<=chXend; j++) {
  263.       //int canvasPosition = nX*(i-chYstart)+(j-chXstart)+1;
  264.       int canvasPosition = nX*((int)par2-i)+(j-chXstart)+1;
  265.       if (debug) printf("canvas %d\n",canvasPosition);
  266.         canvas7->cd(canvasPosition);
  267.         char hname[128];
  268.         int chPosition = map[j][i];
  269.         sprintf(hname, "h2d%d", chPosition);
  270.         TH2F* h_2d = (TH2F*)rootfile->Get(hname);
  271.         h_2d->Draw("colz");
  272.       } //x
  273.     }
  274.    
  275.     // Number of photoelectrons - Poissonian correction
  276.     TCanvas* canvas8 = new TCanvas("canvas8","canvas8", 1000,1000);
  277.     canvas8->cd();
  278.     gStyle->SetOptStat(0);
  279.       char hname[128];
  280.       int chPosition = map[1][2];
  281.       sprintf(hname, "h2d%d", chPosition);
  282.       TH2F* h_2d = (TH2F*)rootfile->Get(hname);
  283.  
  284.       Int_t binsX = h_2d->GetXaxis()->GetNbins();
  285.       Int_t minX  = h_2d->GetXaxis()->GetFirst();
  286.       Int_t maxX  = h_2d->GetXaxis()->GetLast()+1;
  287.       Int_t binsY = h_2d->GetYaxis()->GetNbins();
  288.       Int_t minY  = h_2d->GetYaxis()->GetFirst();
  289.       Int_t maxY  = h_2d->GetYaxis()->GetLast()+1;
  290.       Double_t xLowUser  = h_2d->GetXaxis()->GetBinLowEdge(minX);
  291.       Double_t xUpUser   = h_2d->GetXaxis()->GetBinUpEdge(maxX);
  292.       Double_t yLowUser  = h_2d->GetYaxis()->GetBinLowEdge(minY);
  293.       Double_t yUpUser =   h_2d->GetYaxis()->GetBinUpEdge(maxY);
  294.       if (debug) printf("xLow %f xUp %f\n",xLowUser,xUpUser);
  295.       TH2F* h_corrected = new TH2F("h_corrected","h_corrected",binsX, xLowUser, xUpUser, binsY, yLowUser, yUpUser);
  296.      
  297.       double noise = getNoise(h_2d, 1, 89);
  298.       if(debug) printf("Noise = %f\n", noise);
  299.       for(int k=minX; k<=maxX; k++) {
  300.         for(int j=minY; j<=maxY; j++) {
  301.           double signal = h_2d->GetBinContent(k,j); // detected
  302.           //double p = ((signal - noise) > 1) ? (signal-noise) : 1;
  303.           double p = signal - noise;
  304.           p /= 10000.;
  305.           double p0 = 1.0 - p; // events with zero photons
  306.           //double eta = (-log(p0) * p0 ) / (1-p0-0.00001);
  307.           double eta = -log(p0); // constant of the poissonian statistics
  308.           if (debug) printf("p=%f p0=%f log(p0)=%f eta=%f\n",p,p0,log(p0),eta);
  309.           double x = k*(xUpUser-xLowUser)/double(binsX);
  310.           double y = j*(yUpUser-yLowUser)/double(binsY);
  311.           h_corrected->Fill(x+3,y+8, eta);
  312.         }
  313.        }
  314.       h_corrected->SetTitle("n_pe = - ln P(0);x[mm];y[mm]");
  315.       h_corrected->GetZaxis()->SetRangeUser(-0.05,0.3);
  316.       h_corrected->Draw("colz");
  317.      
  318.       // collection efficiency
  319.       int nPoints =0;
  320.       double efficiency=0;
  321.       for (int i=18; i<=58; i++) {
  322.         for (int j=19; j<=59; j++) {
  323.           double signal = h_corrected->GetBinContent(i,j);
  324.           if(debug) printf("signal %f\n",signal);
  325.           efficiency += signal;
  326.           nPoints++;
  327.         }
  328.       }  
  329.     printf("Signal sum = %f\n   # of points = %d\n",efficiency,nPoints);
  330.   }
  331.  
  332.   /** Draws the sum of channel signals
  333.     * Each channel is a 2d ('h2d') histogram
  334.     * Suitable for 8x8 chs scan
  335.     */
  336.   if( strstr(plopt, "sum") != NULL ) {
  337.     int nX = 7 - 0 + 1;
  338.     int nY = 7 - 0 + 1;
  339.           TCanvas* canvas12 = new TCanvas("canvas12","c2",8*200, 8*200);
  340.           canvas12->cd();
  341.           gStyle->SetOptStat(0);
  342.  
  343.     // final histogram parameters
  344.     TH2F* h0 = (TH2F*) rootfile->Get("h2d0");
  345.           Int_t binsX = h0->GetXaxis()->GetNbins();
  346.     Int_t minX  = h0->GetXaxis()->GetFirst();
  347.     Int_t maxX  = h0->GetXaxis()->GetLast()+1;
  348.     Int_t binsY = h0->GetYaxis()->GetNbins();
  349.     Int_t minY  = h0->GetYaxis()->GetFirst();
  350.     Int_t maxY  = h0->GetYaxis()->GetLast()+1;
  351.     Double_t xLowUser  = h0->GetXaxis()->GetBinLowEdge(minX);
  352.     Double_t xUpUser = h0->GetXaxis()->GetBinUpEdge(maxX);
  353.     Double_t yLowUser  = h0->GetYaxis()->GetBinLowEdge(minY);
  354.     Double_t yUpUser = h0->GetYaxis()->GetBinUpEdge(maxY);
  355.     if (debug) printf("xLow %f xUp %f\n",xLowUser,xUpUser);
  356.     TH2F* h_corrected = new TH2F("h_corrected","h_corrected",binsX,xLowUser,xUpUser, binsY,yLowUser,yUpUser);
  357.     TH2F* h[9];
  358.    
  359.     // 2d histogram noise subtraction and poisson scaling
  360.     for(int i=0; i<=7; i++) {
  361.       for(int j=chXstart; j<=chXend; j++) {
  362.         int chPosition = map[j][i];
  363.         char hname[128];
  364.               sprintf(hname, "h2d%d", chPosition);  
  365.               int histogram = nX*(i-0)+(j-chXstart);
  366.               h[histogram] = (TH2F *) rootfile->Get(hname);
  367.               int noise = getNoise(h[histogram], 1, 170);
  368.               if (debug) printf("noise: %d\n",noise);
  369.               for(int k=minX; k<=maxX; k++) {
  370.           for(int l=minY; l<=maxY; l++) {
  371.             int signal = h[histogram]->GetBinContent(k,l); // detected
  372.             //double p = ((signal - noise) > 0.1) ? (signal-noise) : 0.1;
  373.             double p = signal - noise;
  374.             p /= 10000.;
  375.             double p0 = 1.0 - p; // events with zero photons
  376.             //double eta = (-log(p0) * p0 ) / (1-p0-0.00001);
  377.             double eta = -log(p0);
  378.             //printf("p=%f p0=%f log(p0)=%f eta=%f\n",p,p0,log(p0),eta);
  379.             double x = k*(xUpUser-xLowUser)/double(binsX);
  380.             double y = l*(yUpUser-yLowUser)/double(binsY);
  381.             h_corrected->Fill(x-c_xOffset, y-c_yOffset, signal);
  382.           }
  383.         }    
  384.      
  385.       }
  386.     }
  387.  
  388.     h_corrected->SetTitle("SiPM#2 n_p.e.;x[mm];y[mm]");
  389.     //h_corrected->GetZaxis()->SetRangeUser(-0.05,.30);
  390.     h_corrected->Draw("colz");
  391.    
  392.     TCanvas* canvas13 = new TCanvas("canvas13","canvas13",600,300);
  393.     canvas13->Divide(2);
  394.     canvas13->cd(1);
  395.     //h[16]->Draw("colz");
  396.     canvas13->cd(2);
  397.     //h[8]->Draw("colz");
  398.   }
  399.    
  400.   return(0);
  401. }
  402.  
  403. /** Function calculates the noise from one channel
  404.   * it runs through the bins along x and returns the average value
  405.   */
  406. double getNoise(TH2F* histogram, int yStart, int yEnd)
  407. {
  408.   double noise=0;
  409.   int count=0;
  410.   for(int j=yStart; j<yEnd; j++) {
  411.     double value = histogram->GetBinContent(j,2);
  412.     //if (noise < value) noise = value;
  413.     noise += value;
  414.     count++;
  415.     }
  416.   return (noise/double(count));
  417. }
  418.  
  419.