//
//   Check the calorimter calibrations - Emiliano Mocchiutti
//
//   FCaloCHKCALIB.c      version 1.01  (2006-03-02)
//
//   The only input needed is the path to the directory created by YODA for the data file you want to analyze.
//
//   Changelog:
//
//   1.00 - 1.01 (2006-03-02): Works on YODA v6 output (single file), does not require anymore calocommon package.
//
//   0.00 - 1.00 (2006-03-02): Clone of CaloCHKCALIB.c
//
#include <iostream>
#include <fstream>
//
#include <TTree.h>
#include <TObject.h>
#include <TString.h>
#include <TFile.h>
#include <TCanvas.h>
#include <TH1.h>
#include <TPolyLine.h>
#include <TH1F.h>
#include <TH2D.h>
#include <TLatex.h>
#include <TPad.h>
#include <TPaveLabel.h>
#include <TStyle.h>
//
#include <event/CalibCalPedEvent.h>
//
using namespace std;
//
void stringcopy(TString& s1, const TString& s2, Int_t from=0, Int_t to=0){    
    if ( to == 0 ){
	Int_t t2length = s2.Length();
	s1 = "";
	to = t2length;
    };
    for (Int_t i = from; i<to; i++){
	s1.Append(s2[i],1);
    };
}
void stringappend(TString& s1, const TString& s2){    
    Int_t t2length = s2.Length();
    for (Int_t i = 0; i<t2length; i++){
	s1.Append(s2[i],1);
    };
}

TString getFilename(const TString filename){
    const string fil = (const char*)filename;
    Int_t posiz = fil.find("dw_");
    if ( posiz == -1 ) posiz = fil.find("DW_");
    if ( posiz == -1 ) return 0;
    Int_t posiz2 = posiz+13;
    TString file2;
    stringcopy(file2,filename,posiz,posiz2);
    TString pdat(".dat");
    stringappend(file2,pdat);
    return file2;
}

typedef struct Calib {
    Int_t iev;
    Int_t cstwerr[4];
    Float_t cperror[4];
    Float_t mip[2][22][96];
    Float_t calped[2][22][96];
    Float_t calgood[2][22][96];
    Float_t calthr[2][22][6];
    Float_t calrms[2][22][96];
    Float_t calbase[2][22][6];
    Float_t calvar[2][22][6];
    Float_t calpuls[2][22][96];
} calib;

