#include "stdio.h"
 
#include "TROOT.h"
 
#include "TFile.h"
 
#include "TNtuple.h"
 
#include "TH1F.h"
 
#include "TH2F.h"
 
#include "TF1.h"
 
#include "TMath.h"
 
#include "TStyle.h"
 
#include "TCanvas.h"
 
#include "TLine.h"
 
#include "zlib/zlib.h"
 
 
 
int scan2d(char * fname="test", char *plopt="2d", double value=0){      
 
  
 
        char fullname[256];
 
        
 
        //get ROOT file with histograms
 
        char fnameroot[1024];
 
        TFile * rootfile;
 
        TDirectory *dir;
 
        
 
        sprintf(fnameroot, "root/%s.root", fname); 
 
        rootfile = (TFile *) gROOT->FindObject(fname); 
 
        if(rootfile==NULL) rootfile = new TFile(fnameroot);
 
        if(rootfile==NULL) {
 
          printf("Cannot open root file %s!!!\n",fnameroot); 
 
          return 0;
 
        }
 
        dir = (TDirectory*) rootfile;
 
        
 
        
 
        // set draw style
 
        gStyle->SetOptStat("ne");
 
        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(11);
 
        gStyle->SetOptStat();
 
        gStyle->SetPadRightMargin(0.15);
 
        gStyle->SetPadLeftMargin(0.12);
 
        //gStyle->SetTitleYOffset(1.4);
 
        
 
  TCanvas *c;
 
  int cc=-1;
 
  char hname[256];
 
  TH1F *hp1d; TH2F *hp2d; 
 
                
 
  c = new TCanvas("2D Scan", fname, 650, 0, 600, 1200);
 
        c ->Divide(2,4);                
 
  for (int i=0;i<8;i++){
 
                          TVirtualPad *pad=c->cd(i%4*2+i/4+1);
 
                          
 
                          if( strstr(plopt, "2d") != NULL ) {
 
                            pad->SetLogz();
 
                            pad->SetLogy(0);
 
                                  sprintf(hname, "h2d%d",i); hp2d = (TH2F *) dir->Get(hname);
 
                                  (hp2d->GetYaxis())->SetLabelOffset(0.01);
 
                                  hp2d->SetTitle("; X [mm]; Y[mm]");
 
                                  hp2d->DrawCopy("COLZ");
 
                                  sprintf(fullname, "ps/%s_2d.pdf", fname);
 
                                }
 
                                if( strstr(plopt, "adc") != NULL ) {
 
                                  pad->SetLogy(1);
 
                                  pad->SetLogz(0);
 
                                  
 
                                  sprintf(hname, "hadc%d",i); hp1d = (TH1F *) dir->Get(hname);
 
                                  if (value>0) (hp1d->GetXaxis())->SetRangeUser(0,value);
 
                                  (hp1d->GetYaxis())->SetLabelOffset(0.01);
 
                                  hp1d->SetTitle("; ADC (a.u.); N");
 
                                  hp1d->DrawCopy();
 
                                  sprintf(fullname, "ps/%s_adc.pdf", fname);
 
                                  
 
                                }  
 
  }
 
        
 
        c->SaveAs(fullname);
 
return 0;
 
}