Exemple #1
0
//____________________________________________________________________
void fitCircle(Int_t n=10000) {
   //generates n points around a circle and fit them
   TCanvas *c1 = new TCanvas("c1","c1",600,600);
   c1->SetGrid();
   gr = new TGraph(n);
   if (n> 999) gr->SetMarkerStyle(1);
   else        gr->SetMarkerStyle(3);
   TRandom3 r;
   Double_t x,y;
   for (Int_t i=0;i<n;i++) {
      r.Circle(x,y,r.Gaus(4,0.3));
      gr->SetPoint(i,x,y);
   }
   c1->DrawFrame(-5,-5,5,5);
   gr->Draw("p");

   //Fit a circle to the graph points
   TVirtualFitter::SetDefaultFitter("Minuit");  //default is Minuit
   TVirtualFitter *fitter = TVirtualFitter::Fitter(0, 3);
   fitter->SetFCN(myfcn);

   fitter->SetParameter(0, "x0",   0, 0.1, 0,0);
   fitter->SetParameter(1, "y0",   0, 0.1, 0,0);
   fitter->SetParameter(2, "R",    1, 0.1, 0,0);

   Double_t arglist[1] = {0};
   fitter->ExecuteCommand("MIGRAD", arglist, 0);

   //Draw the circle on top of the points
   TArc *arc = new TArc(fitter->GetParameter(0),
      fitter->GetParameter(1),fitter->GetParameter(2));
   arc->SetLineColor(kRed);
   arc->SetLineWidth(4);
   arc->Draw();
}
Exemple #2
0
int TwoHistoFit2D(bool global = true) {

    // create two histograms

    int nbx1 = 50;
    int nby1 = 50;
    int nbx2 = 50;
    int nby2 = 50;
    double xlow1 = 0.;
    double ylow1 = 0.;
    double xup1 = 10.;
    double yup1 = 10.;
    double xlow2 = 5.;
    double ylow2 = 5.;
    double xup2 = 20.;
    double yup2 = 20.;

    TH2D * h1 = new TH2D("h1","core",nbx1,xlow1,xup1,nby1,ylow1,yup1);
    TH2D * h2 = new TH2D("h2","tails",nbx2,xlow2,xup2,nby2,ylow2,yup2);

    double iniParams[10] = { 100, 6., 2., 7., 3, 100, 12., 3., 11., 2. };
    // create fit function
    TF2 * func = new TF2("func",my2Dfunc,xlow2,xup2,ylow2,yup2, 10);
    func->SetParameters(iniParams);

    // fill Histos
    int n1 = 50000;
    int n2 = 50000;
    //  h1->FillRandom("func", n1);
    //h2->FillRandom("func",n2);
    FillHisto(h1,n1,iniParams);
    FillHisto(h2,n2,iniParams);

    // scale histograms to same heights (for fitting)
    double dx1 = (xup1-xlow1)/double(nbx1);
    double dy1 = (yup1-ylow1)/double(nby1);
    double dx2 = (xup2-xlow2)/double(nbx2);
    double dy2 = (yup2-ylow2)/double(nby2);
//   h1->Sumw2();
//   h1->Scale( 1.0 / ( n1 * dx1 * dy1 ) );
    // scale histo 2 to scale of 1
    h2->Sumw2();
    h2->Scale(  ( double(n1) * dx1 * dy1 )  / ( double(n2) * dx2 * dy2 ) );


    if (global) {
        // fill data structure for fit (coordinates + values + errors)
        std::cout << "Do global fit" << std::endl;
        // fit now all the function together

        // fill data structure for fit (coordinates + values + errors)
        TAxis *xaxis1  = h1->GetXaxis();
        TAxis *yaxis1  = h1->GetYaxis();
        TAxis *xaxis2  = h2->GetXaxis();
        TAxis *yaxis2  = h2->GetYaxis();

        int nbinX1 = h1->GetNbinsX();
        int nbinY1 = h1->GetNbinsY();
        int nbinX2 = h2->GetNbinsX();
        int nbinY2 = h2->GetNbinsY();

        /// reset data structure
        coords = std::vector<std::pair<double,double> >();
        values = std::vector<double>();
        errors = std::vector<double>();


        for (int ix = 1; ix <= nbinX1; ++ix) {
            for (int iy = 1; iy <= nbinY1; ++iy) {
                if ( h1->GetBinContent(ix,iy) > 0 ) {
                    coords.push_back( std::make_pair(xaxis1->GetBinCenter(ix), yaxis1->GetBinCenter(iy) ) );
                    values.push_back( h1->GetBinContent(ix,iy) );
                    errors.push_back( h1->GetBinError(ix,iy) );
                }
            }
        }
        for (int ix = 1; ix <= nbinX2; ++ix) {
            for (int iy = 1; iy <= nbinY2; ++iy) {
                if ( h2->GetBinContent(ix,iy) > 0 ) {
                    coords.push_back( std::make_pair(xaxis2->GetBinCenter(ix), yaxis2->GetBinCenter(iy) ) );
                    values.push_back( h2->GetBinContent(ix,iy) );
                    errors.push_back( h2->GetBinError(ix,iy) );
                }
            }
        }

        TVirtualFitter::SetDefaultFitter("Minuit");
        TVirtualFitter * minuit = TVirtualFitter::Fitter(0,10);
        for (int i = 0; i < 10; ++i) {
            minuit->SetParameter(i, func->GetParName(i), func->GetParameter(i), 0.01, 0,0);
        }
        minuit->SetFCN(myFcn);

        double arglist[100];
        arglist[0] = 0;
        // set print level
        minuit->ExecuteCommand("SET PRINT",arglist,2);

// minimize
        arglist[0] = 5000; // number of function calls
        arglist[1] = 0.01; // tolerance
        minuit->ExecuteCommand("MIGRAD",arglist,2);

        //get result
        double minParams[10];
        double parErrors[10];
        for (int i = 0; i < 10; ++i) {
            minParams[i] = minuit->GetParameter(i);
            parErrors[i] = minuit->GetParError(i);
        }
        double chi2, edm, errdef;
        int nvpar, nparx;
        minuit->GetStats(chi2,edm,errdef,nvpar,nparx);

        func->SetParameters(minParams);
        func->SetParErrors(parErrors);
        func->SetChisquare(chi2);
        int ndf = coords.size()-nvpar;
        func->SetNDF(ndf);

        std::cout << "Chi2 Fit = " << chi2 << " ndf = " << ndf << "  " << func->GetNDF() << std::endl;

        // add to list of functions
        h1->GetListOfFunctions()->Add(func);
        h2->GetListOfFunctions()->Add(func);
    }
    else {
        // fit independently
        h1->Fit(func);
        h2->Fit(func);
    }



    // Create a new canvas.
    TCanvas * c1 = new TCanvas("c1","Two HIstogram Fit example",100,10,900,800);
    c1->Divide(2,2);
    gStyle->SetOptFit();
    gStyle->SetStatY(0.6);

    c1->cd(1);
    h1->Draw();
    func->SetRange(xlow1,ylow1,xup1,yup1);
    func->DrawCopy("cont1 same");
    c1->cd(2);
    h1->Draw("lego");
    func->DrawCopy("surf1 same");
    c1->cd(3);
    func->SetRange(xlow2,ylow2,xup2,yup2);
    h2->Draw();
    func->DrawCopy("cont1 same");
    c1->cd(4);
    h2->Draw("lego");
    gPad->SetLogz();
    func->Draw("surf1 same");

    return 0;
}
int main (int argc, char **argv)
{
  // Get the tree

  TFile *f;

  if (argc >= 3)
    {
      f = new TFile(argv[1]);

      std::cout << ">> Opening file " << argv[1] << " ......" << std::endl;
      if (!f->IsOpen())
	{			// terminate if the file can't be opened
	  std::cerr << "!! File open error:" << argv[1] << std::endl;
	  return 1;
	}
    }
  else
    {				// terminate if there is no input file or more than 1 input file
      std::cerr << "!! No input file" << std::endl;
      return 1;
    }

  TTree *tree = (TTree*)f->Get("tree");

  // get the variables from the ntuple
  float t1gausroot = 0;
  float t2gausroot = 0;
  float t3gausroot = 0;
  float t4gausroot = 0;
  float t5gausroot = 0;
  float t6gausroot = 0;
  float t7gausroot = 0;
  float t8gausroot = 0;
  float ch1Amp = 0;
  float ch2Amp = 0;
  float ch3Amp = 0;
  float ch4Amp = 0;
  float ch5Amp = 0;
  float ch6Amp = 0;
  float ch7Amp = 0;
  float ch8Amp = 0;
  unsigned int ch1QualityBit = 0;
  unsigned int ch2QualityBit = 0;
  unsigned int ch3QualityBit = 0;
  unsigned int ch4QualityBit = 0;
  unsigned int ch5QualityBit = 0;
  unsigned int ch6QualityBit = 0;
  unsigned int ch7QualityBit = 0;
  unsigned int ch8QualityBit = 0;

  tree->SetBranchAddress("t1gausroot",&t1gausroot);
  tree->SetBranchAddress("t2gausroot",&t2gausroot);
  tree->SetBranchAddress("t3gausroot",&t3gausroot);
  tree->SetBranchAddress("t4gausroot",&t4gausroot);
  tree->SetBranchAddress("t5gausroot",&t5gausroot);
  tree->SetBranchAddress("t6gausroot",&t6gausroot);
  tree->SetBranchAddress("t7gausroot",&t7gausroot);
  tree->SetBranchAddress("t8gausroot",&t8gausroot);
  tree->SetBranchAddress("ch1Amp",&ch1Amp);
  tree->SetBranchAddress("ch2Amp",&ch2Amp);
  tree->SetBranchAddress("ch3Amp",&ch3Amp);
  tree->SetBranchAddress("ch4Amp",&ch4Amp);
  tree->SetBranchAddress("ch5Amp",&ch5Amp);
  tree->SetBranchAddress("ch6Amp",&ch6Amp);
  tree->SetBranchAddress("ch7Amp",&ch7Amp);
  tree->SetBranchAddress("ch8Amp",&ch8Amp);
  tree->SetBranchAddress("ch1QualityBit",&ch1QualityBit);
  tree->SetBranchAddress("ch2QualityBit",&ch2QualityBit);
  tree->SetBranchAddress("ch3QualityBit",&ch3QualityBit);
  tree->SetBranchAddress("ch4QualityBit",&ch4QualityBit);
  tree->SetBranchAddress("ch5QualityBit",&ch5QualityBit);
  tree->SetBranchAddress("ch6QualityBit",&ch6QualityBit);
  tree->SetBranchAddress("ch7QualityBit",&ch7QualityBit);
  tree->SetBranchAddress("ch8QualityBit",&ch8QualityBit);

  //create histograms
  TH1F *dt[6];
  TH1F *resolution[6];
  TH1F *dt_slopeCorrected[6];
  TH1F *dt_shower[6];
  TH1F *dt_shower_AmpCut[6];

  TH1F *amp_all[6];
  TH1F *amp_good[6];
  TProfile *hProfCorr[6];
  TH2F *h2DCorr[6];
  TH2F *h2DCorr_Fit[6];
  TH2F *h2DCorr_slopeCorrected[6];

  for(Int_t i = 0; i < 6; i++)
    {
      dt[i] = new TH1F(Form("dt_%d",i), Form("dt_%d",i), 1500,-3.0,3.0);
      dt_slopeCorrected[i] = new TH1F(Form("dt_slopeCorrected_%d",i), Form("dt_slopeCorrected_%d",i), 700,-3.0,3.0);
      dt_shower[i] = new TH1F(Form("dt_shower_%d",i), Form("dt_shower_%d",i), 3000,-3.0,3.0);
      dt_shower_AmpCut[i] = new TH1F(Form("dt_shower_AmpCut%d",i), Form("dt_shower_AmpCut%d",i), 3000,-3.0,3.0);
      amp_all[i] = new TH1F(Form("amp_all_%d",i), Form("amp_all_%d",i), 200,0,0.55);
      amp_good[i] = new TH1F(Form("amp_good_%d",i), Form("amp_good_%d",i), 200,0,0.55);

      h2DCorr_Fit[i] = new TH2F(Form("dt_vs_amp_2D_fit_%d",i), Form("dt_vs_amp_2D_fit_%d",i), 10, 0,0.5, 300, -3.0, 3.0);
      resolution[i] = new TH1F(Form("resolution_%d",i), Form("resolution_%d",i), 10, 0, 0.05);

      hProfCorr[i] = new TProfile(Form("dt_vs_amp_%d",i), Form("dt_vs_amp_%d",i), 100, 0,0.5, -3.0, 3.0);
      h2DCorr[i] = new TH2F(Form("dt_vs_amp_2D_%d",i), Form("dt_vs_amp_2D_%d",i), 100, 0,0.5, 300, -3.0, 3.0);
      h2DCorr_slopeCorrected[i] = new TH2F(Form("dt_vs_amp_corrected_%d",i), Form("dt_vs_amp_corrected_%d",i), 100, 0,0.5, 300, -3.0, 3.0);
    }

  std::string names[6] = {"CH1-CH2", "CH1-CH3", "CH2-CH3", "CH5-CH6", "CH5-CH7", "CH6-CH7"};
  std::string Amps[6] = {"ch2Amp", "ch3Amp", "ch3Amp", "ch6Amp", "ch7Amp", "ch7Amp"};

  std::string amps_xaxis[6] = {"CH1", "CH2", "CH3", "CH5", "CH6", "CH7"};

  //read all entries and fill the histograms
  Long64_t nentries = tree->GetEntries();

  std::cout<<"Number of events in Physics Sample: "<<nentries<<std::endl;
  
  for (Long64_t iEntry=0;iEntry<nentries;iEntry++) 
    {
      tree->GetEntry(iEntry);
      
      // any good pulses
      if( ch1QualityBit==0 && ch2QualityBit==0 )
	{
	  dt[0]->Fill(t1gausroot-t2gausroot);
	  hProfCorr[0]->Fill(ch2Amp,t1gausroot-t2gausroot);
	  h2DCorr[0]->Fill(ch2Amp,t1gausroot-t2gausroot);
	  h2DCorr_Fit[0]->Fill(ch2Amp,t1gausroot-t2gausroot);
	}

      if( ch1QualityBit==0 && ch3QualityBit==0 )
	{
	  dt[1]->Fill(t1gausroot-t3gausroot);
	  hProfCorr[1]->Fill(ch3Amp,t1gausroot-t3gausroot);
	  h2DCorr[1]->Fill(ch3Amp,t1gausroot-t3gausroot);
	  h2DCorr_Fit[1]->Fill(ch3Amp,t1gausroot-t3gausroot);
	}

      if( ch2QualityBit==0 && ch3QualityBit==0 )
	{
	  dt[2]->Fill(t2gausroot-t3gausroot);
	  hProfCorr[2]->Fill(ch3Amp,t2gausroot-t3gausroot);
	  h2DCorr[2]->Fill(ch3Amp,t2gausroot-t3gausroot);
	  h2DCorr_Fit[2]->Fill(ch3Amp,t2gausroot-t3gausroot);
	}

      if( ch5QualityBit==0 && ch6QualityBit==0 )
	{
	  dt[3]->Fill(t5gausroot-t6gausroot);
	  hProfCorr[3]->Fill(ch6Amp,t5gausroot-t6gausroot);
	  h2DCorr[3]->Fill(ch6Amp,t5gausroot-t6gausroot);
	  h2DCorr_Fit[3]->Fill(ch6Amp,t5gausroot-t6gausroot);
	}

      if( ch5QualityBit==0 && ch7QualityBit==0 )
	{
	  dt[4]->Fill(t5gausroot-t7gausroot);
	  hProfCorr[4]->Fill(ch7Amp,t5gausroot-t7gausroot);
	  h2DCorr[4]->Fill(ch7Amp,t5gausroot-t7gausroot);
	  h2DCorr_Fit[4]->Fill(ch7Amp,t5gausroot-t7gausroot);
	}

      if( ch6QualityBit==0 && ch7QualityBit==0 )
	{
	  dt[5]->Fill(t6gausroot-t7gausroot);
	  hProfCorr[5]->Fill(ch7Amp,t6gausroot-t7gausroot);
	  h2DCorr[5]->Fill(ch7Amp,t6gausroot-t7gausroot);
	  h2DCorr_Fit[5]->Fill(ch7Amp,t6gausroot-t7gausroot);
	}
      
      // amplitude plots
      amp_all[0]->Fill(ch1Amp);
      amp_all[1]->Fill(ch2Amp);
      amp_all[2]->Fill(ch3Amp);
      amp_all[3]->Fill(ch5Amp);
      amp_all[4]->Fill(ch6Amp);
      amp_all[5]->Fill(ch7Amp);

      if( ch1QualityBit==0 )
	amp_good[0]->Fill(ch1Amp);
      if( ch2QualityBit==0 )
      	amp_good[1]->Fill(ch2Amp);
      if( ch3QualityBit==0 )
      	amp_good[2]->Fill(ch3Amp);
      if( ch5QualityBit==0 )
      	amp_good[3]->Fill(ch5Amp);
      if( ch6QualityBit==0 )
      	amp_good[4]->Fill(ch6Amp);
      if( ch7QualityBit==0 )
      	amp_good[5]->Fill(ch7Amp);

      // good pulses with shower requirement
      if( ch1QualityBit==0 && ch3QualityBit==0 && ch6Amp>0.49)
	dt_shower[1]->Fill(t1gausroot-t3gausroot);

      if( ch1QualityBit==0 && ch2QualityBit==0 && ch6Amp>0.49) //cherenkov selection
	dt_shower[0]->Fill(t1gausroot-t2gausroot);

      if( ch5QualityBit==0 && ch6QualityBit==0 && ch6Amp>0.49)  //cherenkov selection
	dt_shower[3]->Fill(t5gausroot-t6gausroot);

      if( ch1QualityBit==0 && ch3QualityBit==0 && ch6Amp>0.49)
	if(ch3Amp>0.15)
	  dt_shower_AmpCut[1]->Fill(t1gausroot-t3gausroot);

    }
  
  // Touch up and save
  TCanvas * c = new TCanvas("c","c",600,600);
  for(Int_t i = 0; i < 6; i++)
    {
      c->cd();
      
      // TOFs
      gPad->SetLogy(0);
      dt[i]->SetAxisRange(dt[i]->GetMean()-0.5*fabs(dt[i]->GetMean()),dt[i]->GetMean()+0.5*fabs(dt[i]->GetMean()),"X");
      dt[i]->GetXaxis()->SetTitle(Form("%s [ns]",names[i].c_str()));
      dt[i]->SetTitle(names[i].c_str());
      dt[i]->GetYaxis()->SetTitle("Number of Events");
      dt[i]->Draw();
      dt[i]->Fit("gaus");
      TVirtualFitter * fitter = TVirtualFitter::GetFitter();
      TPaveText *pt = new TPaveText(.15,.75,.35,.85,"NDC");
      pt->AddText(Form("#sigma=%.2f psec",1000*fitter->GetParameter(2)));
      pt->SetFillColor(0);
      pt->Draw();

      c->SaveAs( Form("TOF_%s_Run%s.pdf", names[i].c_str(), argv[2]) );

      // amplitudes
      amp_good[i]->SetLineWidth(2);
      amp_good[i]->SetLineColor(1);
      amp_all[i]->SetLineWidth(3);
      amp_all[i]->SetLineColor(1);
      amp_all[i]->SetLineStyle(2);
      amp_all[i]->SetStats(0);
      amp_all[i]->Draw();
      amp_all[i]->GetXaxis()->SetTitle(Form("%s  Amp [mV]", amps_xaxis[i].c_str()));
      amp_all[i]->GetYaxis()->SetTitle("Number of Events");
      amp_all[i]->Draw();
      amp_good[i]->SetLineColor(1);
      amp_good[i]->Draw("same");
      TLegend *leg = new TLegend(0.5,0.8,0.8,0.9);
      leg->AddEntry(amp_all[i],"All pulses");
      leg->AddEntry(amp_good[i],"Pass all quality bits");
      leg->SetFillColor(0);
      leg->Draw();
      gPad->SetLogy();
  

      c->SaveAs( Form("Amplitudes_%s_Run%s.root", amps_xaxis[i].c_str(), argv[2]) );
    }
  
  gPad->SetLogy(0);
  // shower requirement
  for(Int_t i = 0; i < 4; i++)
    {
      c->cd();
      
      if(i==2) continue;

      dt_shower[i]->SetAxisRange(dt_shower[i]->GetMean()-0.2*fabs(dt_shower[i]->GetMean()),
				 dt_shower[i]->GetMean()+0.2*fabs(dt_shower[i]->GetMean()),"X");
      dt_shower[i]->GetXaxis()->SetTitle(Form("%s [ns]",names[i].c_str()));
      dt_shower[i]->SetTitle(names[i].c_str());
      dt_shower[i]->GetYaxis()->SetTitle("Number of Events");
      dt_shower[i]->Draw();
      dt_shower[i]->SetLineColor(1);
      dt_shower[i]->SetStats(0);
      dt_shower[i]->SetTitle("");
      dt_shower[i]->Draw();
      dt_shower[i]->Fit("gaus");
      dt_shower[i]->GetFunction("gaus")->SetLineColor(1);
      dt_shower[i]->GetFunction("gaus")->SetLineWidth(4);


      TVirtualFitter * fitter = TVirtualFitter::GetFitter();
      TPaveText *pt = new TPaveText(.15,.75,.35,.85,"NDC");
      pt->AddText(Form("#sigma=%.2f psec",1000*fitter->GetParameter(2)));
      pt->SetFillColor(0);
      pt->Draw();

      c->SaveAs( Form("TOF_Shower_%s_Run%s.root", names[i].c_str(), argv[2]) );
    }

  c->cd();
  dt_shower_AmpCut[1]->SetAxisRange(dt_shower_AmpCut[1]->GetMean()-0.5*fabs(dt_shower_AmpCut[1]->GetMean()),
				    dt_shower_AmpCut[1]->GetMean()+0.5*fabs(dt_shower_AmpCut[1]->GetMean()),"X");
  dt_shower_AmpCut[1]->GetXaxis()->SetTitle(Form("%s [ns]",names[1].c_str()));
  dt_shower_AmpCut[1]->SetTitle(names[1].c_str());
  dt_shower_AmpCut[1]->GetYaxis()->SetTitle("Number of Events");
  dt_shower_AmpCut[1]->Draw();
  dt_shower_AmpCut[1]->Fit("gaus");
  TVirtualFitter * fitter3 = TVirtualFitter::GetFitter();
  TPaveText *pt2 = new TPaveText(.15,.75,.35,.85,"NDC");
  pt2->AddText(Form("#sigma=%.2f psec",1000*fitter3->GetParameter(2)));
  pt2->SetFillColor(0);
  pt2->Draw();

  c->SaveAs( Form("TOF_Shower_AmpCut_%s_Run%s.pdf", names[1].c_str(), argv[2]) );

  // slopes
  TF1 * slopes[6];
  for(Int_t i = 0; i < 6; i++)
    {
      slopes[i] = new TF1(Form("slopes_%d",i),"pol1", 0, h2DCorr[i]->GetNbinsX());
      slopes[i]->SetLineColor(1);
      slopes[i]->SetLineWidth(3);
    }

  for(Int_t i = 0; i < 6; i++)
    {
      c->cd();

      hProfCorr[i]->Fit(slopes[i],"Q","", 0, h2DCorr[i]->GetNbinsX());

      UInt_t Number = 3;
      Double_t Red[3]   = { 0.15, 0.6, 0.9};
      Double_t Green[3] = { 0.15, 0.6, 0.9};
      Double_t Blue[3]  = { 0.15, 0.6, 0.9};
      Double_t Stops[3] = { 0.00, 0.25, 1.0};
      
      Int_t nb=50;
      TColor::CreateGradientColorTable(Number,Stops,Red,Green,Blue,nb);
      
      h2DCorr[i]->Draw("colz");
      h2DCorr[i]->SetLineColor(1);
      h2DCorr[i]->SetAxisRange(h2DCorr[i]->GetMean(2) - 0.3*fabs(h2DCorr[i]->GetMean(2)),
			       h2DCorr[i]->GetMean(2) + 0.3*fabs(h2DCorr[i]->GetMean(2)),"Y");
      h2DCorr[i]->SetAxisRange(0, GetLastXBin(h2DCorr[i]) + 0.05,"X");
      h2DCorr[i]->GetYaxis()->SetTitle(Form("Time Difference %s", names[i].c_str()));
      h2DCorr[i]->GetXaxis()->SetTitle(Form("%s", Amps[i].c_str()));
      h2DCorr[i]->SetTitle("");
      h2DCorr[i]->SetStats(0);
      hProfCorr[i]->SetLineColor(1);
      hProfCorr[i]->SetMarkerStyle(8);

      hProfCorr[i]->Draw("same");
      
      c->SaveAs( Form("TOF_vs_Amp_%s_%s_Run%s.pdf", names[i].c_str(), Amps[i].c_str(), argv[2]) );

      
      // resolution vs amplitude plots
      for(int j = 0; j < h2DCorr_Fit[i]->GetNbinsX(); j++)
	{
	  TH1D *py = h2DCorr_Fit[i]->ProjectionY("py", j, j + 1 ); 
	  py->Fit("gaus");
	  TVirtualFitter * fitter22 = TVirtualFitter::GetFitter();
	  resolution[i] -> SetBinContent(j, 1000*fitter22->GetParameter(2));
	  if (1000*fitter22->GetParameter(2) > 100. ) 
	    resolution[i] -> SetBinContent(j, 0.0);
	}
      resolution[i]->GetXaxis()->SetTitle(Form("%s", Amps[i].c_str()));
      resolution[i]->GetYaxis()->SetTitle(Form("Resolution in %s [psec]", names[i].c_str()));
      resolution[i]->Draw();
	    
      c->SaveAs( Form("Resolution_vs_Amp_%s_%s_Run%s.pdf", names[i].c_str(), Amps[i].c_str(), argv[2]) );
    }

  // correction for slopes
  std::cout<<"Second round: slope correction "<<nentries<<std::endl;
  
  for (Long64_t iEntry=0;iEntry<nentries;iEntry++) 
    {
      tree->GetEntry(iEntry);
      
      // any good pulses
      if( ch1QualityBit==0 && ch3QualityBit==0 && ch6Amp>0.49)
	{
	  float a = slopes[1]->GetParameter(0);
	  float b = slopes[1]->GetParameter(1);
	  dt_slopeCorrected[1]->Fill(t1gausroot-t3gausroot + (a+b*0.02) - (a+b*ch3Amp));
	  h2DCorr_slopeCorrected[1]->Fill(ch3Amp,t1gausroot-t3gausroot + (a+b*0.02) - (a+b*ch3Amp));
	}
    }
  
  c->cd();
  dt_slopeCorrected[1]->SetAxisRange(dt_slopeCorrected[1]->GetMean()-0.2*fabs(dt_slopeCorrected[1]->GetMean()),
				     dt_slopeCorrected[1]->GetMean()+0.2*fabs(dt_slopeCorrected[1]->GetMean()),"X");
  dt_slopeCorrected[1]->GetXaxis()->SetTitle(Form("%s [ns]",names[1].c_str()));
  dt_slopeCorrected[1]->SetTitle(names[1].c_str());
  dt_slopeCorrected[1]->GetYaxis()->SetTitle("Number of Events");
  dt_slopeCorrected[1]->SetLineColor(1);
  // dt_slopeCorrected[1]->SetStats(0);
  dt_slopeCorrected[1]->SetTitle("");
  dt_slopeCorrected[1]->Draw();
  dt_slopeCorrected[1]->Fit("gaus");
  dt_slopeCorrected[1]->GetFunction("gaus")->SetLineColor(1);
  dt_slopeCorrected[1]->GetFunction("gaus")->SetLineWidth(4);
  TVirtualFitter * fitter2 = TVirtualFitter::GetFitter();
  TPaveText *pt1 = new TPaveText(.15,.75,.35,.85,"NDC");
  pt1->AddText(Form("#sigma=%.2f psec",1000*fitter2->GetParameter(2)));
  pt1->SetFillColor(0);
  pt1->Draw();
  
  c->SaveAs( Form("TOF_SlopeCorrected_%s_Run%s.pdf", names[1].c_str(), argv[2]) );
  
  // 2D
  c->cd();
  
  h2DCorr_slopeCorrected[1]->Draw("colz");
  h2DCorr_slopeCorrected[1]->SetAxisRange(h2DCorr_slopeCorrected[1]->GetMean(2) - 0.7*fabs(h2DCorr_slopeCorrected[1]->GetMean(2)),
  			   h2DCorr_slopeCorrected[1]->GetMean(2) + 0.7*fabs(h2DCorr_slopeCorrected[1]->GetMean(2)),"Y");
  h2DCorr_slopeCorrected[1]->SetAxisRange(0, GetLastXBin(h2DCorr_slopeCorrected[1]) + 0.05,"X");
  h2DCorr_slopeCorrected[1]->GetYaxis()->SetTitle(Form("Time Difference %s", names[1].c_str()));
  h2DCorr_slopeCorrected[1]->GetXaxis()->SetTitle(Form("%s", Amps[1].c_str()));
  
  c->SaveAs( Form("TOF_vs_Amp_Corrected_%s_%s_Run%s.pdf", names[1].c_str(), Amps[1].c_str(), argv[2]) );
 
}
void makeRisetimeDistributionT9(string filename, string plotname, string plotTitle,
			      double ampCutOnMCP, double ampCutOnLYSO, double ampCutOnTrigger,
			    double beamXMin, double beamXMax, double beamYMin, double beamYMax,
			    int nbins, double xmin, double xmax, double fitmin, double fitmax) {


  TFile *inputfile = TFile::Open(filename.c_str(),"READ");
  
  TTree *tree = (TTree*)inputfile->Get("t1065");

  // get the variables from the ntuple
  float amp[36];
  float risetime[36];
  float gauspeak[36];
  float linearTime30[36];
  float beamX;
  float beamY;

  tree->SetBranchStatus("*",0);
  tree->SetBranchStatus("gauspeak",1);
  tree->SetBranchStatus("amp",1);
  tree->SetBranchStatus("risetime",1);
  tree->SetBranchStatus("linearTime30",1);
  tree->SetBranchStatus("TDCx",1);
  tree->SetBranchStatus("TDCy",1);
  tree->SetBranchAddress("gauspeak",gauspeak);
  tree->SetBranchAddress("amp",amp);
  tree->SetBranchAddress("risetime",risetime);
  tree->SetBranchAddress("linearTime30",linearTime30);
  tree->SetBranchAddress("TDCx",&beamX);
  tree->SetBranchAddress("TDCy",&beamY);

  //create histograms
  TH1F *histRisetime;
  histRisetime = new TH1F("histRisetime","; Integrated Charge [pC];Number of Events", nbins, xmin, xmax);

  
  //read all entries and fill the histograms
  Long64_t nentries = tree->GetEntries();

  std::cout<<"Number of events in Sample: "<<nentries<<std::endl;  
  for (Long64_t iEntry=0;iEntry<nentries;iEntry++) {
    if (iEntry %1000 == 0) 
      cout << "Processing Event " << iEntry << "\n";
    tree->GetEntry(iEntry);    
    // cout << "here1\n";
    float MCPTimeGauss = gauspeak[0];
    float CdTeTime = linearTime30[1];
    float MCPAmp = amp[0];
    float LYSOAmp = amp[2];
    float TriggerAmp = amp[3];
    float CherenkovAmp = amp[7];
    float CdTeAmp = amp[1]*(1.0/63.0957);
    float CdTeRisetime = risetime[1];
    // cout << "here2\n";
       
    //use MCP amplitude cut for electron ID
    //cout << "test: " << MCPAmp << " " << siliconIntegral << "\n";
    if( !(MCPAmp > ampCutOnMCP)) continue;
    if( !(TriggerAmp > ampCutOnTrigger)) continue;
    if( !(LYSOAmp > ampCutOnLYSO)) continue;
    if(!(beamX > beamXMin && beamX < beamXMax)) continue;
    if(!(beamY > beamYMin && beamY < beamYMax)) continue;
     // cout << "here3\n";

    //don't fill overflow bins
    //if (1000* siliconIntegral * attenuationFactor / amplificationFactor > xmax) continue;
    
    histRisetime->Fill( CdTeRisetime );

    //cout << CdTeRisetime << " " << beamX << " " << beamY << " " << CdTeAmp << "\n";

    //cout << 1000* amp[21] << " : " << amplificationFactor << " : " << siliconIntegral * attenuationFactor / amplificationFactor << "\n";
 
  }


  TCanvas * c = 0;


  //Energy plot
  c = new TCanvas("c","c",600,600);  
  c->SetRightMargin(0.05);
  c->SetLeftMargin(0.17);
  histRisetime->SetAxisRange(xmin,xmax,"X");
  histRisetime->SetTitle("");
  histRisetime->GetXaxis()->SetTitle("Risetime [ns]");
  histRisetime->GetXaxis()->SetTitleSize(0.045);
  histRisetime->GetXaxis()->SetLabelSize(0.045);
  histRisetime->GetYaxis()->SetTitle("Number of Events");
  histRisetime->GetYaxis()->SetTitleOffset(1.3);
  histRisetime->GetYaxis()->SetTitleSize(0.05);
  histRisetime->GetYaxis()->SetLabelSize(0.045);
  histRisetime->GetYaxis()->SetLabelOffset(0.015);
  histRisetime->GetYaxis()->SetTitleOffset(1.7);
  histRisetime->SetMaximum(1.2*histRisetime->GetMaximum());
  histRisetime->Draw();
  histRisetime->SetStats(0);
  histRisetime->Fit("gaus","","",fitmin,fitmax);
  TVirtualFitter * fitter = TVirtualFitter::GetFitter();
  
  TLatex *tex = new TLatex();
  tex->SetNDC();
  tex->SetTextSize(0.050);
  tex->SetTextFont(42);
  tex->SetTextColor(kBlack);
  tex->DrawLatex(0.45, 0.85, Form("Mean = %.2f %s",fitter->GetParameter(1),"ns"));
  tex->DrawLatex(0.45, 0.80, Form("#sigma = %.2f %s",fitter->GetParameter(2),"ns"));

  tex->DrawLatex(0.18, 0.93, Form("%s", plotTitle.c_str()));

  c->SaveAs( Form("%s_risetime.gif", plotname.c_str()) );
  c->SaveAs( Form("%s_risetime.pdf", plotname.c_str()) );
 

}
void makegraph_xyhits_mchamp1000(){

  //bool save_plots = false;
  bool save_plots = true;

  TCanvas* canvas = new TCanvas("c1","c1",10,10,700,550);
  //canvas_style(canvas);

  //TH2* h1=new TH2F("h1","",2000,0,2000,2000,-1000,1000);
  TH2* h1=new TH2F("h1","",1000,-1000,0,1000,0,1000);
  h1->SetStats(kFALSE);
  h1->SetTitle(";x [cm];y [cm]");
  //h2_style(h1,3,1,1,1001,50,-1111.,-1111.,510,510,20,1,1.4,0);

  const Int_t n_hits = 20;
  Float_t x[n_hits] = {-522.992,
		       -524.292,
		       -525.592,
		       -526.892,
		       -606.525,
		       -607.825,
		       -609.124,
		       -610.424,
		       -630.144,
		       -631.444,
		       -632.744,
		       -634.044,
		       -741.681,
		       -743.086,
		       -744.378,
		       -745.665,
		       -765.758,
		       -767.126,
		       -768.413,
		       -769.755};
  
  Float_t y[n_hits] = {82.024,
		       82.388,
		       82.661,
		       82.971,
		       101.400,
		       101.678,
		       101.929,
		       102.318,
		       106.826,
		       107.214,
		       107.421,
		       107.769,
		       132.755,
		       132.924,
		       133.286,
		       133.657,
		       138.406,
		       138.637,
		       139.007,
		       139.285};

  graph = new TGraph(n_hits, x, y);
  //gr_style(graph,1,1,1,1001,50,-1111,-1111,510,510,20,1,1.3,1);
  graph->SetMarkerStyle(20);
  graph->SetMarkerSize(1);
  graph->SetMarkerColor(1);
  graph->SetTitle(";x [cm];y [cm]");
  //graph->GetYaxis()->SetRangeUser(0,20);


  //Fit a circle to the graph points
  TVirtualFitter::SetDefaultFitter("Minuit");  //default is Minuit
  TVirtualFitter *fitter = TVirtualFitter::Fitter(0, 3);
  Int_t a,c;
  Double_t b,f,par;
  fitter->SetFCN(myfcn);

  fitter->SetParameter(0, "x0",   0,    0.1, -1000, 1000);
  fitter->SetParameter(1, "y0",   1000, 0.1, 0,     2000);
  fitter->SetParameter(2, "R",    1000, 0.1, 0,     1000);

  Double_t arglist[1] = {0};
  fitter->ExecuteCommand("MIGRAD", arglist, 0);

  //Draw the circle on top of the points
  TArc *arc = new TArc(fitter->GetParameter(0),
		       fitter->GetParameter(1),fitter->GetParameter(2));
  arc->SetLineColor(kRed);
  arc->SetLineWidth(4);



  Leg1 = new TLegend(0.45,0.75,0.75,0.85);
  //Leg1->AddEntry(graph_noChaCut,"Average Before TS","p");
  Leg1->AddEntry(graph,"After TS","p");
  //Leg1->AddEntry(graph_predicted,"Predicted After TS","p");
  Leg1->SetBorderSize(0);
  Leg1->SetTextSize(0.04);
  Leg1->SetFillColor(0);


  //TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV","NDC");
  //TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV    3.4 fb^{-1}","NDC");
  //TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV    4.4 fb^{-1}","NDC");
  TPaveLabel *text1 = new TPaveLabel(.17,.85,.37,.89,"CMS Preliminary    #sqrt{s}=8 TeV","NDC");
  text1->SetBorderSize(0);
  text1->SetTextSize(0.7);
  text1->SetFillColor(0);

  TPaveLabel *text2 = new TPaveLabel(.17,.85,.37,.89,"1000 GeV H++, Event 58654","NDC");
  text2->SetBorderSize(0);
  text2->SetTextSize(0.7);
  text2->SetFillColor(0);


  TPaveText* text1a = new TPaveText(-500,500,-300,700);
  text1a->SetBorderSize(0);
  text1a->SetTextSize(0.05);
  text1a->SetFillColor(0);
  double p0  = fitter->GetParameter(0);
  double p1  = fitter->GetParameter(1);
  double p2  = fitter->GetParameter(2);
  double e0  = fitter->GetParError(0);
  double e1  = fitter->GetParError(1);
  double e2  = fitter->GetParError(2);
  char p0name[50], p1name[50], p2name[50];
  sprintf(p0name,"x0 = %.2f #pm %.2f",p0,e0);
  sprintf(p1name,"y0 = %.2f #pm %.2f",p1,e1);
  sprintf(p2name,"R = %.2f #pm %.2f",p2,e2);
  text1a->AddText(p0name);
  text1a->AddText(p1name);
  text1a->AddText(p2name);


  canvas->cd();
  h1->Draw();
  arc->Draw("same");
  graph->Draw("Psame");
  //text1->Draw();
  text1a->Draw();
  text2->Draw();
  //Leg1->Draw();
  if(save_plots) canvas->SaveAs("../plots/hits_xy_mchamp1000.eps");

  //return 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();


}