void FCaloCHKCALIB(TString filename, Long64_t calibnumber = 0, TString outDir = "", Int_t figmatra = 0, TString saveas = "ps"){
    gStyle->SetPaperSize(19.,25.);
    //
    Float_t ccalrmsthr=0.99;
    Float_t ccalpedthr=0.99;
    Float_t ccalbadthr=0.005;
    Float_t ccalthrthr=0.98;
    Float_t ccalvarthr=0.99;
    Float_t ccalbasthr=0.99;
    //
    Float_t ccalrms;
    Float_t ccalped;
    Float_t ccalbad;
    Float_t ccalthr;
    Float_t ccalvar;
    Float_t ccalbas;
    //
    struct Calib calib;
    stringstream titolo;
    stringstream xviewev;
    stringstream yviewev;
    const char* startingdir = gSystem->WorkingDirectory();
    if ( !strcmp(outDir.Data(),"") ) outDir = startingdir;
    TString fififile =  getFilename(filename);
    const char *file;
    file = fififile;
    //
    ifstream myfile;
    myfile.open(filename.Data());
    if ( !myfile ){
	printf(" No such file, exiting...\n");
	return;
    };
    myfile.close();
    //
    TFile *File = new TFile(filename.Data());
    //
    TTree *tr = (TTree*)File->Get("CalibCalPed");
    pamela::CalibCalPedEvent *ce = 0;
    //
    UInt_t found;
    tr->SetBranchStatus("*",0,&found); //disable all branches
    //
    found = 0;
    tr->SetBranchStatus("iev*",1,&found);
    found = 0;
    tr->SetBranchStatus("cstwerr*",1,&found);
    //    printf("enabled %i branches \n",found);
    found = 0;
    tr->SetBranchStatus("cperror*",1,&found);
    //printf("enabled %i branches \n",found);
    found = 0;
    tr->SetBranchStatus("cal*",1,&found);
    //printf("enabled %i branches \n",found);
    found = 0;
    tr->SetBranchStatus("CalibCalPed*",1,&found);
    //printf("enabled %i branches \n",found);
    //
    tr->SetBranchAddress("CalibCalPed", &ce);
    //
    Long64_t ncalibs    = tr->GetEntries();
    if ( ncalibs == 0 ){
	printf(" No calibrations in this files! \n Exiting... \n");
	return;
    };
    printf("\n This file contains %i entries which corrispond to %i calibrations \n\n",(int)ncalibs,(int)ncalibs/4);
    Long64_t minev = 0;
    Long64_t maxev = ncalibs;
    if ( calibnumber ){
	minev = (calibnumber - 1)* 4;
	maxev = minev + 4;
    };
    TCanvas *figura1;
    TCanvas *figura2 = 0;
    TCanvas *figura3;
    TCanvas *rapporto = 0;
    Int_t cmask = 127 ;
    Int_t cestw = 0;
    Int_t ver[4][23];
    //
    for (Int_t k = 0; k < 4; k++ ){
	for (Int_t m = 0; m < 23 ; m++ ){
	    ver[k][m] = 0 ;
	};
    };
    //
    //
    const string fil = (const char*)filename;
    Int_t posiz = fil.find("dw_");
    if ( posiz == -1 ) posiz = fil.find("DW_");
    Int_t posiz2 = posiz+13;
    TString file2;
    stringcopy(file2,filename,posiz,posiz2);
    //
    const char *figrec = file2;
    const char *outdir = outDir;
    const char *format = saveas;
    stringstream figsave;    
    stringstream figsave1;    
    stringstream figsave2;    
    //
    // to check or not to check? this is the problem...
    //
    Bool_t check = false;
    //
    for (Int_t ci = minev; ci < maxev ; ci+=4){
	//
	Int_t incalrms = 0;
	Int_t outcalrms = 0;
	Int_t totcalbad = 0;
	Int_t incalped = 0;
	Int_t outcalped = 0;
	Int_t incalthr = 0;
	Int_t outcalthr = 0;
	Int_t incalvar = 0;
	Int_t outcalvar = 0;
	Int_t incalbas = 0;
	Int_t outcalbas = 0;
	//
	for ( Int_t s=0 ; s<4 ;s++  ){
	    tr->GetEntry(ci+s);
	    calib.iev = ce->iev;
	    //
	    //
	    //
	    cestw = 0;
	    if ( ce->cstwerr[s] ){
		cestw =  ce->cstwerr[s] & cmask ;
		ver[s][16]++;
	    };
	    if ( cestw ){
		if ( cestw & (1 << 0) ) ver[s][6]++ ;
		if ( cestw & (1 << 1) ) ver[s][5]++ ;
		if ( cestw & (1 << 2) )	ver[s][4]++ ;
		if ( cestw & (1 << 3) ) ver[s][3]++ ;
		if ( cestw & (1 << 4) ) ver[s][2]++ ;
		if ( cestw & (1 << 5) ) ver[s][1]++ ;
		if ( cestw & (1 << 6) ) ver[s][0]++ ;		
	    };
	    if ( ce->cperror[s] != 0. ){
		if (ce->cperror[s] == 128) ver[s][7]++ ; 
		if (ce->cperror[s] == 129) ver[s][8]++ ;
		if (ce->cperror[s] == 130) {
		    ver[s][9]++ ;
		    ver[s][16]--;
		};
		if (ce->cperror[s] == 132) ver[s][11]++ ;
		if (ce->cperror[s] == 140) ver[s][19]++ ; 
		if (ce->cperror[s] == 141) ver[s][20]++ ; 
		if (ce->cperror[s] == 142) ver[s][22]++ ; 
	    };
	    //
	    for ( Int_t d=0 ; d<11 ;d++  ){
		Int_t pre = -1;
		for ( Int_t j=0; j<96 ;j++){
		    if ( j%16 == 0 ) pre++;
		    if ( s == 2 ){
			calib.calped[0][2*d+1][j] = ce->calped[3][d][j];
			calib.cstwerr[3] = ce->cstwerr[3];
			calib.cperror[3] = ce->cperror[3];
			calib.calgood[0][2*d+1][j] = ce->calgood[3][d][j];
			calib.calthr[0][2*d+1][pre] = ce->calthr[3][d][pre];
			calib.calrms[0][2*d+1][j] = ce->calrms[3][d][j];
			calib.calbase[0][2*d+1][pre] = ce->calbase[3][d][pre];
			calib.calvar[0][2*d+1][pre] = ce->calvar[3][d][pre];
		    };
		    if ( s == 3 ){
			calib.calped[0][2*d][j] = ce->calped[1][d][j];
			calib.cstwerr[1] = ce->cstwerr[1];
			calib.cperror[1] = ce->cperror[1];
			calib.calgood[0][2*d][j] = ce->calgood[1][d][j];
			calib.calthr[0][2*d][pre] = ce->calthr[1][d][pre];
			calib.calrms[0][2*d][j] = ce->calrms[1][d][j];
			calib.calbase[0][2*d][pre] = ce->calbase[1][d][pre];
			calib.calvar[0][2*d][pre] = ce->calvar[1][d][pre];
		    };
		    if ( s == 0 ){
			calib.calped[1][2*d][j] = ce->calped[0][d][j];
			calib.cstwerr[0] = ce->cstwerr[0];
			calib.cperror[0] = ce->cperror[0];
			calib.calgood[1][2*d][j] = ce->calgood[0][d][j];
			calib.calthr[1][2*d][pre] = ce->calthr[0][d][pre];
			calib.calrms[1][2*d][j] = ce->calrms[0][d][j];
			calib.calbase[1][2*d][pre] = ce->calbase[0][d][pre];
			calib.calvar[1][2*d][pre] = ce->calvar[0][d][pre];
		    };
		    if ( s == 1 ){
			calib.calped[1][2*d+1][j] = ce->calped[2][d][j];
			calib.cstwerr[2] = ce->cstwerr[2];
			calib.cperror[2] = ce->cperror[2];
			calib.calgood[1][2*d+1][j] = ce->calgood[2][d][j];
			calib.calthr[1][2*d+1][pre] = ce->calthr[2][d][pre];
			calib.calrms[1][2*d+1][j] = ce->calrms[2][d][j];
			calib.calbase[1][2*d+1][pre] = ce->calbase[2][d][pre];
			calib.calvar[1][2*d+1][pre] = ce->calvar[2][d][pre];
		    };
		};
	    };
	};
	//
	// Book the histograms:
	//
	//
	Int_t i = (ci-minev)/4;
	xviewev.str("");
	xviewev << "x-view event " << (i+1);
	yviewev.str("");
	yviewev << "y-view event " << (i+1);
	TH2F *Xview = new TH2F(xviewev.str().c_str(),"",96,-0.5,95.5,22,-0.5,21.5);
	TH2F *Yview = new TH2F(yviewev.str().c_str(),"",96,-0.5,95.5,22,-0.5,21.5);	
	//
	// figures:
	//
	gDirectory->Delete("C14");
	gDirectory->Delete("C15");
	gDirectory->Delete("C16");
	TH1F *calped = new TH1F("C14","calped",4230,-3.5,4227.5);
	TH1F *calrms = new TH1F("C15","calrms",4230,-3.5,4228.5);
	TH1F *calbad = new TH1F("C16","calgood",4230,-3.5,4228.5);
	//
	gDirectory->Delete("C17");
	gDirectory->Delete("C18");
	gDirectory->Delete("C19");
	TH1F *calthr = new TH1F("C17","calthr",271,-4.5,268.5);
	TH1F *calvar = new TH1F("C18","calvar",271,-4.5,268.5);
	TH1F *calbase = new TH1F("C19","calbase",271,-4.5,268.5);
	//
	Int_t bgcolor = 10;
	TPad *pd1 = 0;
	TPad *pd2 = 0;
	TPad *palette = 0;
	TLatex *t=new TLatex();
	if ( figmatra ){
	    figura2 = new TCanvas("Calorimeter:_strip_RMS", "Calorimeter:_strip_RMS", 750, 650);
	    figura2->SetFillColor(10);
	    figura2->Range(0,0,100,100);
	    bgcolor = 10;
	    pd1 = new TPad("pd1","This is pad1",0.02,0.05,0.88,0.49,bgcolor);
	    pd2 = new TPad("pd2","This is pad2",0.02,0.51,0.88,0.95,bgcolor);
	    palette = new TPad("palette","This is palette",0.90,0.05,0.98,0.90,bgcolor);
	    figura2->cd();
	    gStyle->SetOptStat("");
	    t=new TLatex();
	    t->SetTextFont(32);
	    t->SetTextColor(1);
	    t->SetTextSize(0.03);
	    t->SetTextAlign(12);
	    t->DrawLatex(90.,92.5,"ADC ch.");
	    pd1->Range(0,0,100,100);
	    pd2->Range(0,0,100,100);
	    palette->Range(0,0,100,100);
	    pd1->SetTicks();
	    pd2->SetTicks();
	    pd1->Draw();
	    pd2->Draw();
	    palette->Draw();
	    palette->cd();
	    // palette
	    TPaveLabel *box1 = new TPaveLabel(2,2,98,14,"OFF","");
	    box1->SetTextFont(32);
	    box1->SetTextColor(1);	
	    box1->SetTextSize(0.25);	
	    box1->SetFillColor(10);
	    box1->Draw();
	    TPaveLabel *box2 = new TPaveLabel(2,16,98,28,"0-1.5","");
	    box2->SetTextFont(32);
	    box2->SetTextColor(1);		
	    box2->SetTextSize(0.25);	
	    box2->SetFillColor(38);
	    box2->Draw();
	    TPaveLabel *box3 = new TPaveLabel(2,30,98,42,"1.5-4","");
	    box3->SetTextFont(32);
	    box3->SetTextColor(1);	
	    box3->SetTextSize(0.25);	
	    box3->SetFillColor(4);
	    box3->Draw();
	    TPaveLabel *box4 = new TPaveLabel(2,44,98,56,"4-6.5","");
	    box4->SetTextFont(32);
	    box4->SetTextColor(1);	
	    box4->SetTextSize(0.25);	
	    box4->SetFillColor(3);
	    box4->Draw();
	    TPaveLabel *box5 = new TPaveLabel(2,58,98,70,"6.5-11.5","");
	    box5->SetTextFont(32);
	    box5->SetTextColor(1);	
	    box5->SetTextSize(0.25);	
	    box5->SetFillColor(2);
	    box5->Draw();
	    TPaveLabel *box6 = new TPaveLabel(2,72,98,84,">11.5","");
	    box6->SetTextFont(32);
	    box6->SetTextColor(1);	
	    box6->SetTextSize(0.25);	
	    box6->SetFillColor(6);
	    box6->Draw();
	    TPaveLabel *box7 = new TPaveLabel(2,86,98,98,"BAD","");
	    box7->SetTextFont(32);
	    box7->SetTextColor(10);	
	    box7->SetTextSize(0.25);	
	    box7->SetFillColor(1);
	    box7->Draw();
	    //
	    pd1->cd();	   
	    gStyle->SetOptStat("");
	    Xview->SetXTitle("strip");
	    Xview->SetYTitle("X - plane");
	    Xview->GetYaxis()->SetTitleOffset(0.5);
	    Xview->SetFillColor(bgcolor);	    
	    Xview->Fill(1.,1.,1.);	    
	    Xview->Draw("box");
	    pd1->Update();
	    pd2->cd();
	    gStyle->SetOptStat("");
	    Yview->SetXTitle("strip");
	    Yview->SetYTitle("Y - plane");
	    Yview->GetYaxis()->SetTitleOffset(0.5);	
	    Yview->SetFillColor(bgcolor);	    
	    Yview->Fill(1.,1.,1.);	    
	    Yview->Draw("box");
	    pd2->Update();
	};
	//
	// run over views and planes
	//
	Int_t j = 0;
	Int_t g = 0;
	gStyle->SetOptStat("");
	for (Int_t m = 0; m < 22; m++){
	    for (Int_t l = 0; l < 2; l++){
		for (Int_t n = 0; n < 96; n++){	
		    //
		    calped->Fill((float)j,calib.calped[l][m][n]);		    		    
		    if ( (calib.calped[l][m][n] > 700. || calib.calped[l][m][n] < -700.) && (j < 4032 || j > 4048) ){
			outcalped++;
		    } else {
			incalped++;
		    };
		    calrms->Fill((float)j,(calib.calrms[l][m][n]/4.));		    		    
		    if ( (calib.calrms[l][m][n]/4.) > 7. || (calib.calrms[l][m][n]/4.) < 1. ){
			outcalrms++;
		    } else {
			incalrms++;
		    };
		    calbad->Fill((float)j,(float)calib.calgood[l][m][n]);		    		    
		    if ( calib.calgood[l][m][n] ) totcalbad++;
		    //
		    if ( n < 6 ){			
			calthr->Fill((float)g,(float)calib.calthr[l][m][n]);		    		    			
			if ( calib.calthr[l][m][n] > 21. || calib.calthr[l][m][n] < 12. ){
			    outcalthr++;
			} else {
			    incalthr++;
			};
			calvar->Fill((float)g,(float)calib.calvar[l][m][n]);		    		    			
			if ( calib.calvar[l][m][n] > 8. || calib.calvar[l][m][n] < 1. ){
			    outcalvar++;
			} else {
			    incalvar++;
			};
			calbase->Fill((float)g,(float)calib.calbase[l][m][n]);		    		    			
			if ( calib.calbase[l][m][n] > 4500. || calib.calbase[l][m][n] < 2000. ){
			    outcalbas++;
			} else {
			    incalbas++;
			};
			g++;
		    };
		    //
		    j++;
		    //
		    if ( figmatra ){			
			figura2->cd();
			xviewev.str("");
			xviewev << "x-view " << i;
			xviewev << " event " << n;
			xviewev << " " << m;
			xviewev << " " << l;
			yviewev.str("");
			yviewev << "y-view " << i;
			yviewev << " event " << n;
			yviewev << " " << m;
			yviewev << " " << l;
			TH2F *Xview = new TH2F(xviewev.str().c_str(),"",96,-0.5,95.5,22,-0.5,21.5);
			TH2F *Yview = new TH2F(yviewev.str().c_str(),"",96,-0.5,95.5,22,-0.5,21.5);			   
			if ( calib.calrms[l][m][n] > 0 ){
			    Xview->SetFillColor(38);
			    Yview->SetFillColor(38);
			};
			if ( calib.calrms[l][m][n] > 6 ){
			    Xview->SetFillColor(4);
			    Yview->SetFillColor(4);
			};
			if ( calib.calrms[l][m][n] > 16 ){
			    Xview->SetFillColor(3);
			    Yview->SetFillColor(3);
			};
			if ( calib.calrms[l][m][n] > 26 ){
			    Xview->SetFillColor(2);
			    Yview->SetFillColor(2);
			};
			if ( calib.calrms[l][m][n] > 46 ){
			    Xview->SetFillColor(6);
			    Yview->SetFillColor(6);
			};
			if ( calib.calrms[l][m][n] <= 0 || calib.calrms[l][m][n] >30000 || calib.calped[l][m][n]>25000){
			    Xview->SetFillColor(10);
			    Yview->SetFillColor(10);
			};
			if ( calib.calgood[l][m][n] != 0 ){
			    Xview->SetFillColor(1);
			    Yview->SetFillColor(1);
			};
			if ( l == 0 ) {
			    Xview->Fill(n,m,1.);
			    pd1->cd();				    
			    Xview->Draw("box same");
			};				    
			if ( l == 1 ) {
			    Yview->Fill(n,m,1.);
			    pd2->cd();
			    Yview->Draw("box same");
			};		    
		    };
		};
	    };
	};
	if ( figmatra ){
	    figura2->cd();
	    gStyle->SetOptStat("");
	    gStyle->SetOptDate(0);
	    t=new TLatex();
	    t->SetTextFont(32);
	    t->SetTextColor(1);
	    t->SetTextSize(0.03);
	    t->SetTextAlign(12);
	    titolo.str("");
	    titolo << "C13 - Calorimeter: strip RMS - file ";
	    titolo << file;
	    titolo << " - calibration number ";
	    titolo << (i+1);
	    t->DrawLatex(0.5,97.,titolo.str().c_str());
	    pd1->Update();
	    pd2->Update();
	    figura2->Update();
	};
	//
	figura1 = new TCanvas("Calorimeter_calped_calrms_calgood", "Calorimeter_calped_calrms_calgood", 750, 950);
	figura1->SetFillColor(10);
	figura1->Range(0,0,100,100);
	//
	ccalped = (float)incalped/((float)outcalped + (float)incalped);
	Int_t f1pd1col = 10;
	if ( ccalped < ccalpedthr ) { 
	    check = true;	
	    f1pd1col = 45;
	};
	//
	ccalrms = (float)incalrms/((float)outcalrms + (float)incalrms);
	Int_t f1pd2col = 10;
	if ( ccalrms < ccalrmsthr ) { 
	    check = true;	
	    f1pd2col = 45;
	};
	//
	ccalbad = (float)totcalbad/4224.;
	Int_t f1pd3col = 10;
	if ( ccalbad > ccalbadthr ) { 
	    check = true;	
	    f1pd3col = 45;
	};
	//
	TPad *f1pd1 = new TPad("f1pd1","This is f1pad1",0.02,0.684,0.98,0.95,f1pd1col);
	TPad *f1pd2 = new TPad("f1pd2","This is f1pad2",0.02,0.367,0.98,0.634,f1pd2col);
	TPad *f1pd3 = new TPad("f1pd3","This is f1pad3",0.02,0.05,0.98,0.317,f1pd3col);
	figura1->Clear();
	figura1->cd();
	f1pd1->SetTicks();
	f1pd2->SetTicks();
	f1pd3->SetTicks();
	f1pd1->Draw();
	f1pd2->Draw();
	f1pd3->Draw();
	figura1->Draw();
	figura3 = new TCanvas("Calorimeter_calthr_calvar_calbase", "Calorimeter_calthr_calvar_calbase", 750, 950);
	figura3->SetFillColor(10);
	figura3->Range(0,0,100,100);
	//
	ccalthr = (float)incalthr/((float)outcalthr + (float)incalthr);
	Int_t f3pd1col = 10;
	if ( ccalthr < ccalthrthr ) { 
	    check = true;	
	    f3pd1col = 45;
	};
	//
	ccalvar = (float)incalvar/((float)outcalvar + (float)incalvar);
	Int_t f3pd2col = 10;
	if ( ccalvar < ccalvarthr ) { 
	    check = true;	
	    f3pd2col = 45;
	};
	//
	ccalbas = (float)incalbas/((float)outcalbas + (float)incalbas);
	Int_t f3pd3col = 10;
	if ( ccalbas < ccalbasthr ) { 
	    check = true;	
	    f3pd3col = 45;
	};
	//
	TPad *f3pd1 = new TPad("f3pd1","This is f3pad1",0.02,0.684,0.98,0.95,f3pd1col);
	TPad *f3pd2 = new TPad("f3pd2","This is f3pad2",0.02,0.367,0.98,0.634,f3pd2col);
	TPad *f3pd3 = new TPad("f3pd3","This is f3pad3",0.02,0.05,0.98,0.317,f3pd3col);
	figura3->Clear();
	figura3->cd();
	f3pd1->SetTicks();
	f3pd2->SetTicks();
	f3pd3->SetTicks();
	f3pd1->Draw();
	f3pd2->Draw();
	f3pd3->Draw();
	figura3->Draw();       
	//
	gStyle->SetOptStat("N");
	figura1->cd();
	gStyle->SetNdivisions(322,"x");
	t=new TLatex();
	t->SetTextFont(32);
	t->SetTextColor(1);
	t->SetTextSize(0.025);
	t->SetTextAlign(12);
	titolo.str("");
	titolo << "EXPERT - Calorimeter: calped/calrms/calgood - file ";
	titolo << file;
	titolo << " - calibration number ";
	titolo << (i+1);
	t->DrawLatex(0.5,97.,titolo.str().c_str());
	t->SetTextSize(0.03);
	f1pd1->cd();
	//
	calped->GetXaxis()->SetNdivisions(322);
	calped->SetXTitle("strip");
	calped->SetYTitle("ADC channels");
	calped->Draw();
	TPolyLine *banda1;
	Double_t xc[4] = {0.,4224.,4224.,0.};
	Double_t yc[4] = {-700.,-700.,700.,700.};
	banda1 = new TPolyLine(4,xc,yc);
	banda1->SetLineColor(5);
	banda1->SetFillColor(5);
	banda1->SetLineWidth(1);
	banda1->Draw("fSAME");
	TPolyLine *banda2;
	Double_t xc2[4] = {4031.5,4047.5,4047.5,4031.5};
	Double_t yc2[4] = {-2500.,-2500.,28000.,28000.};
	banda2 = new TPolyLine(4,xc2,yc2);
	banda2->SetLineColor(5);
	banda2->SetFillColor(5);
	banda2->SetLineWidth(1);
	banda2->Draw("fSAME");
	calped->Draw("SAME");
	f1pd2->cd();
	f1pd2->SetLogy();
	calrms->GetXaxis()->SetNdivisions(322);
	calrms->Draw();
	Double_t xd[4] = {0.,4224.,4224.,0.};
	Double_t yd[4] = {1.,1.,7.,7.};
	banda1 = new TPolyLine(4,xd,yd);
	banda1->SetLineColor(5);
	banda1->SetFillColor(5);
	banda1->SetLineWidth(1);
	banda1->Draw("fSAME");
	calrms->SetXTitle("strip");
	calrms->SetYTitle("ADC channels");
	calrms->Draw("SAME");
	f1pd3->cd();
	gStyle->SetNdivisions(344,"x");
	calbad->GetXaxis()->SetNdivisions(322);
	calbad->Draw();
	calbad->SetXTitle("strip");
	calbad->SetYTitle("0=good 255=bad");
	f1pd1->Update();
	f1pd2->Update();
	f1pd3->Update();
	figura1->Update();
	//
	figura3->cd();
	gStyle->SetNdivisions(644,"x");
	t=new TLatex();
	t->SetTextFont(32);
	t->SetTextColor(1);
	t->SetTextSize(0.025);
	t->SetTextAlign(12);
	titolo.str("");
	titolo << "EXPERT - Calorimeter: calthr/calvar/calbase - file ";
	titolo << file;
	titolo << " - calibration number ";
	titolo << (i+1);
	t->DrawLatex(0.5,97.,titolo.str().c_str());
	t->SetTextSize(0.03);
	//
	f3pd1->cd();
	calthr->GetXaxis()->SetNdivisions(644);
	calthr->SetXTitle("pre-amplifier");
	calthr->SetYTitle("ADC channels");
	calthr->Draw();
	Double_t xe[4] = {0.,264.,264.,0.};
	Double_t ye[4] = {12.,12.,21.,21.};
	banda1 = new TPolyLine(4,xe,ye);
	banda1->SetLineColor(5);
	banda1->SetFillColor(5);
	banda1->SetLineWidth(1);
	banda1->Draw("fSAME");
	calthr->Draw("SAME");
	f3pd2->cd();
	calvar->GetXaxis()->SetNdivisions(644);
	calvar->SetXTitle("pre-amplifier");
	calvar->SetYTitle("ADC channels");
	calvar->Draw();
	Double_t xt[4] = {0.,264.,264.,0.};
	Double_t yt[4] = {1.,1.,8.,8.};
	banda1 = new TPolyLine(4,xt,yt);
	banda1->SetLineColor(5);
	banda1->SetFillColor(5);
	banda1->SetLineWidth(1);
	banda1->Draw("fSAME");
	calvar->Draw("SAME");
	f3pd3->cd();
	calbase->GetXaxis()->SetNdivisions(644);
	calbase->SetXTitle("pre-amplifier");
	calbase->SetYTitle("ADC channels");
	calbase->Draw();
	Double_t xg[4] = {0.,264.,264.,0.};
	Double_t yg[4] = {2000.,2000.,4500.,4500.};
	banda1 = new TPolyLine(4,xg,yg);
	banda1->SetLineColor(5);
	banda1->SetFillColor(5);
	banda1->SetLineWidth(1);
	banda1->Draw("fSAME");
	calbase->Draw("SAME");
	f3pd1->Update();
	f3pd2->Update();
	f3pd3->Update();
	figura3->Update();
	//
	//
	//
	if ( ci == maxev-4 ) {
	    //
	    //  report sheet:
	    //
	    stringstream errore;
	    rapporto= new TCanvas("Calorimeter calibration report", "Calorimeter calibration report", 750, 950);
	    rapporto->cd();
	    rapporto->SetFillColor(10);
	    rapporto->Range(0,0,100,100);
	    t=new TLatex();
	    t->SetTextFont(32);
	    t->SetTextColor(1);
	    t->SetTextSize(0.035);
	    t->SetTextAlign(12);
	    errore.str("");
	    errore << "BASIC - C20 - Calibrations in file: " << file;
	    errore << " ";
	    t->SetTextSize(0.02);
	    t->DrawLatex(2.,99.,errore.str().c_str());
	    //
	    TPad *pad1;
	    TPad *pad2;
	    TPad *pad3;
	    TPad *pad4;
	    pad1 = new TPad("pad1","This is pad1",0.02,0.47,0.49,0.90,19);
	    pad2 = new TPad("pad2","This is pad2",0.51,0.47,0.98,0.90,19);
	    pad3 = new TPad("pad3","This is pad3",0.02,0.02,0.49,0.45,19);
	    pad4 = new TPad("pad4","This is pad4",0.51,0.02,0.98,0.45,19);
	    pad1->Range(0,0,100,100);
	    pad2->Range(0,0,100,100);
	    pad3->Range(0,0,100,100);
	    pad4->Range(0,0,100,100);
	    //
	    pad1->Draw();
	    pad2->Draw();
	    pad3->Draw();
	    pad4->Draw();
	    //
	    char *sezione = 0;
	    for (Int_t si = 0; si < 4; si++){
		if (si == 2) 
		    { 
			pad1->cd() ; 
			sezione = "** Section YE (x even) **";
		    }
		if (si == 3) 
		    {
			pad2->cd();
			sezione = "** Section YO (x odd) **";
		    }
		if (si == 0)
		    {
			pad3->cd();
			sezione = "** Section XE (y odd) **";
		    }
		if (si == 1)
		    {
			pad4->cd();
			sezione = "** Section XO (y even) **";
		    }
		t->SetTextFont(32);
		t->SetTextColor(1); 
		t->SetTextSize(0.05); 
		t->SetTextAlign(12);
		t->DrawLatex(33.,97.,sezione);
		t->SetTextSize(0.05); 
		for (Int_t j = 0; j < 23; j++){
		    if ( ver[si][j] ) {
			t->SetTextColor(50); 
			if (j == 0) {
			    errore.str("");
			    errore << "* DSP ack error: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,30.,errore.str().c_str());
			    check = true;	
			}
			if (j == 1) {
			    errore.str("");
			    errore << "* Temperature alarm: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,74.,errore.str().c_str());
			    check = true;	
			}
			if (j == 2) {
			    errore.str("");
			    errore << "* Latch up alarm: " << ver[si][j];
			    errore << " time(s).";
			    t->DrawLatex(2.,65.,errore.str().c_str());
			    check = true;	
			}
			if (j == 3) {
			    errore.str("");
			    errore << "RAW mode: " << ver[si][j];
			    errore << " time(s)";
			    t->SetTextColor(38); 
			    t->DrawLatex(2.,90.,errore.str().c_str());
			}
			if (j == 4) {
			    errore.str("");
			    errore << "* CMD length error: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,66.,errore.str().c_str());
			    check = true;	
			}	       
			if (j == 5) {
			    errore.str("");
			    errore << "* Execution error: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,62.,errore.str().c_str());
			    check = true;	
			}
			if (j == 6) {
			    errore.str("");
			    errore << "* CRC error (st. word): " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,58.,errore.str().c_str());
			    check = true;	
			}
			if (j == 7) {
			    errore.str("");
			    errore << "View or command not recognized: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,54.,errore.str().c_str());
			    check = true;	
			}
			//
			if (j == 8) {
			    errore.str("");
			    errore << "Missing section: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,50.,errore.str().c_str());
			    check = true;	
			}
			if (j == 9) {
			    errore.str("");
			    errore << "RAW MODE COMMAND: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,42.,errore.str().c_str());
			} 
			if (j == 11) {
			    errore.str("");
			    errore << "CRC error (data): " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,38.,errore.str().c_str());
			    check = true;	
			}
			if (j == 16) {
			    errore.str("");
			    errore << "Number of calibrations: " << ver[si][j];
			    t->SetTextColor(38); 
			    t->DrawLatex(2.,86.,errore.str().c_str());
			}
			if (j == 19) {
			    errore.str("");
			    errore << "Pedestal checksum wrong: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,14.,errore.str().c_str());
			    check = true;	
			}
			if (j == 20) {
			    errore.str("");
			    errore << "Thresholds checksum wrong: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,10.,errore.str().c_str());
			    check = true;	
			}
			if (j == 22) { 
			    errore.str("");
			    errore << "Packet length is zero (YODA input error), skipped: " << ver[si][j];
			    errore << " time(s)";
			    t->DrawLatex(2.,3.,errore.str().c_str());
			    check = true;	
			};
		    };
		}; 
	    };
	    rapporto->cd();
	    t->SetTextFont(32);
	    t->SetTextColor(1);
	    t->SetTextSize(0.035);
	    t->SetTextAlign(12);
	    t->DrawLatex(7.,95.,"Calorimeter CALIBRATION quick look: ");
	    //printf("vediamo: ccalped %f ccalrms %f ccalbad %f ccalthr %f ccalvar %f ccalbas %f \n",ccalped,ccalrms,ccalbad,ccalthr,ccalvar,ccalbas);
	    if ( check ) {
		t->SetTextColor(50);
		t->DrawLatex(65.,95.,"WARNING, CHECK!");		
		t->SetTextColor(1);
	    } else {
		t->SetTextColor(38);
		t->DrawLatex(65.,95.,"OK!");
		t->SetTextColor(1);
	    };     
	    rapporto->Update();
	};   
	//
	if ( !strcmp(format,"ps") ) {
	    if ( ci == minev ) {
		figsave.str("");
		figsave << outdir << "/" ;
		figsave << figrec << "_chkcalib.";
		figsave << format;
		figsave << "(";
	    };	    	    
	    if ( figmatra ) {
		figura2->Print(figsave.str().c_str(),"Portrait");
		if ( ci == minev ) {
		    figsave.str("");
		    figsave << outdir << "/" ;
		    figsave << figrec << "_chkcalib.";
		    figsave << format;
		};
	    };
	    //
	    figura1->Print(figsave.str().c_str(),"Portrait");
	    if ( ci == minev ) {
		figsave.str("");
		figsave << outdir << "/" ;
		figsave << figrec << "_chkcalib.";
		figsave << format;
	    };
	    //	    
	    figura3->Print(figsave.str().c_str(),"Portrait");
	    //
	    if ( ci == maxev-4 ) {
		figsave.str("");
		figsave << outdir << "/" ;
		figsave << figrec << "_chkcalib.";
		figsave << format;
		figsave << ")";
		rapporto->Print(figsave.str().c_str(),"Portrait");
	    };	    	    
	} else {
	    if ( figmatra ) {
		figsave.str("");
		figsave << outdir << "/" ;
		figsave << figrec << "_chkcalib1_";
		figsave << (i+1) << ".";
		figsave << format;
		figura2->SaveAs(figsave.str().c_str());
	    };
	    //
	    figsave1.str("");
	    figsave1 << outdir << "/" ;
	    figsave1 << figrec << "_chkcalib2_";
	    figsave1 << (i+1) << ".";
	    figsave1 << format;
	    figura1->SaveAs(figsave1.str().c_str());
	    //
	    figsave2.str("");
	    figsave2 << outdir << "/" ;
	    figsave2 << figrec << "_chkcalib3_";
	    figsave2 << (i+1) << ".";
	    figsave2 << format;
	    figura3->SaveAs(figsave2.str().c_str());
	    //
	    if ( ci == maxev-4 ) {
		figsave.str("");
		figsave << outdir << "/" ;
		figsave << figrec << "_chkcalib_report.";
		figsave << format;
		rapporto->SaveAs(figsave.str().c_str());
	    };	    	    
	};
    };
    printf("\n");	
    return;
}
