Exemplo n.º 1
0
void ratioPlots_Zxx()
{

  // llbb_Mass_reco mfJetEta_450_600 mfJetEta_250_300 lljjMass_reco mjj_HighMass_reco drll_HighMass_reco

   TString Variable = "Zxx_Mass_reco";
//   TString Variable2 = "Zbb_Mass_reco";
   TString x_title = "M_{llxx}";
   Int_t N_Rebin = 10;

   Double_t yTopLimit = 3;

   TFile *f1 = TFile::Open("/home/fynu/amertens/storage/test/MG_PY6_/output/MG_PY6_/MG_PY6v9.root");
   TH1D *h1 = (TH1D*)f1->Get(Variable);
   h1->Sumw2();
//   h1->Add((TH1D*)f1->Get(Variable2));
   h1->SetDirectory(0);
   f1->Close();

   TFile *f2 = TFile::Open("/home/fynu/amertens/storage/test/aMCNLO_PY8_/output/aMCNLO_PY8_/aMCNLO_PY8v9.root");
   TH1D *h2 = (TH1D*)f2->Get(Variable);
   h2->Sumw2();
//   h2->Add((TH1D*)f2->Get(Variable2));
   h2->SetDirectory(0);
   f2->Close();

/*
   TFile *f3 = TFile::Open("/home/fynu/amertens/storage/test/MG_PY8_/output/MG_PY8_/MG_PY8.root");
   TH1D *h3 = (TH1D*)f3->Get(Variable);
   h3->SetDirectory(0);
   f3->Close();
*/
//   h1->Sumw2();
//   h2->Sumw2();
//   h3->Sumw2();

   cout << "MG_PY6      : " << h1->Integral() << endl;
   cout << "aMC@NLO_PY8 : " << h2->Integral() << endl;


   //h1->Scale(1.0/151456.0);
   //h2->Scale(1.0/1.45192e+09);
   //h2->Scale(1./12132.9);
   h1->Scale(1.0/h1->Integral());
   h2->Scale(1.0/h2->Integral());

   h1->Sumw2();
   h2->Sumw2();



//   h3->Scale(1.0/h3->Integral());

   h1->Rebin(N_Rebin);
   h2->Rebin(N_Rebin);
//   h3->Rebin(N_Rebin);

   TH1D *h1c = h1->Clone();
   h1c->Sumw2();
   TH1D *h2c = h2->Clone();
   h2c->Sumw2();

   TH1D *h1c2 = h1->Clone();
   h1c2->Sumw2();

   h2c->Add(h1c,-1);
   h2c->Divide(h1c);


   h1->SetTitle("");
   h2->SetTitle("");
//   h3->SetTitle("");


   h1->SetLineColor(kRed);
//   h3->SetLineColor(kGreen);

   TCanvas *c1 = new TCanvas("c1","example",600,700);
   TPad *pad1 = new TPad("pad1","pad1",0,0.5,1,1);
   pad1->SetBottomMargin(0);
   gStyle->SetOptStat(0);
   pad1->Draw();
   pad1->cd();
   h2->DrawCopy();
//   h3->DrawCopy("same");
   h1->GetYaxis()->SetLabelSize(0.1);
   h1->GetYaxis()->SetRangeUser(0, 0.2);// ,yTopLimit);
   h1->GetYaxis()->SetTitleSize(0.06);
   h1->GetYaxis()->SetTitleOffset(0.7);



   h1->Draw("same");

   TLegend *leg = new TLegend(0.6,0.7,0.89,0.89);
   leg->SetLineColor(0);
   leg->SetFillColor(0);
   //leg->AddEntry(h1,"t#bar{t} uncertainty","f");
   leg->AddEntry(h1,"MG5 + PY6","l");
   leg->AddEntry(h2,"aMC@NLO + PY8","l");
//   leg->AddEntry(h3,"MG5 + PY8","l");
   leg->Draw();

   
   c1->cd();

   TPad *pad2 = new TPad("pad2","pad2",0,0,1,0.5);
   pad2->SetTopMargin(0);
   pad2->SetBottomMargin(0.4);
   pad2->Draw();
   pad2->cd();
   pad2->SetGrid();
   h2->SetStats(0);
   h2->Divide(h1);
   //h2->SetMarkerStyle(21);
   h2->Draw("ep");
   h2->GetYaxis()->SetLabelSize(0.1);
   h2->GetYaxis()->SetRangeUser(-0.5, 2.5);// ,yTopLimit);
   h2->GetYaxis()->SetTitle("aMC@NLO+PY8 / MG5+PY6");
   h2->GetYaxis()->SetTitleSize(0.06);
   h2->GetYaxis()->SetTitleOffset(0.7);
   h2->GetXaxis()->SetLabelSize(0.1);
   h2->GetXaxis()->SetTitle(x_title);
   h2->GetXaxis()->SetTitleSize(0.16);
   h2->GetXaxis()->SetTitleOffset(0.9);
 //  Double_t matrix[4][4];
   h2->Fit("pol3","","",50.0,1200.0);
   TF1 *ratio = h2->GetFunction("pol3");
   TVirtualFitter *fitter = TVirtualFitter::GetFitter();
   TMatrixD matrix(4,4,fitter->GetCovarianceMatrix());
   Double_t errorPar00 = fitter->GetCovarianceMatrixElement(0,0);
   Double_t errorPar11 = fitter->GetCovarianceMatrixElement(1,1);
   Double_t errorPar22 = fitter->GetCovarianceMatrixElement(2,2);
   Double_t errorPar33 = fitter->GetCovarianceMatrixElement(3,3);
//   c1->cd();

   matrix.Print();

   //const TMatrixDSym m = matrix;
   const TMatrixDEigen eigen(matrix);
   const TMatrixD eigenVal = eigen.GetEigenValues();
   const TMatrixD V = eigen.GetEigenVectors(); 

   cout << "V" << endl;

   V.Print();

   cout << "eigenVal" << endl;

   eigenVal.Print();



   cout << "Recomputed diag" << endl;

   //const TMatrixD Vt(TMatrixD::kTransposed,V);
   //const TMatrixD Vinv = V.Invert();
   const TMatrixD Vt(TMatrixD::kTransposed,V);
   //cout << "V-1" << endl;
   //Vinv.Print();
   cout << "Vt" << endl;
   Vt.Print();

   const TMatrixD VAVt = Vt*matrix*V;
   VAVt.Print();


   const TVectorD FittedParam(4);
   FittedParam(0) = fitter->GetParameter(0);
   FittedParam(1) = fitter->GetParameter(1);
   FittedParam(2) = fitter->GetParameter(2);
   FittedParam(3) = fitter->GetParameter(3);
   FittedParam.Print();


   //const TVectorD FittedParamNB(4);
   const TVectorD PNb = V*FittedParam;
   cout << "Pnb" << endl;
   PNb.Print();
  

   cout << " Generating other parameters values " << endl;

   cout <<" V " << V(0,0) << endl;

   TRandom3 r;
   const TVectorD NewP(4);

   TH1D *hist100 = new TH1D("h100","h100",200,-5,5);
   TH1D *hist200 = new TH1D("h200","h200",200,-5,5);
   TH1D *hist400 = new TH1D("h400","h400",200,-5,5);
   TH1D *hist600 = new TH1D("h600","h600",100,-5,5);
   TH1D *hist800 = new TH1D("h800","h800",100,-5,5);
   TH1D *hist1000 = new TH1D("h1000","h1000",100,-5,5);

   TH1D *histp0 = new TH1D("p0","p0",100,-0.2,0.3);
   TH1D *histp1 = new TH1D("p1","p1",100,0.0,0.01);
   TH1D *histp2 = new TH1D("p2","p2",100,-0.00001,0);
   TH1D *histp3 = new TH1D("p3","p3",100,0,0.000000002);



   for (Int_t i = 0; i< 500; i++){
     NewP(0) = r.Gaus(PNb(0),sqrt(eigenVal(0,0)));
     NewP(1) = r.Gaus(PNb(1),sqrt(eigenVal(1,1)));
     NewP(2) = r.Gaus(PNb(2),sqrt(eigenVal(2,2)));
     NewP(3) = r.Gaus(PNb(3),sqrt(eigenVal(3,3)));
     //NewP.Print();

     //FittedParam.Print();

     const TVectorD NewP2 = Vt*NewP;
     //NewP2.Print();

     histp0->Fill(NewP2(0));
     histp1->Fill(NewP2(1));
     histp2->Fill(NewP2(2));
     histp3->Fill(NewP2(3));



     TF1 *newFit=new TF1("test","[0]+x*[1]+[2]*pow(x,2)+[3]*pow(x,3)",0,1400);
     newFit->SetParameters(NewP2(0),NewP2(1),NewP2(2),NewP2(3));
     newFit->SetLineColor(kBlue);

     Double_t area=0;
     for(Int_t it=1; it < 16; it++){
       //cout << "bin : " << it << " " << h1c2->GetBinContent(it) << endl;
       area += h1c2->GetBinContent(it)*newFit->Eval(100*it+50);
       }
   
     //newFit->Draw("same");
     //cout <<"val: " << newFit->Eval(200) << endl;
     hist100->Fill(newFit->Eval(100)/area);
     hist200->Fill(newFit->Eval(200)/area);
     hist400->Fill(newFit->Eval(400)/area);
     hist600->Fill(newFit->Eval(600)/area);
     hist800->Fill(newFit->Eval(800)/area);
     hist1000->Fill(newFit->Eval(1000)/area);
     }

   c1->cd();
   TCanvas *c2 = new TCanvas("c2","c2",1000,1000);
   c2->cd();
   c2->Divide(3,2);
   c2->cd(1);
   hist100->Draw();
   c2->cd(2);
   hist200->Draw();
   c2->cd(3);
   hist400->Draw();
   c2->cd(4);
   hist600->Draw();
   c2->cd(5);
   hist800->Draw();
   c2->cd(6);
   hist1000->Draw();



Double_t m_100,m_200,m_400,m_600,m_800,m_1000;
Double_t s_100,s_200,s_400,s_600,s_800,s_1000;

hist100->Fit("gaus","","",0.3,1.2);
TVirtualFitter *fitter = TVirtualFitter::GetFitter();
m_100 = fitter->GetParameter(1);
s_100 = fitter->GetParameter(2);

hist200->Fit("gaus","","",0.5,1.2);
TVirtualFitter *fitter = TVirtualFitter::GetFitter();
m_200 = fitter->GetParameter(1);
s_200 = fitter->GetParameter(2);

hist400->Fit("gaus","","",0.8,1.2);
TVirtualFitter *fitter = TVirtualFitter::GetFitter();
m_400 = fitter->GetParameter(1);
s_400 = fitter->GetParameter(2);

hist600->Fit("gaus","","",0.8,1.3);
TVirtualFitter *fitter = TVirtualFitter::GetFitter();
m_600 = fitter->GetParameter(1);
s_600 = fitter->GetParameter(2);

hist800->Fit("gaus","","",0.5,2);
TVirtualFitter *fitter = TVirtualFitter::GetFitter();
m_800 = fitter->GetParameter(1);
s_800 = fitter->GetParameter(2);

hist1000->Fit("gaus","","",0.5,2.5);
TVirtualFitter *fitter = TVirtualFitter::GetFitter();
m_1000 = fitter->GetParameter(1);
s_1000 = fitter->GetParameter(2);


Double_t x[6],y[6],ym[6],yup[6],ydown[6];
x[0]=100; x[1]=200; x[2]=400;x[3]=600; x[4]=800; x[5]=1000;
yup[0]=ratio->Eval(100)+s_100;
yup[1]=ratio->Eval(200)+s_200;
yup[2]=ratio->Eval(400)+s_400;
yup[3]=ratio->Eval(600)+s_600;
yup[4]=ratio->Eval(800)+s_800;
yup[5]=ratio->Eval(1000)+s_1000;
ydown[0]=ratio->Eval(100)-s_100;
ydown[1]=ratio->Eval(200)-s_200;
ydown[2]=ratio->Eval(400)-s_400;
ydown[3]=ratio->Eval(600)-s_600;
ydown[4]=ratio->Eval(800)-s_800;
ydown[5]=ratio->Eval(1000)-s_1000;

y[0]=1+s_100/ratio->Eval(100);
y[1]=1+s_200/ratio->Eval(200);
y[2]=1+s_400/ratio->Eval(400);
y[3]=1+s_600/ratio->Eval(600);
y[4]=1+s_800/ratio->Eval(800);
y[5]=1+s_1000/ratio->Eval(1000);

ym[0]=-s_100/m_100;
ym[1]=-s_200/m_200;
ym[2]=-s_400/m_400;
ym[3]=-s_600/m_600;
ym[4]=-s_800/m_800;
ym[5]=-s_1000/m_1000;


TGraph* g = new TGraph(6,x,y);
TGraph* gm = new TGraph(6,x,ym);
TGraph* gup = new TGraph(6,x,yup);
TGraph* gdown = new TGraph(6,x,ydown);


TCanvas *c3 = new TCanvas("c3","c3",1000,1000);
c3->cd();

//gup->Draw("AC*");
//gdown->Draw("C*");
g->Draw("AC*");

gPad->SetBottomMargin(0.2);
gPad->SetLeftMargin(0.2);
gStyle->SetOptStat(0);

g->GetXaxis()->SetTitle("M_{Zbb}");
g->GetXaxis()->SetRangeUser(50,1100);
g->GetYaxis()->SetLabelSize(0.06);
g->GetYaxis()->SetTitle("Uncertainty");
g->GetYaxis()->SetTitleSize(0.06);
g->GetYaxis()->SetTitleOffset(1.4);
g->GetXaxis()->SetLabelSize(0.06);
g->GetXaxis()->SetTitleSize(0.06);
g->GetXaxis()->SetTitleOffset(1);
g->GetYaxis()->SetNdivisions(5);

TFile f("syst_zxx.root","recreate");
g->Write();
f.Close();


//gm->Draw("C*");
//g->SetMaximum(1);
//g->SetMinimum(-1);
//h2c->Draw("same");


TH1D *h22=h2->Clone();

TCanvas *c5 =  new TCanvas("c5","c5",1000,1000);

gPad->SetBottomMargin(0.2);
gPad->SetLeftMargin(0.2);
gStyle->SetOptStat(0);

h22->Draw();
h22->GetXaxis()->SetRangeUser(50,1100);
h22->GetYaxis()->SetLabelSize(0.06);
h22->GetYaxis()->SetTitleSize(0.06);
h22->GetYaxis()->SetTitleOffset(1.4);
h22->GetXaxis()->SetLabelSize(0.06);
h22->GetXaxis()->SetTitleSize(0.06);
h22->GetXaxis()->SetTitleOffset(1);

ratio->SetLineColor(kRed);
ratio->Draw("same");

gup->Draw("C");
gdown->Draw("C");


TLegend *leg = new TLegend(0.6,0.7,0.89,0.89);
leg->SetLineColor(0);
leg->SetFillColor(0);
leg->AddEntry(h22,"aMC@NLO / MG5","lep");
leg->AddEntry(ratio,"best fit","l");
leg->AddEntry(gup,"Syst Error (#pm 1 #sigma)","l");
leg->Draw();



TCanvas *c4 = new TCanvas("c4","c4",1000,1000);
c4->Divide(2,2);
c4->cd(1);
histp0->Draw();
c4->cd(2);
histp1->Draw();
c4->cd(3);
histp2->Draw();
c4->cd(4);
histp3->Draw();


}
Exemplo n.º 2
0
int makeInvMassHistosNoBGKK(){
    //Set global style stuff
    gROOT->Reset();
    gROOT->SetStyle("Plain");
    gStyle->SetPalette(1);
    gStyle->SetCanvasColor(kWhite);
    gStyle->SetCanvasBorderMode(0);
    gStyle->SetPadBorderMode(0);
    gStyle->SetTitleBorderSize(0);
    gStyle->SetOptStat(0);
    gStyle->SetOptFit(1);
    gStyle->SetErrorX(0);
    gStyle->SetTitleW(0.9);
    gStyle->SetTitleSize(0.05, "xyz");
    gStyle->SetTitleSize(0.06, "h");

    int NUM_PT_BINS = 20;
    int NUM_MASS_BINS = 1000;
    double MASS_LOW = 0.0;
    double MASS_HIGH = 2.0;
    string particles [8];
    particles[0] = "K*^{+} + K*^{0}";
    particles[1] = "K*^{-} + #bar{K}*^{0}";
    particles[2] = "K*^{+}";
    particles[3] = "K*^{-}";
    particles[4] = "K*^{0}";
    particles[5] = "#bar{K}*^{0}";
    particles[6] = "K*^{0} + #bar{K}*^{0}";
    particles[7] = "K*^{+} + K*^{-}";
//at decay point
//    string folder = "/Users/jtblair/Downloads/kstar_data/decayed/pt02/";
//reconstructed
    string folder = "/Users/jtblair/Downloads/kstar_data/reconstructed/pt02/";

    string files[20];
    files[0] = "invm_[0.0,0.2].dat";
    files[1] = "invm_[0.2,0.4].dat";
    files[2] = "invm_[0.4,0.6].dat";
    files[3] = "invm_[0.6,0.8].dat";
    files[4] = "invm_[0.8,1.0].dat";
    files[5] = "invm_[1.0,1.2].dat";
    files[6] = "invm_[1.2,1.4].dat";
    files[7] = "invm_[1.4,1.6].dat";   
    files[8] = "invm_[1.6,1.8].dat";
    files[9] = "invm_[1.8,2.0].dat";
    files[10] = "invm_[2.0,2.2].dat";
    files[11] = "invm_[2.2,2.4].dat";
    files[12] = "invm_[2.4,2.6].dat";
    files[13] = "invm_[2.6,2.8].dat";
    files[14] = "invm_[2.8,3.0].dat";
    files[15] = "invm_[3.0,3.2].dat";
    files[16] = "invm_[3.2,3.4].dat";
    files[17] = "invm_[3.4,3.6].dat";
    files[18] = "invm_[3.6,3.8].dat";
    files[19] = "invm_[3.8,4.0].dat";
/*
    string files[8];
    files[0] = "invm_[0.0,0.5].dat";
    files[1] = "invm_[0.5,1.0].dat";
    files[2] = "invm_[1.0,1.5].dat";
    files[3] = "invm_[1.5,2.0].dat";
    files[4] = "invm_[2.0,2.5].dat";
    files[5] = "invm_[2.5,3.0].dat";
    files[6] = "invm_[3.0,3.5].dat";
    files[7] = "invm_[3.5,4.0].dat";
*/

    Int_t PARTICLE_NUM = 5;

    TFile *output = new TFile("20170721_KKbarAdded2_fixedwidth42_recon_pf100_scaled_error05.root", "RECREATE");

    TH1D *kstar0mass = new TH1D("kstar0mass", Form("Fit value of M*_{0} vs. p_{T} for %s", particles[PARTICLE_NUM].c_str()), NUM_PT_BINS, 0.0, 4.0);
    TH1D *kstar0width = new TH1D("kstar0width", Form("#Gamma_{tot}(M=M*_{0}) vs p_{T} for %s", particles[PARTICLE_NUM].c_str()), NUM_PT_BINS, 0.0, 4.0);
    TH1D *kstar0collWidth = new TH1D("kstar0collWidth", Form("Fit value of #Gamma_{coll} component vs. p_{T} for %s", particles[PARTICLE_NUM].c_str()), NUM_PT_BINS,0.0, 4.0);
    TH1D *kstar0decWidth = new TH1D("kstar0decWidth", Form("#Gamma_{dec}(M=M*_{0}) component vs. p_{T} for %s;p_{T} (GeV/c);Width (GeV/c^2)", particles[PARTICLE_NUM].c_str()), NUM_PT_BINS,0.0, 4.0);
   
    kstar0mass->GetXaxis()->SetTitle("p_{T} (GeV/c)");
    kstar0mass->GetYaxis()->SetTitle("Mass (GeV/c^{2})");
    kstar0width->GetXaxis()->SetTitle("p_{T} (GeV/c)");
    kstar0width->GetYaxis()->SetTitle("Width (GeV/c^2)");
    kstar0collWidth->GetXaxis()->SetTitle("p_{T} (GeV/c)");
    kstar0collWidth->GetYaxis()->SetTitle("Width (GeV/c^2)");

    kstar0mass->SetStats(kFALSE);
    kstar0width->SetStats(kFALSE);
    kstar0collWidth->SetStats(kFALSE);
    kstar0decWidth->SetStats(kFALSE);

    TF1 *massline = new TF1("massline", "[0]", 0.0, 4.0);
    massline->SetParameter(0, 0.892);
    massline->SetLineColor(2);
    massline->SetLineStyle(7);

    TF1 *widthline = new TF1("widthline", "[0]", 0.0, 4.0);
    widthline->SetParameter(0, 0.042);

    double mass = 0.0, width = 0.0, collWidth = 0.0, massBG=0.0;
    double massError = 0.0, widthError= 0.0, collWidthError = 0.0, massBGError=0.0;

    TCanvas *canvas[9];
    TCanvas *diffCanvas[9];
    TPaveStats *st;
    TPad *pad;

    //ofstream integrals;
    //integrals.open("kstarbar_integrals.txt");

    for(int nfile = 0; nfile < NUM_PT_BINS; nfile++){
        double meanPT = (double)(nfile*2+1)/10.0;
        string filename = folder+files[nfile];
        string ptLower = filename.substr(filename.find("[")+1, 3);
        string ptHigher = filename.substr(filename.find(",")+1, 3);   
        TH1D* histos[8];
        TH1D* newHistos[8];
        TH1D* diffHistos[8];
        TH1D* bg[8];
        for(int i=0; i<8; i++){
            if(nfile<5){
                histos[i] = new TH1D(Form("ptbin0%dparticle%d",nfile*2+1, i), Form("Invariant Mass for (%s), %s < p_{T} < %s",particles[i].c_str(), ptLower.c_str(), ptHigher.c_str()), NUM_MASS_BINS, MASS_LOW, MASS_HIGH);
            newHistos[i] = new TH1D(Form("newptbin0%dparticle%d",nfile*2+1, i), Form("Invariant Mass for (%s), %s < p_{T} < %s",particles[i].c_str(), ptLower.c_str(), ptHigher.c_str()), 250, MASS_LOW, MASS_HIGH);

            }else{
                histos[i] = new TH1D(Form("ptbin%dparticle%d",nfile*2+1, i), Form("Invariant Mass for (%s), %s < p_{T} < %s",particles[i].c_str(), ptLower.c_str(), ptHigher.c_str()), NUM_MASS_BINS, MASS_LOW, MASS_HIGH);
                newHistos[i] = new TH1D(Form("newptbin%dparticle%d",nfile*2+1, i), Form("Invariant Mass for (%s), %s < p_{T} < %s",particles[i].c_str(), ptLower.c_str(), ptHigher.c_str()), 250, MASS_LOW, MASS_HIGH);

            }
            histos[i]->GetXaxis()->SetTitle("Invariant Mass (GeV/c^{2})");
            histos[i]->GetYaxis()->SetTitle("Counts");
        }

        ifstream input;
        input.open(filename.c_str());
        string line = "";
        if(input.good()){
            getline(input, line);
        }

        double massBin=0.0;
        double invMass[8];
        for(int i=0; i<8; i++){
            invMass[i] = 0.0;
        }
        int lineNumber = 1;
        while(1){
            input >> massBin >> invMass[0] >> invMass[1] >> invMass[2] >> invMass[3] >> invMass[4] >> invMass[5] >> invMass[6] >> invMass[7];
            if(!input.good())break;
            for(int i =0; i<8; i++){
                histos[i]->SetBinContent(lineNumber, invMass[i]/500.0);
            }
            if(lineNumber > 440 && lineNumber < 460 && nfile==6){
//               printf("mass: %.12f invMass[6]: %.12f\n", massBin, invMass[6]);
            }
            lineNumber++;
        }
         

        printf("****** Fits for file: %s ******\n", filename.c_str());
        for(int i=PARTICLE_NUM; i<PARTICLE_NUM+1; i++){
           
            //add the K*0 distribution to the K*0bar (K*0 = 4 for decay, K*0 = 3 for reconstructed)
            histos[i]->Add(histos[3]);
            if(nfile==0){
                canvas[i] = new TCanvas(Form("c%i", i),Form("c%i", i), 0,0,900,900);
                canvas[i]->Divide(5,4);
                diffCanvas[i] = new TCanvas(Form("diffC%i", i),Form("diffC%i", i), 0,0,900,900);
                diffCanvas[i]->Divide(5,4);
            }
            //rebin
            //histos[i]->Sumw2();
            histos[i]->Rebin(4);

            //Fixing the errors to a percentage of the signal region:
            for(int ibin=1; ibin < histos[i]->GetNbinsX(); ibin++){
                histos[i]->SetBinError(ibin, histos[i]->GetBinContent((int)(0.892*(250.0/2.0)))*0.05);
                newHistos[i]->SetBinContent(ibin, histos[i]->GetBinContent(ibin));
                newHistos[i]->SetBinError(ibin, histos[i]->GetBinError(ibin));
            }
            
            pad = (TPad*)canvas[i]->cd(nfile+1);
            histos[i]->SetLineColor(1);
            histos[i]->SetLineWidth(1);
            histos[i]->GetXaxis()->SetRangeUser(0.7, 1.2);
            histos[i]->GetYaxis()->SetRangeUser(0, 1.5*histos[i]->GetBinContent(histos[i]->GetMaximumBin()));
            //histos[i]->SetStats(kFALSE);
            
            //histos[i]->Draw("HIST");

            printf("mean PT: %f\n", meanPT);

            TF1 *fit = new TF1(Form("fitPTbin%d00particle%d", nfile*2+1, i), FitFunRelBW, 0.68, 1.05, 5);
            //TF1 *fit = new TF1(Form("fitPTbin%d00particle%d", nfile*2+1, i), "gaus(0)", 0.86, 0.92);


            fit->SetParNames("BW Area", "Mass", "Width", "PT", "Temp");
            fit->SetParameters(TMath::Power(10.0, (float)(nfile)/1.7), 0.89, 0.1, 0.5, 0.130);
            //fit->SetParNames("BW Area", "Mass", "Width");
            //fit->SetParameters(100, 0.89, 0.0474);
            //fit->SetParLimits(0, -10, 1.5e9);
            Float_t max = histos[i]->GetXaxis()->GetBinCenter(histos[i]->GetMaximumBin());
            //if(max < 0.91 && max > 0.892){
            //    fit->SetParLimits(1, max-0.001, max+0.001);
            //}else{
                fit->SetParLimits(1, 0.82, 0.98);
            //}
            //fit->SetParLimits(2, 0.005, 0.15);
            fit->FixParameter(2, 0.042);
            fit->FixParameter(3, meanPT);
            //fit->SetParLimits(4, 0.05, 0.2);
            fit->FixParameter(4, 0.100001);
            fit->SetLineColor(2);

            printf("%s\n", fit->GetName());

            histos[i]->Fit(Form("fitPTbin%d00particle%d", nfile*2+1, i), "BRIM", "SAME");
            TVirtualFitter *fitter = TVirtualFitter::GetFitter();
           
            histos[i]->SetStats(1);
            histos[i]->Draw();
            gPad->Update();
            pad->Update();
            st = (TPaveStats*)histos[i]->FindObject("stats");
            st->SetX1NDC(0.524);
            st->SetY1NDC(0.680);
            st->SetX2NDC(0.884);
            st->SetY2NDC(0.876);
            //fit->Draw("SAME");
            //histos[i]->Draw();
            gPad->Update();
            pad->Update();
            printf("\n");
    
            diffHistos[i] = (TH1D*)histos[i]->Clone(Form("diffPTbin%d00particl%d", nfile*2+1, i));
            diffHistos[i]->Add(fit, -1);
            diffCanvas[i]->cd(nfile+1);
            diffHistos[i]->Draw("HIST E");
            diffHistos[i]->Write();

            //counting bins
            Float_t integral = histos[i]->Integral(1, 500)*500.0;
            //integrals << integral <<" \n";
            histos[i]->Write();
            fit->Write();
            //Do mass and width vs. pT plots just for K*0
            if(i==PARTICLE_NUM){
                mass = fit->GetParameter(1);
                massError = fit->GetParError(1);

                collWidth = fit->GetParameter(2);
                collWidthError = fit->GetParError(2);

                width = Gamma(mass, collWidth);

                kstar0mass->SetBinContent(nfile+1, mass);
                kstar0mass->SetBinError(nfile+1, massError);

                kstar0width->SetBinContent(nfile+1, width);
                Double_t widthError = TMath::Sqrt((GammaDerivative(mass)**2)*fitter->GetCovarianceMatrixElement(1,1) + fitter->GetCovarianceMatrixElement(2,2) + 2.0*GammaDerivative(mass)*fitter->GetCovarianceMatrixElement(1,2));
                kstar0width->SetBinError(nfile+1, widthError);

                kstar0collWidth->SetBinContent(nfile+1, collWidth);
                kstar0collWidth->SetBinError(nfile+1, collWidthError);

                kstar0decWidth->SetBinContent(nfile+1, width - collWidth);
                Double_t decWidthError = TMath::Sqrt((GammaDerivative(mass)**2)*fitter->GetCovarianceMatrixElement(1,1));
                kstar0decWidth->SetBinError(nfile+1, decWidthError);

                if(nfile==4){
                    TCanvas *singlecanvas = new TCanvas("singlecanvas", "singlecanvas", 0,0,600,600);
                    singlecanvas->cd();
                    printf("Got here! \n");
                    histos[i]->Draw("HIST E SAME");

                    fit->SetLineColor(8);
                    fit->SetLineStyle(1);
                    
                    fit->Draw("SAME");
                    if(fitter){
                        printf("sig11: %f, sig12: %f, sig21: %f, sig22: %f GammaDer(0.8): %f GammaDer(0.85): %f GammaDer(0.9): %f\n", TMath::Sqrt(fitter->GetCovarianceMatrixElement(1,1)), fitter->GetCovarianceMatrixElement(2,1), fitter->GetCovarianceMatrixElement(1,2), TMath::Sqrt(fitter->GetCovarianceMatrixElement(2,2)), GammaDerivative(0.8), GammaDerivative(0.85), GammaDerivative(0.9));
                    }
                }
            }
        }
        printf("************************************************************\n");
         
    }
        //integrals.close();
/*
    TH2D *gammaPlot = new TH2D("gammaPlot", "#Gamma_{tot}(M_{0}*);M_{0}*;#Gamma_{coll};#Gamma_{tot}", 100, 0.82, 0.9, 100, 0.0, 0.08);
    for(int im = 0; im<100; im++){
        for(int ig = 0; ig < 100; ig++){
            gammaPlot->SetBinContent(im+1, ig+1, Gamma(((0.9-0.82)/(100.0))*((double)(im)) + 0.82, ((0.08)/100.0)*((double)(ig))));
        }
    }

    TH1D *gammaMassDpnd = gammaPlot->ProjectionX("gammaMassDpnd");
*/
    TCanvas *masscanvas = new TCanvas("masscanvas", "masscanvas", 50,50, 600, 600);
    masscanvas->cd();
    kstar0mass->Draw();
    massline->Draw("SAME");
    masscanvas->Write();

    for(int i=PARTICLE_NUM; i<PARTICLE_NUM+1; i++){
        canvas[i]->Write();
    }
    kstar0mass->Write();
    kstar0collWidth->Write();
    kstar0decWidth->Write();
    kstar0width->Write();
//    gammaPlot->Write();
//    gammaMassDpnd->Write();
}
void Demo_TryExtrapolationInXT_Exp0_LogLogFits(Bool_t xt=kTRUE, Float_t expo=0.)
{
        SetStyle();
        gStyle->SetOptFile(0);
        gStyle->SetOptStat(0);
        gStyle->SetOptFit(0);
 
        xt=kTRUE;

	//define dummy histogram and some style parameters
	TH1F *dum;
 	dum = new TH1F("dum","",160,5e-4,0.3); 
	dum->SetMinimum(1e-14);
	dum->SetMaximum(1);
	dum->SetTitle(Form(";x_{T};#sqrt{s}^{%0.1f}  E d^{3}#sigma/dp^{3}",expo));
	dum->SetLineWidth(0);
	dum->SetStats(0);
	dum->GetXaxis()->CenterTitle();
	dum->GetYaxis()->CenterTitle();
	dum->GetXaxis()->SetTitleSize(0.05);
	dum->GetYaxis()->SetTitleSize(0.05);
	dum->GetXaxis()->SetTitleOffset(1.17);
	dum->GetYaxis()->SetTitleOffset(1.3);

	gROOT->LoadMacro("/net/hidsk0001/d00/scratch/krajczar/ppRefForpPb_PilotRun/interpolation_HIN10005_kk/data_table_to_graph.C");

	//get 7 TeV points
	TGraphErrors *cms_7000_g = data_table_to_graph("cms",7000,xt,expo);
	cms_7000_g->SetMarkerColor(kBlack);
	
	TF1 *cms_7000_fit = new TF1("cms_7000_fit","[0]*pow(1.0+(x/[1]),[2])",10.*2./7000.,0.1);//Fit from 10 GeV/c
	cms_7000_fit->SetLineWidth(1);
	cms_7000_fit->SetParameters(3e22,2.5e-4,-7);
	cms_7000_g->Fit(cms_7000_fit,"REMW0");
	
	//get 2.36 TeV points
//	TGraphErrors *cms_2360_g = data_table_to_graph("cms",2360,xt,expo);
//	cms_2360_g->SetMarkerColor(kMagenta+3);
	
//	TF1 *cms_2360_fit = new TF1("cms_2360_fit","[0]*pow(1.0+(x/[1]),[2])",2e-3,0.1);
//	cms_2360_fit->SetLineWidth(1);
//	cms_2360_fit->SetParameters(3e22,2.5e-4,-7);
//	cms_2360_g->Fit(cms_2360_fit,"REMW0");

	//get 2.76 TeV points (KK using existing txt files)
	TGraphErrors *cms_2760_g = data_table_to_graph("cms",2760,xt,expo);
	cms_2760_g->SetMarkerColor(kMagenta+3);
	TF1 *cms_2760_fit = new TF1("cms_2760_fit","[0]*pow(1.0+(x/[1]),[2])",10.*2./2760.,0.1);//Fit from 10 GeV/c
	cms_2760_fit->SetLineColor(kMagenta+3);
	cms_2760_fit->SetLineWidth(1);
	cms_2760_fit->SetParameters(3e22,2.5e-4,-7);
	cms_2760_g->Fit(cms_2760_fit,"REMW0");
	
	//get 1.96 TeV points
	TGraphErrors *cdf_1960_g = data_table_to_graph("cdf",1960,xt,expo);
	cdf_1960_g->SetMarkerColor(kOrange-3);
	cdf_1960_g->SetMarkerStyle(30);
	
	//TGraphErrors *cdfold_1960_g = data_table_to_graph("cdfold",1960,xt);
	//cdfold_1960_g->SetMarkerColor(kBlue);
	//cdfold_1960_g->SetMarkerStyle(30);
	
	TF1 *cdf_1960_fit = new TF1("cdf_1960_fit","[0]*pow(1.0+(x/[1]),[2])",2.*10./1960.,0.1);//Fit from 10 GeV/c
	cdf_1960_fit->SetLineColor(kOrange-3);
	cdf_1960_fit->SetLineWidth(1);
	cdf_1960_fit->SetParameters(3e22,2.5e-4,-7);
	cdf_1960_g->Fit(cdf_1960_fit,"REMW0");
	
	//get 1.8 TeV points
	TGraphErrors *cdf_1800_g = data_table_to_graph("cdf",1800,xt,expo);
	cdf_1800_g->SetMarkerColor(kGreen+3);
	cdf_1800_g->SetMarkerStyle(28);

	TF1 *cdf_1800_fit = new TF1("cdf_1800_fit","[0]*pow(1.0+(x/[1]),[2])",2.*10./1800.,0.1);//Fit from 10 GeV/c
	cdf_1800_fit->SetLineColor(kGreen+3);
	cdf_1800_fit->SetLineWidth(1);
	cdf_1800_fit->SetParameters(3e22,2.5e-4,-7.2);
	cdf_1800_fit->FixParameter(2,-7.2);
	cdf_1800_g->Fit(cdf_1800_fit,"REMW0");
	
	//get 0.9 TeV points
	TGraphErrors *cms_900_g = data_table_to_graph("cms",900,xt,expo);
	cms_900_g->SetMarkerColor(kRed);
	
	TF1 *cms_900_fit = new TF1("cms_900_fit","[0]*pow(1.0+(x/[1]),[2])",2.*10./900.,0.01);//Fit from 10 GeV/c
	cms_900_fit->SetLineColor(kRed);
	cms_900_fit->SetLineWidth(1);
	cms_900_fit->SetParameters(3e22,2.5e-4,-7);
	cms_900_g->Fit(cms_900_fit,"REMW0");
	
//	TGraphErrors *ua1_900_g = data_table_to_graph("ua1",900,xt,expo);
//	ua1_900_g->SetMarkerColor(kCyan+1);
//	ua1_900_g->SetMarkerStyle(26);

	// get 0.63 TeV points
	TGraphErrors *cdf_630_g = data_table_to_graph("cdf",630,xt,expo);
	cdf_630_g->SetMarkerColor(kOrange+3);
	cdf_630_g->SetMarkerStyle(27);

	//draw graphs to canvas
	TCanvas *c1 = new TCanvas("c1","spectra interpolation",600,600);
	dum->Draw();
        //Fits are already drawn, no draw the points on top of the fits
	cdf_1960_g->Draw("pz");
	//cdfold_1960_g->Draw("pz");
//	cdf_1800_g->Draw("pz");
//	if(!xt) ua1_900_g->Draw("pz"); // abs(eta) within 2.5 changes high xt behavior
//	cdf_630_g->Draw("pz");
	cms_900_g->Draw("pz");         // draw the CMS points on top
//	cms_2360_g->Draw("pz");
	cms_2760_g->Draw("pz"); //KK
	cms_7000_g->Draw("pz");

	//make legend
	TLegend *leg1 = new TLegend(0.2,0.21,0.50,0.51,"p+p(#bar{p})");
	leg1->SetBorderSize(0);
	leg1->SetFillStyle(1);
	leg1->SetFillColor(0);
	leg1->AddEntry(cms_7000_g,"7 TeV  (CMS)","lp");
	leg1->AddEntry(cms_2760_g,"2.76 TeV (CMS)","lp");
//	leg1->AddEntry(cms_2360_g,"2.36 TeV (CMS)","lp");
	leg1->AddEntry(cdf_1960_g,"1.96 TeV  (CDF)","lp");
//	leg1->AddEntry(cdf_1800_g,"1.8 TeV  (CDF)","lp");
	leg1->AddEntry(cms_900_g,"0.9 TeV  (CMS)","lp");
//	if(!xt) leg1->AddEntry(ua1_900_g,"0.9 TeV  (UA1)  |#eta|<2.5","lp");
//	leg1->AddEntry(cdf_630_g,"0.63 TeV  (CDF)","lp");
	leg1->Draw();
	
	gPad->SetLogy();
	//if(xt) gPad->SetLogx();
	gPad->SetLogx();
	
	cms_7000_fit->Draw("same");
	cms_2760_fit->Draw("same");
	cdf_1960_fit->Draw("same");
//	cdf_1800_fit->Draw("same");
	cms_900_fit->Draw("same");

	TCanvas *c3 = new TCanvas("c3","Individual xT fits, residuals",600,500);
	TH1F *hratio = new TH1F("hratio",";x_{T};ratio",160,0.0003,0.07); //was 0.003-0.04
	hratio->SetMaximum(2.0);
	hratio->SetMinimum(0.0);
	hratio->SetStats(0);
	hratio->Draw();
		
	TGraphErrors* ratio_cdf_1960_g = divide_graph_by_function(cdf_1960_g,cdf_1960_fit);
	ratio_cdf_1960_g->SetName("ratio_cdf_1960_g");
	ratio_cdf_1960_g->SetLineColor(kOrange-9);
	ratio_cdf_1960_g->SetMarkerSize(0.9);
	ratio_cdf_1960_g->Draw("samepz");
		
	TF1 *fit1960 = new TF1("fit1960","[0]+[1]*log(x)+[2]/x/x",0.001,0.035);
	fit1960->SetLineWidth(2);
	fit1960->SetLineColor(kOrange-3);
	ratio_cdf_1960_g->Fit(fit1960,"REMW");
	fit1960->Draw("same");
		
//	TGraphErrors* ratio_cdf_1800_g = divide_graph_by_function(cdf_1800_g,cdf_1800_fit);
//	ratio_cdf_1800_g->Draw("samepz");
		
//	TGraphErrors* ratio_cdf_630_g = divide_graph_by_function(cdf_630_g,merge_fit);
	//ratio_cdf_630_g->Draw("pz");
	
	TGraphErrors* ratio_cms_7000_g = divide_graph_by_function(cms_7000_g,cms_7000_fit);
	ratio_cms_7000_g->SetName("ratio_cms_7000_g");
	ratio_cms_7000_g->Draw("samepz");
		
	TF1 *fit7000 = new TF1("fit7000","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.001,0.1);
	fit7000->SetLineWidth(2);
	ratio_cms_7000_g->Fit(fit7000,"REMW");

	TGraphErrors* ratio_cms_2760_g = divide_graph_by_function(cms_2760_g,cms_2760_fit);
	ratio_cms_2760_g->SetName("ratio_cms_2760_g");
	ratio_cms_2760_g->SetLineColor(kMagenta+3);
	ratio_cms_2760_g->Draw("samepz");
		
        TF1 *fit2760 = new TF1("fit2760","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.001,0.1);
	fit2760->SetLineWidth(2);
	fit2760->SetLineColor(kMagenta+3);
	ratio_cms_2760_g->Fit(fit2760,"REM");//REMW
		
	TGraphErrors* ratio_cms_900_g = divide_graph_by_function(cms_900_g,cms_900_fit);
	ratio_cms_900_g->SetName("ratio_cms_900_g");
	ratio_cms_900_g->Draw("samepz");
		
        TF1 *fit900 = new TF1("fit900","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.007,0.1);
        fit900->SetParameters(5.61766e-01,5.24904e+01,-2.27817e+03,3.43955e+04);
	fit900->SetLineWidth(2);
	fit900->SetLineColor(2);
	ratio_cms_900_g->Fit(fit900,"REM");//REMW
	ratio_cms_900_g->SetLineColor(kRed);
		
	TGaxis *A1 = new TGaxis(0.0003,7.0,0.07,7.0,2510*0.0003,2510*0.07,410,"-");
	A1->SetTitle("p_{T} for #sqrt{s}=5.02 TeV");
	A1->Draw();

        //Real fit should be the fit*resid
	TH1D *h900 = new TH1D("h900","900 GeV fitted spectra;x_{T}",30400,0.005,0.05);
	h900->SetLineColor(kRed);
	TH1D *h1960 = new TH1D("h1960","1.96 TeV fitted spectra;x_{T}",30400,0.005,0.05);
	h1960->SetLineColor(kOrange-3);
	TH1D *h7000 = new TH1D("h7000","7 TeV fitted spectra;x_{T}",30400,0.005,0.05);
	TH1D *h1800 = new TH1D("h1800","1.8 TeV fitted spectra;x_{T}",30400,0.005,0.05);
	h1800->SetLineColor(kGreen+3);
	TH1D *h630 = new TH1D("h630","0.63 TeV fitted spectra;x_{T}",30400,0.005,0.05);
	h630->SetLineColor(kOrange+3);
	TH1D *h2760_EdTxt = new TH1D("h2760_EdTxt","2.76 TeV fitted spectra;x_{T}",30400,0.005,0.05);
	h2760_EdTxt->SetLineColor(kMagenta+3);

	for(int hbin=1; hbin<=30400; hbin++) {
		float xtbin = h900->GetBinCenter(hbin);
		h900->SetBinContent(hbin,cms_900_fit->Eval(xtbin)*fit900->Eval(xtbin));
		h1960->SetBinContent(hbin,cdf_1960_fit->Eval(xtbin)*fit1960->Eval(xtbin));
		h7000->SetBinContent(hbin,cms_7000_fit->Eval(xtbin)*fit7000->Eval(xtbin));
		h2760_EdTxt->SetBinContent(hbin,cms_2760_fit->Eval(xtbin)*fit2760->Eval(xtbin));
	}

	TCanvas *c5 = new TCanvas("c5","final x_{T} fits",600,500);
	TH1D* dumDirectInt = new TH1D("dumDirectInt","Final fits; x_{T} (GeV/c)",120,5e-4,0.3);
	dumDirectInt->SetMaximum(1);
	dumDirectInt->SetMinimum(1e-14);
	dumDirectInt->GetXaxis()->SetRangeUser(0.5,120.);
	dumDirectInt->SetStats(0);
	dumDirectInt->GetYaxis()->SetTitle("Ed^{3}#sigma/dp^{3}");
	dumDirectInt->Draw();
	h900->Draw("same");
	h1960->Draw("same");
	h7000->Draw("same");
	h2760_EdTxt->Draw("same");
	gPad->SetLogy();
	gPad->SetLogx();

	// inspect direct interpolations
	TCanvas *c6 = new TCanvas("c6","interpolations",600,500);
	c6->Divide(3,4);
	float s[6]; float xs[6]; float es[6]={0.0,0.0,0.0,0.0,0.0,0.0}; float exs[6];   TGraphErrors *gXS[12];
        float s_log[6]; float xs_log[6]; float es_log[6]={0.0,0.0,0.0,0.0,0.0,0.0}; float exs_log[6];
        TGraphErrors *gXS_log[12]; //KK test
        TGraphErrors *gXS_log_lemma[12]; //KK test
	float s1[1]={2.76}; float xs1[1]; float ex1[1]={0.0}; float ey1[1]; 		TGraphErrors *gXS1[12];
	float s1_5020[1]={5.02}; float xs1_5020[1]; float ex1_5020[1]={0.0}; float ey1_5020[1]; TGraphErrors *gXS1_5020[12];
	float s2[1]={2.76}; float xs2[1]; float ex2[1]={0.0}; float ey2[1]; 		TGraphErrors *gXS2[12];
	float s2_5020[1]={5.02}; float xs2_5020[1]; float ex2_5020[1]={0.0}; float ey2_5020[1]; TGraphErrors *gXS2_5020[12];
	float s900[1]={0.9}; float xs900[1]; float ex900[1]={0.0}; float ey900[1]; TGraphErrors *gXS900[12];
	float s1960[1]={1.96}; float xs1960[1]; float ex1960[1]={0.0}; float ey1960[1]; TGraphErrors *gXS1960[12];
	float s2760[1]={2.76}; float xs2760[1]; float ex2760[1]={0.0}; float ey2760[1]; TGraphErrors *gXS2760[12];
	float s7000[1]={7.0}; float xs7000[1]; float ex7000[1]={0.0}; float ey7000[1]; TGraphErrors *gXS7000[12];

	TF1 *fitXS[12];  TH1F *dumXS[12];
        TF1 *fitXS_log[12];
	float xtbins[12]={0.0051,0.007,0.01,0.015,0.02,0.025,0.03,0.035,0.04,0.042,0.045,0.049};
        //2pT/sqrt(s) = xT ==>> xT=0.0051 -> pT=12.8 GeV/c; xT=0.049 -> pT=123 GeV/c

        //errors        
        TMVA::TSpline1 *err_cms_900_xt = errors_from_graph(cms_900_g,0.115);
        TMVA::TSpline1 *err_cms_2760_xt = errors_from_graph(cms_2760_g,0.11);
        TMVA::TSpline1 *err_cms_7000_xt = errors_from_graph(cms_7000_g,0.04);	
        TMVA::TSpline1 *err_cdf_1960_xt = errors_from_graph(cdf_1960_g,0.06);

	for(Int_t ipt=0; ipt<=11; ipt++) {
						
		c6->cd(ipt+1);
		int npoints=0;

		xs[npoints]=h900->GetBinContent(h900->FindBin(xtbins[ipt])); s[npoints]=0.9; exs[npoints]=err_cms_900_xt->Eval(xtbins[ipt])*xs[npoints]; xs900[0]=xs[npoints]; ey900[0]=exs[npoints];
		xs_log[npoints]=log10(xs[npoints]); s_log[npoints]=log10(s[npoints]); exs_log[npoints]=TMath::Max(fabs(log10(xs[npoints]-exs[npoints])-log10(xs[npoints])),fabs(log10(xs[npoints]+exs[npoints])-log10(xs[npoints])));
                npoints++;

		xs[npoints]=h1960->GetBinContent(h1960->FindBin(xtbins[ipt])); s[npoints]=1.96; exs[npoints]=err_cdf_1960_xt->Eval(xtbins[ipt])*xs[npoints]; xs1960[0]=xs[npoints]; ey1960[0]=exs[npoints];
		xs_log[npoints]=log10(xs[npoints]); s_log[npoints]=log10(s[npoints]); exs_log[npoints]=TMath::Max(fabs(log10(xs[npoints]-exs[npoints])-log10(xs[npoints])),fabs(log10(xs[npoints]+exs[npoints])-log10(xs[npoints])));
                npoints++;
 
		xs[npoints]=h2760_EdTxt->GetBinContent(h2760_EdTxt->FindBin(xtbins[ipt])); s[npoints]=2.76; exs[npoints]=err_cms_2760_xt->Eval(xtbins[ipt])*xs[npoints]; xs2760[0]=xs[npoints]; ey2760[0]=exs[npoints];
		xs_log[npoints]=log10(xs[npoints]); s_log[npoints]=log10(s[npoints]); exs_log[npoints]=TMath::Max(fabs(log10(xs[npoints]-exs[npoints])-log10(xs[npoints])),fabs(log10(xs[npoints]+exs[npoints])-log10(xs[npoints])));
                npoints++;

		xs[npoints]=h7000->GetBinContent(h7000->FindBin(xtbins[ipt])); s[npoints]=7.0; exs[npoints]=err_cms_7000_xt->Eval(xtbins[ipt])*xs[npoints]; xs7000[0]=xs[npoints]; ey7000[0]=exs[npoints];
		xs_log[npoints]=log10(xs[npoints]); s_log[npoints]=log10(s[npoints]); exs_log[npoints]=TMath::Max(fabs(log10(xs[npoints]-exs[npoints])-log10(xs[npoints])),fabs(log10(xs[npoints]+exs[npoints])-log10(xs[npoints])));
                npoints++;
			
		dumXS[ipt] = new TH1F(Form("dumXS%d",ipt),Form("p_{T} = %0.0f GeV/c;#sqrt{s} [TeV]",xtbins[ipt]),100,0,20);
		dumXS[ipt]->SetMinimum(0.25*xs[npoints-1]);
		dumXS[ipt]->SetMaximum(4.0*xs[0]);
		dumXS[ipt]->SetStats(0);
		dumXS[ipt]->GetXaxis()->SetRangeUser(0.5,10.0);
              	dumXS[ipt]->GetXaxis()->CenterTitle();
               	dumXS[ipt]->GetYaxis()->CenterTitle();
               	dumXS[ipt]->GetYaxis()->SetTitle("Ed#sigma^{3}/dp^{3}");
               	dumXS[ipt]->GetXaxis()->SetTitleSize(0.10);
               	dumXS[ipt]->GetYaxis()->SetTitleSize(0.10);
               	dumXS[ipt]->GetYaxis()->SetLabelSize(0.10);
               	dumXS[ipt]->GetXaxis()->SetLabelSize(0.10);
               	dumXS[ipt]->GetXaxis()->SetTitleOffset(0.6);
               	dumXS[ipt]->GetYaxis()->SetTitleOffset(0.8);
		dumXS[ipt]->Draw();
		gPad->SetLogy();  
                gPad->SetLogx();

                std::cerr<< "npoints: " << npoints << std::endl;

		gXS[ipt] = new TGraphErrors(npoints,s,xs,es,exs);  gXS[ipt]->SetName(Form("gXS%d",ipt));
		gXS[ipt]->SetMarkerStyle(20);
		gXS_log[ipt] = new TGraphErrors(npoints,s_log,xs_log,es_log,exs_log); gXS_log[ipt]->SetName(Form("gXS_log%d",ipt));

		gXS900[ipt] = new TGraphErrors(1,s900,xs900,ex900,ey900); gXS900[ipt]->SetName(Form("gXS900_%d",ipt)); 
		gXS900[ipt]->SetMarkerStyle(20);  gXS900[ipt]->SetMarkerColor(kRed); gXS900[ipt]->Draw("pz");

		gXS1960[ipt] = new TGraphErrors(1,s1960,xs1960,ex1960,ey1960); gXS1960[ipt]->SetName(Form("gXS1960_%d",ipt)); 
		gXS1960[ipt]->SetMarkerStyle(30);  gXS1960[ipt]->SetMarkerColor(kOrange-3); gXS1960[ipt]->Draw("pz");

		gXS2760[ipt] = new TGraphErrors(1,s2760,xs2760,ex2760,ey2760); gXS2760[ipt]->SetName(Form("gXS2760_%d",ipt)); 
		gXS2760[ipt]->SetMarkerStyle(20);  gXS2760[ipt]->SetMarkerColor(kMagenta+3); gXS2760[ipt]->Draw("pz");	

		gXS7000[ipt] = new TGraphErrors(1,s7000,xs7000,ex7000,ey7000); gXS7000[ipt]->SetName(Form("gXS7000_%d",ipt)); 
		gXS7000[ipt]->SetMarkerStyle(20);  gXS7000[ipt]->SetMarkerColor(kBlack); gXS7000[ipt]->Draw("pz");	
			
		fitXS_log[ipt] = new TF1(Form("fitXS_log%d",ipt),"pol2",-0.52288,0.85733);
		gXS_log[ipt]->Fit(fitXS_log[ipt], "REM0"); //"REMW");

		// full covariance errors on fit
		TVirtualFitter *fitter = TVirtualFitter::GetFitter();
		TMatrixD matrix(3,3,fitter->GetCovarianceMatrix());
		Double_t e00 = fitter->GetCovarianceMatrixElement(0,0);
		Double_t e11 = fitter->GetCovarianceMatrixElement(1,1);
		Double_t e22 = fitter->GetCovarianceMatrixElement(2,2);
		Double_t e01 = fitter->GetCovarianceMatrixElement(0,1);
		Double_t e02 = fitter->GetCovarianceMatrixElement(0,2);
		Double_t e12 = fitter->GetCovarianceMatrixElement(1,2);

                //Due to properties of the covariance matrix:
                Double_t e10 = e01;
                Double_t e20 = e02;
                Double_t e21 = e12;

                gXS_log_lemma[ipt] = new TGraphErrors(); gXS_log_lemma[ipt]->SetName(Form("gXS_log_lemma%d",ipt));
                int kkk = 0;
                for(int kk = log10(0.8*s[0])*10000.; kk <= log10(1.3*s[npoints-1])*10000.; kk++) {
                      float kk_lemma = kk/10000.;
                      float value = fitXS_log[ipt]->Eval(kk_lemma);
                      gXS_log_lemma[ipt]->SetPoint(kkk,TMath::Power(10,kk_lemma),TMath::Power(10,value));
                      kkk++;
                }
                gXS_log_lemma[ipt]->SetLineColor(2);
                gXS_log_lemma[ipt]->Draw("same");
			
		cout << "cov(0,0) = " << e00 
		<< "\ncov(1,1) = " << e11 
		<< "\ncov(2,2) = " << e22 
		<< "\ncov(0,1) = " << e01 
		<< "\ncov(0,2) = " << e02 
		<< "\ncov(1,2) = " << e12 
		<< endl;
			
                //0.7007 = log10(5.02)
		Double_t fullerr2 = e00 + e11*0.7007*0.7007 + e22*0.7007*0.7007*0.7007*0.7007 + 2*e01*0.7007 + 2*e02*0.7007*0.7007 + 2*e12*0.7007*0.7007*0.7007;
                //Plan (1.,0.7007,0.7007^2)(COV)(1.,0.7007,0.7007^2):
                Double_t where = 0.7007;
                Double_t fullerr2_alternative = e00 + 2.*e01*where + 2.*e02*where*where + 2.*e12*where*where*where + e11*where*where + e22*where*where*where*where;
			
		cout << "full covariance error = " << TMath::Sqrt(fullerr2) << endl;
		cout << "full covariance error alternative: = " << TMath::Sqrt(fullerr2_alternative) << endl;
                float error_in_percentage = 100.*(TMath::Power(10,TMath::Sqrt(fullerr2))-1.);
                cout << "   on " << fitXS_log[ipt]->Eval(0.7007) << std::endl;
                cout << "   error in percentage: " << error_in_percentage << std::endl;

                //5020
		xs2_5020[0] = TMath::Power(10,fitXS_log[ipt]->Eval(0.7007));
		ey2_5020[0] = error_in_percentage*0.01*xs2_5020[0];
		gXS2_5020[ipt] = new TGraphErrors(1,s2_5020,xs2_5020,ex2_5020,ey2_5020); gXS2_5020[ipt]->SetName(Form("gXS2_5020_%d",ipt));
		gXS2_5020[ipt]->SetMarkerColor(7);
		gXS2_5020[ipt]->SetLineColor(7);
		gXS2_5020[ipt]->SetMarkerStyle(kOpenSquare);
		gXS2_5020[ipt]->Draw("pz");
	}
}