#include <stdio.h>
 
#include <stdlib.h>
 
#include <TROOT.h>
 
#include <TF1.h>
 
#include <TH1F.h>
 
#include <TH1D.h>
 
#include <TH2D.h>
 
#include <TCanvas.h>
 
#include <TStyle.h>
 
#include <TSystem.h>
 
#include <TFile.h>
 
#include <TDirectory.h>
 
#include <TApplication.h>
 
#include <TPaveText.h>
 
 
 
 
 
void onBrowserClose() {
 
   gApplication->Terminate(0);
 
}
 
 
 
int febreport(char *path, int run){
 
   
 
        char hname[0xFF];
 
        char name[0xFF];
 
 
 
                gROOT->SetMacroPath("../macros");
 
                gROOT->LoadMacro("slowcxml2root.cxx+");
 
                gROOT->LoadMacro("febana.cxx+");
 
                //gROOT->ProcessLine(".L H2Dload.cxx");
 
                sprintf(hname,"%s/febtest_%04d.root", path,  run);
 
                
 
 printf("File name =%s\n",hname);
 
 TFile *file0 = new TFile(hname);
 
 file0->ls();
 
 TCanvas *c[4];
 
 for (int k=0;k<4;k++){
 
   sprintf(name,"brd%d", k);
 
   c[k]= new TCanvas( name, name, 900, 600);
 
   c[k]->Divide(2,3);
 
 }
 
 
 
 sprintf(name,"slowcxml2root(\"%s/febslowc_%04d.xml\")",path, run); 
 
 printf("%s\n",name);
 
 gROOT->ProcessLine(name);
 
 TH2D *h2dp=(TH2D *) gDirectory->Get("h2d_0;1");
 
 for (int k=0;k<4;k++){
 
   sprintf(name,"vth1_%d_0",k);    //you define histogram vth1_0_0;1
 
   TH2D * h0 = (TH2D *) gDirectory->Get(name);   //this tell you directory to your histogram
 
   int brd;
 
   if (!h0) continue;
 
   sscanf(h0->GetTitle(),"%*s%*s%*s%*s sn=%d",&brd);
 
  // printf("sn = %d\n",brd);  
 
        c[k]->cd(1);
 
   hpt(k);
 
        c[k]->cd(2);
 
         //sprintf(name,"title%d",k);
 
     TPaveText *pt1 = new TPaveText(.2,.2,.7,.7);
 
         
 
     if (pt1) pt1->Draw();
 
         sprintf(name,"RUN=febtest_%04d.root", run);
 
         pt1->AddText(name);
 
         sprintf(name,"Position= FEB %d", k);
 
         pt1->AddText(name);
 
         sprintf(name,"SN=%d", brd);
 
         pt1->AddText(name);
 
        
 
 
 
   c[k]->cd(3);
 
   hplot(k);
 
   c[k]->cd(4);
 
   h0->Draw();
 
   
 
   
 
  
 
   if (h2dp) {
 
     double max = h2dp->GetMaximum();
 
     sprintf(name,"board%d", brd);
 
     TH2D *hth= new TH2D(name, "Threshold scan with Test Pulse;channel;VTH1",h2dp->GetNbinsX()/4, -0.5,143.5, h2dp->GetNbinsY() , h2dp->GetYaxis()->GetXmin(), h2dp->GetYaxis()->GetXmax() );
 
         sprintf(name,"fwhm%d", brd);
 
         TH1D *hfwhm= new TH1D(name, "FWHM;channel",h2dp->GetNbinsX()/4, -0.5,143.5 );
 
     hth->SetStats(false);
 
     for (int x=0;x<hth->GetNbinsX();x++){
 
            double fwhm=0;
 
            for (int y=0;y<hth->GetNbinsY();y++){
 
                   double val= h2dp->GetBinContent(k*144+x+1,y+1);
 
                   hth->SetBinContent(x+1, y+1, val);
 
                   if (val> max*0.5) fwhm+=1;
 
            }
 
                hfwhm->SetBinContent(x+1,fwhm);
 
     }
 
         hfwhm->SetMaximum(100);
 
         hfwhm->SetMinimum(0);
 
         
 
         c[k]->cd(5);
 
     hth->Draw("colz");
 
         c[k]->cd(6);
 
         hfwhm->Draw();
 
   } 
 
  
 
   sprintf(name,"pdf/board_%d_%04d.pdf(", brd, run);
 
   printf("%s\n",name);
 
   c[k]->Print(name,"pdf");
 
 }  
 
 
 
//printf("name=%s\n",name);
 
 
 
febana(run);
 
        
 
   // printf("arguments path=%s run=%d\n",path, run );
 
 return 0;      
 
}