double* IfitBin(TH1D* dataInput, TH1D* sigTemplate, TH1D* bkgTemplate)
{
   // Start TFractionFitter
   double Spara(0), eSpara(0);
   double Bpara(0), eBpara(0);
   TObjArray *mc = new TObjArray(2);
   mc->Add(sigTemplate);
   mc->Add(bkgTemplate);

   TFractionFitter *fitTemplate = new TFractionFitter(dataInput, mc);
   fitTemplate->Constrain(0, 0.0, 1.0);
   fitTemplate->Constrain(1, 0.0, 1.0);
   int status = fitTemplate->Fit();
   cout<<" Fitting status = "<<status<<endl;
   if (status == 0) {
      fitTemplate->GetResult(0, Spara, eSpara);
      fitTemplate->GetResult(1, Bpara, eBpara);

      cout<<" Fitting result = "<<endl;
      cout<<"               Chi2 = "<<fitTemplate->GetChisquare()<<endl;
      cout<<"                NDF = "<<fitTemplate->GetNDF()<<endl;
      cout<<"               Prob = "<<fitTemplate->GetProb()<<endl;
      cout<<"      Signal fraction     = "<<Spara<<"; Error = "<<eSpara<<endl;
      cout<<"      Background fraction = "<<Bpara<<"; Error = "<<eBpara<<endl;
   }

   TH1D *FitResultReal = (TH1D*)sigTemplate->Clone();
   TH1D *FitResultFake = (TH1D*)bkgTemplate->Clone();
   TH1D *FitResultAll  = (TH1D*)dataInput->Clone();
   FitResultReal->SetName("ResultReal");
   FitResultFake->SetName("ResultFake");
   FitResultAll->SetName("ResultAll");
   FitResultReal->Scale(1./FitResultReal->Integral()*Spara*dataInput->Integral());
   FitResultFake->Scale(1./FitResultFake->Integral()*Bpara*dataInput->Integral());
   FitResultAll->Reset();
   FitResultAll->Add(FitResultReal);
   FitResultAll->Add(FitResultFake);
   
   TCanvas *c1 = new TCanvas("c1", "", 600, 400);
   c1->cd();
   FitResultAll->SetXTitle("#sigma_{i#etai#eta}");
   FitResultAll->SetYTitle("Number of photons");
   FitResultAll->SetMinimum(0);
   FitResultAll->SetMaximum(FitResultAll->GetMaximum()*1.4);
   FitResultAll->SetLineColor(1);
   FitResultAll->SetLineWidth(2);
   FitResultAll->Draw();
   dataInput->SetMarkerStyle(21);
   dataInput->SetMarkerSize(0.7);
   dataInput->SetLineColor(1);
   dataInput->SetLineWidth(2);
   dataInput->Draw("PE1same");
   FitResultReal->SetLineColor(2);
   FitResultReal->SetFillColor(2);
   FitResultReal->SetFillStyle(3002);
   FitResultReal->Draw("same");
   FitResultFake->SetLineColor(4);
   FitResultFake->SetFillColor(4);
   FitResultFake->SetFillStyle(3004);
   FitResultFake->Draw("same");
   TLegend *leg1 = new TLegend(0.5,0.5,0.9,0.85);
   char text[200];
   leg1->SetFillColor(0);
   leg1->SetShadowColor(0);
   leg1->SetFillStyle(0);
   leg1->SetBorderSize(0);
   leg1->SetLineColor(0);
   sprintf(text,"Data: %5.1f events", dataInput->Integral());
   leg1->AddEntry(dataInput, text, "pl");
   sprintf(text,"Fitted: %5.1f events", FitResultAll->Integral());
   leg1->AddEntry(FitResultAll, text, "l");
   sprintf(text,"Signal %5.1f #pm %5.1f events", FitResultReal->Integral(), eSpara/Spara*FitResultReal->Integral());
   leg1->AddEntry(FitResultReal, text, "f");
   sprintf(text,"Background %5.1f #pm %5.1f events", FitResultFake->Integral(), eBpara/Bpara*FitResultFake->Integral());
   leg1->AddEntry(FitResultFake, text, "f");
   leg1->Draw();

   return;
}
//______________________________________________________________________________
void TFractionFitFCN(Int_t& npar, Double_t* gin, Double_t& f, Double_t* par, Int_t flag) {
   // Function called by the minimisation package. The actual functionality is passed
   // on to the TFractionFitter::ComputeFCN member function.

   TFractionFitter* fitter = dynamic_cast<TFractionFitter*>(fractionFitter->GetObjectFit());
   if (!fitter) {
      Error("TFractionFitFCN","Invalid fit object encountered!");
      return;
   }
   fitter->ComputeFCN(npar, gin, f, par, flag);
}
Example #3
0
void frac_fit(){
  double Ndata []  = {315239.0, 40524.1, 8625.2};
  double NdataE[]  = {   579.8,   202.8,   95.4};
  double NW []     = {258150.0,    0.38,   14.8};
  double NWE[]     = {   502.8,    0.38,    3.1};
  double NZ []     = { 19473.9, 37494.8,  181.4};
  double NZE[]     = {    61.7,    75.4,    5.2};
  double Ntt []    = { 33156.3,  1637.8, 8214.0};
  double NttE[]    = {    32.9,     8.2,   18.3};
  double Nother [] = { 20934.8,   606.9,  470.8};
  double NotherE[] = {   494.4,     5.3,   10.7};

  TH1F *W     = new TH1F("W"    ,"W"    , 3, 0.5, 3.5);
  TH1F *Z     = new TH1F("Z"    ,"Z"    , 3, 0.5, 3.5);
  TH1F *tt    = new TH1F("tt"   ,"tt"   , 3, 0.5, 3.5);
  TH1F *other = new TH1F("other","other", 3, 0.5, 3.5);
  TH1F *data  = new TH1F("data" ,"data" , 3, 0.5, 3.5);
	
  TH1F *weightW  = new TH1F("W_weight" , "W_weight" , 3, 0.5, 3.5);	
  TH1F *weightZ  = new TH1F("Z_weight" , "Z_weight" , 3, 0.5, 3.5);	
  TH1F *weighttt = new TH1F("tt_weight", "tt_weight", 3, 0.5, 3.5);	

  for(int i=0; i<3; i++){
    double Nw_prime     = TMath::Power(NW[i]/NWE[i]        , 2); 
    double NZ_prime     = TMath::Power(NZ[i]/NZE[i]        , 2); 
    double Ntt_prime    = TMath::Power(Ntt[i]/NttE[i]      , 2); 
    //double Nother_prime = TMath::Power(Nother[i]/NotherE[i], 2); 
    W ->SetBinContent(i+1, Nw_prime );
    Z ->SetBinContent(i+1, NZ_prime );
    tt->SetBinContent(i+1, Ntt_prime);

    weightW -> SetBinContent(i+1, NW[i] /W ->GetBinContent(i+1));   
    weightZ -> SetBinContent(i+1, NZ[i] /Z ->GetBinContent(i+1));   
    weighttt-> SetBinContent(i+1, Ntt[i]/tt->GetBinContent(i+1));   
    //    other   -> SetBinContent(i+1, Nother[i] );
    data    -> SetBinContent(i+1, Ndata[i]);
  }

  W ->SetLineColor(kRed  );
  Z ->SetLineColor(kBlue );
  tt->SetLineColor(kGreen);

	for(int i=1; i<=3; i++){
	//	cout << "data: " << data->GetBinContent(i) << " W " << W->GetBinContent(i) << " Z " << Z->GetBinContent(i) << " tt " << tt->GetBinContent(i) << endl; 	
	//	cout << "data weight=1, W weight "  << weightW->GetBinContent(i) << " Z weight: " << weightZ->GetBinContent(i) << " weight tt " << weighttt->GetBinContent(i) << endl; 
	}
	
  TObjArray *mc= new TObjArray(4);
  mc->Add(W);
  mc->Add(Z);
  mc->Add(tt);
  //  mc->Add(other);
  TFractionFitter *fit = new TFractionFitter(data,mc);
  //fit->GetFitter()->FixParameter(3);
	fit->SetWeight(0,weightW);
	fit->SetWeight(1,weightZ);
	fit->SetWeight(2,weighttt);
	//fit->Constrain(0,0,2);
	//fit->Constrain(1,0,2);
	//fit->Constrain(2,0,2);

  fit->Fit();
  double R[3]; 

  for(int i=0; i<3; i++){
    double x=0;
    double xE=0;
    fit->GetResult(i,x,xE);
    double N=0; 
    if (i==0) N = NW[i] ; 
    if (i==1) N = NZ[i] ;
    if (i==2) N = Ntt[i];
	  cout << "x " << x << endl; 
	  cout << " initial fraction: " << Ndata[i]/N << endl; 
    R[i]=x*(Ndata[i]/N);
    cout << "R: " << R[i] << " +/- " << (xE/x)*R[i] << endl; 

  }
}
Example #4
0
void ReactorNuAnalysis()
{
	// -------- VARIABLE DEFINITIONS and SETUP --------
	// Style settings
	gStyle->SetOptStat("");
	
	// Constants
	Double_t NuSpectrumMinE = 0.0;  // [MeV]
	Double_t NuSpectrumMaxE = 10.0; // [MeV]
	Int_t seed = 43534;
	const Double_t Gfermi = 1.16637e-11;																			 // [MeV^-2]
	const Double_t Sin2ThetaW = 0.2387;
	const Double_t InverseMeVtoCm = 1.97e-11;
	const Double_t elementaryCharge = 1.602176565e-19;
	
	// Spectrum files (these are just samples for now)
	const char ReactorNeutronBackgroundFile[] = "SampleData.txt";
	
	// Define constants relevant for this run
	Double_t OnOffTimeRatio = 1.0/1.0;
	Double_t time = 100 * 24.0*3600.0;																				 // [sec]
	Double_t detMass = 5000.0;																								 // [g]
	Double_t distance = 200.0;																								 // [cm]
	Double_t activity = 6.0/200.0/elementaryCharge;														 // [sec^-1]
	const Int_t nNeutrons = 14;
	const Int_t nProtons = 14;
	const Double_t Qweak = nNeutrons - (1 - 4*Sin2ThetaW) * nProtons;
	const Double_t NucleonMass = (nNeutrons * 939.565) + (nProtons * 938.272);			 // [MeV]
	
	// Define the histograms
	TH1F* RecoilEvtHistogram = new TH1F("RecoilEvtHistogram","^{28}Si recoil energy spectrum;Energy [eV];Events / 10 eV",50,0.0,500.0);
	TH1F* NeutrinoEvtHistogram = new TH1F("NeutrinoEvtHistogram","#bar{#nu}_{e} energy spectrum;Energy [MeV];Events / 0.5 MeV",50,0.0,20);
	TH1F* TheoryRecoilEvtHistogram = new TH1F("TheoryRecoilEvtHistogram","High-statistics (\"theoretical\") Coherent Recoil Spectrum;Energy [eV];Events / 10 eV",50,0.0,500.0);
	TH1F* TheoryNeutrinoEvtHistogram = new TH1F("TheoryNeutrinoEvtHistogram","High-statistics (\"theoretical\") Neutrino Energy Spectrum;Energy [MeV] / 0.5 MeV;Events",50,0.0,20);
	TH1F* TheoryRecoilEvtHistogramFit = new TH1F("TheoryRecoilEvtHistogram","MC Fit;Events",50,0.0,0.002);
	TH1F* EMNoLukeBackground = new TH1F("EMNoLukeBackground","Background from EM recoils (before Luke Effect amplification;Energy [MeV];Events)",50,0.0,0.002);
	TH1F* ReactorNeutronBackground = new TH1F("ReactorNeutronBackground","Background from reactor neutrons;Energy [MeV];Events",50,0.0,0.002);
	TH1F* ReactorOnCosmoNeutronBackground = new TH1F("ReactorOnCosmoNeutronBackground","Reactor-on background from muon-induced neutrons;Energy [MeV];Events)",50,0.0,0.002);
	TH1F* ReactorOffCosmoNeutronBackground = new TH1F("ReactorOffCosmoNeutronBackground","Reactor-off background from muon-induced neutrons;Energy [MeV];Events)",50,0.0,0.002);	
	TH1F* ReactorOnHisto = new TH1F("ReactorOnHisto","Recoil Spectrum for Reactor-On Data;Energy [MeV];Events",50,0.0,0.002);
	TH1F* ReactorOffHisto = new TH1F("ReactorOffHisto","Recoil Spectrum for Reactor-Off Data;Energy [MeV];Events",50,0.0,0.002);
	TH1F* BackgroundSubtractedSignal = new TH1F("BackgroundSubtractedSignal","Recoil Spectrum for Reactor-Off Data;Energy [MeV];Events",50,0.0,0.002);
	
	// Energy spectra
	// (Spectral parameterizations from arXiv:1101.2663v3)
	TF1* NeutrinoEnergySpectrum = new TF1("NeutrinoSpectrum", "TMath::Exp([0] + [1]*x + [2]*TMath::Power(x,2) + [3]*TMath::Power(x,3) + [4]*TMath::Power(x,4) + [5]*TMath::Power(x,5))", NuSpectrumMinE, NuSpectrumMaxE);
	NeutrinoEnergySpectrum->SetParameters(3.217, -3.111, 1.395, -0.369, 0.04445, -0.002053);
	TF1* IntegratedRecoilSpectrum = new TF1("IntegratedRecoilSpectrum", "TMath::Power([0]*[1]*[3],2)/(4*TMath::Pi()) * [2] * (x/(1 + [2]/(2*x))) * (1 - ([2]/(4*x*x)) * (x/(1 + [2]/(2*x))))", NuSpectrumMinE, NuSpectrumMaxE);
	IntegratedRecoilSpectrum->SetParameters(Gfermi, Qweak, NucleonMass, InverseMeVtoCm);
	TF1* RecoilSpectrum = new TF1("RecoilSpectrum","IntegratedRecoilSpectrum * NeutrinoSpectrum", 0.0, 10.0);
	TF1* DiffRecoilSpectrumAtConstE = new TF1("DiffRecoilSpectrumAtConstE", "(x<[4])*TMath::Power([0]*[1],2)/(4*TMath::Pi()) * [2] * (1 - ([2] * x)/(2 * TMath::Power([3],2))) + (x>[4])*0", NuSpectrumMinE, 0.01);
	DiffRecoilSpectrumAtConstE->SetParameters(Gfermi, Qweak, NucleonMass);
	
	
	
	
	// -------- HISTOGRAM FILLING --------
	// Fill "experimental" histograms
	Int_t nEvt = GenerateNumOfNuRecoils(time, detMass, distance, activity, nNeutrons, nProtons, RecoilSpectrum, NuSpectrumMinE, NuSpectrumMaxE, seed);
	FillNuRecoilSpectrum(RecoilEvtHistogram, NeutrinoEvtHistogram, nEvt, nNeutrons, nProtons, RecoilSpectrum, DiffRecoilSpectrumAtConstE, NuSpectrumMinE, NuSpectrumMaxE, seed+4);
	FillRecoilSpectrumFromFile(ReactorNeutronBackground, 100.0, 10.0, ReactorNeutronBackgroundFile, seed+1);      // Testing purposes only.  CHANGE ME!!
	FillRecoilSpectrumFromFile(ReactorOnCosmoNeutronBackground, 50.0, 10.0, ReactorNeutronBackgroundFile, seed+2);		// Testing purposes only.  CHANGE ME!!
	FillRecoilSpectrumFromFile(ReactorOffCosmoNeutronBackground, 50.0, 10.0, ReactorNeutronBackgroundFile, seed+3);		// Testing purposes only.  CHANGE ME!!
	
	cout << "nEvt: " << nEvt << endl;
	
	// Fill high-statistics "theoretical" histograms
	FillNuRecoilSpectrum(TheoryRecoilEvtHistogram, TheoryNeutrinoEvtHistogram, 10000, nNeutrons, nProtons, RecoilSpectrum, DiffRecoilSpectrumAtConstE, NuSpectrumMinE, NuSpectrumMaxE, seed+5);
	TheoryNeutrinoEvtHistogram->Scale(nEvt/TheoryNeutrinoEvtHistogram->GetEntries());
	TheoryRecoilEvtHistogram->Scale(nEvt/TheoryNeutrinoEvtHistogram->GetEntries());
	
	// Combine the histograms into total
	ReactorOnHisto->Add(RecoilEvtHistogram);
	ReactorOnHisto->Add(ReactorNeutronBackground);
	ReactorOnHisto->Add(ReactorOnCosmoNeutronBackground);
	
	ReactorOffHisto->Add(ReactorOffCosmoNeutronBackground);
	
	
	
	
	// -------- HYPOTHESIS TESTING --------
	cout << "p-value between Reactor-On and Reactor-Off Data: " << ReactorOnHisto->Chi2Test(ReactorOffHisto) << endl;
	cout << "p-value between the simulated data and the Monte Carlo histogram: " << RecoilEvtHistogram->Chi2Test(TheoryRecoilEvtHistogram) << endl;
	
	
	
	
	// -------- BACKGROUND SUBTRACTION and FITTING --------
	// Normalize reactor-off data to reactor-on data by exposure
	BackgroundSubtractedSignal->Add(ReactorOnHisto, ReactorOffHisto, 1.0, -1.0*OnOffTimeRatio);
	
	// Use TFractionFitter to do fitting
	TObjArray *FractionFitData = new TObjArray(2);
	FractionFitData->Add(TheoryRecoilEvtHistogram);
	FractionFitData->Add(ReactorOffHisto);
	TFractionFitter* ffit = new TFractionFitter(ReactorOnHisto, FractionFitData);
	ffit->Constrain(0,0.1,10.0);
	ffit->Constrain(1,1.0,1.0);
	Int_t status = ffit->Fit();
	TH1F* result = (TH1F*) ffit->GetPlot();
	
	// Build a stacked histogram for plotting
	Double_t param, error;
	THStack *ReactorOnStackedFit = new THStack("ReactorOnStackedFit","Signal fits for Reactor-On data");
	ffit->GetResult(0,param,error);
	TheoryRecoilEvtHistogramFit->Add(TheoryRecoilEvtHistogram,param);
	ReactorOnStackedFit->Add(TheoryRecoilEvtHistogramFit);
	ReactorOnStackedFit->Add(ReactorOffHisto);
	
	
	
	// -------- MAKE PLOTS --------
	// Set drawing settings
	RecoilEvtHistogram->SetLineColor(1);
	NeutrinoEvtHistogram->SetLineColor(1);
	TheoryRecoilEvtHistogram->SetLineColor(2);
	TheoryNeutrinoEvtHistogram->SetLineColor(2);
	ReactorOnHisto->SetLineColor(1);
	result->SetLineColor(2);
	
	// Draw everything
	TCanvas* c1 = new TCanvas("c1");
	RecoilEvtHistogram->Draw("E1");
	TheoryRecoilEvtHistogram->Draw("same");
	legend1 = new TLegend(0.6,0.7,0.89,0.89);
  legend1->AddEntry(RecoilEvtHistogram,"Data","lep");
	legend1->AddEntry(TheoryRecoilEvtHistogram,"Monte Carlo","l");
	legend1->SetFillColor(0);
  legend1->Draw();
	
	TCanvas* c2 = new TCanvas("c2");
	NeutrinoEvtHistogram->Draw("E1");
	TheoryNeutrinoEvtHistogram->Draw("same");
	legend2 = new TLegend(0.6,0.7,0.89,0.89);
  legend2->AddEntry(RecoilEvtHistogram,"Data","lep");
	legend2->AddEntry(TheoryRecoilEvtHistogram,"Monte Carlo","l");
	legend2->SetFillColor(0);
  legend2->Draw();
	
	TCanvas* c3 = new TCanvas("c3");
	ReactorOnHisto->Draw("E1");
	ReactorOffHisto->Draw("E1,same");
	legend3 = new TLegend(0.6,0.7,0.89,0.89);
	legend3->AddEntry(ReactorOnHisto,"Reactor On","lep");
	legend3->AddEntry(ReactorOffHisto,"Reactor Off","lep");
	legend3->SetFillColor(0);
  legend3->Draw();
	
	TCanvas* c4 = new TCanvas("c4");
	ReactorNeutronBackground->Draw("E1");
	
	//This plot is broken: THStack is not being used correctly
	/*TCanvas* c5 = new TCanvas("c5");
	ReactorOnHisto->Draw("E1");
	TheoryRecoilEvtHistogramFit->SetFillColor(kRed);
	TheoryRecoilEvtHistogramFit->SetMarkerStyle(1);
	TheoryRecoilEvtHistogramFit->SetMarkerColor(kRed);
	ReactorOffHisto->SetFillColor(kBlue);
	ReactorOffHisto->SetMarkerStyle(1);
	ReactorOffHisto->SetMarkerColor(kBlue);
	ReactorOnStackedFit->Draw("same");
	legend5 = new TLegend(0.6,0.7,0.89,0.89);
	legend5->AddEntry(ReactorOnHisto,"Reactor On","lep");
	legend5->AddEntry(ReactorOffHisto,"Reactor Off");
	legend5->AddEntry(TheoryRecoilEvtHistogramFit,"Coherent Scattering");
	legend5->SetFillColor(0);
  legend5->Draw();*/
	
	WriteHistogramToFile(RecoilEvtHistogram, "histogramOutput.txt");
	WriteNuRecoilEvents("MonoenergeticEvents.txt", 0.811, nEvt, nNeutrons, nProtons, RecoilSpectrum, DiffRecoilSpectrumAtConstE, NuSpectrumMinE, NuSpectrumMaxE, seed+5);
	
}
Example #5
0
void fractionFit()
{
   char name[1000];
  sprintf(name,"/Users/zach/Research/pythia/npeTemplate/outputs/currentB.root");
  TFile *fB = new TFile(name,"READ");
  sprintf(name,"/Users/zach/Research/pythia/npeTemplate/outputs/currentC.root");
  TFile *fC = new TFile(name,"READ");
   sprintf(name,"/Users/zach/Research/rootFiles/run12NPEhPhi/currentData.root");
  TFile *fD = new TFile(name,"READ");
  if (fB->IsOpen()==kFALSE || fC->IsOpen()==kFALSE)
    { std::cout << "!!!!!! Either B,C, or Data File not found !!!!!!" << std::endl
		<< "Looking for currentB.root, currentC.root, and currentData.root" << std::endl;
      exit(1); }
  
  // Set constants and projection bins
  const Int_t numPtBins = 10;
  Float_t lowpt[14] ={2.5,3.0,3.5,4.0,4.5,5.0,5.5,6.0,6.5,7.0,7.5,8.5,10.,14.0};
  Float_t highpt[14]={3.0,3.5,4.0,4.5,5.0,5.5,6.0,6.5,7.0,7.5,8.5,10.,14.,200.};
  Float_t hptCut=0.5;

  // Make Canvases
  TCanvas* deltaPhi  = new TCanvas("deltaPhi","Pythia Delta Phi",150,0,1150,1000);
  TCanvas* fitResult0 = new TCanvas("fitResult0","RB Extraction HT0",150,0,1150,1000);
  //  TCanvas* fitResult2 = new TCanvas("fitResult2","RB Extraction HT2",150,0,1150,1000);
  deltaPhi->Divide(2,2);
  fitResult0->Divide(4,3);
  //fitResult2->Divide(4,3);

  // Make histos
  TH1D* projB[numPtBins];
  TH1D* projC[numPtBins];
  TH1D* projData0[numPtBins];
  TH1D* projData2[numPtBins];
  
  // Get and Draw histos
  TPaveText* lbl[numPtBins];
  char textLabel[100];
  Int_t plotbin;

  for(Int_t ptbin=0; ptbin<numPtBins; ptbin++)
    {
      if(ptbin!=0 && ptbin!=2 && ptbin!=4 && ptbin!=6)
	continue;
      if(ptbin==0)plotbin=0;
      if(ptbin==2)plotbin=1;
      if(ptbin==4)plotbin=2;
      if(ptbin==6)plotbin=3;
      // Init necessary plotting tools
      lbl[ptbin] = new TPaveText(.2,.76,.5,.82,Form("NB NDC%i",ptbin));
      sprintf(textLabel,"%.1f < P_{T,e} < %.1f",lowpt[ptbin],highpt[ptbin]);
      lbl[ptbin]->AddText(textLabel);
      lbl[ptbin]->SetFillColor(kWhite);

      projB[ptbin] = (TH1D*)fB->Get(Form("projDelPhi_%i",ptbin));
      projC[ptbin] = (TH1D*)fC->Get(Form("projDelPhi_%i",ptbin));
      projData0[ptbin]= (TH1D*)fD->Get(Form("NPEhDelPhi_0_%i",ptbin));
      projData2[ptbin]= (TH1D*)fD->Get(Form("NPEhDelPhi_2_%i",ptbin));
      Int_t RB = 8;
      projB[ptbin]->Rebin(RB);
      projC[ptbin]->Rebin(RB);
      
      // Draw Templates on own plots
      deltaPhi->cd(plotbin+1);
      projData0[ptbin]->SetLineColor(kBlue);
      projData2[ptbin]->SetLineColor(kGreen+3);
      projB[ptbin]->SetLineColor(kRed);
      projC[ptbin]->SetLineColor(kBlack);
      //      projC[ptbin]->GetYaxis()->SetRangeUser(0.,0.5);
      projC[ptbin]    -> Draw();
      projB[ptbin]    -> Draw("same");
      projData0[ptbin]-> Draw("same");
      projData2[ptbin]-> Draw("same");
      lbl[ptbin]      -> Draw("same");

      TLegend* leg = new TLegend(0.5,0.73,0.85,0.85);
      leg->AddEntry(projB[ptbin],"b#bar{b}->NPE","lpe");
      leg->AddEntry(projC[ptbin],"c#bar{c}->NPE","lpe");
      leg->AddEntry(projData0[ptbin],"HT0","lpe");
      leg->AddEntry(projData2[ptbin],"HT2","lpe");
      leg->Draw();

      // Do the actual fit
      TObjArray *mc = new TObjArray(2);   // MC histograms are put in this array
      mc->Add(projC[ptbin]);
      mc->Add(projB[ptbin]);

      fitResult0->cd(ptbin+1);
      TFractionFitter* fit = new TFractionFitter(projData0[ptbin], mc,"V"); // initialise
      fit->Constrain(1,0.0,1.0);               // constrain fraction 1 to be between 0 and 1
      fit->SetRangeX(46,56);                    // use only the first 15 bins in the fit
      Int_t status = fit->Fit();               // perform the fit
      std::cout << "fit status: " << status << std::endl;
      if (status == 0) {                       // check on fit status
	TH1F* result = (TH1F*) fit->GetPlot();
	projData0[ptbin]->Draw("Ep");
	result->Draw("same");
	}

      
      /* fitResult2->cd(ptbin+1);
      TFractionFitter* fit2 = new TFractionFitter(projData2[ptbin], mc); // initialise
      fit2->Constrain(0,0.0,1.0);              // constrain fraction 0
      fit2->Constrain(1,0.0,1.0);              // constrain fraction 1 to be between 0 and 1
      fit2->SetRangeX(23,29);                  // use only the first 15 bins in the fit
      Int_t status2 = fit2->Fit();             // perform the fit
      std::cout << "fit status: " << status2 << std::endl;
      if (status2 == 0) {                       // check on fit status
	TH1F* result2 = (TH1F*) fit2->GetPlot();
	projData2[ptbin]->Draw("Ep");
	result2->Draw("same");
	}*/
    }
}