#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
#include "TProfile.h"
#include "TRandom.h"
#include "TString.h"
#include "TTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include "TLegend.h"
#include "TPaveText.h"
#include "TText.h"
#include "TPad.h"
#include "TLegend.h

gStyle->SetOptStat(111111);
gStyle->SetOptFit(1);

using namespace std;

int efficiencies_plot(int stationiwant, TString filenameshort)
{
    
    //int nlines = 36;    
    //int points = nlines/3;

    filename = "/home/user/re4data/TDC/HVSCAN/" + filenameshort + ".csv";
    cout << filename << endl;
    // Setting plots range and style
    gStyle->SetOptStat(0.1);
    gStyle->SetMarkerSize(1);
    gStyle->SetMarkerStyle(20);
    gStyle->SetMarkerColor(1);
    int volt_bin = 100, volt_min = 7800, volt_max = 10200;
    int current_bin = 2000, current_min = 0; 
    float current_max = 0.5;
    int effi_bin = 50000, effi_min = 0, effi_max = 110, maxbins = 100;
    float temperature, humidity, pressure;
    char run_config_file[256];
    // Text positioning
    float label_font=0.025;
    float axis_offset=0.9;
    float label_size=0.03;
    string variablestring1, variablestring2, variablestring3, variablestring4, etapartition, Trigger_layer;
    
    // Defining variable already present in the dst file
    Float_t     Volt;
    Float_t     efficiency;
    Float_t     env_t;
    Float_t     env_rh;
    Float_t     env_p;
    Float_t     station;
    Float_t     curr1;
    Float_t     curr2;
    Float_t     curr3;
    Float_t     efficiencyerr;
    Float_t     noise;
    Float_t     csize;
    Char_t      trigger_mode[20], eta[10], stat[10], vfat1[8], vfat2[8], vfat3[8], vfat4[8], vfat5[8], vfat6[8], vfat7[8], vfat8[8], vfat9[8], vfat10[8], vfat11[8], vfat12[8];
    
    float i = 0;
    int j = 0;
    char  str2[80], buffer[4], datetowrite[80], titletowrite[80], titletowrite2[80], infovalue1[256], infovalue2[256], infovalue3[256], infovalue4[256], infovalue5[256], infovalue6[256], infovalue31[256], infovalue32[256], infovalue33[256], infovalue34[256], infovalue35[256], infovalue36[256], infovalue37[256], infovalue38[256], infovalue39[256];
    string line, run, date, site, trigger, name, hv1, hv2, hv3, , effi;
    
    TFile *hfile = 0;
    hfile = TFile::Open("hvscan.root","RECREATE");
    
    TTree *rpclab = new TTree("rpclab","904 RPC Lab - PLOTS");
    rpclab->Branch("Volt",&Volt,"Volt/F");
    rpclab->Branch("efficiency",&efficiency,"efficiency/F");
    rpclab->Branch("efficiencyerr",&efficiencyerr,"efficiencyerr/F");
    rpclab->Branch("env_t",&env_t,"env_t/F");
    rpclab->Branch("env_rh",&env_rh,"env_rh/F");
    rpclab->Branch("env_p",&env_p,"env_p/F");
    rpclab->Branch("station",&station,"station/F");
    rpclab->Branch("curr1",&curr1,"curr1/F");
    rpclab->Branch("curr2",&curr2,"curr2/F");
    rpclab->Branch("curr3",&curr3,"curr3/F");
    rpclab->Branch("trigger_mode",trigger_mode,"trigger_mode/C");
    rpclab->Branch("eta",eta,"eta/C");
    rpclab->Branch("noise",&noise,"noise/F");
    rpclab->Branch("csize",&csize,"csize/F");
    
    // Reading the daq info
    bool verbose_openfile = 0;
    string run_config = "/home/user/re4data/TDC/daqinfo";
    if (verbose_openfile) cout << "Run config file: " << run_config << endl;
    strcpy(run_config_file,run_config.c_str());
    
    ifstream rff(run_config_file);
    if (rff.is_open()) {
        if (verbose_openfile) cout << "Run config file: " << run_config_file << " is opened." << endl;
        
        // This is the header of the daqinfo file
        rff >> variablestring1  >> temperature >> humidity >> pressure;
        if (verbose_openfile) cout << "Temp: " << temperature << " humidity: " << humidity << " pressure: " << pressure << endl;
        
        rff >> variablestring2  >> etapartition >> variablestring3 >> Trigger_layer;
        if (verbose_openfile) cout << "Eta partition: " << etapartition << " trigger: " << Trigger_layer << endl;

    }

    //cout << " Opening " << filename << endl;
    ifstream myfile (filename);
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            // Starting Counters
            i = i + 1;
            //cout << "reading line" << i << endl;
            //if ( fmod(i,nlines) == 0) {
            //    j = j + 1;
            //    //cout << "Chamber :" << j << endl;
            //}
            
            // Reading file content
            getline (myfile,line,',');
            run = line;
            //cout << " Line = " << i << endl;//" contents= " << line << endl;
            getline (myfile,line,',');
            if (i==1) date = line;
            if (i==1) replace(date.begin(), date.end(), '/', '-');
            if (i==1) strcpy(datetowrite,date.c_str());
            
            getline (myfile,line,',');
            site = line;
            
            getline (myfile,line,',');
            if (i==1) env_t = ::atof(line.c_str());
            
            getline (myfile,line,',');
            if (i==1) env_p = ::atof(line.c_str());
            
            getline (myfile,line,',');
            if (i==1) env_rh = ::atof(line.c_str());
            
            getline (myfile,line,',');
            if (i==1) name = ::atof(line.c_str());
            if (i==1) strcpy(titletowrite2,line.c_str());
            //cout << " " << titletowrite2 << " " << endl;
            
            getline (myfile,line,',');
            strcpy(eta,line.c_str());
            
            getline (myfile,line,',');
            strcpy(trigger_mode,line.c_str());
            
            getline (myfile,line,',');
            station = ::atof(line.c_str());
            
            getline (myfile,line,',');
            Volt = ::atof(line.c_str());
            
            getline (myfile,line,',');
            hv1 = line;
            
            getline (myfile,line,',');
            hv2 = line;
            
            getline (myfile,line,',');
            hv3 = line;
            
            getline (myfile,line,',');
            curr1 = ::atof(line.c_str());;
            
            getline (myfile,line,',');
            curr2 = ::atof(line.c_str());;
            
            getline (myfile,line,',');
            curr3 = ::atof(line.c_str());;
            
            getline (myfile,line,',');
            if (i==1) strcpy(stat,line.c_str());
            
            getline (myfile,line,',');
            efficiency = ::atof(line.c_str());
            
            getline (myfile,line,',');
            efficiencyerr = ::atof(line.c_str());
            efficiencyerr= efficiencyerr;
            
            getline (myfile,line,',');
            csize = ::atof(line.c_str());
            
            getline (myfile,line,',');
            noise = ::atof(line.c_str());

            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            getline (myfile,line,',');
            
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat1,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat2,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat3,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat4,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat5,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat6,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat7,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat8,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat9,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat10,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat11,line.c_str());
            
            getline (myfile,line,',');
            if (i==1) strcpy(vfat12,line.c_str());
            
            // Taking the rest of the file
            getline (myfile,line,'\n');
            
            
            // Filling root file
            //if ( j == stationiwant) {
            rpclab->Fill();
            //cout << "station " << j << " filled(" << i << ")" << endl;
            //strcpy(titletowrite2,titletowrite);
            //}
        }
        rpclab->Write();
        myfile.close();
    }
    //cout << " Finishe reading the file." << endl;
    
    // Number of lines per detector
    if ( etapartition == "ALL" ) {
        int points = i/9;
    }
    else {
        int points = i/3;
    }
    //cout << " points = " << points << endl;

   TH1F *hnoise = new TH1F("hnoise","hnoise",1000,0,10);
    
    // Working on eta partition = A
    if ( etapartition == "ALL" || etapartition == "A") {
        cout << " processing ETA=A" << endl;

        rpclab->Draw("noise>>hnoise","trigger_mode==\"TOP\" && eta==\"A\"");
        float noiseatop = hnoise->GetMean();
        rpclab->Draw("noise>>hnoise","trigger_mode==\"BOTTOM\" && eta==\"A\"");
        float noiseabottom = hnoise->GetMean();
        rpclab->Draw("noise>>hnoise","trigger_mode==\"DOUBLE\" && eta==\"A\"");
        float noiseadouble = hnoise->GetMean();
//cout << " Noise initialized" << endl;

        // Creating Canvas
        TCanvas *Frameperf = new TCanvas("Frameperf","Chamber performance",0,0,1500,900);
        TPad *pad1 = new TPad("pad1","",0,0,1,1); //1st pad for T
        TPad *pad2 = new TPad("pad2","",0,0,1,1); //2nd pad for P
        pad2->SetFillStyle(4000); //will be transparent
        pad1->Draw();
        pad1->cd();
//cout << " Canvas initialized" << endl;

        // Pre-drawing currents
        TH2F *environ3 = new TH2F("environ3","environ3",volt_bin,volt_min,volt_max,current_bin,current_min,current_max);
        environ3->SetMarkerColor(4);
        environ3->SetMarkerStyle(28);
        environ3->SetMarkerSize(0.8);
        rpclab->Draw("curr1:Volt>>environ3","trigger_mode==\"DOUBLE\" && eta==\"A\"","");
        
        TH2F *environ4 = new TH2F("environ4","environ4",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        environ4->SetMarkerColor(4);
        environ4->SetMarkerStyle(29);
        environ4->SetMarkerSize(0.8);
        rpclab->Draw("curr2:Volt>>environ4","trigger_mode==\"DOUBLE\" && eta==\"A\"","");
        
        TH2F *environ5 = new TH2F("environ5","environ5",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        environ5->SetMarkerColor(4);
        environ5->SetMarkerStyle(31);
        environ5->SetMarkerSize(0.8);
        rpclab->Draw("curr3:Volt>>environ5","trigger_mode==\"DOUBLE\" && eta==\"A\"","");
//cout << " Current initialized" << endl;

        // PERFORMANCES
        //TPad *pad1 = new TPad("pad1","",0,0,1,1); //1st pad for T
        //TPad *pad2 = new TPad("pad2","",0,0,1,1); //2nd pad for P
        //pad2->SetFillStyle(4000); //will be transparent
        //pad1->Draw();
        //pad1->cd();
        
        TH2F *sens1 = new  TH2F("sens1","sens1",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        TH2F *sens2 = new  TH2F("sens2","sens2",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        TH2F *sens3 = new  TH2F("sens3","sens3",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        
        sens1->SetMarkerColor(1);
        sens1->SetMarkerStyle(20);
        sens1->SetMarkerSize(0.9);
        
        sens2->SetMarkerColor(2);
        sens2->SetMarkerStyle(21);
        sens2->SetMarkerSize(0.9);
        
        sens3->SetMarkerColor(3);
        sens3->SetMarkerStyle(22);
        sens3->SetMarkerSize(0.9);
//cout << " TH2F initialized, chamber: " << titletowrite2 << endl;

        strcpy(titletowrite,"Chamber ");
        strcat(titletowrite,titletowrite2);
        strcat(titletowrite," eta = A");
//cout << " Title initialized" << endl;

        sens1->SetTitle(titletowrite);
        sens1->GetXaxis()->SetTitleOffset(axis_offset);
        sens1->GetXaxis()->SetLabelSize(label_size);
        sens1->GetYaxis()->SetTitleSize(0.04);
        sens1->GetXaxis()->SetTitle("HV_{effective}");
        
        sens1->GetYaxis()->SetTitleOffset(axis_offset);
        sens1->GetYaxis()->SetLabelSize(label_size);
        sens1->GetYaxis()->SetTitleSize(0.04);
        sens1->GetYaxis()->SetTitle("Efficiency");
        gPad->SetGrid();
//cout << " Frame initialized" << endl;

        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"TOP\" && eta==\"A\"","goff");
        TGraphErrors *g1 = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g1->SetMarkerColor(1);
        g1->SetMarkerStyle(20);
        g1->SetMarkerSize(0.9);
        g1->SetLineColor(1);
        //g1->Draw("ap");

        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"BOTTOM\" && eta==\"A\"","goff");
        TGraphErrors *g2 = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g2->SetMarkerColor(2);
        g2->SetMarkerStyle(21);
        g2->SetMarkerSize(0.9);
        g2->SetLineColor(2);
        //g2->Draw("ap");
        
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"DOUBLE\" && eta==\"A\"","goff");
        TGraphErrors *g3 = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g3->SetMarkerColor(3);
        g3->SetMarkerStyle(22);
        g3->SetMarkerSize(0.9);
        g3->SetLineColor(3);
        //g3->Draw("ap");
//cout << " Draw performed" << endl;

        TF1 *sigmoid = new TF1("sigmoid"," [0]/(1 + exp( [1] * ([2] - x) ) )");
        sigmoid->SetParNames("Efficiency","Slope","HV @ 50 efficiency");
        
        sigmoid->SetParameter(0, 94);
        sigmoid->SetParLimits(0, 90, 99);
        
        sigmoid->SetParameter(1, 0.001);
        sigmoid->SetParLimits(1, 0.00001, 0.09);
        
        sigmoid->SetParameter(2, 8900);
        sigmoid->SetParLimits(2, 8700, 9400);
        
        //sigmoid->SetParameters(90.0, 0.01, 8600.0);
        sigmoid->SetLineColor(1);
cout << " Fit initialized" << endl;        
        
        // Fitting efficiency1
        g1->Fit("sigmoid","EM");
        TF1 *fit = g1->GetFunction("sigmoid");
        Double_t chi21 = fit->GetChisquare() / fit->GetNDF();
        if (chi21>999) chi21=999;
        Double_t fit_efficiency1 = fit->GetParameter(0);
        Double_t fit_efficiency_err1 = fit->GetParError(0);
        
        Double_t slope1 = fit->GetParameter(1);
        Double_t slope_err1 = fit->GetParError(1);
        
        Double_t hvat501 = fit->GetParameter(2);
        Double_t hvat50_err1 = fit->GetParError(2);
//cout << " Fit1 performed " << endl;        
        
        // Fitting efficiency2
        //sigmoid->SetParameters(90.0, 0.01, 8800.0);
        sigmoid->SetLineColor(2);
        g2->Fit("sigmoid","EM");
        TF1 *fit = g2->GetFunction("sigmoid");
        Double_t chi22 = fit->GetChisquare()  / fit->GetNDF();
        if (chi22>999) chi22=999;
        Double_t fit_efficiency2 = fit->GetParameter(0);
        Double_t fit_efficiency_err2 = fit->GetParError(0);
        
        Double_t slope2 = fit->GetParameter(1);
        Double_t slope_err2 = fit->GetParError(1);
        
        Double_t hvat502 = fit->GetParameter(2);
        Double_t hvat50_err2 = fit->GetParError(2);
//cout << " Fit2 performed " << endl;        
        
        
        // Fitting efficiency3
        sigmoid->SetParameter(0, 94);
        sigmoid->SetParLimits(0, 92.5, 99);
        sigmoid->SetLineColor(3);
        g3->Fit("sigmoid","EM");
        TF1 *fit = g3->GetFunction("sigmoid");
        Double_t chi23 = fit->GetChisquare()  / fit->GetNDF();
        if (chi23>999) chi23=999;
        Double_t fit_efficiency3 = fit->GetParameter(0);
        Double_t fit_efficiency_err3 = fit->GetParError(0);
        
        Double_t slope3 = fit->GetParameter(1);
        Double_t slope_err3 = fit->GetParError(1);
        
        Double_t hvat503 = fit->GetParameter(2);
        Double_t hvat50_err3 = fit->GetParError(2);
cout << " Fit3 performed " << endl;        
        
        
        sens1->Draw("");
        //sens2->Draw("same");
        //sens3->Draw("same");
        
        TMultiGraph  *mg = new TMultiGraph();
        mg->Add(g1);
        mg->Add(g2);
        mg->Add(g3);
        mg->Draw("ep");
//cout << " MultiGraph performed " << endl;
        
        pad1->Update();
        pad1->Modified();
        Frameperf->cd();
        
        
        
        //compute the pad range with suitable margins
        Double_t ymin = current_min;
        Double_t ymax = current_max;
        Double_t dy = (ymax-ymin)/0.8; //% margins top and bottom
        Double_t xmin = volt_min;
        Double_t xmax = volt_max;
        Double_t dx = (xmax-xmin)/0.8; //% margins left and right
        pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);
        pad2->Draw();
        pad2->cd();
//cout << " margins performed " << endl;
        
        environ3->Draw("][sames");
        environ4->Draw("][sames");
        environ5->Draw("][sames");
        pad2->Update();
        
        // draw axis on the right side of the pad
        TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
        axis->SetLabelColor(4);
        axis->SetLineColor(4);
        axis->SetTitleOffset(axis_offset);
        axis->SetLabelSize(label_size);
        axis->SetLabelColor(4);
        axis->SetTitle("Current [uA]");
        axis->SetTitleSize(0.03);
        axis->SetTitleColor(4);
        axis->Draw();
//cout << " Draw right axis " << endl;        
        
        
        // Adding text to the plot
        TPaveText *pt2 = new TPaveText(0.11,0.45,0.35,0.9,"NDC");
        pt2->AddText("Runs info:");
        snprintf(infovalue1, sizeof(infovalue1), "Temp: %3.1f, RH: %2.0f %, P: %3.0f", env_t, env_rh, env_p );
        pt2->AddText(infovalue1);  
        snprintf(infovalue2, sizeof(infovalue2), "Noise: T: %7.5f B: %7.5f D: %7.5f", noiseatop, noiseabottom, noiseadouble );
        pt2->AddText(infovalue2);
        
        snprintf(infovalue3, sizeof(infovalue3), "Stat.: %s,  #chi^{2}_{TOP} / ndf=%6.1f,  #chi^{2}_{BOTTOM} / ndf=%6.1f,  #chi^{2}_{DOUBLE} / ndf=%6.1f", stat,chi21,chi22,chi23 );
        pt2->AddText(infovalue3);
        
        snprintf(infovalue31, sizeof(infovalue31), "Efficiency_{TOP}= %5.2f #pm %4.2f %%", fit_efficiency1,fit_efficiency_err1);
        snprintf(infovalue32, sizeof(infovalue32), "Slope_{TOP}= %6.4f #pm %8.6f", slope1,slope_err1);
        snprintf(infovalue33, sizeof(infovalue33), "HV @ 50 Eff._{TOP}= %4.0f #pm %4.1f V", hvat501,hvat50_err1);
        pt2->AddText(infovalue31);
        pt2->AddText(infovalue32);
        pt2->AddText(infovalue33);
        
        snprintf(infovalue34, sizeof(infovalue34), "Efficiency_{BOTTOM}= %5.2f #pm %4.2f %%", fit_efficiency2,fit_efficiency_err2);
        snprintf(infovalue35, sizeof(infovalue35), "Slope_{BOTTOM}= %6.4f #pm %8.6f", slope2,slope_err2);
        snprintf(infovalue36, sizeof(infovalue36), "HV @ 50 Eff._{BOTTOM}= %4.0f #pm %4.1f V", hvat502,hvat50_err2);
        pt2->AddText(infovalue34);
        pt2->AddText(infovalue35);
        pt2->AddText(infovalue36);
        
        snprintf(infovalue37, sizeof(infovalue37), "Efficiency_{DOUBLE}= %5.2f #pm %4.2f %%", fit_efficiency3,fit_efficiency_err3);
        snprintf(infovalue38, sizeof(infovalue38), "Slope_{DOUBLE}= %6.4f #pm %8.6f", slope3,slope_err3);
        snprintf(infovalue39, sizeof(infovalue39), "HV @ 50 Eff._{DOUBLE}= %4.0f #pm %4.1f V", hvat503,hvat50_err3);
        pt2->AddText(infovalue37);
        pt2->AddText(infovalue38);
        pt2->AddText(infovalue39);
        
        
        snprintf(infovalue4, sizeof(infovalue4), "Thresholds A: %s %s %s %s",vfat1,vfat2,vfat3,vfat4);
        pt2->AddText(infovalue4);
        
        snprintf(infovalue5, sizeof(infovalue5), "Thresholds B: %s %s %s %s",vfat5,vfat6,vfat7,vfat8);
        pt2->AddText(infovalue5);
        
        snprintf(infovalue6, sizeof(infovalue6), "Thresholds C: %s %s %s %s",vfat9,vfat10,vfat11,vfat12);
        pt2->AddText(infovalue6);
        
        pt2->Draw("");
//cout << " Text added " << endl;        
        
        
        // Adding the legend
        leg = new TLegend(0.69,0.50,0.86,0.34);
        leg->AddEntry(sens1,"Efficiency (TOP)","lep");
        leg->AddEntry(sens2,"Efficiency (BOTTOM)","lep");
        leg->AddEntry(sens3,"Efficiency (DOUBLE)","lep");
        leg->AddEntry(environ3,"Current TN","lep");
        leg->AddEntry(environ4,"Current TW","lep");
        leg->AddEntry(environ5,"Current B","lep");
        leg->Draw("");
//cout << " Legend added " << endl;        
        
        // Saving the image file
        //TImage *img = TImage::Create();
        //img->FromPad(Frameperf);
        //img->WriteImage( "/srv/www/htdocs/dqm2/plot/" + filenameshort + "A.gif" );
        //cout << filenameshort << endl;
        //img->WriteImage(filenameshort);
        Frameperf->SaveAs( "/srv/www/htdocs/dqm2/plot/" + filenameshort + "A.gif" );



TCanvas *Frameperfcsize = new TCanvas("Frameperfcsize","Chamber performance",0,0,1500,900);
TH2F *csize1 = new  TH2F("csize1","csize1",volt_bin,8900,volt_max,100,0,5);
TH2F *csize2 = new  TH2F("csize2","csize2",volt_bin,8900,volt_max,100,0,5);
TH2F *csize3 = new  TH2F("csize3","csize3",volt_bin,8900,volt_max,100,0,5);

        csize1->SetMarkerColor(1);
        csize1->SetMarkerStyle(20);
        csize1->SetMarkerSize(0.9);
        
        csize2->SetMarkerColor(2);
        csize2->SetMarkerStyle(21);
        csize2->SetMarkerSize(0.9);
        
        csize3->SetMarkerColor(3);
        csize3->SetMarkerStyle(22);
        csize3->SetMarkerSize(0.9);

rpclab->Draw("csize:Volt>>csize1","trigger_mode==\"TOP\" && eta==\"A\"","");
rpclab->Draw("csize:Volt>>csize2","trigger_mode==\"BOTTOM\" && eta==\"A\"","");
rpclab->Draw("csize:Volt>>csize3","trigger_mode==\"DOUBLE\" && eta==\"A\"","");

csize1->Draw();
csize2->Draw("same");
csize3->Draw("same");


        csize1->SetTitle(titletowrite);
        csize1->GetXaxis()->SetTitleOffset(axis_offset);
        csize1->GetXaxis()->SetLabelSize(label_size);
        csize1->GetYaxis()->SetTitleSize(0.04);
        csize1->GetXaxis()->SetTitle("HV_{effective}");
        
        csize1->GetYaxis()->SetTitleOffset(axis_offset);
        csize1->GetYaxis()->SetLabelSize(label_size);
        csize1->GetYaxis()->SetTitleSize(0.04);
        csize1->GetYaxis()->SetTitle("Cluster Size");
        Frameperfcsize->SaveAs( "/srv/www/htdocs/dqm2/plot/csize" + filenameshort + "A.gif" );

    }

    // Working on eta partition = B
    if ( etapartition == "ALL" || etapartition == "B") {
        cout << " processing ETA=B" << endl;

        rpclab->Draw("noise>>hnoise","trigger_mode==\"TOP\" && eta==\"B\"");
        float noisebtop = hnoise->GetMean();
        rpclab->Draw("noise>>hnoise","trigger_mode==\"BOTTOM\" && eta==\"B\"");
        float noisebbottom = hnoise->GetMean();
        rpclab->Draw("noise>>hnoise","trigger_mode==\"DOUBLE\" && eta==\"B\"");
        float noisebdouble = hnoise->GetMean();

        // Creating Canvas
        TCanvas *FrameperfB = new TCanvas("FrameperfB","Chamber performance",0,0,1500,900);
        TPad *pad1B = new TPad("pad1B","",0,0,1,1); //1st pad for T
        TPad *pad2B = new TPad("pad2B","",0,0,1,1); //2nd pad for P
        pad2B->SetFillStyle(4000); //will be transparent
        pad1B->Draw();
        pad1B->cd();
        
        // Pre-drawing currents
        TH2F *environ3B = new TH2F("environ3B","environ3B",volt_bin,volt_min,volt_max,current_bin,current_min,current_max);
        environ3B->SetMarkerColor(4);
        environ3B->SetMarkerStyle(28);
        environ3B->SetMarkerSize(0.8);
        rpclab->Draw("curr1:Volt>>environ3B","trigger_mode==\"DOUBLE\" && eta==\"B\"","");
        
        TH2F *environ4B = new TH2F("environ4B","environ4B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        environ4B->SetMarkerColor(4);
        environ4B->SetMarkerStyle(29);
        environ4B->SetMarkerSize(0.8);
        rpclab->Draw("curr2:Volt>>environ4B","trigger_mode==\"DOUBLE\" && eta==\"B\"","");
        
        TH2F *environ5B = new TH2F("environ5B","environ5B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        environ5B->SetMarkerColor(4);
        environ5B->SetMarkerStyle(31);
        environ5B->SetMarkerSize(0.8);
        rpclab->Draw("curr3:Volt>>environ5B","trigger_mode==\"DOUBLE\" && eta==\"B\"","");
        
        // PERFORMANCES
        //TPad *pad1 = new TPad("pad1","",0,0,1,1); //1st pad for T
        //TPad *pad2 = new TPad("pad2","",0,0,1,1); //2nd pad for P
        //pad2->SetFillStyle(4000); //will be transparent
        //pad1->Draw();
        //pad1->cd();
        
        TH2F *sens1B = new  TH2F("sens1B","sens1B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        TH2F *sens2B = new  TH2F("sens2B","sens2B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        TH2F *sens3B = new  TH2F("sens3B","sens3B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        
        sens1B->SetMarkerColor(1);
        sens1B->SetMarkerStyle(20);
        sens1B->SetMarkerSize(0.9);
        
        sens2B->SetMarkerColor(2);
        sens2B->SetMarkerStyle(21);
        sens2B->SetMarkerSize(0.9);
        
        sens3B->SetMarkerColor(3);
        sens3B->SetMarkerStyle(22);
        sens3B->SetMarkerSize(0.9);
        
        strcpy(titletowrite,"Chamber ");
        strcat(titletowrite,titletowrite2);
        strcat(titletowrite," eta = B");
        sens1B->SetTitle(titletowrite);
        sens1B->GetXaxis()->SetTitleOffset(axis_offset);
        sens1B->GetXaxis()->SetLabelSize(label_size);
        sens1B->GetYaxis()->SetTitleSize(0.04);
        sens1B->GetXaxis()->SetTitle("HV_{effective}");
        
        sens1B->GetYaxis()->SetTitleOffset(axis_offset);
        sens1B->GetYaxis()->SetLabelSize(label_size);
        sens1B->GetYaxis()->SetTitleSize(0.04);
        sens1B->GetYaxis()->SetTitle("Efficiency");
        gPad->SetGrid();
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"TOP\" && eta==\"B\"","goff");
        TGraphErrors *g1B = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g1B->SetMarkerColor(1);
        g1B->SetMarkerStyle(20);
        g1B->SetMarkerSize(0.9);
        g1B->SetLineColor(1);
        //g1->Draw("ap");
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"BOTTOM\" && eta==\"B\"","goff");
        TGraphErrors *g2B = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g2B->SetMarkerColor(2);
        g2B->SetMarkerStyle(21);
        g2B->SetMarkerSize(0.9);
        g2B->SetLineColor(2);
        //g2->Draw("ap");
        
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"DOUBLE\" && eta==\"B\"","goff");
        TGraphErrors *g3B = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g3B->SetMarkerColor(3);
        g3B->SetMarkerStyle(22);
        g3B->SetMarkerSize(0.9);
        g3B->SetLineColor(3);
        //g3->Draw("ap");
        
        TF1 *sigmoid = new TF1("sigmoid"," [0]/(1 + exp( [1] * ([2] - x) ) )");
        sigmoid->SetParNames("Efficiency","Slope","HV @ 50 efficiency");
        
        sigmoid->SetParameter(0, 90);
        sigmoid->SetParLimits(0, 85, 99);
        
        sigmoid->SetParameter(1, 0.001);
        sigmoid->SetParLimits(1, 0.0001, 0.09);
        
        sigmoid->SetParameter(2, 8900);
        sigmoid->SetParLimits(2, 8700, 9400);
        
        //sigmoid->SetParameters(90.0, 0.01, 8600.0);
        sigmoid->SetLineColor(1);
        
        
        // Fitting efficiency1
        g1B->Fit("sigmoid","EM");
        TF1 *fit2 = g1B->GetFunction("sigmoid");
        Double_t chi21 = fit2->GetChisquare() / fit2->GetNDF();
        if (chi21>999) chi21=999;
        Double_t fit_efficiency1B = fit2->GetParameter(0);
        Double_t fit_efficiency_err1B = fit2->GetParError(0);
        
        Double_t slope1B = fit2->GetParameter(1);
        Double_t slope_err1B = fit2->GetParError(1);
        
        Double_t hvat501B = fit2->GetParameter(2);
        Double_t hvat50_err1B = fit2->GetParError(2);
        
        
        // Fitting efficiency2
        //sigmoid->SetParameters(90.0, 0.01, 8800.0);
        sigmoid->SetLineColor(2);
        g2B->Fit("sigmoid","EM");
        TF1 *fit2 = g2B->GetFunction("sigmoid");
        Double_t chi22 = fit2->GetChisquare()  / fit2->GetNDF();
        if (chi22>999) chi22=999;
        Double_t fit_efficiency2B = fit2->GetParameter(0);
        Double_t fit_efficiency_err2B = fit2->GetParError(0);
        
        Double_t slope2B = fit2->GetParameter(1);
        Double_t slope_err2B = fit2->GetParError(1);
        
        Double_t hvat502B = fit2->GetParameter(2);
        Double_t hvat50_err2B = fit2->GetParError(2);
        
        
        
        // Fitting efficiency3
        sigmoid->SetParameter(0, 94);
        sigmoid->SetParLimits(0, 92.5, 99);
        sigmoid->SetLineColor(3);
        g3B->Fit("sigmoid","EM");
        TF1 *fit2 = g3B->GetFunction("sigmoid");
        Double_t chi23 = fit2->GetChisquare()  / fit2->GetNDF();
        if (chi23>999) chi23=999;
        Double_t fit_efficiency3B = fit2->GetParameter(0);
        Double_t fit_efficiency_err3B = fit2->GetParError(0);
        
        Double_t slope3B = fit2->GetParameter(1);
        Double_t slope_err3B = fit2->GetParError(1);
        
        Double_t hvat503B = fit2->GetParameter(2);
        Double_t hvat50_err3B = fit2->GetParError(2);
        
        
        
        sens1B->Draw("");
        //sens2->Draw("same");
        //sens3->Draw("same");
        
        TMultiGraph  *mg = new TMultiGraph();
        mg->Add(g1B);
        mg->Add(g2B);
        mg->Add(g3B);
        mg->Draw("ep");
        
        pad1B->Update();
        pad1B->Modified();
        FrameperfB->cd();
        
        
        
        //compute the pad range with suitable margins
        Double_t ymin = current_min;
        Double_t ymax = current_max;
        Double_t dy = (ymax-ymin)/0.8; //% margins top and bottom
        Double_t xmin = volt_min;
        Double_t xmax = volt_max;
        Double_t dx = (xmax-xmin)/0.8; //% margins left and right
        pad2B->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);
        pad2B->Draw();
        pad2B->cd();
        
        environ3B->Draw("][sames");
        environ4B->Draw("][sames");
        environ5B->Draw("][sames");
        pad2B->Update();
        
        // draw axis on the right side of the pad
        TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
        axis->SetLabelColor(4);
        axis->SetLineColor(4);
        axis->SetTitleOffset(axis_offset);
        axis->SetLabelSize(label_size);
        axis->SetLabelColor(4);
        axis->SetTitle("Current [uA]");
        axis->SetTitleSize(0.03);
        axis->SetTitleColor(4);
        axis->Draw();
        
        
        
        // Adding text to the plot
        TPaveText *pt2 = new TPaveText(0.11,0.45,0.35,0.9,"NDC");
        pt2->AddText("Runs info:");
        snprintf(infovalue1, sizeof(infovalue1), "Temp: %3.1f, RH: %2.0f %, P: %3.0f", env_t, env_rh, env_p );
        pt2->AddText(infovalue1);
        snprintf(infovalue2, sizeof(infovalue2), "Noise: T: %7.5f B: %7.5f D: %7.5f", noisebtop, noisebbottom, noisebdouble );
        pt2->AddText(infovalue2);        
        
        snprintf(infovalue3, sizeof(infovalue3), "Stat.: %s,  #chi^{2}_{TOP} / ndf=%6.1f,  #chi^{2}_{BOTTOM} / ndf=%6.1f,  #chi^{2}_{DOUBLE} / ndf=%6.1f", stat,chi21,chi22,chi23 );
        pt2->AddText(infovalue3);
        
        snprintf(infovalue31, sizeof(infovalue31), "Efficiency_{TOP}= %5.2f #pm %4.2f %%", fit_efficiency1B,fit_efficiency_err1B);
        snprintf(infovalue32, sizeof(infovalue32), "Slope_{TOP}= %6.4f #pm %8.6f", slope1B,slope_err1B);
        snprintf(infovalue33, sizeof(infovalue33), "HV @ 50 Eff._{TOP}= %4.0f #pm %4.1f V", hvat501B,hvat50_err1B);
        pt2->AddText(infovalue31);
        pt2->AddText(infovalue32);
        pt2->AddText(infovalue33);
        
        snprintf(infovalue34, sizeof(infovalue34), "Efficiency_{BOTTOM}= %5.2f #pm %4.2f %%", fit_efficiency2B,fit_efficiency_err2B);
        snprintf(infovalue35, sizeof(infovalue35), "Slope_{BOTTOM}= %6.4f #pm %8.6f", slope2B,slope_err2B);
        snprintf(infovalue36, sizeof(infovalue36), "HV @ 50 Eff._{BOTTOM}= %4.0f #pm %4.1f V", hvat502B,hvat50_err2B);
        pt2->AddText(infovalue34);
        pt2->AddText(infovalue35);
        pt2->AddText(infovalue36);
        
        snprintf(infovalue37, sizeof(infovalue37), "Efficiency_{DOUBLE}= %5.2f #pm %4.2f %%", fit_efficiency3B,fit_efficiency_err3B);
        snprintf(infovalue38, sizeof(infovalue38), "Slope_{DOUBLE}= %6.4f #pm %8.6f", slope3B,slope_err3B);
        snprintf(infovalue39, sizeof(infovalue39), "HV @ 50 Eff._{DOUBLE}= %4.0f #pm %4.1f V", hvat503B,hvat50_err3B);
        pt2->AddText(infovalue37);
        pt2->AddText(infovalue38);
        pt2->AddText(infovalue39);
        
        
        snprintf(infovalue4, sizeof(infovalue4), "Thresholds A: %s %s %s %s",vfat1,vfat2,vfat3,vfat4);
        pt2->AddText(infovalue4);
        
        snprintf(infovalue5, sizeof(infovalue5), "Thresholds B: %s %s %s %s",vfat5,vfat6,vfat7,vfat8);
        pt2->AddText(infovalue5);
        
        snprintf(infovalue6, sizeof(infovalue6), "Thresholds C: %s %s %s %s",vfat9,vfat10,vfat11,vfat12);
        pt2->AddText(infovalue6);
        
        pt2->Draw("");
        
        
        
        // Adding the legend
        leg = new TLegend(0.69,0.50,0.86,0.34);
        leg->AddEntry(sens1B,"Efficiency (TOP)","lep");
        leg->AddEntry(sens2B,"Efficiency (BOTTOM)","lep");
        leg->AddEntry(sens3B,"Efficiency (DOUBLE)","lep");
        leg->AddEntry(environ3B,"Current TN","lep");
        leg->AddEntry(environ4B,"Current TW","lep");
        leg->AddEntry(environ5B,"Current B","lep");
        leg->Draw("");
        
        
        // Saving the image file
        //TImage *img = TImage::Create();
        //img->FromPad(FrameperfB);
        //img->WriteImage( "/srv/www/htdocs/dqm2/plot/" + filenameshort + "B.gif" );
        //cout << filenameshort << endl;
        //img->WriteImage(filenameshort);
        FrameperfB->SaveAs( "/srv/www/htdocs/dqm2/plot/" + filenameshort + "B.gif" );





TCanvas *FrameperfcsizeB = new TCanvas("FrameperfcsizeB","Chamber performance",0,0,1500,900);
TH2F *csize1B = new  TH2F("csize1B","csize1B",volt_bin,8900,volt_max,100,0,5);
TH2F *csize2B = new  TH2F("csize2B","csize2B",volt_bin,8900,volt_max,100,0,5);
TH2F *csize3B = new  TH2F("csize3B","csize3B",volt_bin,8900,volt_max,100,0,5);

        csize1B->SetMarkerColor(1);
        csize1B->SetMarkerStyle(20);
        csize1B->SetMarkerSize(0.9);
        
        csize2B->SetMarkerColor(2);
        csize2B->SetMarkerStyle(21);
        csize2B->SetMarkerSize(0.9);
        
        csize3B->SetMarkerColor(3);
        csize3B->SetMarkerStyle(22);
        csize3B->SetMarkerSize(0.9);

rpclab->Draw("csize:Volt>>csize1B","trigger_mode==\"TOP\" && eta==\"B\"","");
rpclab->Draw("csize:Volt>>csize2B","trigger_mode==\"BOTTOM\" && eta==\"B\"","");
rpclab->Draw("csize:Volt>>csize3B","trigger_mode==\"DOUBLE\" && eta==\"B\"","");

csize1B->Draw();
csize2B->Draw("same");
csize3B->Draw("same");


        csize1B->SetTitle(titletowrite);
        csize1B->GetXaxis()->SetTitleOffset(axis_offset);
        csize1B->GetXaxis()->SetLabelSize(label_size);
        csize1B->GetYaxis()->SetTitleSize(0.04);
        csize1B->GetXaxis()->SetTitle("HV_{effective}");
        
        csize1B->GetYaxis()->SetTitleOffset(axis_offset);
        csize1B->GetYaxis()->SetLabelSize(label_size);
        csize1B->GetYaxis()->SetTitleSize(0.04);
        csize1B->GetYaxis()->SetTitle("Cluster Size");
        FrameperfcsizeB->SaveAs( "/srv/www/htdocs/dqm2/plot/csize" + filenameshort + "B.gif" );





    }

    // Working on eta partition = C
    if ( etapartition == "ALL" || etapartition == "C") {
        cout << " processing ETA=C" << endl;

        rpclab->Draw("noise>>hnoise","trigger_mode==\"TOP\" && eta==\"C\"");
        float noisectop = hnoise->GetMean();
        rpclab->Draw("noise>>hnoise","trigger_mode==\"BOTTOM\" && eta==\"C\"");
        float noisecbottom = hnoise->GetMean();
        rpclab->Draw("noise>>hnoise","trigger_mode==\"DOUBLE\" && eta==\"C\"");
        float noisecdouble = hnoise->GetMean();

        // Creating Canvas
        TCanvas *FrameperfC = new TCanvas("FrameperfC","Chamber performance",0,0,1500,900);
        TPad *pad1C = new TPad("pad1C","",0,0,1,1); //1st pad for T
        TPad *pad2C = new TPad("pad2C","",0,0,1,1); //2nd pad for P
        pad2C->SetFillStyle(4000); //will be transparent
        pad1C->Draw();
        pad1C->cd();
        
        // Pre-drawing currents
        TH2F *environ3C = new TH2F("environ3C","environ3C",volt_bin,volt_min,volt_max,current_bin,current_min,current_max);
        environ3C->SetMarkerColor(4);
        environ3C->SetMarkerStyle(28);
        environ3C->SetMarkerSize(0.8);
        rpclab->Draw("curr1:Volt>>environ3C","trigger_mode==\"DOUBLE\" && eta==\"C\"","");
        
        TH2F *environ4C = new TH2F("environ4C","environ4B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        environ4C->SetMarkerColor(4);
        environ4C->SetMarkerStyle(29);
        environ4C->SetMarkerSize(0.8);
        rpclab->Draw("curr2:Volt>>environ4C","trigger_mode==\"DOUBLE\" && eta==\"C\"","");
        
        TH2F *environ5C = new TH2F("environ5C","environ5B",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        environ5C->SetMarkerColor(4);
        environ5C->SetMarkerStyle(31);
        environ5C->SetMarkerSize(0.8);
        rpclab->Draw("curr3:Volt>>environ5C","trigger_mode==\"DOUBLE\" && eta==\"C\"","");
        
        // PERFORMANCES
        //TPad *pad1 = new TPad("pad1","",0,0,1,1); //1st pad for T
        //TPad *pad2 = new TPad("pad2","",0,0,1,1); //2nd pad for P
        //pad2->SetFillStyle(4000); //will be transparent
        //pad1->Draw();
        //pad1->cd();
        
        TH2F *sens1C = new  TH2F("sens1C","sens1C",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        TH2F *sens2C = new  TH2F("sens2C","sens2C",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        TH2F *sens3C = new  TH2F("sens3C","sens3C",volt_bin,volt_min,volt_max,effi_bin,effi_min,effi_max);
        
        sens1C->SetMarkerColor(1);
        sens1C->SetMarkerStyle(20);
        sens1C->SetMarkerSize(0.9);
        
        sens2C->SetMarkerColor(2);
        sens2C->SetMarkerStyle(21);
        sens2C->SetMarkerSize(0.9);
        
        sens3C->SetMarkerColor(3);
        sens3C->SetMarkerStyle(22);
        sens3C->SetMarkerSize(0.9);
        
        strcpy(titletowrite,"Chamber ");
        strcat(titletowrite,titletowrite2);
        strcat(titletowrite," eta = C");
        sens1C->SetTitle(titletowrite);
        sens1C->GetXaxis()->SetTitleOffset(axis_offset);
        sens1C->GetXaxis()->SetLabelSize(label_size);
        sens1C->GetYaxis()->SetTitleSize(0.04);
        sens1C->GetXaxis()->SetTitle("HV_{effective}");
        
        sens1C->GetYaxis()->SetTitleOffset(axis_offset);
        sens1C->GetYaxis()->SetLabelSize(label_size);
        sens1C->GetYaxis()->SetTitleSize(0.04);
        sens1C->GetYaxis()->SetTitle("Efficiency");
        gPad->SetGrid();
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"TOP\" && eta==\"C\"","goff");
        TGraphErrors *g1C = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g1C->SetMarkerColor(1);
        g1C->SetMarkerStyle(20);
        g1C->SetMarkerSize(0.9);
        g1C->SetLineColor(1);
        //g1->Draw("ap");
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"BOTTOM\" && eta==\"C\"","goff");
        TGraphErrors *g2C = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g2C->SetMarkerColor(2);
        g2C->SetMarkerStyle(21);
        g2C->SetMarkerSize(0.9);
        g2C->SetLineColor(2);
        //g2->Draw("ap");
        
        
        rpclab->Draw("Volt:efficiency:efficiencyerr","trigger_mode==\"DOUBLE\" && eta==\"C\"","goff");
        TGraphErrors *g3C = new TGraphErrors(points,rpclab->GetV1(),rpclab->GetV2(),0,rpclab->GetV3());
        g3C->SetMarkerColor(3);
        g3C->SetMarkerStyle(22);
        g3C->SetMarkerSize(0.9);
        g3C->SetLineColor(3);
        //g3->Draw("ap");
        
        TF1 *sigmoid = new TF1("sigmoid"," [0]/(1 + exp( [1] * ([2] - x) ) )");
        sigmoid->SetParNames("Efficiency","Slope","HV @ 50 efficiency");
        
        sigmoid->SetParameter(0, 90);
        sigmoid->SetParLimits(0, 85, 99);
        
        sigmoid->SetParameter(1, 0.001);
        sigmoid->SetParLimits(1, 0.0001, 0.09);
        
        sigmoid->SetParameter(2, 8900);
        sigmoid->SetParLimits(2, 8700, 9400);
        
        //sigmoid->SetParameters(90.0, 0.01, 8600.0);
        sigmoid->SetLineColor(1);
        
        
        // Fitting efficiency1
        g1C->Fit("sigmoid","EM");
        TF1 *fit3 = g1C->GetFunction("sigmoid");
        Double_t chi21 = fit3->GetChisquare() / fit3->GetNDF();
        if (chi21>999) chi21=999;
        Double_t fit_efficiency1C = fit3->GetParameter(0);
        Double_t fit_efficiency_err1C = fit3->GetParError(0);
        
        Double_t slope1C = fit3->GetParameter(1);
        Double_t slope_err1C = fit3->GetParError(1);
        
        Double_t hvat501C = fit3->GetParameter(2);
        Double_t hvat50_err1C = fit3->GetParError(2);
        
        
        // Fitting efficiency2
        //sigmoid->SetParameters(90.0, 0.01, 8800.0);
        sigmoid->SetLineColor(2);
        g2C->Fit("sigmoid","EM");
        TF1 *fit3 = g2C->GetFunction("sigmoid");
        Double_t chi22 = fit3->GetChisquare()  / fit3->GetNDF();
        if (chi22>999) chi22=999;
        Double_t fit_efficiency2C = fit3->GetParameter(0);
        Double_t fit_efficiency_err2C = fit3->GetParError(0);
        
        Double_t slope2C = fit3->GetParameter(1);
        Double_t slope_err2C = fit3->GetParError(1);
        
        Double_t hvat502C = fit3->GetParameter(2);
        Double_t hvat50_err2C = fit3->GetParError(2);
        
        
        
        // Fitting efficiency3
        sigmoid->SetParameter(0, 94);
        sigmoid->SetParLimits(0, 92.5, 99);
        sigmoid->SetLineColor(3);
        g3C->Fit("sigmoid","EM");
        TF1 *fit3 = g3C->GetFunction("sigmoid");
        Double_t chi23 = fit3->GetChisquare()  / fit3->GetNDF();
        if (chi23>999) chi23=999;
        Double_t fit_efficiency3C = fit3->GetParameter(0);
        Double_t fit_efficiency_err3C = fit3->GetParError(0);
        
        Double_t slope3C = fit3->GetParameter(1);
        Double_t slope_err3C = fit3->GetParError(1);
        
        Double_t hvat503C = fit3->GetParameter(2);
        Double_t hvat50_err3C = fit3->GetParError(2);
        
        
        
        sens1C->Draw("");
        //sens2->Draw("same");
        //sens3->Draw("same");
        
        TMultiGraph  *mg = new TMultiGraph();
        mg->Add(g1C);
        mg->Add(g2C);
        mg->Add(g3C);
        mg->Draw("ep");
        
        pad1C->Update();
        pad1C->Modified();
        FrameperfC->cd();
        
        
        
        //compute the pad range with suitable margins
        Double_t ymin = current_min;
        Double_t ymax = current_max;
        Double_t dy = (ymax-ymin)/0.8; //% margins top and bottom
        Double_t xmin = volt_min;
        Double_t xmax = volt_max;
        Double_t dx = (xmax-xmin)/0.8; //% margins left and right
        pad2C->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy);
        pad2C->Draw();
        pad2C->cd();
        
        environ3C->Draw("][sames");
        environ4C->Draw("][sames");
        environ5C->Draw("][sames");
        pad2C->Update();
        
        // draw axis on the right side of the pad
        TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,50510,"+L");
        axis->SetLabelColor(4);
        axis->SetLineColor(4);
        axis->SetTitleOffset(axis_offset);
        axis->SetLabelSize(label_size);
        axis->SetLabelColor(4);
        axis->SetTitle("Current [uA]");
        axis->SetTitleSize(0.03);
        axis->SetTitleColor(4);
        axis->Draw();
        
        
        
        // Adding text to the plot
        TPaveText *pt2 = new TPaveText(0.11,0.45,0.35,0.9,"NDC");
        pt2->AddText("Runs info:");
        snprintf(infovalue1, sizeof(infovalue1), "Temp: %3.1f, RH: %2.0f %, P: %3.0f", env_t, env_rh, env_p );
        pt2->AddText(infovalue1);
        snprintf(infovalue2, sizeof(infovalue2), "Noise: T: %7.5f B: %7.5f D: %7.5f", noisectop, noisecbottom, noisecdouble );
        pt2->AddText(infovalue2);

        snprintf(infovalue3, sizeof(infovalue3), "Stat.: %s,  #chi^{2}_{TOP} / ndf=%6.1f,  #chi^{2}_{BOTTOM} / ndf=%6.1f,  #chi^{2}_{DOUBLE} / ndf=%6.1f", stat,chi21,chi22,chi23 );
        pt2->AddText(infovalue3);
        
        snprintf(infovalue31, sizeof(infovalue31), "Efficiency_{TOP}= %5.2f #pm %4.2f %%", fit_efficiency1C,fit_efficiency_err1C);
        snprintf(infovalue32, sizeof(infovalue32), "Slope_{TOP}= %6.4f #pm %8.6f", slope1C,slope_err1C);
        snprintf(infovalue33, sizeof(infovalue33), "HV @ 50 Eff._{TOP}= %4.0f #pm %4.1f V", hvat501C,hvat50_err1C);
        pt2->AddText(infovalue31);
        pt2->AddText(infovalue32);
        pt2->AddText(infovalue33);
        
        snprintf(infovalue34, sizeof(infovalue34), "Efficiency_{BOTTOM}= %5.2f #pm %4.2f %%", fit_efficiency2C,fit_efficiency_err2C);
        snprintf(infovalue35, sizeof(infovalue35), "Slope_{BOTTOM}= %6.4f #pm %8.6f", slope2C,slope_err2C);
        snprintf(infovalue36, sizeof(infovalue36), "HV @ 50 Eff._{BOTTOM}= %4.0f #pm %4.1f V", hvat502C,hvat50_err2C);
        pt2->AddText(infovalue34);
        pt2->AddText(infovalue35);
        pt2->AddText(infovalue36);
        
        snprintf(infovalue37, sizeof(infovalue37), "Efficiency_{DOUBLE}= %5.2f #pm %4.2f %%", fit_efficiency3C,fit_efficiency_err3C);
        snprintf(infovalue38, sizeof(infovalue38), "Slope_{DOUBLE}= %6.4f #pm %8.6f", slope3C,slope_err3C);
        snprintf(infovalue39, sizeof(infovalue39), "HV @ 50 Eff._{DOUBLE}= %4.0f #pm %4.1f V", hvat503C,hvat50_err3C);
        pt2->AddText(infovalue37);
        pt2->AddText(infovalue38);
        pt2->AddText(infovalue39);
        
        
        snprintf(infovalue4, sizeof(infovalue4), "Thresholds A: %s %s %s %s",vfat1,vfat2,vfat3,vfat4);
        pt2->AddText(infovalue4);
        
        snprintf(infovalue5, sizeof(infovalue5), "Thresholds B: %s %s %s %s",vfat5,vfat6,vfat7,vfat8);
        pt2->AddText(infovalue5);
        
        snprintf(infovalue6, sizeof(infovalue6), "Thresholds C: %s %s %s %s",vfat9,vfat10,vfat11,vfat12);
        pt2->AddText(infovalue6);
        
        pt2->Draw("");
        
        
        
        // Adding the legend
        leg = new TLegend(0.69,0.50,0.86,0.34);
        leg->AddEntry(sens1C,"Efficiency (TOP)","lep");
        leg->AddEntry(sens2C,"Efficiency (BOTTOM)","lep");
        leg->AddEntry(sens3C,"Efficiency (DOUBLE)","lep");
        leg->AddEntry(environ3C,"Current TN","lep");
        leg->AddEntry(environ4C,"Current TW","lep");
        leg->AddEntry(environ5C,"Current B","lep");
        leg->Draw("");
        
        
        // Saving the image file
        //TImage *img = TImage::Create();
        //img->FromPad(FrameperfC);
        //img->WriteImage( "/srv/www/htdocs/dqm2/plot/" + filenameshort + "C.gif" );
        //cout << filenameshort << endl;
        //img->WriteImage(filenameshort);
        FrameperfC->SaveAs( "/srv/www/htdocs/dqm2/plot/" + filenameshort + "C.gif" );


TCanvas *FrameperfcsizeC = new TCanvas("FrameperfcsizeC","Chamber performance",0,0,1500,900);
TH2F *csize1C = new  TH2F("csize1C","csize1C",volt_bin,8900,volt_max,100,0,5);
TH2F *csize2C = new  TH2F("csize2C","csize2C",volt_bin,8900,volt_max,100,0,5);
TH2F *csize3C = new  TH2F("csize3C","csize3C",volt_bin,8900,volt_max,100,0,5);

        csize1C->SetMarkerColor(1);
        csize1C->SetMarkerStyle(20);
        csize1C->SetMarkerSize(0.9);
        
        csize2C->SetMarkerColor(2);
        csize2C->SetMarkerStyle(21);
        csize2C->SetMarkerSize(0.9);
        
        csize3C->SetMarkerColor(3);
        csize3C->SetMarkerStyle(22);
        csize3C->SetMarkerSize(0.9);

rpclab->Draw("csize:Volt>>csize1C","trigger_mode==\"TOP\" && eta==\"C\"","");
rpclab->Draw("csize:Volt>>csize2C","trigger_mode==\"BOTTOM\" && eta==\"C\"","");
rpclab->Draw("csize:Volt>>csize3C","trigger_mode==\"DOUBLE\" && eta==\"C\"","");

csize1C->Draw();
csize2C->Draw("same");
csize3C->Draw("same");


        csize1C->SetTitle(titletowrite);
        csize1C->GetXaxis()->SetTitleOffset(axis_offset);
        csize1C->GetXaxis()->SetLabelSize(label_size);
        csize1C->GetYaxis()->SetTitleSize(0.04);
        csize1C->GetXaxis()->SetTitle("HV_{effective}");
        
        csize1C->GetYaxis()->SetTitleOffset(axis_offset);
        csize1C->GetYaxis()->SetLabelSize(label_size);
        csize1C->GetYaxis()->SetTitleSize(0.04);
        csize1C->GetYaxis()->SetTitle("Cluster Size");
        FrameperfcsizeC->SaveAs( "/srv/www/htdocs/dqm2/plot/csize" + filenameshort + "C.gif" );

    }

}
