Subversion Repositories f9daq

Rev

Blame | 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 "TCanvas.h"
  7. #include "TStyle.h"
  8. #include "TPad.h"
  9. #include "TF1.h"
  10. #include "TGraph.h"
  11. #include "TSpectrum.h"
  12. #include "stdio.h"
  13.  
  14. #include "RTUtil.h"
  15.  
  16. double getNoise(TH2F*, int, int);
  17.  
  18. int plot_sipm(char filename[256] = "test", char plopt[256]="all", int chXstart=0, int chXend=7, int chYstart=0, int chYend=7, bool debug = false)
  19. {
  20.   const int c_nChannels = 64;
  21.   const double c_xOffset = 1; // mm
  22.   const double c_yOffset = 0.7;
  23.  
  24.   int map[8][8]={{32,34,53,55,40,42,61,63},
  25.                  {48,50,37,39,56,58,45,47},
  26.                  {33,35,52,54,41,43,60,62},
  27.                  {49,51,36,38,57,59,44,46},
  28.                  {17,19,4,6,25,27,12,14},
  29.                  {1,3,20,22,9,11,28,30},
  30.                  {16,18,5,7,24,26,13,15},
  31.                  {0,2,21,23,8,10,29,31}
  32.                 };
  33.  
  34.   char fnameroot[256];
  35.   TFile* rootfile;
  36.         sprintf(fnameroot, "root/%s.root", filename);
  37.         rootfile = (TFile *) gROOT->FindObject(filename);
  38.         if(rootfile==NULL) rootfile = new TFile(fnameroot);
  39.         if(rootfile==NULL) {
  40.           printf("Cannot open root file %s!!!\n",fnameroot);
  41.           return(0);
  42.         }
  43.  
  44.         // set draw style
  45.         gStyle->SetOptStat("ne");
  46.         gStyle->SetPalette(1, 0);
  47.        
  48.         gStyle->SetPaperSize(TStyle::kA4);
  49.         gStyle->SetStatBorderSize(1);
  50.         gStyle->SetFrameBorderMode(0);
  51.         gStyle->SetFrameFillColor(0);
  52.         gStyle->SetCanvasBorderMode(0);
  53.         gStyle->SetPadBorderMode(0);
  54.         gStyle->SetPadColor(0);
  55.         gStyle->SetCanvasColor(0);
  56.         gStyle->SetStatColor(0);
  57.         gStyle->SetOptFit(11);
  58.         gStyle->SetOptStat();
  59.         gStyle->SetPadRightMargin(0.15);
  60.         gStyle->SetPadLeftMargin(0.12);
  61.         //gStyle->SetTitleYOffset(1.4);
  62.  
  63.  
  64.   if( strstr(plopt, "all") != NULL ) {
  65.     TCanvas *canvas2 = new TCanvas("canvas2","Hits x;;",2000,2000);
  66.     TCanvas *canvas3 = new TCanvas("canvas3","Hits y;;",2000,2000);
  67.     canvas2->Divide(8,8);
  68.     canvas3->Divide(8,8);
  69.     TH1F* h_hitsx;
  70.     TH1F* h_hitsy;
  71.     for(int i=0; i<c_nChannels; i++) {
  72.       canvas2->cd(i+1);
  73.       char hname[128];
  74.       sprintf(hname, "hnhitsx%d", i);
  75.       h_hitsx = (TH1F*)rootfile->Get(hname);
  76.       h_hitsx->Draw();
  77.       canvas3->cd(i+1);
  78.       sprintf(hname, "hnhitsy%d", i);
  79.       h_hitsy = (TH1F*)rootfile->Get(hname);
  80.       h_hitsy->Draw();  
  81.     }
  82.   }
  83.  
  84.   if( strstr(plopt, "x") != NULL ) {
  85.     TCanvas *canvas10 = new TCanvas("canvas10","Ch x;;",500,500);
  86.     TH1F* h_hitsx;
  87.     canvas10->cd();
  88.     char hname[128];
  89.     sprintf(hname, "hnhitsx%d", chXstart);
  90.     h_hitsx = (TH1F*)rootfile->Get(hname);
  91.     h_hitsx->Draw();
  92.   }
  93.  
  94.   if( strstr(plopt, "y") != NULL ) {
  95.     TCanvas *canvas11 = new TCanvas("canvas11","Ch x;;",500,500);
  96.     TH1F* h_hitsy;
  97.     canvas11->cd();
  98.     char hname[128];
  99.     sprintf(hname, "hnhitsy%d", chXstart);
  100.     h_hitsy = (TH1F*)rootfile->Get(hname);
  101.     h_hitsy->Draw();
  102.   }
  103.  
  104.   if( strstr(plopt, "share") != NULL ) {
  105.    /*TCanvas *canvas4 = new TCanvas("canvas1","canvas1",1000,1000);
  106.    int nChannels = chYend-chYstart+1;
  107.    int ncols = nChannels/2;
  108.    printf("nch %d nch\\2 %d\n", nChannels, ncols);
  109.    canvas4->Divide(2,ncols);
  110.    TH1F* h_hitsy;
  111.    for(int i=chYstart; i<=chYend; i++){
  112.      canvas4->cd(i-chYstart+1);
  113.      char hname[128];
  114.      int chPosition = map[0][i];
  115.      sprintf(hname, "hnhitsy%d", chPosition);
  116.      h_hitsy = (TH1F*)rootfile->Get(hname);
  117.      h_hitsy->Draw();
  118.    }*/
  119.    
  120.    TCanvas *canvas4 = new TCanvas("canvas4","canvas4",500,500);
  121.    canvas4->cd();
  122.    for(int i=chXstart; i<=chXend; i++) {
  123.      TH1F* h_hitsx;
  124.      char hname[128];
  125.      int chPosition = map[i][chYstart];
  126.      sprintf(hname, "hnhitsx%d", chPosition);
  127.      h_hitsx = (TH1F*)rootfile->Get(hname);
  128.      h_hitsx->SetTitle("Scan X;x [mm]; Entries");
  129.      h_hitsx->GetYaxis()->SetTitleOffset(1.3);
  130.      h_hitsx->SetStats(0);
  131.      if (i == chXstart)
  132.       h_hitsx->Draw();
  133.      else {
  134.       h_hitsx->SetLineColor(i+1);
  135.       h_hitsx->Draw("same");
  136.      }
  137.    }
  138.    //sprintf(fullname, "ps/%s_Yshare.eps", filename);
  139.          //canvas4->SaveAs(fullname);
  140.    
  141.    TCanvas *canvas5 = new TCanvas("canvas5","canvas5",500,500);
  142.    canvas5->cd();
  143.    for(int i=chYstart; i<=chYend; i++) {
  144.      TH1F* h_hitsy;
  145.      char hname[128];
  146.      int chPosition = map[chXstart][i];
  147.      sprintf(hname, "hnhitsy%d", chPosition);
  148.      h_hitsy = (TH1F*)rootfile->Get(hname);
  149.      h_hitsy->SetTitle("Scan Y;y [mm]; Entries");
  150.      h_hitsy->GetYaxis()->SetTitleOffset(1.3);
  151.      h_hitsy->SetStats(0);
  152.      if (i == chYstart)
  153.       h_hitsy->Draw();
  154.      else {
  155.       h_hitsy->SetLineColor(i+1);
  156.       h_hitsy->Draw("same");
  157.      }
  158.    }
  159.    //sprintf(fullname, "ps/%s_Yshare.eps", filename);
  160.          //canvas5->SaveAs(fullname);
  161.   }
  162.  
  163.   /** Draws the signal from 8 channels in x-row
  164.     * for one specific y bin, so the background and cross-talk
  165.     * can be estimated.
  166.     * Draws also a 2d scan of these channels.
  167.     */
  168.   if (strstr(plopt, "line") != NULL) {
  169.     TCanvas* canvas6 = new TCanvas("canvas6","canvas6",500,500);
  170.     canvas6->cd(0);
  171.     gStyle->SetOptStat(0);
  172.    
  173.     TH2F* h0 = (TH2F*) rootfile->Get("h2d0");
  174.           Int_t binsX = h0->GetXaxis()->GetNbins();
  175.     Int_t minX  = h0->GetXaxis()->GetFirst();
  176.     Int_t maxX  = h0->GetXaxis()->GetLast()+1;
  177.     Int_t binsY = h0->GetYaxis()->GetNbins();
  178.     Int_t minY  = h0->GetYaxis()->GetFirst();
  179.     Int_t maxY  = h0->GetYaxis()->GetLast()+1;
  180.     Double_t xLowUser  = h0->GetXaxis()->GetBinLowEdge(minX);
  181.     Double_t xUpUser = h0->GetXaxis()->GetBinUpEdge(maxX);
  182.     Double_t yLowUser  = h0->GetYaxis()->GetBinLowEdge(minY);
  183.     Double_t yUpUser = h0->GetYaxis()->GetBinUpEdge(maxY);
  184.     if (debug) printf("xLow %f xUp %f\n",xLowUser,xUpUser);
  185.    
  186.     //! 1-dimension position in x vs. hits
  187.     TH2F* h[8];
  188.     TH1F* h_line[8];
  189.     for(int j=0; j<8; j++) {
  190.       h_line[j] = new TH1F("h_line", "h_line", binsX, xLowUser, xUpUser);
  191.     }
  192.    
  193.     for(int j=chXstart; j<=chXend; j++) {
  194.       int chPosition = map[j][chYstart];
  195.       char hname[128];
  196.       sprintf(hname, "h2d%d", chPosition);  
  197.       int histogram = j;
  198.       h[histogram] = (TH2F *) rootfile->Get(hname);
  199.       int noise = getNoise(h[histogram], 1, 160);
  200.       for(int k=minX; k<=maxX; k++) {
  201.         int l=chYstart*20+12;
  202.         //for(int l=12; l<=16; l++) {
  203.           double signal = h[histogram]->GetBinContent(k,l);
  204.           //signal -= noise;
  205.           //signal /= 5*10000.0;
  206.           double eta = -log(1 - signal);
  207.           double x = k*(xUpUser-xLowUser)/double(binsX);
  208.           //double y = l*(yUpUser-yLowUser)/double(binsY);
  209.           h_line[j]->Fill(x-c_xOffset, signal);
  210.         //}
  211.       }
  212.       if (j == chXstart) {
  213.         h_line[j]->SetTitle("SiPM#2 w/o noise subtraction;x[mm];Hits");
  214.         //h_line[j]->GetYaxis()->SetRangeUser(-0.05, 0.3);
  215.         //h_line[j]->GetYaxis()->SetRangeUser(-50, 2500);
  216.         h_line[j]->Draw("");
  217.       }
  218.       else {
  219.         h_line[j]->SetLineColor(j+1);
  220.         h_line[j]->Draw("same");
  221.       }
  222.     }
  223.    
  224.    
  225.     //! 2d scan
  226.     TCanvas* canvas61 = new TCanvas("canvas61","canvas61",8*200,300);
  227.     canvas61->cd();
  228.     TH2F* h_corrected = new TH2F("h_corrected","h_corrected",binsX,xLowUser,xUpUser, binsY,yLowUser,yUpUser);
  229.     for(int i=chXstart; i<=chXend; i++) {
  230.       //int canvasPosition = nX*(i-chYstart)+(j-chXstart)+1;
  231.       //int canvasPosition = nX*(chYend-i)+chXstart+1;
  232.       //if (debug) printf("canvas %d\n",canvasPosition);
  233.         int chPosition = map[i][chYstart];
  234.         char hname[128];
  235.               sprintf(hname, "h2d%d", chPosition);  
  236.               int histogram = i;
  237.               h[histogram] = (TH2F *) rootfile->Get(hname);
  238.               int noise = getNoise(h[histogram], 1, 100);
  239.              
  240.               for(int k=minX; k<=maxX; k++) {
  241.           for(int l=minY; l<=maxY; l++) {
  242.             int signal = h[histogram]->GetBinContent(k,l); // detected
  243.             //p /= 10000.;
  244.             //double p0 = 1.0 - p; // events with zero photons
  245.             //double eta = (-log(p0) * p0 ) / (1-p0-0.00001);
  246.             //double eta = -log(p0);
  247.             //printf("p=%f p0=%f log(p0)=%f eta=%f\n",p,p0,log(p0),eta);      
  248.             //double signal = ((p - noise) > 0.1) ? (p-noise) : 0.1;
  249.             double p = signal - noise;
  250.             p /= 10000.0;
  251.             double eta = -log(1 - p);
  252.             double x = k*(xUpUser-xLowUser)/double(binsX);
  253.             double y = l*(yUpUser-yLowUser)/double(binsY);
  254.             h_corrected->Fill(x-c_xOffset, y-c_yOffset, eta);
  255.           }
  256.         }
  257.     }
  258.     h_corrected->SetTitle("SiPM#2 n_pe = - ln(P0);x[mm];y[mm]");
  259.     h_corrected->GetZaxis()->SetRangeUser(-0.05,0.30);
  260.     h_corrected->Draw("colz");
  261.    
  262.   }
  263.  
  264.   /** Draws the sum of the channels
  265.     * Each channel is a 2d plot
  266.     * Intended for the study of 1 channel
  267.     */
  268.   if (strstr(plopt, "2d") != NULL) {
  269.    
  270.     int nX = chXend - chXstart + 1;
  271.     int nY = chYend - chYstart + 1;
  272.     TCanvas* canvas7 = new TCanvas("canvas7","canvas7", nX*400,nY*400);
  273.     printf("nx %d ny %d\n",nX,nY);
  274.     canvas7->Divide(nX,nY);
  275.     for(int i=chYstart; i<=chYend; i++) {
  276.       for(int j=chXstart; j<=chXend; j++) {
  277.       //int canvasPosition = nX*(i-chYstart)+(j-chXstart)+1;
  278.       int canvasPosition = nX*(chYend-i)+(j-chXstart)+1;
  279.       if (debug) printf("canvas %d\n",canvasPosition);
  280.         canvas7->cd(canvasPosition);
  281.         char hname[128];
  282.         int chPosition = map[j][i];
  283.         sprintf(hname, "h2d%d", chPosition);
  284.         TH2F* h_2d = (TH2F*)rootfile->Get(hname);
  285.         h_2d->Draw("colz");
  286.       } //x
  287.     }
  288.    
  289.     // Number of photoelectrons - Poissonian correction
  290.     TCanvas* canvas8 = new TCanvas("canvas8","canvas8", 1000,1000);
  291.     canvas8->cd();
  292.     gStyle->SetOptStat(0);
  293.       char hname[128];
  294.       int chPosition = map[1][2];
  295.       sprintf(hname, "h2d%d", chPosition);
  296.       TH2F* h_2d = (TH2F*)rootfile->Get(hname);
  297.  
  298.       Int_t binsX = h_2d->GetXaxis()->GetNbins();
  299.       Int_t minX  = h_2d->GetXaxis()->GetFirst();
  300.       Int_t maxX  = h_2d->GetXaxis()->GetLast()+1;
  301.       Int_t binsY = h_2d->GetYaxis()->GetNbins();
  302.       Int_t minY  = h_2d->GetYaxis()->GetFirst();
  303.       Int_t maxY  = h_2d->GetYaxis()->GetLast()+1;
  304.       Double_t xLowUser  = h_2d->GetXaxis()->GetBinLowEdge(minX);
  305.       Double_t xUpUser   = h_2d->GetXaxis()->GetBinUpEdge(maxX);
  306.       Double_t yLowUser  = h_2d->GetYaxis()->GetBinLowEdge(minY);
  307.       Double_t yUpUser =   h_2d->GetYaxis()->GetBinUpEdge(maxY);
  308.       if (debug) printf("xLow %f xUp %f\n",xLowUser,xUpUser);
  309.       TH2F* h_corrected = new TH2F("h_corrected","h_corrected",binsX, xLowUser, xUpUser, binsY, yLowUser, yUpUser);
  310.      
  311.       double noise = getNoise(h_2d, 1, 89);
  312.       if(debug) printf("Noise = %f\n", noise);
  313.       for(int k=minX; k<=maxX; k++) {
  314.         for(int j=minY; j<=maxY; j++) {
  315.           double signal = h_2d->GetBinContent(k,j); // detected
  316.           //double p = ((signal - noise) > 1) ? (signal-noise) : 1;
  317.           double p = signal - noise;
  318.           p /= 10000.;
  319.           double p0 = 1.0 - p; // events with zero photons
  320.           //double eta = (-log(p0) * p0 ) / (1-p0-0.00001);
  321.           double eta = -log(p0); // constant of the poissonian statistics
  322.           if (debug) printf("p=%f p0=%f log(p0)=%f eta=%f\n",p,p0,log(p0),eta);
  323.           double x = k*(xUpUser-xLowUser)/double(binsX);
  324.           double y = j*(yUpUser-yLowUser)/double(binsY);
  325.           h_corrected->Fill(x+3,y+8, eta);
  326.         }
  327.        }
  328.       h_corrected->SetTitle("n_pe = - ln P(0);x[mm];y[mm]");
  329.       h_corrected->GetZaxis()->SetRangeUser(-0.05,0.3);
  330.       h_corrected->Draw("colz");
  331.      
  332.       // collection efficiency
  333.       int nPoints =0;
  334.       double efficiency=0;
  335.       for (int i=18; i<=58; i++) {
  336.         for (int j=19; j<=59; j++) {
  337.           double signal = h_corrected->GetBinContent(i,j);
  338.           if(debug) printf("signal %f\n",signal);
  339.           efficiency += signal;
  340.           nPoints++;
  341.         }
  342.       }  
  343.     printf("Signal sum = %f\n   # of points = %d\n",efficiency,nPoints);
  344.   }
  345.  
  346.   /** Draws the sum of channel signals
  347.     * Each channel is a 2d ('h2d') histogram
  348.     * Suitable for 8x8 chs scan
  349.     */
  350.   if( strstr(plopt, "sum") != NULL ) {
  351.     int nX = chXend - chXstart + 1;
  352.     int nY = chYend - chYstart + 1;
  353.           TCanvas* canvas12 = new TCanvas("canvas12","c2",8*200, 8*200);
  354.           canvas12->cd();
  355.           gStyle->SetOptStat(0);
  356.  
  357.     // final histogram parameters
  358.     TH2F* h0 = (TH2F*) rootfile->Get("h2d0");
  359.           Int_t binsX = h0->GetXaxis()->GetNbins();
  360.     Int_t minX  = h0->GetXaxis()->GetFirst();
  361.     Int_t maxX  = h0->GetXaxis()->GetLast()+1;
  362.     Int_t binsY = h0->GetYaxis()->GetNbins();
  363.     Int_t minY  = h0->GetYaxis()->GetFirst();
  364.     Int_t maxY  = h0->GetYaxis()->GetLast()+1;
  365.     Double_t xLowUser  = h0->GetXaxis()->GetBinLowEdge(minX);
  366.     Double_t xUpUser = h0->GetXaxis()->GetBinUpEdge(maxX);
  367.     Double_t yLowUser  = h0->GetYaxis()->GetBinLowEdge(minY);
  368.     Double_t yUpUser = h0->GetYaxis()->GetBinUpEdge(maxY);
  369.     if (debug) printf("xLow %f xUp %f\n",xLowUser,xUpUser);
  370.     TH2F* h_corrected = new TH2F("h_corrected","h_corrected",binsX,xLowUser,xUpUser, binsY,yLowUser,yUpUser);
  371.     TH2F* h[9];
  372.    
  373.     // 2d histogram noise subtraction and poisson scaling
  374.     for(int i=chYstart; i<=chYend; i++) {
  375.       for(int j=chXstart; j<=chXend; j++) {
  376.         int chPosition = map[j][i];
  377.         char hname[128];
  378.               sprintf(hname, "h2d%d", chPosition);  
  379.               int histogram = nX*(i-chYstart)+(j-chXstart);
  380.               h[histogram] = (TH2F *) rootfile->Get(hname);
  381.               int noise = getNoise(h[histogram], 1, 170);
  382.               if (debug) printf("noise: %d\n",noise);
  383.               for(int k=minX; k<=maxX; k++) {
  384.           for(int l=minY; l<=maxY; l++) {
  385.             int signal = h[histogram]->GetBinContent(k,l); // detected
  386.             //double p = ((signal - noise) > 0.1) ? (signal-noise) : 0.1;
  387.             double p = signal - noise;
  388.             p /= 10000.;
  389.             double p0 = 1.0 - p; // events with zero photons
  390.             //double eta = (-log(p0) * p0 ) / (1-p0-0.00001);
  391.             double eta = -log(p0);
  392.             //printf("p=%f p0=%f log(p0)=%f eta=%f\n",p,p0,log(p0),eta);
  393.             double x = k*(xUpUser-xLowUser)/double(binsX);
  394.             double y = l*(yUpUser-yLowUser)/double(binsY);
  395.             h_corrected->Fill(x-c_xOffset, y-c_yOffset, signal);
  396.           }
  397.         }    
  398.      
  399.       }
  400.     }
  401.  
  402.     h_corrected->SetTitle("SiPM#2 n_p.e.;x[mm];y[mm]");
  403.     //h_corrected->GetZaxis()->SetRangeUser(-0.05,.30);
  404.     h_corrected->Draw("colz");
  405.    
  406.     TCanvas* canvas13 = new TCanvas("canvas13","canvas13",600,300);
  407.     canvas13->Divide(2);
  408.     canvas13->cd(1);
  409.     h[16]->Draw("colz");
  410.     canvas13->cd(2);
  411.     h[8]->Draw("colz");
  412.   }
  413.  
  414.   /** Draws the beam profile and fits it with error function
  415.     * on some background function
  416.     */
  417.   if (strstr(plopt, "beam") != NULL) {
  418.  
  419.   TCanvas* canvas9 = new TCanvas("canvas9","canvas9", 500,500);
  420.   canvas9->cd();
  421.   char hname[128];
  422.   sprintf(hname, "hnhitsx%d", 36);
  423.   TH1F* h_laser = (TH1F*)rootfile->Get(hname);
  424.   h_laser->Draw();
  425.   h_laser->SetStats(1);
  426.  
  427.   TF1* err = new TF1("err","[0]+[1]*TMath::Erf((x-[2])/[3])",17.2,17.50);
  428.   err->SetParameter(0,2500);
  429.   err->SetParameter(1, h_laser->GetMaximum());
  430.   err->SetParameter(2, h_laser->GetBinCenter(h_laser->GetMaximumBin()));
  431.   err->SetParameter(3, 0.001);
  432.   h_laser->Fit(err,"qr");
  433.   h_laser->Fit(err,"lr");
  434.   double sigma = err->GetParameter(3);
  435.   printf("sigma = %2.0f um,     FWHM = %2.0f um\n", sigma*1000, 2.35*sigma*1000);
  436.   }
  437.  
  438.   if (strstr(plopt, "map") != NULL) {
  439.     for (int i=7; i>=0; i--) {
  440.       //for (int j=7; j>=0; j--) printf("(%d, %d) ", j,i);
  441.       for (int j=7; j>=0; j--) printf("%2d ", map[j][i]);
  442.       printf("\n");
  443.     }
  444.      
  445.   }
  446.    
  447.   return(0);
  448. }
  449.  
  450. /** Function calculates the noise from one channel
  451.   * it runs through the bins along x and returns the average value
  452.   */
  453. double getNoise(TH2F* histogram, int yStart, int yEnd)
  454. {
  455.   double noise=0;
  456.   int count=0;
  457.   for(int j=yStart; j<yEnd; j++) {
  458.     double value = histogram->GetBinContent(j,2);
  459.     //if (noise < value) noise = value;
  460.     noise += value;
  461.     count++;
  462.     }
  463.   return (noise/double(count));
  464. }
  465.  
  466.