
#include "../GlobalFunctions.hh"

//////////////////////////////////////////////////////////////////////
/// PiZeroResolution()
/// #Check pi0 resolution with/without using DTF
///
/// AngularEff() #todo
/// # Check angular efficiency based on K* pT cut
///

using namespace std;
string figureFormat = "eps";


std::array<string, 5> ParticlesPiZero {"B",
                                       //"muplus",
                                       //"muminus",
                                       "Kstar",
                                       "pi0",
                                       "gamma1",
                                       "gamma2"
                                      };
std::array<string, 3> ParticlesKShort {"B",
                                       //"muplus",
                                       //"muminus",
                                       "Kstar",
                                       "Kshort"
                                      };
std::array<string, 3> Variables{ "M",
                                 "PT",
                                 "ETA"
                               };
std::array<string,5> ExtraVariables{
    "ThetaK",
    "ThetaL",
    "Phi",
    "PVZ",
    "OpenAngle",
};
std::array<string, 2> DTFoptions{//"DTF_PDG",
    //"PDG",
    "DTF",
    ""
};

string particleLatex(string particle){ //one doesn't have to think much when making labels and titles
    if (particle == "B")        return ("B^{+}");
    if (particle == "B+")        return ("B^{+}");
    if (particle == "Bplus")    return ("B^{+}");
    if (particle == "B_plus")   return ("B^{+}");
    if (particle == "Kstar")    return ("K^{*+}");
    if (particle == "K_star")   return ("K^{*+}");
    if (particle == "Kplus")    return ("K^{+}");
    if (particle == "K+")    return ("K^{+}");
    if (particle == "K_plus")   return ("K^{+}");
    if (particle == "Kshort")   return ("K_{s}");
    if (particle == "K_short")  return ("K_{s}");
    if (particle == "piplus")   return ("#pi^{+}");
    if (particle == "pi_plus")  return ("#pi^{+}");
    if (particle == "pizero")   return ("#pi^{0}");
    if (particle == "pi_zero")  return ("#pi^{0}");
    if (particle == "pi0")      return ("#pi^{0}");
    if (particle == "mu")       return ("#mu");
    if (particle == "mu_plus")  return ("#mu^{+}");
    if (particle == "muplus")   return ("#mu^{+}");
    if (particle == "mu_minus") return ("#mu^{-}");
    if (particle == "muminus")  return ("#mu^{-}");
    if (particle == "gamma")    return ("#gamma");
    if (particle == "gamma")    return ("#gamma");
    if (particle=="ThetaK")     return ("#theta_{K}"); //just easier to put it here
    if (particle=="ThetaL")     return ("#theta_{L}");//just easier to put it here
    if (particle=="Phi")        return ("#Phi");//just easier to put it here
    cout << "Particle "<< particle <<"  not in the list!" << endl;
    return "";
}
string variableLatex(string var){
    if (var=="PT")  return ("p_{T}");
    if (var=="PX")  return ("p_{X}");
    if (var=="PY")  return ("p_{Y}");
    if (var=="PZ")  return ("p_{Z}");
    if (var=="ETA") return ("#eta");
    if (var=="M")   return ("m");
    if (var=="PVZ")   return ("PVZ [mm]");
    if (var=="KstPt")   return ("K* p_{T} [MeV]");
    if (var=="ThetaK")   return ("#theta_{K}");
    if (var=="ThetaL")   return ("#theta_{L}");
    if (var=="Phi")   return ("#Phi");
    if (var=="OpenAngle")   return ("Open angle (#gamma#gamma angle)");
    cout << "Variable "<< var <<" is not in the list!" << endl;
    return "";
}
string optionLatex(string opt){
    if (opt=="DTF_PDG") return ("#splitline{info from DTF}{with masses fixed to PDG}");
    if (opt=="DTF")     return ("info from DTF");
    if (opt=="TRUE")     return ("TRUE info");
    if (opt=="PDG")     return ("#splitline{info from TupleTool}{with masses fixed to PDG}");
    if (opt=="")        return ("info from TupleTool");
    if (opt=="PULL")        return ("pull (TupleTool)");
    if (opt=="PULL_DTF")        return ("pull (DTF)");
    if (opt=="RES_SYS")        return ("Systematics #pm resolution (TupleTool)");
    if (opt=="RES_SYS_DTF")        return ("Systematics #pm resolution (DTF)");
    if (opt=="RES")        return ("Resolution (TupleTool)");
    if (opt=="RES_DTF")        return ("Resolution (DTF)");
    if (opt=="SYS")        return ("Systematics (TupleTool)");
    if (opt=="SYS_DTF")        return ("Systematics (DTF)");
    cout << "Option " << opt << " not in the list!" << endl;
    return "";
}
string histName(string particle, string var, string opt){
    return (variableLatex(var)+"("+ particleLatex(particle)+ "):" + optionLatex(opt));
}

void init_1D(TH1 *&hist, string particle, string var, string opt, string opt_plot, string YaxisTitle, int bins, double low_edge, double up_edge){

    string tmpName = variableLatex(var) + "{"+ particleLatex(particle) +"}" + (opt_plot == "" ? "" : ": " + opt_plot);
    string tmpTitle = "h_" + particle + "_" + var +"_" + opt;
    hist = new TH1D(tmpTitle.c_str(), histName(particle, var, opt).c_str(), bins, low_edge, up_edge);
    hist->GetXaxis()->SetTitle(tmpName.c_str());
    hist->GetYaxis()->SetTitle(YaxisTitle.c_str());
    return;
}
void init_1D(TH1 *&hist, string particle, string var, string opt,  string opt_plot, string YaxisTitle, int bins, const double *array){

    string tmpName = variableLatex(var) + "{"+ particleLatex(particle) +"}" + (opt_plot == "" ? "" : ": " + opt_plot);
    string tmpTitle = "h_" + particle + "_" + var + "_" +opt;
    hist = new TH1D(tmpTitle.c_str(), histName(particle, var, opt).c_str(),bins, array);
    hist->GetXaxis()->SetTitle(tmpName.c_str());
    hist->GetYaxis()->SetTitle(YaxisTitle.c_str());
    return;
}

void init_2D(TH2 *&hist, string particle, string varX, string varY, string opt, string YaxisTitle, string ZaxisTitle, int bins, double low_edge, double up_edge){

    string tmpXName = variableLatex(varX) + "{"+ particleLatex(particle) +"}";
    string tmpTitle = "h2_" + particle + "_" + varX +"_" + varY   + "_" + opt;
    hist = new TH2D(tmpTitle.c_str(), histName(particle, varX, opt).c_str(),bins, low_edge, up_edge, bins, low_edge, up_edge);
    hist->GetXaxis()->SetTitle(tmpXName.c_str());
    YaxisTitle = YaxisTitle + variableLatex(varY) + "{"+ particleLatex(particle) +"}";
    hist->GetYaxis()->SetTitle(YaxisTitle.c_str());
    hist->GetZaxis()->SetTitle(ZaxisTitle.c_str());
    return;
}
void init_2D(TH2 *&hist, string particle, string varX, string varY, string opt, string YaxisTitle, string ZaxisTitle, int Xbins, double Xlow_edge, double Xup_edge, int Ybins, double Ylow_edge, double Yup_edge){

    string tmpName = variableLatex(varX) + "{"+ particleLatex(particle) +"}";
    string tmpTitle = "h2_" + particle + "_" + varX + "_" + varY + "_" + opt;
    hist = new TH2D(tmpTitle.c_str(), histName(particle, varX, opt).c_str(),Xbins, Xlow_edge, Xup_edge, Ybins, Ylow_edge, Yup_edge);
    hist->GetXaxis()->SetTitle(tmpName.c_str());
    YaxisTitle = YaxisTitle + variableLatex(varY) + "{"+ particleLatex(particle) +"}";
    hist->GetYaxis()->SetTitle(YaxisTitle.c_str());
    hist->GetZaxis()->SetTitle(ZaxisTitle.c_str());
    return;
}
void init_2D(TH2 *&hist, string particle, string varX, string varY, string opt, string YaxisTitle, string ZaxisTitle, int bins, const double *array){

    string tmpName = variableLatex(varX) + "{"+ particleLatex(particle) +"}";
    string tmpTitle = "h2_" + particle + "_" + varX + "_" +varY +  "_" + opt;
    hist = new TH2D(tmpTitle.c_str(), histName(particle, varX, opt).c_str(),bins, array, bins, array);
    hist->GetXaxis()->SetTitle(tmpName.c_str());
    YaxisTitle = YaxisTitle + variableLatex(varY) + "{"+ particleLatex(particle) +"}";
    hist->GetYaxis()->SetTitle(YaxisTitle.c_str());
    hist->GetZaxis()->SetTitle(ZaxisTitle.c_str());
    return;
}
void init_2D(TH2 *&hist, string particle, string varX, string varY, string opt, string YaxisTitle, string ZaxisTitle, int Xbins, double *Xarray, int Ybins, double *Yarray){

    string tmpName = variableLatex(varX) + "{"+ particleLatex(particle) +"}";
    string tmpTitle = "h2_" + particle + "_" + varX+ "_" +varY + "_" + opt;
    hist = new TH2D(tmpTitle.c_str(), histName(particle, varX, opt).c_str(),Xbins, Xarray, Ybins, Yarray);
    hist->GetXaxis()->SetTitle(tmpName.c_str());
    YaxisTitle = YaxisTitle + variableLatex(varY) + "{"+ particleLatex(particle) +"}";
    hist->GetYaxis()->SetTitle(YaxisTitle.c_str());
    hist->GetZaxis()->SetTitle(ZaxisTitle.c_str());
    return;
}

void designCanvas(TCanvas* canvas){

    canvas->SetRightMargin(0.04);
    canvas->SetTopMargin(0.08);
    canvas->SetLeftMargin(0.14);
    canvas->SetBottomMargin(0.12);
}

void designCanvas2D(TCanvas* canvas){
    canvas->SetRightMargin(0.2);
    canvas->SetTopMargin(0.08);
    canvas->SetLeftMargin(0.12);
    canvas->SetBottomMargin(0.1);
}
void designPlots(TH1D *hist, int color){
    hist->SetLineWidth(2);
    hist->SetLineColor(color);
    hist->GetYaxis()->SetTitleOffset(1.7);
    hist->GetXaxis()->SetTitleOffset(1.0);
    hist->SetTitle("");
}

void designLegend(TLegend *leg, string legTitle, TH1D *hist, string opt){
    leg->SetTextSize(0.03);
    leg->SetHeader(legTitle.c_str());
    leg->AddEntry(hist,optionLatex(opt).c_str(),"l");
}

string output_path(string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false){
    if(Kst2Kpluspi0Resolved){
        if(!MC){
            return Form("%s/resolution/data/%s%s/",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str());
        }
        else{
            if(ReferenceChannel){
                return Form("%s/resolution/MC/RefKplusPi0/%s%s/",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str());
            }
            else if(PHSP){
                return Form("%s/resolution/PHSP/KplusPi0/%s%s/",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str());
            }
            else{
                return Form("%s/resolution/MC/KplusPi0/%s%s/",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str());
            }
        }
    }
}

void design_pull(TH1 *pull, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string selection = preselected? "Preselected" : "Stripped";
    string name = output_path(year, magnet, MC, ReferenceChannel, PHSP) + pull->GetName() + "_" + selection;

    TCanvas *canvas = new TCanvas(Form("c_%s",pull->GetName()), "", 10,10,600,600);
    designCanvas(canvas);
    canvas->cd();
    //add two 3-sigma lines:
    TLine * lzero  = new TLine(pull->GetXaxis()->GetXmin(), 0.,pull->GetXaxis()->GetXmin(), 0.);
    lzero->SetLineStyle(5);
    lzero->SetLineWidth(gStyle->GetLineWidth());

    pull->GetYaxis()->SetRangeUser(-1,1);
    pull->GetYaxis()->SetTitleOffset(1.4);
    pull->GetXaxis()->SetTitleOffset(1.6);
    pull->SetMarkerSize(1);
    pull->SetMarkerStyle(47);
    pull->SetMarkerColor(kCyan+2);
    pull->Draw("P");
    lzero->Draw("SAME");
    canvas->SaveAs((name+ (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;

}
void design_fit(TH1 *hist, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string selection = preselected? "Preselected" : "Stripped";
    string name = output_path(year, magnet, MC, ReferenceChannel, PHSP) + hist->GetName() + "_" + selection;
    TCanvas *canvas = new TCanvas(Form("c_%s",hist->GetName()), "", 10,10,600,600);
    designCanvas(canvas);
    canvas->cd();
    if (name.find("Theta") != string::npos) {
        hist->GetYaxis()->SetRangeUser(-0.25,0.25);
        hist->GetYaxis()->SetTitleOffset(1.6);
    }
    else if (name.find("Phi") != string::npos) {
        hist->GetYaxis()->SetTitleOffset(1.6);
        hist->GetYaxis()->SetRangeUser(-0.25,0.25);
    }
    else{
        hist->GetYaxis()->SetTitleOffset(1.6);
        hist->GetYaxis()->SetRangeUser(-0.25,0.25);
    }
    hist->GetXaxis()->SetTitleOffset(1.4);
    hist->SetMarkerSize(1);
    hist->SetMarkerStyle(47);
    hist->SetMarkerColor(kCyan+2);
    hist->Draw("PE");
    canvas->SaveAs((name+ (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;
}
void design_resolution(TH1 *res, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string name =  res->GetName();

    TCanvas *canvas = new TCanvas(Form("c_%s",res->GetName()), "", 10,10,600,600);
    designCanvas(canvas);
    canvas->cd();
    if (name.find("Theta") != string::npos) {
        res->GetYaxis()->SetTitleOffset(1.6);
        res->GetYaxis()->SetRangeUser(0.0,0.05);
    }
    else if (name.find("Kplus") != string::npos) {
        res->GetYaxis()->SetTitleOffset(1.6);
        res->GetYaxis()->SetRangeUser(0.0,0.3);
    }
    else if (name.find("Phi") != string::npos) {
        res->GetYaxis()->SetTitleOffset(1.6);
        res->GetYaxis()->SetRangeUser(0.0,0.05);
    }
    else{
        res->GetYaxis()->SetTitleOffset(1.6);
        res->GetYaxis()->SetRangeUser(0.0,0.05);
    }
    res->GetXaxis()->SetTitleOffset(1.4);
    res->SetMarkerSize(1);
    res->SetMarkerStyle(47);
    res->SetMarkerColor(kCyan+2);
    res->Draw("PE");
    name = output_path(year, magnet, MC, ReferenceChannel, PHSP) + name + (preselected ? "_Preselected" : "_Stripped");
    canvas->SaveAs((name+ (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;
}
void design_systematics(TH1 *sys, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string selection = preselected? "Preselected" : "Stripped";
    string name = output_path(year, magnet, MC, ReferenceChannel, PHSP) +  sys->GetName() + "_" + selection;
    TCanvas *canvas = new TCanvas(Form("c_%s",sys->GetName()), "", 10,10,600,600);
    designCanvas(canvas);
    canvas->cd();
    if (name.find("Theta") != string::npos) {
        sys->GetYaxis()->SetRangeUser(-0.05,0.05);
        sys->GetYaxis()->SetTitleOffset(1.6);
    }
    else if (name.find("Phi") != string::npos) {
        sys->GetYaxis()->SetTitleOffset(1.6);
        sys->GetYaxis()->SetRangeUser(-0.05,0.05);
    }
    else{
        sys->GetYaxis()->SetTitleOffset(1.6);
        sys->GetYaxis()->SetRangeUser(-0.05,0.05);
    }
    sys->GetXaxis()->SetTitleOffset(1.4);
    sys->SetMarkerSize(1);
    sys->SetMarkerStyle(47);
    sys->SetMarkerColor(kCyan+2);
    sys->Draw("PE");
    canvas->SaveAs((name+ (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;
}
void design_comparison(TH1 *rec, TH1 *trueMC, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string selection = preselected? "Preselected" : "Stripped";
    string name = output_path(year, magnet, MC, ReferenceChannel, PHSP) +  rec->GetName() + "_" + selection;
    TCanvas *canvas = new TCanvas(Form("c_%s",rec->GetName()), "", 10,10,600,600);
    designCanvas(canvas);
    canvas->cd();
    //rec->GetYaxis()->SetRangeUser(-0.25,0.25);
    rec->GetYaxis()->SetTitleOffset(1.3);
    rec->GetXaxis()->SetTitleOffset(1.4);
    rec->SetLineColor(1);
    rec->SetLineWidth(3);
    //MC->GetYaxis()->SetRangeUser(-0.25,0.25);
    trueMC->GetYaxis()->SetTitleOffset(1.3);
    trueMC->GetXaxis()->SetTitleOffset(1.4);
    trueMC->SetLineColor(2);
    trueMC->SetLineWidth(3);


    trueMC->Draw("");
    rec->Draw("SAME");

    canvas->SaveAs((name + "_comparison" + (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;
}
void design_corr(TH2 *corr, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string selection = preselected? "Preselected" : "Stripped";
    string name = output_path(year, magnet, MC, ReferenceChannel, PHSP) + corr->GetName() + "_" + selection;
    TCanvas *canvas = new TCanvas(Form("c_%s",corr->GetName()), "", 10,10,700,600);
    designCanvas2D(canvas);
    canvas->cd();
    corr->GetYaxis()->SetTitleOffset(1.5);
    corr->GetXaxis()->SetTitleOffset(1.0);
    corr->GetZaxis()->SetTitleOffset(1.25);
    corr->Draw("COLZ");
    canvas->SaveAs((name+ (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;

}
void design_diff(TH2 *diff, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    string selection = preselected? "Preselected" : "Stripped";
    string name = output_path(year, magnet, MC, ReferenceChannel, PHSP) + diff->GetName() + "_" + selection;
    TCanvas *canvas = new TCanvas(Form("c_%s",diff->GetName()), "", 10,10,700,600);
    designCanvas2D(canvas);
    canvas->cd();
    if (name.find("Theta") != string::npos) {
        diff->GetYaxis()->SetRangeUser(-0.5,0.5);
    }
    if (name.find("Phi") != string::npos) {
            diff->GetYaxis()->SetRangeUser(-0.5,0.5);
    }
    diff->GetYaxis()->SetTitleOffset(1.5);
    diff->GetXaxis()->SetTitleOffset(1.0);
    diff->GetZaxis()->SetTitleOffset(1.25);
    diff->Draw("COLZ");
    canvas->SaveAs((name+ (truthMatched ?  "_TM" : "")+".eps").c_str(),"eps");
    canvas->Clear();
    delete canvas;

}

int ResPresel(std::string year = "2011", std::string magnet = "down",
              bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool preselected = true, bool truthMatched = true) {
    //no point in keepeng the MC option, but it doesn't hurt neither
    MC = true;
    TFile* output = 0;
    TChain *tree;
    if (!(MC || ReferenceChannel || PHSP)) truthMatched = false;
    if (!preselected){
        //Kst2Kpluspi0Resolved case
        if(Kst2Kpluspi0Resolved){
            if(!MC){
                tree=new TChain("b2KstKpi0mumuResolvedTuple/DecayTree");
                if(smallSample) tree->Add(Form("%s/data/%s%s/*B2Kstmumu*13.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                else{
                    tree->Add(Form("%s/data/%s%s/*.root",path_to_data.c_str(),year.c_str(),magnet.c_str()));
                    std::cout << "Adding " << Form("%s/data/%s%s/*B2Kstmumu*.root",path_to_data.c_str(),year.c_str(),magnet.c_str()) << endl;
                }
            }
            else{
                tree=new TChain("b2KstKpi0mumuResolvedTuple/DecayTree");
                if(ReferenceChannel){
                    if(smallSample) tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/*B2KstJpsi*1.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                    else tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/*B2KstJpsi*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                }
                else if(PHSP){
                    if(smallSample) tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/*B2Kstmumu*1.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                    else tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                }
                else{
                    if(smallSample) tree->Add(Form("%s/data/MC/KplusPi0/%s%s/*B2Kstmumu*1.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                    else tree->Add(Form("%s/data/MC/KplusPi0/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                }
            }
        }

        //Kst2Kpluspi0Merged case
        if(Kst2Kpluspi0Merged){
            tree=new TChain("b2KstKpi0mumuMergedTuple/DecayTree");
            if(!MC){
                if(smallSample) tree->Add(Form("%s/data/%s%s/*B2Kstmumu*13.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                else tree->Add(Form("%s/data/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
            }
            else{
                if(smallSample) tree->Add(Form("%s/data/MC/KplusPi0/%s%s/*B2Kstmumu*1.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                else tree->Add(Form("%s/data/MC/KplusPi0/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
            }
        }

        //Kst2Kspiplus case
        if(Kst2Kspiplus){
            if(!MC){
                tree=new TChain("b2KstKs0pimumu_Tuple/DecayTree");
                if(smallSample) tree->Add(Form("%s/data/%s%s/*B2Kstmumu*23.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                else tree->Add(Form("%s/data/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
            }
            else{
                tree=new TChain("b2KstKs0pimumu_Tuple/DecayTree");
                if(ReferenceChannel){
                    if(smallSample) tree->Add(Form("%s/data/MC/RefKshortPiplus/%s%s/*B2KstJpsi*13.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                    else tree->Add(Form("%s/data/MC/RefKshortPiplus/%s%s/*B2KstJpsi*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                }
                else if(PHSP){
                    if(smallSample) tree->Add(Form("%s/data/PHSP/KshortPiplus/%s%s/*B2Kstmumu*1.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                    else tree->Add(Form("%s/data/PHSP/KshortPiplus/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                }
                else{
                    if(smallSample) tree->Add(Form("%s/data/MC/KshortPiplus/%s%s/*B2Kstmumu*1.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                    else tree->Add(Form("%s/data/MC/KshortPiplus/%s%s/*B2Kstmumu*.root",path_to_data.c_str(), year.c_str(),magnet.c_str()));
                }
            }
        }

    }
    else{ //preselected
        string name = (truthMatched ? "DecayTreeTruthMatched" : "DecayTree");
        tree=new TChain(name.c_str());
        // Kst2Kpluspi0Resolved case
        if(Kst2Kpluspi0Resolved){
            if(!MC){
                tree->Add(Form("%s/data/%s%s/%s%s_pi0Resolved.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                cout << Form("%s/data/%s%s/%s%s_pi0Resolved.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()) << endl;
            }
            else{
                if(ReferenceChannel){
                    tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Resolved.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
                else if(PHSP){
                    tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Resolved.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
                else{
                    tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Resolved.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
            }
        }
        // Kst2Kpluspi0Merged case
        if(Kst2Kpluspi0Merged){
            if(!MC){
                tree->Add(Form("%s/data/%s%s/%s%s_pi0Merged.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
            }
            else{
                if(ReferenceChannel){
                    tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Merged.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
                else if(PHSP){
                    tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Merged.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
                else{
                    tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Mergedroot",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
            }
        }
        // Kst2Kspiplus case
        if(Kst2Kspiplus){
            if(!MC){
                tree->Add(Form("%s/data/%s%s/%s%s_piplus.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
            }
            else{
                if(ReferenceChannel){
                    tree->Add(Form("%s/data/MC/RefKshortPiplus/%s%s/%s%s_piplus.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
                else if(PHSP){
                    tree->Add(Form("%s/data/PHSP/KshortPiplus/%s%s/%s%s_piplus.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
                else{
                    tree->Add(Form("%s/data/MC/KshortPiplus/%s%s/%s%s_piplus.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str()));
                }
            }
        }
    }

    int N = tree->GetEntries();

    if(N == 0){
        std::cout << "[ERROR]\t\tNo events found!" << std::endl;
        return 0;
    }

    /////////////////////
    // Detector values //
    /////////////////////
    TLorentzVector LorVec_B_plus;
    TLorentzVector LorVec_K_star_plus;
    TLorentzVector LorVec_K_plus;
    TLorentzVector LorVec_pi_zero_merged;
    TLorentzVector LorVec_pi_zero_resolved;
    TLorentzVector LorVec_K_short;
    TLorentzVector LorVec_pi_plus;
    TLorentzVector LorVec_KS_pi_plus;
    TLorentzVector LorVec_KS_pi_minus;
    TLorentzVector LorVec_mu_minus;
    TLorentzVector LorVec_mu_plus;
    TLorentzVector LorVec_gamma1;
    TLorentzVector LorVec_gamma2;
    TLorentzVector LorVec_DiGamma;

    //potentially misIDed particles:
    TLorentzVector LorVec_pi_plus_MisIDed;
    TLorentzVector LorVec_mu_plus_MisIDed;
    TLorentzVector LorVec_B_plus_From_DoubleMisIDed;
    TLorentzVector LorVec_Jpsi_From_DoubleMisIDed;
    TLorentzVector LorVec_K_star_plus_From_DoubleMisIDed;

    //recombined particles:
    TLorentzVector LorVec_DiMuon;
    TLorentzVector LorVec_KaonPion;
    TLorentzVector LorVec_ReconB;
    TLorentzVector LorVec_ReconBPiZero;
    TLorentzVector LorVec_KshortMuMu;
    TLorentzVector LorVec_KplusMuMu;

    //Other variables:
    Double_t OpenAngle;

    /////////////////////
    //   DTF values    //
    /////////////////////
    TLorentzVector LorVec_DTF_B_plus;
    TLorentzVector LorVec_DTF_K_star_plus;
    TLorentzVector LorVec_DTF_K_plus;
    TLorentzVector LorVec_DTF_pi_zero_merged;
    TLorentzVector LorVec_DTF_pi_zero_resolved;
    TLorentzVector LorVec_DTF_K_short;
    TLorentzVector LorVec_DTF_pi_plus;
    TLorentzVector LorVec_DTF_KS_pi_plus;
    TLorentzVector LorVec_DTF_KS_pi_minus;
    TLorentzVector LorVec_DTF_mu_minus;
    TLorentzVector LorVec_DTF_mu_plus;
    TLorentzVector LorVec_DTF_gamma1;
    TLorentzVector LorVec_DTF_gamma2;
    TLorentzVector LorVec_DTF_DiGamma;

    //potentially misIDed particles:
    TLorentzVector LorVec_DTF_pi_plus_MisIDed;
    TLorentzVector LorVec_DTF_mu_plus_MisIDed;
    TLorentzVector LorVec_DTF_B_plus_From_DoubleMisIDed;
    TLorentzVector LorVec_DTF_Jpsi_From_DoubleMisIDed;
    TLorentzVector LorVec_DTF_K_star_plus_From_DoubleMisIDed;

    //recombined particles:
    TLorentzVector LorVec_DTF_DiMuon;
    TLorentzVector LorVec_DTF_KaonPion;
    TLorentzVector LorVec_DTF_ReconB;
    TLorentzVector LorVec_DTF_ReconBPiZero;
    TLorentzVector LorVec_DTF_KshortMuMu;
    TLorentzVector LorVec_DTF_KplusMuMu;

    //Other variables:
    Double_t OpenAngle_DTF;

    /////////////////////
    //   TRUE values    //
    /////////////////////
    TLorentzVector LorVec_TRUE_B_plus;
    TLorentzVector LorVec_TRUE_K_star_plus;
    TLorentzVector LorVec_TRUE_K_plus;
    TLorentzVector LorVec_TRUE_pi_zero_merged;
    TLorentzVector LorVec_TRUE_pi_zero_resolved;
    TLorentzVector LorVec_TRUE_K_short;
    TLorentzVector LorVec_TRUE_pi_plus;
    TLorentzVector LorVec_TRUE_KS_pi_plus;
    TLorentzVector LorVec_TRUE_KS_pi_minus;
    TLorentzVector LorVec_TRUE_mu_minus;
    TLorentzVector LorVec_TRUE_mu_plus;
    TLorentzVector LorVec_TRUE_gamma1;
    TLorentzVector LorVec_TRUE_gamma2;
    TLorentzVector LorVec_TRUE_DiGamma;

    //potentially misIDed particles:
    TLorentzVector LorVec_TRUE_pi_plus_MisIDed;
    TLorentzVector LorVec_TRUE_mu_plus_MisIDed;
    TLorentzVector LorVec_TRUE_B_plus_From_DoubleMisIDed;
    TLorentzVector LorVec_TRUE_Jpsi_From_DoubleMisIDed;
    TLorentzVector LorVec_TRUE_K_star_plus_From_DoubleMisIDed;

    //recombined particles:
    TLorentzVector LorVec_TRUE_DiMuon;
    TLorentzVector LorVec_TRUE_KaonPion;

    //Other variables:
    Double_t OpenAngle_TRUE;


    /////////////////////
    //    Branches     //
    /////////////////////

    ///B+
    Double_t B_plus_PX    = 0.;
    Double_t B_plus_PY    = 0.;
    Double_t B_plus_PZ    = 0.;
    Double_t B_plus_PT    = 0.;
    Double_t B_plus_PE    = 0.;
    Double_t B_plus_ETA   = 0.;
    Double_t B_plus_PHI   = 0.;

    Double_t B_plus_TRUE_PX    = 0.;
    Double_t B_plus_TRUE_PY    = 0.;
    Double_t B_plus_TRUE_PZ    = 0.;
    Double_t B_plus_TRUE_PT    = 0.;
    Double_t B_plus_TRUE_PE    = 0.;
    Double_t B_plus_TRUE_ETA   = 0.;
    Double_t B_plus_TRUE_PHI   = 0.;

    Double_t B_plus_PX_DTF    = 0.;
    Double_t B_plus_PY_DTF    = 0.;
    Double_t B_plus_PZ_DTF    = 0.;
    Double_t B_plus_PT_DTF    = 0.;
    Double_t B_plus_PE_DTF    = 0.;
    Double_t B_plus_ETA_DTF   = 0.;
    Double_t B_plus_PHI_DTF   = 0.;

    ///K*+
    Double_t K_star_plus_PX     = 0.;
    Double_t K_star_plus_PY     = 0.;
    Double_t K_star_plus_PZ     = 0.;
    Double_t K_star_plus_PT     = 0.;
    Double_t K_star_plus_PE     = 0.;
    Double_t K_star_plus_ETA    = 0.;
    Double_t K_star_plus_PHI    = 0.;

    Double_t K_star_plus_TRUE_PX     = 0.;
    Double_t K_star_plus_TRUE_PY     = 0.;
    Double_t K_star_plus_TRUE_PZ     = 0.;
    Double_t K_star_plus_TRUE_PT     = 0.;
    Double_t K_star_plus_TRUE_PE     = 0.;
    Double_t K_star_plus_TRUE_ETA    = 0.;
    Double_t K_star_plus_TRUE_PHI    = 0.;

    Double_t K_star_plus_PX_DTF      = 0.;
    Double_t K_star_plus_PY_DTF      = 0.;
    Double_t K_star_plus_PZ_DTF      = 0.;
    Double_t K_star_plus_PT_DTF      = 0.;
    Double_t K_star_plus_PE_DTF      = 0.;
    Double_t K_star_plus_ETA_DTF     = 0.;
    Double_t K_star_plus_PHI_DTF     = 0.;

    ///K+
    Double_t K_plus_PX      = 0.;
    Double_t K_plus_PY      = 0.;
    Double_t K_plus_PZ      = 0.;
    Double_t K_plus_PT      = 0.;
    Double_t K_plus_PE      = 0.;
    Double_t K_plus_ETA     = 0.;
    Double_t K_plus_PHI     = 0.;

    Double_t K_plus_TRUE_PX      = 0.;
    Double_t K_plus_TRUE_PY      = 0.;
    Double_t K_plus_TRUE_PZ      = 0.;
    Double_t K_plus_TRUE_PT      = 0.;
    Double_t K_plus_TRUE_PE      = 0.;
    Double_t K_plus_TRUE_ETA     = 0.;
    Double_t K_plus_TRUE_PHI     = 0.;

    Double_t K_plus_PX_DTF  = 0.;
    Double_t K_plus_PY_DTF  = 0.;
    Double_t K_plus_PZ_DTF  = 0.;
    Double_t K_plus_PE_DTF  = 0.;
    Double_t K_plus_PT_DTF  = 0.;
    Double_t K_plus_ETA_DTF = 0.;
    Double_t K_plus_PHI_DTF = 0.;

    Float_t f_K_plus_PX_DTF[100];
    Float_t f_K_plus_PY_DTF[100];
    Float_t f_K_plus_PZ_DTF[100];
    Float_t f_K_plus_PE_DTF[100];


    ///pi0 (merged)
    Double_t pi_zero_merged_PX  = 0.;
    Double_t pi_zero_merged_PY  = 0.;
    Double_t pi_zero_merged_PZ  = 0.;
    Double_t pi_zero_merged_PT  = 0.;
    Double_t pi_zero_merged_PE  = 0.;
    Double_t pi_zero_merged_ETA = 0.;
    Double_t pi_zero_merged_PHI = 0.;

    Double_t pi_zero_merged_TRUE_PX     = 0.;
    Double_t pi_zero_merged_TRUE_PY     = 0.;
    Double_t pi_zero_merged_TRUE_PZ     = 0.;
    Double_t pi_zero_merged_TRUE_PT     = 0.;
    Double_t pi_zero_merged_TRUE_PE     = 0.;
    Double_t pi_zero_merged_TRUE_ETA    = 0.;
    Double_t pi_zero_merged_TRUE_PHI    = 0.;

    Double_t pi_zero_merged_PX_DTF    = 0.;
    Double_t pi_zero_merged_PY_DTF    = 0.;
    Double_t pi_zero_merged_PZ_DTF    = 0.;
    Double_t pi_zero_merged_PT_DTF    = 0.;
    Double_t pi_zero_merged_PE_DTF    = 0.;
    Double_t pi_zero_merged_ETA_DTF   = 0.;
    Double_t pi_zero_merged_PHI_DTF   = 0.;

    ///pi0 (resolved)
    Double_t pi_zero_resolved_PX     = 0.;
    Double_t pi_zero_resolved_PY     = 0.;
    Double_t pi_zero_resolved_PZ     = 0.;
    Double_t pi_zero_resolved_PT     = 0.;
    Double_t pi_zero_resolved_PE     = 0.;
    Double_t pi_zero_resolved_ETA    = 0.;
    Double_t pi_zero_resolved_PHI    = 0.;

    Double_t pi_zero_resolved_TRUE_PX     = 0.;
    Double_t pi_zero_resolved_TRUE_PY     = 0.;
    Double_t pi_zero_resolved_TRUE_PZ     = 0.;
    Double_t pi_zero_resolved_TRUE_PT     = 0.;
    Double_t pi_zero_resolved_TRUE_PE     = 0.;
    Double_t pi_zero_resolved_TRUE_ETA    = 0.;
    Double_t pi_zero_resolved_TRUE_PHI    = 0.;

    Double_t pi_zero_resolved_PX_DTF    = 0.;
    Double_t pi_zero_resolved_PY_DTF    = 0.;
    Double_t pi_zero_resolved_PZ_DTF    = 0.;
    Double_t pi_zero_resolved_PT_DTF    = 0.;
    Double_t pi_zero_resolved_PE_DTF    = 0.;
    Double_t pi_zero_resolved_ETA_DTF   = 0.;
    Double_t pi_zero_resolved_PHI_DTF   = 0.;


    ///Kshort
    Double_t K_short_PX     = 0.;
    Double_t K_short_PY     = 0.;
    Double_t K_short_PZ     = 0.;
    Double_t K_short_PT     = 0.;
    Double_t K_short_PE     = 0.;
    Double_t K_short_ETA    = 0.;
    Double_t K_short_PHI    = 0.;

    Double_t K_short_TRUE_PX     = 0.;
    Double_t K_short_TRUE_PY     = 0.;
    Double_t K_short_TRUE_PZ     = 0.;
    Double_t K_short_TRUE_PT     = 0.;
    Double_t K_short_TRUE_PE     = 0.;
    Double_t K_short_TRUE_ETA    = 0.;
    Double_t K_short_TRUE_PHI    = 0.;

    Double_t K_short_PX_DTF     = 0.;
    Double_t K_short_PY_DTF     = 0.;
    Double_t K_short_PZ_DTF     = 0.;
    Double_t K_short_PT_DTF     = 0.;
    Double_t K_short_PE_DTF     = 0.;
    Double_t K_short_ETA_DTF    = 0.;
    Double_t K_short_PHI_DTF    = 0.;

    Float_t f_K_short_M_DTF[100];
    Double_t K_short_M_DTF = 0.;

    ///pi+ (KS)
    Double_t Ks_pi_plus_PX     = 0.;
    Double_t Ks_pi_plus_PY     = 0.;
    Double_t Ks_pi_plus_PZ     = 0.;
    Double_t Ks_pi_plus_PT     = 0.;
    Double_t Ks_pi_plus_PE     = 0.;
    Double_t Ks_pi_plus_ETA    = 0.;
    Double_t Ks_pi_plus_PHI    = 0.;

    Double_t Ks_pi_plus_TRUE_PX     = 0.;
    Double_t Ks_pi_plus_TRUE_PY     = 0.;
    Double_t Ks_pi_plus_TRUE_PZ     = 0.;
    Double_t Ks_pi_plus_TRUE_PT     = 0.;
    Double_t Ks_pi_plus_TRUE_PE     = 0.;
    Double_t Ks_pi_plus_TRUE_ETA    = 0.;
    Double_t Ks_pi_plus_TRUE_PHI    = 0.;

    Float_t f_Ks_pi_plus_PX_DTF[100];
    Float_t f_Ks_pi_plus_PY_DTF[100];
    Float_t f_Ks_pi_plus_PZ_DTF[100];
    Float_t f_Ks_pi_plus_PT_DTF[100];
    Float_t f_Ks_pi_plus_PE_DTF[100];
    Float_t f_Ks_pi_plus_ID_DTF[100];

    Double_t Ks_pi_plus_PX_DTF = 0.;
    Double_t Ks_pi_plus_PY_DTF = 0.;
    Double_t Ks_pi_plus_PZ_DTF = 0.;
    Double_t Ks_pi_plus_PT_DTF = 0.;
    Double_t Ks_pi_plus_PE_DTF = 0.;
    Double_t Ks_pi_plus_ETA_DTF = 0.;
    Double_t Ks_pi_plus_PHI_DTF = 0.;


    ///pi- (KS)
    Double_t Ks_pi_minus_PX     = 0.;
    Double_t Ks_pi_minus_PY     = 0.;
    Double_t Ks_pi_minus_PZ     = 0.;
    Double_t Ks_pi_minus_PT     = 0.;
    Double_t Ks_pi_minus_PE     = 0.;
    Double_t Ks_pi_minus_ETA    = 0.;
    Double_t Ks_pi_minus_PHI    = 0.;

    Double_t Ks_pi_minus_TRUE_PX     = 0.;
    Double_t Ks_pi_minus_TRUE_PY     = 0.;
    Double_t Ks_pi_minus_TRUE_PZ     = 0.;
    Double_t Ks_pi_minus_TRUE_PT     = 0.;
    Double_t Ks_pi_minus_TRUE_PE     = 0.;
    Double_t Ks_pi_minus_TRUE_ETA    = 0.;
    Double_t Ks_pi_minus_TRUE_PHI    = 0.;

    Float_t f_Ks_pi_minus_PX_DTF[100];
    Float_t f_Ks_pi_minus_PY_DTF[100];
    Float_t f_Ks_pi_minus_PZ_DTF[100];
    Float_t f_Ks_pi_minus_PT_DTF[100];
    Float_t f_Ks_pi_minus_PE_DTF[100];
    Float_t f_Ks_pi_minus_ID_DTF[100];

    Double_t Ks_pi_minus_PX_DTF  = 0.;
    Double_t Ks_pi_minus_PY_DTF  = 0.;
    Double_t Ks_pi_minus_PZ_DTF  = 0.;
    Double_t Ks_pi_minus_PT_DTF  = 0.;
    Double_t Ks_pi_minus_PE_DTF  = 0.;
    Double_t Ks_pi_minus_ETA_DTF = 0.;
    Double_t Ks_pi_minus_PHI_DTF = 0.;

    ///pi+
    Double_t pi_plus_PX     = 0.;
    Double_t pi_plus_PY     = 0.;
    Double_t pi_plus_PZ     = 0.;
    Double_t pi_plus_PT     = 0.;
    Double_t pi_plus_PE     = 0.;
    Double_t pi_plus_ETA    = 0.;
    Double_t pi_plus_PHI    = 0.;

    Double_t pi_plus_TRUE_PX     = 0.;
    Double_t pi_plus_TRUE_PY     = 0.;
    Double_t pi_plus_TRUE_PZ     = 0.;
    Double_t pi_plus_TRUE_PT     = 0.;
    Double_t pi_plus_TRUE_PE     = 0.;
    Double_t pi_plus_TRUE_ETA    = 0.;
    Double_t pi_plus_TRUE_PHI    = 0.;

    Float_t f_pi_plus_PX_DTF[100];
    Float_t f_pi_plus_PY_DTF[100];
    Float_t f_pi_plus_PZ_DTF[100];
    Float_t f_pi_plus_PE_DTF[100];
    Float_t f_pi_plus_PT_DTF[100];
    Float_t f_pi_plus_ID_DTF[100];

    Double_t pi_plus_PX_DTF  = 0.;
    Double_t pi_plus_PY_DTF  = 0.;
    Double_t pi_plus_PZ_DTF  = 0.;
    Double_t pi_plus_PT_DTF  = 0.;
    Double_t pi_plus_PE_DTF  = 0.;
    Double_t pi_plus_ETA_DTF = 0.;
    Double_t pi_plus_PHI_DTF = 0.;

    ///mu+
    Double_t mu_minus_PX     = 0.;
    Double_t mu_minus_PY     = 0.;
    Double_t mu_minus_PZ     = 0.;
    Double_t mu_minus_PT     = 0.;
    Double_t mu_minus_PE     = 0.;
    Double_t mu_minus_ETA    = 0.;
    Double_t mu_minus_PHI    = 0.;

    Double_t mu_minus_TRUE_PX     = 0.;
    Double_t mu_minus_TRUE_PY     = 0.;
    Double_t mu_minus_TRUE_PZ     = 0.;
    Double_t mu_minus_TRUE_PT     = 0.;
    Double_t mu_minus_TRUE_PE     = 0.;
    Double_t mu_minus_TRUE_ETA    = 0.;
    Double_t mu_minus_TRUE_PHI    = 0.;

    Double_t mu_minus_PX_DTF     = 0.;
    Double_t mu_minus_PY_DTF     = 0.;
    Double_t mu_minus_PZ_DTF     = 0.;
    Double_t mu_minus_PT_DTF     = 0.;
    Double_t mu_minus_PE_DTF     = 0.;
    Double_t mu_minus_ETA_DTF    = 0.;
    Double_t mu_minus_PHI_DTF    = 0.;

    Float_t f_mu_minus_PX_DTF[100];
    Float_t f_mu_minus_PY_DTF[100];
    Float_t f_mu_minus_PZ_DTF[100];
    Float_t f_mu_minus_PT_DTF[100];
    Float_t f_mu_minus_PE_DTF[100];

    ///mu+
    Double_t mu_plus_PX     = 0.;
    Double_t mu_plus_PY     = 0.;
    Double_t mu_plus_PZ     = 0.;
    Double_t mu_plus_PT     = 0.;
    Double_t mu_plus_PE     = 0.;
    Double_t mu_plus_ETA    = 0.;
    Double_t mu_plus_PHI    = 0.;

    Double_t mu_plus_TRUE_PX     = 0.;
    Double_t mu_plus_TRUE_PY     = 0.;
    Double_t mu_plus_TRUE_PZ     = 0.;
    Double_t mu_plus_TRUE_PT     = 0.;
    Double_t mu_plus_TRUE_PE     = 0.;
    Double_t mu_plus_TRUE_ETA    = 0.;
    Double_t mu_plus_TRUE_PHI    = 0.;

    Double_t mu_plus_PX_DTF     = 0.;
    Double_t mu_plus_PY_DTF     = 0.;
    Double_t mu_plus_PZ_DTF     = 0.;
    Double_t mu_plus_PT_DTF     = 0.;
    Double_t mu_plus_PE_DTF     = 0.;
    Double_t mu_plus_ETA_DTF    = 0.;
    Double_t mu_plus_PHI_DTF    = 0.;

    Float_t f_mu_plus_PX_DTF[100];
    Float_t f_mu_plus_PY_DTF[100];
    Float_t f_mu_plus_PZ_DTF[100];
    Float_t f_mu_plus_PE_DTF[100];

    ///gamma1
    Double_t gamma1_PX     = 0.;
    Double_t gamma1_PY     = 0.;
    Double_t gamma1_PZ     = 0.;
    Double_t gamma1_PT     = 0.;
    Double_t gamma1_PE     = 0.;
    Double_t gamma1_ETA    = 0.;
    Double_t gamma1_PHI    = 0.;

    Double_t gamma1_TRUE_PX     = 0.;
    Double_t gamma1_TRUE_PY     = 0.;
    Double_t gamma1_TRUE_PZ     = 0.;
    Double_t gamma1_TRUE_PT     = 0.;
    Double_t gamma1_TRUE_PE     = 0.;
    Double_t gamma1_TRUE_ETA    = 0.;
    Double_t gamma1_TRUE_PHI    = 0.;

    Double_t gamma1_PX_DTF      = 0.;
    Double_t gamma1_PY_DTF      = 0.;
    Double_t gamma1_PZ_DTF      = 0.;
    Double_t gamma1_PT_DTF      = 0.;
    Double_t gamma1_PE_DTF      = 0.;
    Double_t gamma1_ETA_DTF     = 0.;
    Double_t gamma1_PHI_DTF     = 0.;

    Float_t f_gamma1_PX_DTF[100];
    Float_t f_gamma1_PY_DTF[100];
    Float_t f_gamma1_PZ_DTF[100];
    Float_t f_gamma1_PE_DTF[100];

    ///gamma2
    Double_t gamma2_PX     = 0.;
    Double_t gamma2_PY     = 0.;
    Double_t gamma2_PZ     = 0.;
    Double_t gamma2_PT     = 0.;
    Double_t gamma2_PE     = 0.;
    Double_t gamma2_ETA    = 0.;
    Double_t gamma2_PHI    = 0.;

    Double_t gamma2_PX_DTF      = 0.;
    Double_t gamma2_PY_DTF      = 0.;
    Double_t gamma2_PZ_DTF      = 0.;
    Double_t gamma2_PT_DTF      = 0.;
    Double_t gamma2_PE_DTF      = 0.;
    Double_t gamma2_ETA_DTF     = 0.;
    Double_t gamma2_PHI_DTF     = 0.;

    Double_t gamma2_TRUE_PX     = 0.;
    Double_t gamma2_TRUE_PY     = 0.;
    Double_t gamma2_TRUE_PZ     = 0.;
    Double_t gamma2_TRUE_PT     = 0.;
    Double_t gamma2_TRUE_PE     = 0.;
    Double_t gamma2_TRUE_ETA    = 0.;
    Double_t gamma2_TRUE_PHI    = 0.;

    Float_t f_gamma2_PX_DTF[100];
    Float_t f_gamma2_PY_DTF[100];
    Float_t f_gamma2_PZ_DTF[100];
    Float_t f_gamma2_PE_DTF[100];

    //angles
    Double_t B_plus_ThetaL = 0.;
    Double_t B_plus_ThetaK = 0.;
    Double_t B_plus_Phi = 0.;

    Double_t B_plus_ThetaL_DTF = 0.;
    Double_t B_plus_ThetaK_DTF = 0.;
    Double_t B_plus_Phi_DTF = 0.;

    Double_t B_plus_TRUE_ThetaL = 0.;
    Double_t B_plus_TRUE_ThetaK = 0.;
    Double_t B_plus_TRUE_Phi = 0.;

    Double_t open_angle = 0.;
    Double_t open_angle_DTF = 0.;
    Double_t TRUE_open_angle = 0.;


    //global variables
    Double_t B_plus_ENDVERTEX_Z = 0.;
    Double_t B_plus_ENDVERTEX_ZERR = 0.;
    Double_t B_plus_OWNPV_Z = 0.;
    Double_t B_plus_OWNPV_ZERR = 0.;
    Double_t B_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t B_plus_TRUEENDVERTEX_Z = 0.;
    Double_t B_plus_TOPPV_Z = 0.;//TODO: what is this?
    Float_t f_B_plus_DTF_PV_Z[100];
    Double_t B_plus_DTF_PV_Z = 0.;

    Double_t K_star_plus_ENDVERTEX_Z = 0.;
    Double_t K_star_plus_ENDVERTEX_ZERR = 0.;
    Double_t K_star_plus_OWNPV_Z = 0.;
    Double_t K_star_plus_OWNPV_ZERR = 0.;
    Double_t K_star_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t K_star_plus_TRUEENDVERTEX_Z = 0.;

    Double_t K_plus_OWNPV_Z = 0.;
    Double_t K_plus_OWNPV_ZERR = 0.;
    Double_t K_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t K_plus_TRUEENDVERTEX_Z = 0.;

    Double_t pi_zero_resolved_TRUEORIGINVERTEX_Z = 0.;
    Double_t pi_zero_resolved_TRUEENDVERTEX_Z = 0.;

    Double_t gamma1_TRUEORIGINVERTEX_Z = 0.;
    Double_t gamma1_TRUEENDVERTEX_Z = 0.;

    Double_t gamma2_TRUEORIGINVERTEX_Z = 0.;
    Double_t gamma2_TRUEENDVERTEX_Z = 0.;

    Double_t mu_plus_OWNPV_Z = 0.;
    Double_t mu_plus_OWNPV_ZERR = 0.;
    Double_t mu_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t mu_plus_TRUEENDVERTEX_Z = 0.;

    Double_t mu_minus_OWNPV_Z = 0.;
    Double_t mu_minus_OWNPV_ZERR = 0.;
    Double_t mu_minus_TRUEORIGINVERTEX_Z = 0.;
    Double_t mu_minus_TRUEENDVERTEX_Z = 0.;


    tree->SetBranchStatus("*",0);
    tree->SetBranchStatus( "*P", 1 );
    tree->SetBranchStatus( "*PX", 1 );
    tree->SetBranchStatus( "*PY", 1);
    tree->SetBranchStatus( "*PZ", 1);
    tree->SetBranchStatus( "*PE", 1);
    tree->SetBranchStatus( "*PT", 1 );
    tree->SetBranchStatus( "*TRUEP*", 1 );
    tree->SetBranchStatus( "*ETA*", 1 );
    tree->SetBranchStatus( "*PHI", 1 );

    tree->SetBranchStatus( "*Phi",1 );
    tree->SetBranchStatus( "*ThetaL",1 );
    tree->SetBranchStatus( "*ThetaK",1 );

    tree->SetBranchStatus("*ENDVERTEX_Z*",1);
    tree->SetBranchStatus("*OWNPV_Z*",1);
    tree->SetBranchStatus("*TRUEORIGINVERTEX_Z*",1);
    tree->SetBranchStatus("*TRUEENDVERTEX_Z*",1);

    //particle masses and momenta
    tree -> SetBranchAddress( "B_plus_PX" ,  &B_plus_PX );
    tree -> SetBranchAddress( "B_plus_PY" ,  &B_plus_PY );
    tree -> SetBranchAddress( "B_plus_PZ" ,  &B_plus_PZ );
    tree -> SetBranchAddress( "B_plus_PT" ,  &B_plus_PT );
    tree -> SetBranchAddress( "B_plus_PE" ,  &B_plus_PE );
    tree -> SetBranchAddress( "B_plus_ETA" , &B_plus_ETA );
    if (!preselected) tree -> SetBranchAddress( "B_plus_PHI" , &B_plus_PHI );

    tree -> SetBranchAddress( "B_plus_TRUEP_X" , &B_plus_TRUE_PX );
    tree -> SetBranchAddress( "B_plus_TRUEP_Y" , &B_plus_TRUE_PY );
    tree -> SetBranchAddress( "B_plus_TRUEP_Z" , &B_plus_TRUE_PZ );
    tree -> SetBranchAddress( "B_plus_TRUEP_E" , &B_plus_TRUE_PE );
    tree -> SetBranchAddress( "B_plus_TRUEPT" , &B_plus_TRUE_PT );

    tree -> SetBranchAddress( "K_star_plus_PX" ,    &K_star_plus_PX );
    tree -> SetBranchAddress( "K_star_plus_PY" ,    &K_star_plus_PY );
    tree -> SetBranchAddress( "K_star_plus_PZ" ,    &K_star_plus_PZ );
    tree -> SetBranchAddress( "K_star_plus_PE" ,    &K_star_plus_PE );
    tree -> SetBranchAddress( "K_star_plus_PT" ,    &K_star_plus_PT );

    tree -> SetBranchAddress( "K_star_plus_TRUEP_X" , &K_star_plus_TRUE_PX );
    tree -> SetBranchAddress( "K_star_plus_TRUEP_Y" , &K_star_plus_TRUE_PY );
    tree -> SetBranchAddress( "K_star_plus_TRUEP_Z" , &K_star_plus_TRUE_PZ );
    tree -> SetBranchAddress( "K_star_plus_TRUEP_E" , &K_star_plus_TRUE_PE );
    tree -> SetBranchAddress( "K_star_plus_TRUEPT" , &K_star_plus_TRUE_PT );

    if(Kst2Kpluspi0Merged){ //TODO: in case one really wants to do that, add DTF info
        tree -> SetBranchAddress( "pi_zero_merged_PX" , &pi_zero_merged_PX );
        tree -> SetBranchAddress( "pi_zero_merged_PY" , &pi_zero_merged_PY );
        tree -> SetBranchAddress( "pi_zero_merged_PZ" , &pi_zero_merged_PZ );
        tree -> SetBranchAddress( "pi_zero_merged_PE" , &pi_zero_merged_PE );
        tree -> SetBranchAddress( "pi_zero_merged_PT" , &pi_zero_merged_PT );
        tree -> SetBranchAddress( "pi_zero_merged_ETA" , &pi_zero_merged_ETA );
        tree -> SetBranchAddress( "pi_zero_merged_PHI" , &pi_zero_merged_PHI );

        tree -> SetBranchAddress( "pi_zero_merged_TRUEP_X" , &pi_zero_merged_TRUE_PX );
        tree -> SetBranchAddress( "pi_zero_merged_TRUEP_Y" , &pi_zero_merged_TRUE_PY );
        tree -> SetBranchAddress( "pi_zero_merged_TRUEP_Z" , &pi_zero_merged_TRUE_PZ );
        tree -> SetBranchAddress( "pi_zero_merged_TRUEP_E" , &pi_zero_merged_TRUE_PE );
        tree -> SetBranchAddress( "pi_zero_merged_TRUEPT"  , &pi_zero_merged_TRUE_PT );

        tree -> SetBranchAddress( "pi_zero_merged_PX_DTF" , &pi_zero_merged_PX_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PY_DTF" , &pi_zero_merged_PY_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PZ_DTF" , &pi_zero_merged_PZ_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PE_DTF" , &pi_zero_merged_PE_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PT_DTF" , &pi_zero_merged_PT_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_ETA_DTF" , &pi_zero_merged_ETA_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PHI_DTF" , &pi_zero_merged_PHI_DTF );
    }

    if(Kst2Kpluspi0Resolved){
        tree -> SetBranchAddress( "pi_zero_resolved_PX" , &pi_zero_resolved_PX );
        tree -> SetBranchAddress( "pi_zero_resolved_PY" , &pi_zero_resolved_PY );
        tree -> SetBranchAddress( "pi_zero_resolved_PZ" , &pi_zero_resolved_PZ );
        tree -> SetBranchAddress( "pi_zero_resolved_PT" , &pi_zero_resolved_PT );
        tree -> SetBranchAddress( "pi_zero_resolved_PE" , &pi_zero_resolved_PE );
        tree -> SetBranchAddress( "pi_zero_resolved_ETA" , &pi_zero_resolved_ETA );
        if (!preselected) tree -> SetBranchAddress( "pi_zero_resolved_PHI" , &pi_zero_resolved_PHI );

        tree -> SetBranchAddress( "pi_zero_resolved_TRUEP_X" , &pi_zero_resolved_TRUE_PX );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUEP_Y" , &pi_zero_resolved_TRUE_PY );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUEP_Z" , &pi_zero_resolved_TRUE_PZ );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUEP_E" , &pi_zero_resolved_TRUE_PE );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUEPT" ,  &pi_zero_resolved_TRUE_PT );

        tree -> SetBranchAddress( "gamma1_PX" , &gamma1_PX );
        tree -> SetBranchAddress( "gamma1_PY" , &gamma1_PY );
        tree -> SetBranchAddress( "gamma1_PZ" , &gamma1_PZ );
        tree -> SetBranchAddress( "gamma1_PE" , &gamma1_PE );
        tree -> SetBranchAddress( "gamma1_PT" , &gamma1_PT );

        tree -> SetBranchAddress( "gamma1_TRUEP_X" , &gamma1_TRUE_PX );
        tree -> SetBranchAddress( "gamma1_TRUEP_Y" , &gamma1_TRUE_PY );
        tree -> SetBranchAddress( "gamma1_TRUEP_Z" , &gamma1_TRUE_PZ );
        tree -> SetBranchAddress( "gamma1_TRUEP_E" , &gamma1_TRUE_PE );
        tree -> SetBranchAddress( "gamma1_TRUEPT" , &gamma1_TRUE_PT );

        tree -> SetBranchAddress( "gamma2_PX" , &gamma2_PX );
        tree -> SetBranchAddress( "gamma2_PY" , &gamma2_PY );
        tree -> SetBranchAddress( "gamma2_PZ" , &gamma2_PZ );
        tree -> SetBranchAddress( "gamma2_PE" , &gamma2_PE );
        tree -> SetBranchAddress( "gamma2_PT" , &gamma2_PT );

        tree -> SetBranchAddress( "gamma2_TRUEP_X" , &gamma2_TRUE_PX );
        tree -> SetBranchAddress( "gamma2_TRUEP_Y" , &gamma2_TRUE_PY );
        tree -> SetBranchAddress( "gamma2_TRUEP_Z" , &gamma2_TRUE_PZ );
        tree -> SetBranchAddress( "gamma2_TRUEP_E" , &gamma2_TRUE_PE );
        tree -> SetBranchAddress( "gamma2_TRUEPT" , &gamma2_TRUE_PT );

        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_0_PX" , &f_gamma1_PX_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_0_PY" , &f_gamma1_PY_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_0_PZ" , &f_gamma1_PZ_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_0_PE" , &f_gamma1_PE_DTF );

        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_PX" , &f_gamma2_PX_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_PY" , &f_gamma2_PY_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_PZ" , &f_gamma2_PZ_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_pi0_gamma_PE" , &f_gamma2_PE_DTF );
    }

    if(Kst2Kpluspi0Merged || Kst2Kpluspi0Resolved){
        tree -> SetBranchAddress( "K_plus_PX" , &K_plus_PX );
        tree -> SetBranchAddress( "K_plus_PY" , &K_plus_PY );
        tree -> SetBranchAddress( "K_plus_PZ" , &K_plus_PZ );
        tree -> SetBranchAddress( "K_plus_PE" , &K_plus_PE );
        tree -> SetBranchAddress( "K_plus_PT" , &K_plus_PT );
        tree -> SetBranchAddress( "K_plus_ETA" , &K_plus_ETA );

        tree -> SetBranchAddress( "K_plus_TRUEP_X" ,  &K_plus_TRUE_PX );
        tree -> SetBranchAddress( "K_plus_TRUEP_Y" ,  &K_plus_TRUE_PY );
        tree -> SetBranchAddress( "K_plus_TRUEP_Z" ,  &K_plus_TRUE_PZ );
        tree -> SetBranchAddress( "K_plus_TRUEP_E" ,  &K_plus_TRUE_PE );
        tree -> SetBranchAddress( "K_plus_TRUEPT" ,   &K_plus_TRUE_PT );

        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_Kplus_PX" , &f_K_plus_PX_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_Kplus_PY" , &f_K_plus_PY_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_Kplus_PZ" , &f_K_plus_PZ_DTF );
        tree -> SetBranchAddress( "B_plus_DTF_Kst_892_plus_Kplus_PE" , &f_K_plus_PE_DTF );
        if (!preselected) tree -> SetBranchAddress( "K_plus_PHI" , &K_plus_PHI );

    }

    tree -> SetBranchAddress( "mu_minus_PX" , &mu_minus_PX );
    tree -> SetBranchAddress( "mu_minus_PY" , &mu_minus_PY );
    tree -> SetBranchAddress( "mu_minus_PZ" , &mu_minus_PZ );
    tree -> SetBranchAddress( "mu_minus_PE" , &mu_minus_PE );
    tree -> SetBranchAddress( "mu_minus_PT" , &mu_minus_PT );

    tree -> SetBranchAddress( "mu_minus_TRUEP_X" , &mu_minus_TRUE_PX );
    tree -> SetBranchAddress( "mu_minus_TRUEP_Y" , &mu_minus_TRUE_PY );
    tree -> SetBranchAddress( "mu_minus_TRUEP_Z" , &mu_minus_TRUE_PZ );
    tree -> SetBranchAddress( "mu_minus_TRUEP_E" , &mu_minus_TRUE_PE );
    tree -> SetBranchAddress( "mu_minus_TRUEPT" ,  &mu_minus_TRUE_PT );

    tree -> SetBranchAddress( "mu_plus_PX" , &mu_plus_PX );
    tree -> SetBranchAddress( "mu_plus_PY" , &mu_plus_PY );
    tree -> SetBranchAddress( "mu_plus_PZ" , &mu_plus_PZ );
    tree -> SetBranchAddress( "mu_plus_PE" , &mu_plus_PE );
    tree -> SetBranchAddress( "mu_plus_PT" , &mu_plus_PT );

    tree -> SetBranchAddress( "mu_plus_TRUEP_X" , &mu_plus_TRUE_PX );
    tree -> SetBranchAddress( "mu_plus_TRUEP_Y" , &mu_plus_TRUE_PY );
    tree -> SetBranchAddress( "mu_plus_TRUEP_Z" , &mu_plus_TRUE_PZ );
    tree -> SetBranchAddress( "mu_plus_TRUEP_E" , &mu_plus_TRUE_PE );
    tree -> SetBranchAddress( "mu_plus_TRUEPT" , &mu_plus_TRUE_PT );

    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_0_PX" , &f_mu_minus_PX_DTF );
    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_0_PY" , &f_mu_minus_PY_DTF );
    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_0_PZ" , &f_mu_minus_PZ_DTF );
    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_0_PE" , &f_mu_minus_PE_DTF );

    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_PX" , &f_mu_plus_PX_DTF );
    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_PY" , &f_mu_plus_PY_DTF );
    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_PZ" , &f_mu_plus_PZ_DTF );
    tree -> SetBranchAddress( "B_plus_DTF_J_psi_1S_muminus_PE" , &f_mu_plus_PE_DTF );

    /* TODO
    tree -> SetBranchAddress( "B_plus_ThetaL_DTF" , &B_plus_ThetaL_DTF);
    tree -> SetBranchAddress( "B_plus_ThetaK_DTF" , &B_plus_ThetaK_DTF);
    tree -> SetBranchAddress( "B_plus_Phi_DTF" ,    &B_plus_Phi_DTF );
*/
    tree -> SetBranchAddress( "B_plus_ThetaL" , &B_plus_ThetaL);
    tree -> SetBranchAddress( "B_plus_ThetaK" , &B_plus_ThetaK);
    tree -> SetBranchAddress( "B_plus_Phi" ,    &B_plus_Phi );

    tree -> SetBranchAddress( "B_plus_TRUEThetaL" , &B_plus_TRUE_ThetaL);
    tree -> SetBranchAddress( "B_plus_TRUEThetaK" , &B_plus_TRUE_ThetaK);
    tree -> SetBranchAddress( "B_plus_TRUEPhi" ,    &B_plus_TRUE_Phi );


    //global variables
    tree -> SetBranchAddress( "B_plus_ENDVERTEX_Z", &B_plus_ENDVERTEX_Z);
    tree -> SetBranchAddress( "B_plus_ENDVERTEX_ZERR", &B_plus_ENDVERTEX_ZERR);
    tree -> SetBranchAddress( "B_plus_OWNPV_Z", &B_plus_OWNPV_Z);
    tree -> SetBranchAddress( "B_plus_OWNPV_ZERR", &B_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "B_plus_TRUEORIGINVERTEX_Z", &B_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "B_plus_TRUEENDVERTEX_Z", &B_plus_TRUEENDVERTEX_Z);
    tree -> SetBranchAddress( "B_plus_DTF_PV_Z", &f_B_plus_DTF_PV_Z);
    tree -> SetBranchAddress( "B_plus_TOPPV_Z", &B_plus_TOPPV_Z);//TODO: what is this?

    tree -> SetBranchAddress( "K_star_plus_ENDVERTEX_Z", &K_star_plus_ENDVERTEX_Z);
    tree -> SetBranchAddress( "K_star_plus_ENDVERTEX_ZERR", &K_star_plus_ENDVERTEX_ZERR);
    tree -> SetBranchAddress( "K_star_plus_OWNPV_Z", &K_star_plus_OWNPV_Z);
    tree -> SetBranchAddress( "K_star_plus_OWNPV_ZERR", &K_star_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "K_star_plus_TRUEORIGINVERTEX_Z", &K_star_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "K_star_plus_TRUEENDVERTEX_Z", &K_star_plus_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "K_plus_OWNPV_Z", &K_plus_OWNPV_Z);
    tree -> SetBranchAddress( "K_plus_OWNPV_ZERR", &K_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "K_plus_TRUEORIGINVERTEX_Z", &K_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "K_plus_TRUEENDVERTEX_Z", &K_plus_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "pi_zero_resolved_TRUEORIGINVERTEX_Z", &pi_zero_resolved_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "pi_zero_resolved_TRUEENDVERTEX_Z", &pi_zero_resolved_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "gamma1_TRUEORIGINVERTEX_Z", &gamma1_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "gamma1_TRUEENDVERTEX_Z", &gamma1_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "gamma2_TRUEORIGINVERTEX_Z", &gamma2_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "gamma2_TRUEENDVERTEX_Z", &gamma2_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "mu_plus_OWNPV_Z", &mu_plus_OWNPV_Z);
    tree -> SetBranchAddress( "mu_plus_OWNPV_ZERR", &mu_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "mu_plus_TRUEORIGINVERTEX_Z", &mu_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "mu_plus_TRUEENDVERTEX_Z", &mu_plus_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "mu_minus_OWNPV_Z", &mu_minus_OWNPV_Z);
    tree -> SetBranchAddress( "mu_minus_OWNPV_ZERR", &mu_minus_OWNPV_ZERR);
    tree -> SetBranchAddress( "mu_minus_TRUEORIGINVERTEX_Z", &mu_minus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "mu_minus_TRUEENDVERTEX_Z", &mu_minus_TRUEENDVERTEX_Z);


    // Create outputfile
    // Kst2Kpluspi0Resolved case
    string newName = "";
    preselected ?  newName = "_resolution_preselected" : newName ="_resolution";
    if (truthMatched) newName = newName + "_TM";

    if(Kst2Kpluspi0Resolved){
        if(!MC){
            output = new TFile(Form("%s/data/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
        }
        else{
            if(ReferenceChannel){
                output = new TFile(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else if(PHSP){
                output = new TFile(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else{
                output = new TFile(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
        }
    }
    // Kst2Kpluspi0Merged case
    if(Kst2Kpluspi0Merged){
        if(!MC){
            output = new TFile(Form("%s/data/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
        }
        else{
            if(ReferenceChannel){
                output = new TFile(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else if(PHSP){
                output = new TFile(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else{
                output = new TFile(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
        }
    }
    // Kst2Kspiplus case
    if(Kst2Kspiplus){
        if(!MC){
            output = new TFile(Form("%s/data/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
        }
        else{
            if(ReferenceChannel){
                output = new TFile(Form("%s/data/MC/RefKshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else if(PHSP){
                output = new TFile(Form("%s/data/PHSP/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else{
                output = new TFile(Form("%s/data/MC/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
        }
    }

    //Initialize new tree
    output->cd();
    TTree * resolution_tree = tree->CloneTree(0);

    /////////////////////////////
    //   Initialize branches   //
    /////////////////////////////
    // Rename true branches
    // Initialize DTF branches

    //-----------------------------------------------------------------------------------
    if (preselected) resolution_tree -> Branch( "B_plus_PHI" , &B_plus_PHI, "B_plus_PHI/D");

    resolution_tree -> Branch( "B_plus_TRUE_PX" , &B_plus_TRUE_PX, "B_plus_TRUE_PX/D");
    resolution_tree -> Branch( "B_plus_TRUE_PY" , &B_plus_TRUE_PY, "B_plus_TRUE_PY/D");
    resolution_tree -> Branch( "B_plus_TRUE_PZ" , &B_plus_TRUE_PZ, "B_plus_TRUE_PZ/D");
    resolution_tree -> Branch( "B_plus_TRUE_PE" , &B_plus_TRUE_PE, "B_plus_TRUE_PE/D");
    resolution_tree -> Branch( "B_plus_TRUE_PT" , &B_plus_TRUE_PT, "B_plus_TRUE_PT/D");
    resolution_tree -> Branch( "B_plus_TRUE_ETA" , &B_plus_TRUE_ETA, "B_plus_TRUE_ETA/D");
    resolution_tree -> Branch( "B_plus_TRUE_PHI" , &B_plus_TRUE_PHI, "B_plus_TRUE_PHI/D");

    resolution_tree -> Branch( "B_plus_PX_DTF" ,  &B_plus_PX_DTF, "B_plus_PX_DTF/D");
    resolution_tree -> Branch( "B_plus_PY_DTF" ,  &B_plus_PY_DTF, "B_plus_PY_DTF/D");
    resolution_tree -> Branch( "B_plus_PZ_DTF" ,  &B_plus_PZ_DTF, "B_plus_PZ_DTF/D");
    resolution_tree -> Branch( "B_plus_PT_DTF" ,  &B_plus_PT_DTF, "B_plus_PT_DTF/D");
    resolution_tree -> Branch( "B_plus_PE_DTF" ,  &B_plus_PE_DTF, "B_plus_PE_DTF/D");
    resolution_tree -> Branch( "B_plus_ETA_DTF",  &B_plus_ETA_DTF, "B_plus_ETA_DTF/D");
    resolution_tree -> Branch( "B_plus_PHI_DTF",  &B_plus_PHI_DTF, "B_plus_PHI_DTF/D");
    //-----------------------------------------------------------------------------------
    resolution_tree -> Branch( "K_star_plus_TRUE_PX" , &K_star_plus_TRUE_PX, "K_star_plus_TRUE_PX/D");
    resolution_tree -> Branch( "K_star_plus_TRUE_PY" , &K_star_plus_TRUE_PY, "K_star_plus_TRUE_PY/D");
    resolution_tree -> Branch( "K_star_plus_TRUE_PZ" , &K_star_plus_TRUE_PZ, "K_star_plus_TRUE_PZ/D");
    resolution_tree -> Branch( "K_star_plus_TRUE_PE" , &K_star_plus_TRUE_PE, "K_star_plus_TRUE_PE/D");
    resolution_tree -> Branch( "K_star_plus_TRUE_PT" , &K_star_plus_TRUE_PT, "K_star_plus_TRUE_PT/D");
    resolution_tree -> Branch( "K_star_plus_TRUE_ETA" , &K_star_plus_TRUE_ETA, "K_star_plus_TRUE_ETA/D");
    resolution_tree -> Branch( "K_star_plus_TRUE_PHI" , &K_star_plus_TRUE_PHI, "K_star_plus_TRUE_PHI/D");

    resolution_tree -> Branch( "K_star_plus_PX_DTF" ,  &K_star_plus_PX_DTF, "K_star_plus_PX_DTF/D");
    resolution_tree -> Branch( "K_star_plus_PY_DTF" ,  &K_star_plus_PY_DTF, "K_star_plus_PY_DTF/D");
    resolution_tree -> Branch( "K_star_plus_PZ_DTF" ,  &K_star_plus_PZ_DTF, "K_star_plus_PZ_DTF/D");
    resolution_tree -> Branch( "K_star_plus_PT_DTF" ,  &K_star_plus_PT_DTF, "K_star_plus_PT_DTF/D");
    resolution_tree -> Branch( "K_star_plus_PE_DTF" ,  &K_star_plus_PE_DTF, "K_star_plus_PE_DTF/D");
    resolution_tree -> Branch( "K_star_plus_ETA_DTF",  &K_star_plus_ETA_DTF, "K_star_plus_ETA_DTF/D");
    resolution_tree -> Branch( "K_star_plus_PHI_DTF",  &K_star_plus_PHI_DTF, "K_star_plus_PHI_DTF/D");
    //-----------------------------------------------------------------------------------

    if(Kst2Kpluspi0Merged){ //TODO: in case one really wants to do that, add DTF info
        resolution_tree -> Branch( "pi_zero_merged_TRUE_PX" , &pi_zero_merged_TRUE_PX, "pi_zero_merged_TRUE_PX/D" );
        resolution_tree -> Branch( "pi_zero_merged_TRUE_PY" , &pi_zero_merged_TRUE_PY, "pi_zero_merged_TRUE_PY/D"  );
        resolution_tree -> Branch( "pi_zero_merged_TRUE_PZ" , &pi_zero_merged_TRUE_PZ, "pi_zero_merged_TRUE_PZ/D"  );
        resolution_tree -> Branch( "pi_zero_merged_TRUE_PE" , &pi_zero_merged_TRUE_PE, "pi_zero_merged_TRUE_PE/D"  );
        resolution_tree -> Branch( "pi_zero_merged_TRUE_PT"  , &pi_zero_merged_TRUE_PT, "pi_zero_merged_TRUE_PT/D"  );
        resolution_tree -> Branch( "pi_zero_merged_TRUE_ETA", &pi_zero_merged_TRUE_ETA, "pi_zero_merged_TRUE_ETA/D"  );
        resolution_tree -> Branch( "pi_zero_merged_TRUE_PHI", &pi_zero_merged_TRUE_PHI, "pi_zero_merged_TRUE_PHI/D"  );

        resolution_tree -> Branch( "pi_zero_merged_PX_DTF" , &pi_zero_merged_PX_DTF, "pi_zero_merged_PX_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_merged_PY_DTF" , &pi_zero_merged_PY_DTF, "pi_zero_merged_PY_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_merged_PZ_DTF" , &pi_zero_merged_PZ_DTF, "pi_zero_merged_PZ_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_merged_PE_DTF" , &pi_zero_merged_PE_DTF, "pi_zero_merged_PE_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_merged_PT_DTF" , &pi_zero_merged_PT_DTF, "pi_zero_merged_PT_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_merged_ETA_DTF" , &pi_zero_merged_ETA_DTF, "pi_zero_merged_ETA_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_merged_PHI_DTF" , &pi_zero_merged_PHI_DTF, "pi_zero_merged_PHI_DTF/D"  );
    }
    //-----------------------------------------------------------------------------------

    if(Kst2Kpluspi0Resolved){
        resolution_tree -> Branch( "pi_zero_resolved_PHI", &pi_zero_resolved_PHI, "pi_zero_resolved_PHI/D"  );

        resolution_tree -> Branch( "pi_zero_resolved_TRUE_PX" , &pi_zero_resolved_TRUE_PX , "pi_zero_resolved_TRUE_PX/D" );
        resolution_tree -> Branch( "pi_zero_resolved_TRUE_PY" , &pi_zero_resolved_TRUE_PY , "pi_zero_resolved_TRUE_PY/D" );
        resolution_tree -> Branch( "pi_zero_resolved_TRUE_PZ" , &pi_zero_resolved_TRUE_PZ, "pi_zero_resolved_TRUE_PZ/D"  );
        resolution_tree -> Branch( "pi_zero_resolved_TRUE_PE" , &pi_zero_resolved_TRUE_PE, "pi_zero_resolved_TRUE_PE/D"  );
        resolution_tree -> Branch( "pi_zero_resolved_TRUE_PT" ,  &pi_zero_resolved_TRUE_PT , "pi_zero_resolved_TRUE_PT/D" );
        resolution_tree -> Branch( "pi_zero_resolved_TRUE_ETA", &pi_zero_resolved_TRUE_ETA , "pi_zero_resolved_TRUE_ETA/D" );
        resolution_tree -> Branch( "pi_zero_resolved_TRUE_PHI", &pi_zero_resolved_TRUE_PHI, "pi_zero_resolved_TRUE_PHI/D"  );

        resolution_tree -> Branch( "pi_zero_resolved_PX_DTF" , &pi_zero_resolved_PX_DTF , "pi_zero_resolved_PX_DTF/D" );
        resolution_tree -> Branch( "pi_zero_resolved_PY_DTF" , &pi_zero_resolved_PY_DTF, "pi_zero_resolved_PY_DTF/D"  );
        resolution_tree -> Branch( "pi_zero_resolved_PZ_DTF" , &pi_zero_resolved_PZ_DTF , "pi_zero_resolved_PZ_DTF/D" );
        resolution_tree -> Branch( "pi_zero_resolved_PT_DTF" , &pi_zero_resolved_PT_DTF , "pi_zero_resolved_PT_DTF/D" );
        resolution_tree -> Branch( "pi_zero_resolved_PE_DTF" , &pi_zero_resolved_PE_DTF , "pi_zero_resolved_PE_DTF/D" );
        resolution_tree -> Branch( "pi_zero_resolved_ETA_DTF" , &pi_zero_resolved_ETA_DTF , "pi_zero_resolved_ETA_DTF/D" );
        resolution_tree -> Branch( "pi_zero_resolved_PHI_DTF" , &pi_zero_resolved_PHI_DTF, "pi_zero_resolved_PHI_DTF/D"  );

        //-----------------------------------------------------------------------------------
        resolution_tree -> Branch( "gamma1_TRUE_PX" , &gamma1_TRUE_PX , "gamma1_TRUE_PX/D");
        resolution_tree -> Branch( "gamma1_TRUE_PY" , &gamma1_TRUE_PY , "gamma1_TRUE_PY/D" );
        resolution_tree -> Branch( "gamma1_TRUE_PZ" , &gamma1_TRUE_PZ , "gamma1_TRUE_PZ/D" );
        resolution_tree -> Branch( "gamma1_TRUE_PE" , &gamma1_TRUE_PE , "gamma1_TRUE_PE/D");
        resolution_tree -> Branch( "gamma1_TRUE_PT" , &gamma1_TRUE_PT , "gamma1_TRUE_PT/D");
        resolution_tree -> Branch( "gamma1_TRUE_ETA", &gamma1_TRUE_ETA, "gamma1_TRUE_ETA/D" );
        resolution_tree -> Branch( "gamma1_TRUE_PHI", &gamma1_TRUE_PHI, "gamma1_TRUE_PHI/D" );

        resolution_tree -> Branch( "gamma2_TRUE_PX" , &gamma2_TRUE_PX, "gamma2_TRUE_PX/D" );
        resolution_tree -> Branch( "gamma2_TRUE_PY" , &gamma2_TRUE_PY, "gamma2_TRUE_PY/D" );
        resolution_tree -> Branch( "gamma2_TRUE_PZ" , &gamma2_TRUE_PZ, "gamma2_TRUE_PZ/D" );
        resolution_tree -> Branch( "gamma2_TRUE_PE" , &gamma2_TRUE_PE , "gamma2_TRUE_PE/D");
        resolution_tree -> Branch( "gamma2_TRUE_PT" ,  &gamma2_TRUE_PT, "gamma2_TRUE_PT/D" );
        resolution_tree -> Branch( "gamma2_TRUE_ETA", &gamma2_TRUE_ETA, "gamma2_TRUE_ETA/D" );
        resolution_tree -> Branch( "gamma2_TRUE_PHI", &gamma2_TRUE_PHI, "gamma2_TRUE_PHI/D" );

        resolution_tree -> Branch( "gamma1_PX_DTF" , &gamma1_PX_DTF, "gamma1_PX_DTF/D");
        resolution_tree -> Branch( "gamma1_PY_DTF" , &gamma1_PY_DTF, "gamma1_PY_DTF/D" );
        resolution_tree -> Branch( "gamma1_PZ_DTF" , &gamma1_PZ_DTF, "gamma1_PZ_DTF/D" );
        resolution_tree -> Branch( "gamma1_PE_DTF" , &gamma1_PE_DTF, "gamma1_PE_DTF/D" );
        resolution_tree -> Branch( "gamma1_PT_DTF" , &gamma1_PT_DTF, "gamma1_PT_DTF/D");
        resolution_tree -> Branch( "gamma1_ETA_DTF" , &gamma1_ETA_DTF , "gamma1_ETA_DTF/D");
        resolution_tree -> Branch( "gamma1_PHI_DTF" , &gamma1_PHI_DTF , "gamma1_PHI_DTF/D");

        resolution_tree -> Branch( "gamma2_PX_DTF" , &gamma2_PX_DTF, "gamma2_PX_DTF/D" );
        resolution_tree -> Branch( "gamma2_PY_DTF" , &gamma2_PY_DTF, "gamma2_PY_DTF/D");
        resolution_tree -> Branch( "gamma2_PZ_DTF" , &gamma2_PZ_DTF, "gamma2_PZ_DTF/D");
        resolution_tree -> Branch( "gamma2_PE_DTF" , &gamma2_PE_DTF, "gamma2_PE_DTF/D" );
        resolution_tree -> Branch( "gamma2_PT_DTF" , &gamma2_PT_DTF, "gamma2_PT_DTF/D");
        resolution_tree -> Branch( "gamma2_ETA_DTF" , &gamma2_ETA_DTF , "gamma2_ETA_DTF/D");
        resolution_tree -> Branch( "gamma2_PHI_DTF" , &gamma2_PHI_DTF , "gamma2_PHI_DTF/D");


    }
    //-----------------------------------------------------------------------------------
    if(Kst2Kpluspi0Merged || Kst2Kpluspi0Resolved){

        resolution_tree -> Branch( "K_plus_TRUE_PX" ,  &K_plus_TRUE_PX , "K_plus_TRUE_PY/D");
        resolution_tree -> Branch( "K_plus_TRUE_PY" ,  &K_plus_TRUE_PY , "K_plus_TRUE_PY/D");
        resolution_tree -> Branch( "K_plus_TRUE_PZ" ,  &K_plus_TRUE_PZ , "K_plus_TRUE_PY/D" );
        resolution_tree -> Branch( "K_plus_TRUE_PE" ,  &K_plus_TRUE_PE , "K_plus_TRUE_PY/D" );
        resolution_tree -> Branch( "K_plus_TRUE_PT" ,  &K_plus_TRUE_PT , "K_plus_TRUE_PY/D" );
        resolution_tree -> Branch( "K_plus_TRUE_ETA" , &K_plus_TRUE_ETA , "K_plus_TRUE_PY/D" );
        resolution_tree -> Branch( "K_plus_TRUE_PHI" , &K_plus_TRUE_PHI , "K_plus_TRUE_PY/D" );


        resolution_tree -> Branch( "K_plus_PX_DTF" , &K_plus_PX_DTF , "K_plus_PX_DTF/D");
        resolution_tree -> Branch( "K_plus_PY_DTF" , &K_plus_PY_DTF , "K_plus_PY_DTF/D");
        resolution_tree -> Branch( "K_plus_PZ_DTF" , &K_plus_PZ_DTF , "K_plus_PZ_DTF/D");
        resolution_tree -> Branch( "K_plus_PE_DTF" , &K_plus_PE_DTF , "K_plus_PE_DTF/D");
        resolution_tree -> Branch( "K_plus_PT_DTF" , &K_plus_PT_DTF , "K_plus_PT_DTF/D");
        resolution_tree -> Branch( "K_plus_ETA_DTF" , &K_plus_ETA_DTF , "K_plus_ETA_DTF/D");
        resolution_tree -> Branch( "K_plus_PHI_DTF" , &K_plus_PHI_DTF , "K_plus_PHI_DTF/D");

    }
    //-----------------------------------------------------------------------------------

    resolution_tree -> Branch( "mu_minus_TRUE_PX" , &mu_minus_TRUE_PX , "mu_minus_TRUE_PX/D");
    resolution_tree -> Branch( "mu_minus_TRUE_PY" , &mu_minus_TRUE_PY , "mu_minus_TRUE_PY/D");
    resolution_tree -> Branch( "mu_minus_TRUE_PZ" , &mu_minus_TRUE_PZ , "mu_minus_TRUE_PZ/D");
    resolution_tree -> Branch( "mu_minus_TRUE_PE" , &mu_minus_TRUE_PE , "mu_minus_TRUE_PE/D");
    resolution_tree -> Branch( "mu_minus_TRUE_PT" ,  &mu_minus_TRUE_PT , "mu_minus_TRUE_PT/D");
    resolution_tree -> Branch( "mu_minus_TRUE_ETA" , &mu_minus_TRUE_ETA , "mu_minus_TRUE_ETA/D");
    resolution_tree -> Branch( "mu_minus_TRUE_PHI" , &mu_minus_TRUE_PHI , "mu_minus_TRUE_PHI/D");

    resolution_tree -> Branch( "mu_plus_TRUE_PX" , &mu_plus_TRUE_PX , "mu_plus_TRUE_PX/D");
    resolution_tree -> Branch( "mu_plus_TRUE_PY" , &mu_plus_TRUE_PY , "mu_plus_TRUE_PY/D");
    resolution_tree -> Branch( "mu_plus_TRUE_PZ" , &mu_plus_TRUE_PZ , "mu_plus_TRUE_PZ/D");
    resolution_tree -> Branch( "mu_plus_TRUE_PE" , &mu_plus_TRUE_PE , "mu_plus_TRUE_PE/D");
    resolution_tree -> Branch( "mu_plus_TRUE_PT" , &mu_plus_TRUE_PT , "mu_plus_TRUE_PT/D");
    resolution_tree -> Branch( "mu_plus_TRUE_ETA" , &mu_plus_TRUE_ETA , "mu_plus_TRUE_ETA/D");
    resolution_tree -> Branch( "mu_plus_TRUE_PHI" , &mu_plus_TRUE_PHI , "mu_plus_TRUE_PHI/D");


    resolution_tree -> Branch( "mu_minus_PX_DTF" , &mu_minus_PX_DTF , "mu_minus_PX_DTF/D");
    resolution_tree -> Branch( "mu_minus_PY_DTF" , &mu_minus_PY_DTF , "mu_minus_PY_DTF/D");
    resolution_tree -> Branch( "mu_minus_PZ_DTF" , &mu_minus_PZ_DTF , "mu_minus_PZ_DTF/D");
    resolution_tree -> Branch( "mu_minus_PE_DTF" , &mu_minus_PE_DTF , "mu_minus_PE_DTF/D");
    resolution_tree -> Branch( "mu_minus_PT_DTF" , &mu_minus_PT_DTF , "mu_minus_PT_DTF/D");
    resolution_tree -> Branch( "mu_minus_ETA_DTF" , &mu_minus_ETA_DTF , "mu_minus_ETA_DTF/D");
    resolution_tree -> Branch( "mu_minus_PHI_DTF" , &mu_minus_PHI_DTF , "mu_minus_PHI_DTF/D");

    resolution_tree -> Branch( "mu_plus_PX_DTF" , &mu_plus_PX_DTF , "mu_plus_PX_DTF/D");
    resolution_tree -> Branch( "mu_plus_PY_DTF" , &mu_plus_PY_DTF , "mu_plus_PY_DTF/D");
    resolution_tree -> Branch( "mu_plus_PZ_DTF" , &mu_plus_PZ_DTF , "mu_plus_PZ_DTF/D");
    resolution_tree -> Branch( "mu_plus_PE_DTF" , &mu_plus_PE_DTF , "mu_plus_PE_DTF/D");
    resolution_tree -> Branch( "mu_plus_PT_DTF" , &mu_plus_PT_DTF , "mu_plus_PT_DTF/D");
    resolution_tree -> Branch( "mu_plus_ETA_DTF" , &mu_plus_ETA_DTF , "mu_plus_ETA_DTF/D");
    resolution_tree -> Branch( "mu_plus_PHI_DTF" , &mu_plus_PHI_DTF , "mu_plus_PHI_DTF/D");

    //-----------------------------------------------------------------------------------
    resolution_tree -> Branch( "B_plus_DTF_PV_Z" , &B_plus_DTF_PV_Z, "B_plus_DTF_PV_Z/D");
    resolution_tree -> Branch( "open_angle" , &open_angle, "open_angle/D");
    resolution_tree -> Branch( "open_angle_DTF" , &open_angle_DTF, "open_angle_DTF/D");
    resolution_tree -> Branch( "TRUE_open_angle", &TRUE_open_angle , "TRUE_open_angle/D");

    resolution_tree -> Branch( "B_plus_ThetaL_DTF" , &B_plus_ThetaL_DTF, "B_plus_ThetaL_DTF/D");
    resolution_tree -> Branch( "B_plus_ThetaK_DTF" , &B_plus_ThetaK_DTF, "B_plus_ThetaK_DTF/D");
    resolution_tree -> Branch( "B_plus_Phi_DTF" ,    &B_plus_Phi_DTF , "B_plus_Phi_DTF/D");



    //////////////////////////////////
    /////                       //////
    ////     Loop over events    /////
    /////                       //////
    //////////////////////////////////
    for (int i=0; i<N; i++){

        //output the different counters!
        if ((0ul == (i % 10000ul) || i + 1 == N) && i != 0) {
            cout << "Read event " << i + 1 << "/" << N
                 << endl;
        }

        tree->GetEntry(i);


        /////////////////////
        //  DTF-values     //
        /////////////////////
        LorVec_DTF_mu_minus = TLorentzVector        (f_mu_minus_PX_DTF[0],           f_mu_minus_PY_DTF[0],            f_mu_minus_PZ_DTF[0],            f_mu_minus_PE_DTF[0]);
        LorVec_DTF_mu_plus = TLorentzVector         (f_mu_plus_PX_DTF[0],            f_mu_plus_PY_DTF[0],             f_mu_plus_PZ_DTF[0],             f_mu_plus_PE_DTF[0]);
        if(Kst2Kspiplus){
            /// Ks, pi+
            LorVec_DTF_pi_plus = TLorentzVector     (f_pi_plus_PX_DTF[0],            f_pi_plus_PY_DTF[0],             f_pi_plus_PZ_DTF[0],             f_pi_plus_PE_DTF[0]);
            LorVec_DTF_KS_pi_plus = TLorentzVector  (f_Ks_pi_plus_PX_DTF[0],         f_Ks_pi_plus_PY_DTF[0],          f_Ks_pi_plus_PZ_DTF[0],          f_Ks_pi_plus_PE_DTF[0]);
            LorVec_DTF_KS_pi_minus = TLorentzVector (f_Ks_pi_minus_PX_DTF[0],        f_Ks_pi_minus_PY_DTF[0],         f_Ks_pi_minus_PZ_DTF[0],         f_Ks_pi_minus_PE_DTF[0]);
            LorVec_DTF_pi_plus_MisIDed.SetXYZM    (f_pi_plus_PX_DTF[0],            f_pi_plus_PY_DTF[0],             f_pi_plus_PZ_DTF[0],             PDGMASS.MU);
            LorVec_DTF_mu_plus_MisIDed.SetXYZM    (f_mu_plus_PX_DTF[0],            f_mu_plus_PY_DTF[0],             f_mu_plus_PZ_DTF[0],             PDGMASS.PI_PLUS);
        }
        else{
            /// K+, pi0
            LorVec_DTF_K_plus = TLorentzVector    (f_K_plus_PX_DTF[0],             f_K_plus_PY_DTF[0],              f_K_plus_PZ_DTF[0],             f_K_plus_PE_DTF[0]);
            LorVec_DTF_gamma1 = TLorentzVector    (f_gamma1_PX_DTF[0],             f_gamma1_PY_DTF[0],              f_gamma1_PZ_DTF[0],             f_gamma1_PE_DTF[0]);
            LorVec_DTF_gamma2 = TLorentzVector    (f_gamma2_PX_DTF[0],             f_gamma2_PY_DTF[0],              f_gamma2_PZ_DTF[0],             f_gamma2_PE_DTF[0]);
        }

        //directly recombine the non-stable particles for later use (K*+ not really needed/used for Kst2Kspiplus)
        if(Kst2Kspiplus){
            LorVec_DTF_K_short = LorVec_DTF_KS_pi_plus + LorVec_DTF_KS_pi_minus;
            LorVec_DTF_KaonPion = LorVec_DTF_K_short + LorVec_DTF_pi_plus;
        }
        if(Kst2Kpluspi0Resolved){
            LorVec_DTF_DiGamma = LorVec_DTF_gamma1 + LorVec_DTF_gamma2;
            LorVec_DTF_pi_zero_resolved = LorVec_DTF_DiGamma;
            LorVec_DTF_KaonPion = LorVec_DTF_K_plus + LorVec_DTF_pi_zero_resolved;
        }
        LorVec_DTF_K_star_plus = LorVec_DTF_KaonPion;
        LorVec_DTF_DiMuon = LorVec_DTF_mu_minus + LorVec_DTF_mu_plus;
        LorVec_DTF_B_plus = LorVec_DTF_K_star_plus + LorVec_DTF_DiMuon;
        open_angle_DTF =LorVec_DTF_gamma1.Angle(LorVec_DTF_gamma2.Vect());


        /////////////////////
        //    TRUE values   //
        /////////////////////
        LorVec_TRUE_B_plus.SetXYZM           (B_plus_TRUE_PX,             B_plus_TRUE_PY,              B_plus_TRUE_PZ,              PDGMASS.B_PLUS);
        LorVec_TRUE_K_star_plus.SetXYZM      (K_star_plus_TRUE_PX,        K_star_plus_TRUE_PY,         K_star_plus_TRUE_PZ,         PDGMASS.K_STAR_PLUS);
        LorVec_TRUE_mu_minus.SetXYZM         (mu_minus_TRUE_PX,           mu_minus_TRUE_PY,            mu_minus_TRUE_PZ,            PDGMASS.MU);
        LorVec_TRUE_mu_plus.SetXYZM          (mu_plus_TRUE_PX,            mu_plus_TRUE_PY,             mu_plus_TRUE_PZ,             PDGMASS.MU);
        if(Kst2Kspiplus){
            /// Ks, pi+
            LorVec_TRUE_K_short.SetXYZM          (K_short_TRUE_PX,            K_short_TRUE_PY,             K_short_TRUE_PZ,             PDGMASS.K_SHORT);
            LorVec_TRUE_pi_plus.SetXYZM          (pi_plus_TRUE_PX,            pi_plus_TRUE_PY,             pi_plus_TRUE_PZ,             PDGMASS.PI_PLUS);
        }
        else{
            /// K+, pi0
            LorVec_TRUE_K_plus.SetXYZM           (K_plus_TRUE_PX,             K_plus_TRUE_PY,              K_plus_TRUE_PZ,              PDGMASS.K_PLUS);
            LorVec_TRUE_pi_zero_merged.SetXYZM   (pi_zero_merged_TRUE_PX,     pi_zero_merged_TRUE_PY,      pi_zero_merged_TRUE_PZ,      PDGMASS.PI_ZERO);
            LorVec_TRUE_pi_zero_resolved.SetXYZM (pi_zero_resolved_TRUE_PX,   pi_zero_resolved_TRUE_PY,    pi_zero_resolved_TRUE_PZ,    PDGMASS.PI_ZERO);
            LorVec_TRUE_gamma1.SetXYZM           (gamma1_TRUE_PX,             gamma1_TRUE_PY,              gamma1_TRUE_PZ,              PDGMASS.GAMMA);
            LorVec_TRUE_gamma2.SetXYZM           (gamma2_TRUE_PX,             gamma2_TRUE_PY,              gamma2_TRUE_PZ,              PDGMASS.GAMMA);
        }
        //Recombination of 'Parent'-Particles
        LorVec_TRUE_DiMuon = LorVec_TRUE_mu_minus + LorVec_TRUE_mu_plus;
        if(Kst2Kspiplus){
            LorVec_TRUE_KaonPion = LorVec_TRUE_K_short + LorVec_TRUE_pi_plus;
        }
        if(Kst2Kpluspi0Merged){
            LorVec_TRUE_KaonPion = LorVec_TRUE_K_plus + LorVec_TRUE_pi_zero_merged;
        }
        if(Kst2Kpluspi0Resolved){
            LorVec_TRUE_DiGamma = LorVec_TRUE_gamma1 + LorVec_TRUE_gamma2;
            LorVec_TRUE_KaonPion = LorVec_TRUE_K_plus + LorVec_TRUE_DiGamma;
        }
        TRUE_open_angle =LorVec_TRUE_gamma1.Angle(LorVec_TRUE_gamma2.Vect());

        //////////////////////
        //  detector values //
        //////////////////////
        LorVec_B_plus = TLorentzVector        (B_plus_PX,             B_plus_PY,              B_plus_PZ,              B_plus_PE);
        LorVec_K_star_plus = TLorentzVector   (K_star_plus_PX,        K_star_plus_PY,         K_star_plus_PZ,         K_star_plus_PE);
        LorVec_mu_minus = TLorentzVector      (mu_minus_PX,           mu_minus_PY,            mu_minus_PZ,            mu_minus_PE);
        LorVec_mu_plus = TLorentzVector       (mu_plus_PX,            mu_plus_PY,             mu_plus_PZ,             mu_plus_PE);
        if(Kst2Kspiplus){
            /// Ks, pi+
            LorVec_K_short = TLorentzVector       (K_short_PX,            K_short_PY,             K_short_PZ,             K_short_PE);
            LorVec_pi_plus = TLorentzVector       (pi_plus_PX,            pi_plus_PY,             pi_plus_PZ,             pi_plus_PE);
            LorVec_KS_pi_plus = TLorentzVector    (Ks_pi_plus_PX,         Ks_pi_plus_PY,          Ks_pi_plus_PZ,          Ks_pi_plus_PE);
            LorVec_KS_pi_minus = TLorentzVector   (Ks_pi_minus_PX,        Ks_pi_minus_PY,         Ks_pi_minus_PZ,         Ks_pi_minus_PE);

            LorVec_pi_plus_MisIDed.SetXYZM  (pi_plus_PX,            pi_plus_PY,             pi_plus_PZ,             PDGMASS.MU);
            LorVec_mu_plus_MisIDed.SetXYZM  (mu_plus_PX,            mu_plus_PY,             mu_plus_PZ,             PDGMASS.PI_PLUS);
        }
        else{
            /// K+, pi0
            LorVec_K_plus = TLorentzVector            (K_plus_PX,             K_plus_PY,              K_plus_PZ,              K_plus_PE);
            LorVec_pi_zero_merged = TLorentzVector	(pi_zero_merged_PX,     pi_zero_merged_PY,      pi_zero_merged_PZ,      pi_zero_merged_PE);
            LorVec_pi_zero_resolved = TLorentzVector  (pi_zero_resolved_PX,   pi_zero_resolved_PY,    pi_zero_resolved_PZ,    pi_zero_resolved_PE);
            LorVec_gamma1 = TLorentzVector            (gamma1_PX,             gamma1_PY,              gamma1_PZ,              gamma1_PE);
            LorVec_gamma2 = TLorentzVector            (gamma2_PX,             gamma2_PY,              gamma2_PZ,              gamma2_PE);
        }
        //Recombination of 'Parent'-Particles
        LorVec_DiMuon = LorVec_mu_minus + LorVec_mu_plus;
        if(Kst2Kspiplus){
            LorVec_KaonPion = LorVec_K_short + LorVec_pi_plus;
        }
        if(Kst2Kpluspi0Merged){
            LorVec_KaonPion = LorVec_K_plus + LorVec_pi_zero_merged;
        }
        if(Kst2Kpluspi0Resolved){
            LorVec_DiGamma = LorVec_gamma1 + LorVec_gamma2;
            LorVec_KaonPion = LorVec_K_plus + LorVec_DiGamma;
        }
        TRUE_open_angle =LorVec_gamma1.Angle(LorVec_gamma2.Vect());

        //Recombination of Particles
        if(Kst2Kspiplus){
            LorVec_DTF_KshortMuMu       = LorVec_DTF_K_short    + LorVec_DTF_DiMuon;
            LorVec_KshortMuMu           = LorVec_K_short        + LorVec_DiMuon;
        }
        if(Kst2Kpluspi0Merged){
            LorVec_DTF_KplusMuMu        = LorVec_DTF_K_plus     + LorVec_DTF_DiMuon;
            LorVec_KplusMuMu            = LorVec_K_plus         + LorVec_DiMuon;
        }
        if(Kst2Kpluspi0Resolved){
            LorVec_DTF_KplusMuMu        = LorVec_DTF_K_plus     + LorVec_DTF_DiMuon;
            LorVec_KplusMuMu            = LorVec_K_plus         + LorVec_DiMuon;
            LorVec_DTF_ReconBPiZero     = LorVec_DTF_DiMuon     + LorVec_DTF_K_plus     + LorVec_DTF_pi_zero_resolved;
            LorVec_ReconBPiZero         = LorVec_DiMuon         + LorVec_K_plus         + LorVec_pi_zero_resolved;
            LorVec_DTF_pi_zero_resolved      = LorVec_DTF_DiGamma;
            LorVec_pi_zero_resolved          = LorVec_DiGamma;
        }

        LorVec_DTF_K_star_plus      = LorVec_DTF_KaonPion;
        LorVec_K_star_plus          = LorVec_KaonPion;
        LorVec_DTF_ReconB       = LorVec_DTF_DiMuon     + LorVec_DTF_K_star_plus;
        LorVec_ReconB           = LorVec_DiMuon         + LorVec_K_star_plus;

        //-----------------------------------------------------------------------------------

        B_plus_TRUE_ETA = LorVec_TRUE_B_plus.Eta();
        B_plus_TRUE_PHI = LorVec_TRUE_B_plus.Phi();

        if(preselected) B_plus_PHI = LorVec_ReconB.Phi();

        B_plus_PX_DTF = LorVec_DTF_B_plus.Px();
        B_plus_PY_DTF = LorVec_DTF_B_plus.Py();
        B_plus_PZ_DTF = LorVec_DTF_B_plus.Pz();
        B_plus_PT_DTF = LorVec_DTF_B_plus.Pt();
        B_plus_PE_DTF = LorVec_DTF_B_plus.E();
        B_plus_ETA_DTF = LorVec_DTF_B_plus.Eta();
        B_plus_PHI_DTF = LorVec_DTF_B_plus.Phi();
        //-----------------------------------------------------------------------------------

        K_star_plus_TRUE_ETA = LorVec_TRUE_K_star_plus.Eta();
        K_star_plus_TRUE_PHI = LorVec_TRUE_K_star_plus.Phi();

        K_star_plus_PX_DTF = LorVec_DTF_K_star_plus.Px();
        K_star_plus_PY_DTF = LorVec_DTF_K_star_plus.Py();
        K_star_plus_PZ_DTF = LorVec_DTF_K_star_plus.Pz();
        K_star_plus_PT_DTF = LorVec_DTF_K_star_plus.Pt();
        K_star_plus_PE_DTF = LorVec_DTF_K_star_plus.E();
        K_star_plus_ETA_DTF = LorVec_DTF_K_star_plus.Eta();
        K_star_plus_PHI_DTF = LorVec_DTF_K_star_plus.Phi();
        //-----------------------------------------------------------------------------------

        if(Kst2Kpluspi0Merged){ //TODO: in case one really wants to do that, add DTF info
            pi_zero_merged_TRUE_ETA = LorVec_TRUE_pi_zero_merged.Eta();
            pi_zero_merged_TRUE_PHI = LorVec_TRUE_pi_zero_merged.Phi();

            pi_zero_merged_PX_DTF = LorVec_DTF_pi_zero_merged.Px();
            pi_zero_merged_PY_DTF = LorVec_DTF_pi_zero_merged.Py();
            pi_zero_merged_PZ_DTF = LorVec_DTF_pi_zero_merged.Pz();
            pi_zero_merged_PT_DTF = LorVec_DTF_pi_zero_merged.Pt();
            pi_zero_merged_PE_DTF = LorVec_DTF_pi_zero_merged.E();
            pi_zero_merged_ETA_DTF = LorVec_DTF_pi_zero_merged.Eta();
            pi_zero_merged_PHI_DTF = LorVec_DTF_pi_zero_merged.Phi();
        }
        //-----------------------------------------------------------------------------------

        if(Kst2Kpluspi0Resolved){
            if(preselected) pi_zero_resolved_PHI = LorVec_pi_zero_resolved.Phi();
            pi_zero_resolved_TRUE_ETA = LorVec_TRUE_pi_zero_resolved.Eta();
            pi_zero_resolved_TRUE_PHI = LorVec_TRUE_pi_zero_resolved.Phi();

            pi_zero_resolved_PX_DTF = LorVec_DTF_pi_zero_resolved.Px();
            pi_zero_resolved_PY_DTF = LorVec_DTF_pi_zero_resolved.Py();
            pi_zero_resolved_PZ_DTF = LorVec_DTF_pi_zero_resolved.Pz();
            pi_zero_resolved_PT_DTF = LorVec_DTF_pi_zero_resolved.Pt();
            pi_zero_resolved_PE_DTF = LorVec_DTF_pi_zero_resolved.E();
            pi_zero_resolved_ETA_DTF = LorVec_DTF_pi_zero_resolved.Eta();
            pi_zero_resolved_PHI_DTF = LorVec_DTF_pi_zero_resolved.Phi();
            //-----------------------------------------------------------------------------------

            gamma1_TRUE_ETA = LorVec_TRUE_gamma1.Eta();
            gamma1_TRUE_PHI = LorVec_TRUE_gamma1.Phi();

            gamma2_TRUE_ETA = LorVec_TRUE_gamma2.Eta();
            gamma2_TRUE_PHI = LorVec_TRUE_gamma2.Phi();


            if(preselected){
                gamma1_ETA= LorVec_gamma1.Eta();
                gamma1_PHI= LorVec_gamma1.Phi();

                gamma2_ETA= LorVec_gamma2.Eta();
                gamma2_PHI= LorVec_gamma2.Phi();

            }
            gamma1_PX_DTF = LorVec_DTF_gamma1.Px();
            gamma1_PY_DTF = LorVec_DTF_gamma1.Py();
            gamma1_PZ_DTF = LorVec_DTF_gamma1.Pz();
            gamma1_PT_DTF = LorVec_DTF_gamma1.Pt();
            gamma1_PE_DTF = LorVec_DTF_gamma1.E();
            gamma1_ETA_DTF = LorVec_DTF_gamma1.Eta();
            gamma1_PHI_DTF = LorVec_DTF_gamma1.Phi();

            gamma2_PX_DTF = LorVec_DTF_gamma2.Px();
            gamma2_PY_DTF = LorVec_DTF_gamma2.Py();
            gamma2_PZ_DTF = LorVec_DTF_gamma2.Pz();
            gamma2_PT_DTF = LorVec_DTF_gamma2.Pt();
            gamma2_PE_DTF = LorVec_DTF_gamma2.E();
            gamma2_ETA_DTF = LorVec_DTF_gamma2.Eta();
            gamma2_PHI_DTF = LorVec_DTF_gamma2.Phi();
        }
        //-----------------------------------------------------------------------------------

        if(Kst2Kpluspi0Merged || Kst2Kpluspi0Resolved){


            K_plus_TRUE_ETA = LorVec_TRUE_K_star_plus.Eta();
            K_plus_TRUE_PHI = LorVec_TRUE_K_star_plus.Phi();

            if(preselected){
                K_plus_PHI = LorVec_K_plus.Phi();
            }
            K_plus_PX_DTF = LorVec_DTF_K_plus.Px();
            K_plus_PY_DTF = LorVec_DTF_K_plus.Py();
            K_plus_PZ_DTF = LorVec_DTF_K_plus.Pz();
            K_plus_PT_DTF = LorVec_DTF_K_plus.Pt();
            K_plus_PE_DTF = LorVec_DTF_K_plus.E();
            K_plus_ETA_DTF = LorVec_DTF_K_plus.Eta();
            K_plus_PHI_DTF = LorVec_DTF_K_plus.Phi();
        }
        //-----------------------------------------------------------------------------------

        mu_minus_TRUE_ETA = LorVec_TRUE_mu_minus.Eta();
        mu_minus_TRUE_PHI = LorVec_TRUE_mu_minus.Phi();

        mu_plus_TRUE_ETA = LorVec_TRUE_mu_plus.Eta();
        mu_plus_TRUE_PHI = LorVec_TRUE_mu_plus.Phi();

        if(preselected){
            mu_minus_ETA= LorVec_mu_minus.Eta();
            mu_minus_PHI= LorVec_mu_minus.Phi();

            mu_plus_ETA= LorVec_mu_plus.Eta();
            mu_plus_PHI= LorVec_mu_plus.Phi();
        }
        mu_minus_PX_DTF = LorVec_DTF_mu_minus.Px();
        mu_minus_PY_DTF = LorVec_DTF_mu_minus.Py();
        mu_minus_PZ_DTF = LorVec_DTF_mu_minus.Pz();
        mu_minus_PT_DTF = LorVec_DTF_mu_minus.Pt();
        mu_minus_PE_DTF = LorVec_DTF_mu_minus.E();
        mu_minus_ETA_DTF = LorVec_DTF_mu_minus.Eta();
        mu_minus_PHI_DTF = LorVec_DTF_mu_minus.Phi();

        mu_plus_PX_DTF = LorVec_DTF_mu_plus.Px();
        mu_plus_PY_DTF = LorVec_DTF_mu_plus.Py();
        mu_plus_PZ_DTF = LorVec_DTF_mu_plus.Pz();
        mu_plus_PT_DTF = LorVec_DTF_mu_plus.Pt();
        mu_plus_PE_DTF = LorVec_DTF_mu_plus.E();
        mu_plus_ETA_DTF = LorVec_DTF_mu_plus.Eta();
        mu_plus_PHI_DTF = LorVec_DTF_mu_plus.Phi();

        resolution_tree->Fill();
    } //end of event loop
    //-----------------------------------------------------------------------------------
    B_plus_DTF_PV_Z = (Double_t)(f_B_plus_DTF_PV_Z[0]);

    //TODO
    resolution_tree -> Branch( "B_plus_ThetaL_DTF" , &B_plus_ThetaL_DTF);
    resolution_tree -> Branch( "B_plus_ThetaK_DTF" , &B_plus_ThetaK_DTF);
    resolution_tree -> Branch( "B_plus_Phi_DTF" ,    &B_plus_Phi_DTF );

    //-----------------------------------------------------------------------------------
    resolution_tree->SetBranchStatus("*TRUEPT*",0);
    resolution_tree->SetBranchStatus("*TRUEP_*",0);
    resolution_tree->SetBranchStatus("*CONE*",0);
    resolution_tree->SetBranchStatus("*892*",0);
    resolution_tree->SetBranchStatus("*0.*",0);
    resolution_tree->SetBranchStatus("*J_psi_1S*",0);
    resolution_tree->SetBranchStatus("*TRUETAU*",0);
    if (preselected) resolution_tree->SetBranchStatus("*NEW*",0);

    tree->Delete();
    resolution_tree->CloneTree(-1)->Write();


    output->Close();
    cout << "[INFO]\t\t Histograms created." << endl;
    return 1;
}


int ResPreselAllMC(Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if(Run != 1 && Run != 2){
        std::cout << "[ERROR]\t\tInvalid Run number given: " << Run  << ". Exit program!" << std::endl;
        return 0;
    }

    if ( ResPresel((Run == 1 ? "2011" : "2015"),"down", true, false, false, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2011" : "2015"),"up"  , true, false, false, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2012" : "2016"),"down", true, false, false, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2012" : "2016"),"up"  , true, false, false, preselected, truthMatched) == 0) return 0;
    return 1;

}
int ResPreselAllRefChannel(Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if(Run != 1 && Run != 2){
        std::cout << "[ERROR]\t\tInvalid Run number given: " << Run  << ". Exit program!" << std::endl;
        return 0;
    }
    if ( ResPresel((Run == 1 ? "2011" : "2015"),"down", true, true, false, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2011" : "2015"),"up"  , true, true, false, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2012" : "2016"),"down", true, true, false, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2012" : "2016"),"up"  , true, true, false, preselected, truthMatched) == 0) return 0;
    return 1;

}
int ResPreselAllPHSP(Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if(Run != 1 && Run != 2){
        std::cout << "[ERROR]\t\tInvalid Run number given: " << Run  << ". Exit program!" << std::endl;
        return 0;
    }
    if ( ResPresel((Run == 1 ? "2011" : "2015"),"down", true, false, true, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2011" : "2015"),"up"  , true, false, true, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2012" : "2016"),"down", true, false, true, preselected, truthMatched) == 0) return 0;
    if ( ResPresel((Run == 1 ? "2012" : "2016"),"up"  , true, false, true, preselected, truthMatched) == 0) return 0;
    return 1;

}
int ResPreselAll( Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if (ResPreselAllMC      (Run, preselected, truthMatched)==0) return 0;
    if (Run == 2) cout << "[INFO]\tNo reference channel for run 2, Skipping.";
    else{
        if (ResPreselAllRefChannel  (Run, preselected, truthMatched)==0) return 0;
    }
    if (ResPreselAllPHSP    (Run, preselected, truthMatched)==0) return 0;
    return 1;
}
int ResPreselComplete( Int_t Run = 1){
    if (ResPreselAllMC          (Run, 0, 0)==0) return 0;
    if (ResPreselAllMC          (Run, 1, 0)==0) return 0;
    if (ResPreselAllMC          (Run, 1, 1)==0) return 0;
    if (ResPreselAllRefChannel  (Run, 0, 0)==0) return 0;
    if (ResPreselAllRefChannel  (Run, 1, 0)==0) return 0;
    if (ResPreselAllRefChannel  (Run, 1, 1)==0) return 0;
    if (ResPreselAllPHSP        (Run, 0, 0)==0) return 0;
    if (ResPreselAllPHSP        (Run, 1, 0)==0) return 0;
    if (ResPreselAllPHSP        (Run, 1, 1)==0) return 0;
    return 1;
}


double pull(double rec, double truth){
    return double(rec-truth);
}
void res_sys(TH2* ref,TH1* res_sys, TH1* sys, TH1* res, TF1 *func, int nBins, TFile *output,
             bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){

    gStyle->SetOptStat(0);
    string name = ref->GetName();
    //--------------------------------------------
    //  output->cd(); //remove to improve performance

    design_diff(ref,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    ref->FitSlicesY(func,0,-1,0);
    TH2F *ref_0 = (TH2F*)output->Get((name +"_0").c_str()); //const
    TH2F *ref_1 = (TH2F*)output->Get((name +"_1").c_str()); //mean
    TH2F *ref_2 = (TH2F*)output->Get((name +"_2").c_str()); //sigma
    ref_0->Write();
    ref_1->Write();
    ref_2->Write();
    for (int b = 1; b < nBins+1; b++){
        res_sys->SetBinContent(b,ref_1->GetBinContent(b));
        res_sys->SetBinError(b,ref_2->GetBinContent(b));

        res->SetBinContent(b,ref_2->GetBinContent(b));
        res->SetBinError(b,ref_2->GetBinError(b));

        sys->SetBinContent(b,ref_1->GetBinContent(b));
        sys->SetBinError(b,ref_1->GetBinError(b));

    }

    design_fit          (res_sys,   preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    design_systematics  (sys,       preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    design_resolution   (res,       preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);

    res_sys->Write();
    sys->Write();
    res->Write();

    ref_0->Clear();
    ref_1->Clear();
    ref_2->Clear();
    delete ref_0;
    delete ref_1;
    delete ref_2;
    return;
}

void pull(TH1* res,TH1* pull, int nBins, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){

    //  output->cd(); //remove to improve performance
    for (int b = 1; b < nBins+1; b++){
        if (res->GetBinError(b) < 1e-8){
            cout << "[ERROR]\tIn " << res->GetName() << " zero error. Check the fits!" << endl;
            cout << "[ERROR]\tSetting pull to 0" << endl;
            pull->SetBinContent(b,0);
            continue;
        }
        //  cout <<  b << " " << res->GetBinContent(b) << endl;
        pull->SetBinContent(b,double(res->GetBinContent(b))/res->GetBinError(b));
    }
    pull->Write();
    design_pull(pull,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    return;
}
void res(TH1* res_sys,TH1* res, int nBins, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){

    //  output->cd(); //remove to improve performance
    /*
    for (int b = 1; b < nBins+1; b++){
        res->SetBinContent(b,res_sys->GetBinError(b));
    }
    res->Write();
    design_resolution(res,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    */
    return;
}
void sys(TH1* res_sys,TH1* sys, int nBins, bool preselected, string year, string magnet, bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool truthMatched = false){
    /*
    //  output->cd(); //remove to improve performance
    for (int b = 1; b < nBins+1; b++){
        sys->SetBinContent(b,res_sys->GetBinContent(b));
    }
    sys->Write();
    design_systematics(sys,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    */
    return;
}


int ResHist(std::string year = "2011", std::string magnet = "down", bool MC = true, bool ReferenceChannel = false, bool PHSP = false, bool preselected = true, bool truthMatched = false){

    MC = true;
    // Kst2Kpluspi0Resolved case

    bool OnlyDTF = false; //don't save non-DTF var

    TFile *input = 0;
    string newName = "";
    string legTitle = "";
    string TM = "";
    preselected ?  newName = "_resolution_preselected" : newName ="_resolution";
    truthMatched ?  TM = "_TM" : "";
    newName = newName + TM;
    Kst2Kspiplus?  legTitle= particleLatex("Kstar") +" #rightarrow " +particleLatex("Kshort") + particleLatex("piplus") :legTitle= particleLatex("Kstar") +" #rightarrow " +particleLatex("Kplus") + particleLatex("pizero");
    preselected ?  legTitle= legTitle + ": preselected" : legTitle = legTitle+": stripped";
    truthMatched ?  legTitle= "#splitline{" + legTitle+":}{TruthMatched}" : legTitle = legTitle;
    if (MC){
        if(ReferenceChannel) legTitle = legTitle +  " {MCRef}";
        else if (PHSP) legTitle = legTitle +  " {PHSP MC}";
        else legTitle = legTitle +  " {MC}";
    }
    TChain *tree =new TChain(truthMatched ? "DecayTreeTruthMatched" : "DecayTree");

    bool bothPol = false;
    if (magnet == "downup" || magnet == "updown"){
        bothPol = true;
        magnet = "down";
    }


    if(Kst2Kpluspi0Resolved){
        if(!MC){
            tree->Add(Form("%s/data/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),(magnet).c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
            if (bothPol){
                magnet = "down";
                tree->Add(Form("%s/data/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),(magnet).c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
            }
        }
        else{
            if(ReferenceChannel){
                tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
            else if(PHSP){
                tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
            else{
                tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
        }
    }
    // Kst2Kpluspi0Merged case
    if(Kst2Kpluspi0Merged){
        if(!MC){
            tree->Add(Form("%s/data/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
            if (bothPol){
                magnet = "down";
                tree->Add(Form("%s/data/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
            }
        }
        else{
            if(ReferenceChannel){
                tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/MC/RefKplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
            else if(PHSP){
                tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/PHSP/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
            else{
                tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
        }
    }
    // Kst2Kspiplus case
    if(Kst2Kspiplus){
        if(!MC){
            tree->Add(Form("%s/data/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
            if (bothPol){
                magnet = "down";
                tree->Add(Form("%s/data/MC/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
            }
        }
        else{
            if(ReferenceChannel){
                tree->Add(Form("%s/data/MC/RefKshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/MC/RefKshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
            else if(PHSP){
                tree->Add(Form("%s/data/PHSP/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/PHSP/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
            else{
                tree->Add(Form("%s/data/MC/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                if (bothPol){
                    magnet = "down";
                    tree->Add(Form("%s/data/MC/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()));
                }
            }
        }
    }
    if (bothPol) magnet = "both";


    /////////////////////
    //    Branches     //
    /////////////////////
    //Just load everything in case it's needed later
    /////////////////////

    ///B+
    Double_t B_plus_PX    = 0.;
    Double_t B_plus_PY    = 0.;
    Double_t B_plus_PZ    = 0.;
    Double_t B_plus_PT    = 0.;
    Double_t B_plus_PE    = 0.;
    Double_t B_plus_ETA   = 0.;
    Double_t B_plus_PHI   = 0.;

    Double_t B_plus_TRUE_PX    = 0.;
    Double_t B_plus_TRUE_PY    = 0.;
    Double_t B_plus_TRUE_PZ    = 0.;
    Double_t B_plus_TRUE_PT    = 0.;
    Double_t B_plus_TRUE_PE    = 0.;
    Double_t B_plus_TRUE_ETA   = 0.;
    Double_t B_plus_TRUE_PHI   = 0.;

    Double_t B_plus_PX_DTF    = 0.;
    Double_t B_plus_PY_DTF    = 0.;
    Double_t B_plus_PZ_DTF    = 0.;
    Double_t B_plus_PT_DTF    = 0.;
    Double_t B_plus_PE_DTF    = 0.;
    Double_t B_plus_ETA_DTF   = 0.;
    Double_t B_plus_PHI_DTF   = 0.;

    ///K*+
    Double_t K_star_plus_PX     = 0.;
    Double_t K_star_plus_PY     = 0.;
    Double_t K_star_plus_PZ     = 0.;
    Double_t K_star_plus_PT     = 0.;
    Double_t K_star_plus_PE     = 0.;
    Double_t K_star_plus_ETA    = 0.;
    Double_t K_star_plus_PHI    = 0.;

    Double_t K_star_plus_TRUE_PX     = 0.;
    Double_t K_star_plus_TRUE_PY     = 0.;
    Double_t K_star_plus_TRUE_PZ     = 0.;
    Double_t K_star_plus_TRUE_PT     = 0.;
    Double_t K_star_plus_TRUE_PE     = 0.;
    Double_t K_star_plus_TRUE_ETA    = 0.;
    Double_t K_star_plus_TRUE_PHI    = 0.;

    Double_t K_star_plus_PX_DTF      = 0.;
    Double_t K_star_plus_PY_DTF      = 0.;
    Double_t K_star_plus_PZ_DTF      = 0.;
    Double_t K_star_plus_PT_DTF      = 0.;
    Double_t K_star_plus_PE_DTF      = 0.;
    Double_t K_star_plus_ETA_DTF     = 0.;
    Double_t K_star_plus_PHI_DTF     = 0.;

    ///K+
    Double_t K_plus_PX      = 0.;
    Double_t K_plus_PY      = 0.;
    Double_t K_plus_PZ      = 0.;
    Double_t K_plus_PT      = 0.;
    Double_t K_plus_PE      = 0.;
    Double_t K_plus_ETA     = 0.;
    Double_t K_plus_PHI     = 0.;

    Double_t K_plus_TRUE_PX      = 0.;
    Double_t K_plus_TRUE_PY      = 0.;
    Double_t K_plus_TRUE_PZ      = 0.;
    Double_t K_plus_TRUE_PT      = 0.;
    Double_t K_plus_TRUE_PE      = 0.;
    Double_t K_plus_TRUE_ETA     = 0.;
    Double_t K_plus_TRUE_PHI     = 0.;

    Double_t K_plus_PX_DTF  = 0.;
    Double_t K_plus_PY_DTF  = 0.;
    Double_t K_plus_PZ_DTF  = 0.;
    Double_t K_plus_PE_DTF  = 0.;
    Double_t K_plus_PT_DTF  = 0.;
    Double_t K_plus_ETA_DTF = 0.;
    Double_t K_plus_PHI_DTF = 0.;



    ///pi0 (merged)
    Double_t pi_zero_merged_PX  = 0.;
    Double_t pi_zero_merged_PY  = 0.;
    Double_t pi_zero_merged_PZ  = 0.;
    Double_t pi_zero_merged_PT  = 0.;
    Double_t pi_zero_merged_PE  = 0.;
    Double_t pi_zero_merged_ETA = 0.;
    Double_t pi_zero_merged_PHI = 0.;

    Double_t pi_zero_merged_TRUE_PX     = 0.;
    Double_t pi_zero_merged_TRUE_PY     = 0.;
    Double_t pi_zero_merged_TRUE_PZ     = 0.;
    Double_t pi_zero_merged_TRUE_PT     = 0.;
    Double_t pi_zero_merged_TRUE_PE     = 0.;
    Double_t pi_zero_merged_TRUE_ETA    = 0.;
    Double_t pi_zero_merged_TRUE_PHI    = 0.;

    Double_t pi_zero_merged_PX_DTF    = 0.;
    Double_t pi_zero_merged_PY_DTF    = 0.;
    Double_t pi_zero_merged_PZ_DTF    = 0.;
    Double_t pi_zero_merged_PT_DTF    = 0.;
    Double_t pi_zero_merged_PE_DTF    = 0.;
    Double_t pi_zero_merged_ETA_DTF   = 0.;
    Double_t pi_zero_merged_PHI_DTF   = 0.;

    ///pi0 (resolved)
    Double_t pi_zero_resolved_PX     = 0.;
    Double_t pi_zero_resolved_PY     = 0.;
    Double_t pi_zero_resolved_PZ     = 0.;
    Double_t pi_zero_resolved_PT     = 0.;
    Double_t pi_zero_resolved_PE     = 0.;
    Double_t pi_zero_resolved_ETA    = 0.;
    Double_t pi_zero_resolved_PHI    = 0.;

    Double_t pi_zero_resolved_TRUE_PX     = 0.;
    Double_t pi_zero_resolved_TRUE_PY     = 0.;
    Double_t pi_zero_resolved_TRUE_PZ     = 0.;
    Double_t pi_zero_resolved_TRUE_PT     = 0.;
    Double_t pi_zero_resolved_TRUE_PE     = 0.;
    Double_t pi_zero_resolved_TRUE_ETA    = 0.;
    Double_t pi_zero_resolved_TRUE_PHI    = 0.;

    Double_t pi_zero_resolved_PX_DTF    = 0.;
    Double_t pi_zero_resolved_PY_DTF    = 0.;
    Double_t pi_zero_resolved_PZ_DTF    = 0.;
    Double_t pi_zero_resolved_PT_DTF    = 0.;
    Double_t pi_zero_resolved_PE_DTF    = 0.;
    Double_t pi_zero_resolved_ETA_DTF   = 0.;
    Double_t pi_zero_resolved_PHI_DTF   = 0.;


    ///Kshort
    Double_t K_short_PX     = 0.;
    Double_t K_short_PY     = 0.;
    Double_t K_short_PZ     = 0.;
    Double_t K_short_PT     = 0.;
    Double_t K_short_PE     = 0.;
    Double_t K_short_ETA    = 0.;
    Double_t K_short_PHI    = 0.;

    Double_t K_short_TRUE_PX     = 0.;
    Double_t K_short_TRUE_PY     = 0.;
    Double_t K_short_TRUE_PZ     = 0.;
    Double_t K_short_TRUE_PT     = 0.;
    Double_t K_short_TRUE_PE     = 0.;
    Double_t K_short_TRUE_ETA    = 0.;
    Double_t K_short_TRUE_PHI    = 0.;

    Double_t K_short_PX_DTF     = 0.;
    Double_t K_short_PY_DTF     = 0.;
    Double_t K_short_PZ_DTF     = 0.;
    Double_t K_short_PT_DTF     = 0.;
    Double_t K_short_PE_DTF     = 0.;
    Double_t K_short_ETA_DTF    = 0.;
    Double_t K_short_PHI_DTF    = 0.;

    Float_t f_K_short_M_DTF[100];
    Double_t K_short_M_DTF = 0.;

    ///pi+ (KS)
    Double_t Ks_pi_plus_PX     = 0.;
    Double_t Ks_pi_plus_PY     = 0.;
    Double_t Ks_pi_plus_PZ     = 0.;
    Double_t Ks_pi_plus_PT     = 0.;
    Double_t Ks_pi_plus_PE     = 0.;
    Double_t Ks_pi_plus_ETA    = 0.;
    Double_t Ks_pi_plus_PHI    = 0.;

    Double_t Ks_pi_plus_TRUE_PX     = 0.;
    Double_t Ks_pi_plus_TRUE_PY     = 0.;
    Double_t Ks_pi_plus_TRUE_PZ     = 0.;
    Double_t Ks_pi_plus_TRUE_PT     = 0.;
    Double_t Ks_pi_plus_TRUE_PE     = 0.;
    Double_t Ks_pi_plus_TRUE_ETA    = 0.;
    Double_t Ks_pi_plus_TRUE_PHI    = 0.;


    Double_t Ks_pi_plus_PX_DTF = 0.;
    Double_t Ks_pi_plus_PY_DTF = 0.;
    Double_t Ks_pi_plus_PZ_DTF = 0.;
    Double_t Ks_pi_plus_PT_DTF = 0.;
    Double_t Ks_pi_plus_PE_DTF = 0.;
    Double_t Ks_pi_plus_ETA_DTF = 0.;
    Double_t Ks_pi_plus_PHI_DTF = 0.;


    ///pi- (KS)
    Double_t Ks_pi_minus_PX     = 0.;
    Double_t Ks_pi_minus_PY     = 0.;
    Double_t Ks_pi_minus_PZ     = 0.;
    Double_t Ks_pi_minus_PT     = 0.;
    Double_t Ks_pi_minus_PE     = 0.;
    Double_t Ks_pi_minus_ETA    = 0.;
    Double_t Ks_pi_minus_PHI    = 0.;

    Double_t Ks_pi_minus_TRUE_PX     = 0.;
    Double_t Ks_pi_minus_TRUE_PY     = 0.;
    Double_t Ks_pi_minus_TRUE_PZ     = 0.;
    Double_t Ks_pi_minus_TRUE_PT     = 0.;
    Double_t Ks_pi_minus_TRUE_PE     = 0.;
    Double_t Ks_pi_minus_TRUE_ETA    = 0.;
    Double_t Ks_pi_minus_TRUE_PHI    = 0.;

    Double_t Ks_pi_minus_PX_DTF  = 0.;
    Double_t Ks_pi_minus_PY_DTF  = 0.;
    Double_t Ks_pi_minus_PZ_DTF  = 0.;
    Double_t Ks_pi_minus_PT_DTF  = 0.;
    Double_t Ks_pi_minus_PE_DTF  = 0.;
    Double_t Ks_pi_minus_ETA_DTF = 0.;
    Double_t Ks_pi_minus_PHI_DTF = 0.;

    ///pi+
    Double_t pi_plus_PX     = 0.;
    Double_t pi_plus_PY     = 0.;
    Double_t pi_plus_PZ     = 0.;
    Double_t pi_plus_PT     = 0.;
    Double_t pi_plus_PE     = 0.;
    Double_t pi_plus_ETA    = 0.;
    Double_t pi_plus_PHI    = 0.;

    Double_t pi_plus_TRUE_PX     = 0.;
    Double_t pi_plus_TRUE_PY     = 0.;
    Double_t pi_plus_TRUE_PZ     = 0.;
    Double_t pi_plus_TRUE_PT     = 0.;
    Double_t pi_plus_TRUE_PE     = 0.;
    Double_t pi_plus_TRUE_ETA    = 0.;
    Double_t pi_plus_TRUE_PHI    = 0.;

    Double_t pi_plus_PX_DTF  = 0.;
    Double_t pi_plus_PY_DTF  = 0.;
    Double_t pi_plus_PZ_DTF  = 0.;
    Double_t pi_plus_PT_DTF  = 0.;
    Double_t pi_plus_PE_DTF  = 0.;
    Double_t pi_plus_ETA_DTF = 0.;
    Double_t pi_plus_PHI_DTF = 0.;

    ///mu+
    Double_t mu_minus_PX     = 0.;
    Double_t mu_minus_PY     = 0.;
    Double_t mu_minus_PZ     = 0.;
    Double_t mu_minus_PT     = 0.;
    Double_t mu_minus_PE     = 0.;
    Double_t mu_minus_ETA    = 0.;
    Double_t mu_minus_PHI    = 0.;

    Double_t mu_minus_TRUE_PX     = 0.;
    Double_t mu_minus_TRUE_PY     = 0.;
    Double_t mu_minus_TRUE_PZ     = 0.;
    Double_t mu_minus_TRUE_PT     = 0.;
    Double_t mu_minus_TRUE_PE     = 0.;
    Double_t mu_minus_TRUE_ETA    = 0.;
    Double_t mu_minus_TRUE_PHI    = 0.;

    Double_t mu_minus_PX_DTF     = 0.;
    Double_t mu_minus_PY_DTF     = 0.;
    Double_t mu_minus_PZ_DTF     = 0.;
    Double_t mu_minus_PT_DTF     = 0.;
    Double_t mu_minus_PE_DTF     = 0.;
    Double_t mu_minus_ETA_DTF    = 0.;
    Double_t mu_minus_PHI_DTF    = 0.;

    ///mu+
    Double_t mu_plus_PX     = 0.;
    Double_t mu_plus_PY     = 0.;
    Double_t mu_plus_PZ     = 0.;
    Double_t mu_plus_PT     = 0.;
    Double_t mu_plus_PE     = 0.;
    Double_t mu_plus_ETA    = 0.;
    Double_t mu_plus_PHI    = 0.;

    Double_t mu_plus_TRUE_PX     = 0.;
    Double_t mu_plus_TRUE_PY     = 0.;
    Double_t mu_plus_TRUE_PZ     = 0.;
    Double_t mu_plus_TRUE_PT     = 0.;
    Double_t mu_plus_TRUE_PE     = 0.;
    Double_t mu_plus_TRUE_ETA    = 0.;
    Double_t mu_plus_TRUE_PHI    = 0.;

    Double_t mu_plus_PX_DTF     = 0.;
    Double_t mu_plus_PY_DTF     = 0.;
    Double_t mu_plus_PZ_DTF     = 0.;
    Double_t mu_plus_PT_DTF     = 0.;
    Double_t mu_plus_PE_DTF     = 0.;
    Double_t mu_plus_ETA_DTF    = 0.;
    Double_t mu_plus_PHI_DTF    = 0.;


    ///gamma1
    Double_t gamma1_PX     = 0.;
    Double_t gamma1_PY     = 0.;
    Double_t gamma1_PZ     = 0.;
    Double_t gamma1_PT     = 0.;
    Double_t gamma1_PE     = 0.;
    Double_t gamma1_ETA    = 0.;
    Double_t gamma1_PHI    = 0.;

    Double_t gamma1_TRUE_PX     = 0.;
    Double_t gamma1_TRUE_PY     = 0.;
    Double_t gamma1_TRUE_PZ     = 0.;
    Double_t gamma1_TRUE_PT     = 0.;
    Double_t gamma1_TRUE_PE     = 0.;
    Double_t gamma1_TRUE_ETA    = 0.;
    Double_t gamma1_TRUE_PHI    = 0.;

    Double_t gamma1_PX_DTF      = 0.;
    Double_t gamma1_PY_DTF      = 0.;
    Double_t gamma1_PZ_DTF      = 0.;
    Double_t gamma1_PT_DTF      = 0.;
    Double_t gamma1_PE_DTF      = 0.;
    Double_t gamma1_ETA_DTF     = 0.;
    Double_t gamma1_PHI_DTF     = 0.;

    ///gamma2
    Double_t gamma2_PX     = 0.;
    Double_t gamma2_PY     = 0.;
    Double_t gamma2_PZ     = 0.;
    Double_t gamma2_PT     = 0.;
    Double_t gamma2_PE     = 0.;
    Double_t gamma2_ETA    = 0.;
    Double_t gamma2_PHI    = 0.;

    Double_t gamma2_PX_DTF      = 0.;
    Double_t gamma2_PY_DTF      = 0.;
    Double_t gamma2_PZ_DTF      = 0.;
    Double_t gamma2_PT_DTF      = 0.;
    Double_t gamma2_PE_DTF      = 0.;
    Double_t gamma2_ETA_DTF     = 0.;
    Double_t gamma2_PHI_DTF     = 0.;

    Double_t gamma2_TRUE_PX     = 0.;
    Double_t gamma2_TRUE_PY     = 0.;
    Double_t gamma2_TRUE_PZ     = 0.;
    Double_t gamma2_TRUE_PT     = 0.;
    Double_t gamma2_TRUE_PE     = 0.;
    Double_t gamma2_TRUE_ETA    = 0.;
    Double_t gamma2_TRUE_PHI    = 0.;


    //angles
    Double_t B_plus_ThetaL = 0.;
    Double_t B_plus_ThetaK = 0.;
    Double_t B_plus_Phi = 0.;

    Double_t B_plus_ThetaL_DTF = 0.;
    Double_t B_plus_ThetaK_DTF = 0.;
    Double_t B_plus_Phi_DTF = 0.;

    Double_t B_plus_TRUE_ThetaL = 0.;
    Double_t B_plus_TRUE_ThetaK = 0.;
    Double_t B_plus_TRUE_Phi = 0.;

    Double_t open_angle = 0.;
    Double_t open_angle_DTF = 0.;
    Double_t TRUE_open_angle = 0.;


    //global variables
    Double_t B_plus_ENDVERTEX_Z = 0.;
    Double_t B_plus_ENDVERTEX_ZERR = 0.;
    Double_t B_plus_OWNPV_Z = 0.;
    Double_t B_plus_OWNPV_ZERR = 0.;
    Double_t B_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t B_plus_TRUEENDVERTEX_Z = 0.;
    Double_t B_plus_TOPPV_Z = 0.;//TODO: what is this?
    Double_t B_plus_DTF_PV_Z = 0.;

    Double_t K_star_plus_ENDVERTEX_Z = 0.;
    Double_t K_star_plus_ENDVERTEX_ZERR = 0.;
    Double_t K_star_plus_OWNPV_Z = 0.;
    Double_t K_star_plus_OWNPV_ZERR = 0.;
    Double_t K_star_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t K_star_plus_TRUEENDVERTEX_Z = 0.;

    Double_t K_plus_OWNPV_Z = 0.;
    Double_t K_plus_OWNPV_ZERR = 0.;
    Double_t K_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t K_plus_TRUEENDVERTEX_Z = 0.;

    Double_t pi_zero_resolved_TRUEORIGINVERTEX_Z = 0.;
    Double_t pi_zero_resolved_TRUEENDVERTEX_Z = 0.;

    Double_t gamma1_TRUEORIGINVERTEX_Z = 0.;
    Double_t gamma1_TRUEENDVERTEX_Z = 0.;

    Double_t gamma2_TRUEORIGINVERTEX_Z = 0.;
    Double_t gamma2_TRUEENDVERTEX_Z = 0.;

    Double_t mu_plus_OWNPV_Z = 0.;
    Double_t mu_plus_OWNPV_ZERR = 0.;
    Double_t mu_plus_TRUEORIGINVERTEX_Z = 0.;
    Double_t mu_plus_TRUEENDVERTEX_Z = 0.;

    Double_t mu_minus_OWNPV_Z = 0.;
    Double_t mu_minus_OWNPV_ZERR = 0.;
    Double_t mu_minus_TRUEORIGINVERTEX_Z = 0.;
    Double_t mu_minus_TRUEENDVERTEX_Z = 0.;



    tree->SetBranchStatus("*",1);

    //particle masses and momenta
    tree -> SetBranchAddress( "B_plus_PX" ,  &B_plus_PX );
    tree -> SetBranchAddress( "B_plus_PY" ,  &B_plus_PY );
    tree -> SetBranchAddress( "B_plus_PZ" ,  &B_plus_PZ );
    tree -> SetBranchAddress( "B_plus_PT" ,  &B_plus_PT );
    tree -> SetBranchAddress( "B_plus_PE" ,  &B_plus_PE );
    tree -> SetBranchAddress( "B_plus_ETA" , &B_plus_ETA );
    tree -> SetBranchAddress( "B_plus_PHI" , &B_plus_PHI );

    tree -> SetBranchAddress( "B_plus_TRUE_PX" , &B_plus_TRUE_PX );
    tree -> SetBranchAddress( "B_plus_TRUE_PY" , &B_plus_TRUE_PY );
    tree -> SetBranchAddress( "B_plus_TRUE_PZ" , &B_plus_TRUE_PZ );
    tree -> SetBranchAddress( "B_plus_TRUE_PE" , &B_plus_TRUE_PE );
    tree -> SetBranchAddress( "B_plus_TRUE_PT" , &B_plus_TRUE_PT );
    tree -> SetBranchAddress( "B_plus_TRUE_ETA" , &B_plus_TRUE_ETA );
    tree -> SetBranchAddress( "B_plus_TRUE_PHI" , &B_plus_TRUE_PHI );

    tree -> SetBranchAddress( "B_plus_PX_DTF" ,  &B_plus_PX_DTF );
    tree -> SetBranchAddress( "B_plus_PY_DTF" ,  &B_plus_PY_DTF );
    tree -> SetBranchAddress( "B_plus_PZ_DTF" ,  &B_plus_PZ_DTF );
    tree -> SetBranchAddress( "B_plus_PT_DTF" ,  &B_plus_PT_DTF );
    tree -> SetBranchAddress( "B_plus_PE_DTF" ,  &B_plus_PE_DTF );
    tree -> SetBranchAddress( "B_plus_ETA_DTF" , &B_plus_ETA_DTF );
    tree -> SetBranchAddress( "B_plus_PHI_DTF" , &B_plus_PHI_DTF );


    tree -> SetBranchAddress( "K_star_plus_PX" ,    &K_star_plus_PX );
    tree -> SetBranchAddress( "K_star_plus_PY" ,    &K_star_plus_PY );
    tree -> SetBranchAddress( "K_star_plus_PZ" ,    &K_star_plus_PZ );
    tree -> SetBranchAddress( "K_star_plus_PE" ,    &K_star_plus_PE );
    tree -> SetBranchAddress( "K_star_plus_PT" ,    &K_star_plus_PT );
    tree -> SetBranchAddress( "K_star_plus_PHI" ,    &K_star_plus_PHI );
    tree -> SetBranchAddress( "K_star_plus_ETA" ,    &K_star_plus_ETA );

    tree -> SetBranchAddress( "K_star_plus_TRUE_PX" , &K_star_plus_TRUE_PX );
    tree -> SetBranchAddress( "K_star_plus_TRUE_PY" , &K_star_plus_TRUE_PY );
    tree -> SetBranchAddress( "K_star_plus_TRUE_PZ" , &K_star_plus_TRUE_PZ );
    tree -> SetBranchAddress( "K_star_plus_TRUE_PE" , &K_star_plus_TRUE_PE );
    tree -> SetBranchAddress( "K_star_plus_TRUE_PT" , &K_star_plus_TRUE_PT );
    tree -> SetBranchAddress( "K_star_plus_TRUE_ETA" , &K_star_plus_TRUE_ETA );
    tree -> SetBranchAddress( "K_star_plus_TRUE_PHI" , &K_star_plus_TRUE_PHI );

    tree -> SetBranchAddress( "K_star_plus_PX_DTF" ,    &K_star_plus_PX_DTF );
    tree -> SetBranchAddress( "K_star_plus_PY_DTF" ,    &K_star_plus_PY_DTF );
    tree -> SetBranchAddress( "K_star_plus_PZ_DTF" ,    &K_star_plus_PZ_DTF );
    tree -> SetBranchAddress( "K_star_plus_PE_DTF" ,    &K_star_plus_PE_DTF );
    tree -> SetBranchAddress( "K_star_plus_PT_DTF" ,    &K_star_plus_PT_DTF );
    tree -> SetBranchAddress( "K_star_plus_PHI_DTF" ,    &K_star_plus_PHI_DTF );
    tree -> SetBranchAddress( "K_star_plus_ETA_DTF" ,    &K_star_plus_ETA_DTF );


    if(Kst2Kpluspi0Merged){ //TODO: in case one really wants to do that, add DTF info
        tree -> SetBranchAddress( "pi_zero_merged_PX" , &pi_zero_merged_PX );
        tree -> SetBranchAddress( "pi_zero_merged_PY" , &pi_zero_merged_PY );
        tree -> SetBranchAddress( "pi_zero_merged_PZ" , &pi_zero_merged_PZ );
        tree -> SetBranchAddress( "pi_zero_merged_PE" , &pi_zero_merged_PE );
        tree -> SetBranchAddress( "pi_zero_merged_PT" , &pi_zero_merged_PT );
        tree -> SetBranchAddress( "pi_zero_merged_ETA" , &pi_zero_merged_ETA );
        tree -> SetBranchAddress( "pi_zero_merged_PHI" , &pi_zero_merged_PHI );

        tree -> SetBranchAddress( "pi_zero_merged_TRUE_PX" , &pi_zero_merged_TRUE_PX );
        tree -> SetBranchAddress( "pi_zero_merged_TRUE_PY" , &pi_zero_merged_TRUE_PY );
        tree -> SetBranchAddress( "pi_zero_merged_TRUE_PZ" , &pi_zero_merged_TRUE_PZ );
        tree -> SetBranchAddress( "pi_zero_merged_TRUE_PE" , &pi_zero_merged_TRUE_PE );
        tree -> SetBranchAddress( "pi_zero_merged_TRUE_PT"  , &pi_zero_merged_TRUE_PT );
        tree -> SetBranchAddress( "pi_zero_merged_TRUE_ETA"  , &pi_zero_merged_TRUE_ETA );
        tree -> SetBranchAddress( "pi_zero_merged_TRUE_PHI"  , &pi_zero_merged_TRUE_PHI );

        tree -> SetBranchAddress( "pi_zero_merged_PX_DTF" , &pi_zero_merged_PX_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PY_DTF" , &pi_zero_merged_PY_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PZ_DTF" , &pi_zero_merged_PZ_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PE_DTF" , &pi_zero_merged_PE_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PT_DTF" , &pi_zero_merged_PT_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_ETA_DTF" , &pi_zero_merged_ETA_DTF );
        tree -> SetBranchAddress( "pi_zero_merged_PHI_DTF" , &pi_zero_merged_PHI_DTF );
    }

    if(Kst2Kpluspi0Resolved){
        tree -> SetBranchAddress( "pi_zero_resolved_PX" , &pi_zero_resolved_PX );
        tree -> SetBranchAddress( "pi_zero_resolved_PY" , &pi_zero_resolved_PY );
        tree -> SetBranchAddress( "pi_zero_resolved_PZ" , &pi_zero_resolved_PZ );
        tree -> SetBranchAddress( "pi_zero_resolved_PT" , &pi_zero_resolved_PT );
        tree -> SetBranchAddress( "pi_zero_resolved_PE" , &pi_zero_resolved_PE );
        tree -> SetBranchAddress( "pi_zero_resolved_ETA" , &pi_zero_resolved_ETA );
        tree -> SetBranchAddress( "pi_zero_resolved_PHI" , &pi_zero_resolved_PHI );

        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_PX" , &pi_zero_resolved_TRUE_PX );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_PY" , &pi_zero_resolved_TRUE_PY );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_PZ" , &pi_zero_resolved_TRUE_PZ );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_PE" , &pi_zero_resolved_TRUE_PE );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_PT" , &pi_zero_resolved_TRUE_PT );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_ETA" , &pi_zero_resolved_TRUE_ETA );
        tree -> SetBranchAddress( "pi_zero_resolved_TRUE_PHI" , &pi_zero_resolved_TRUE_PHI );

        tree -> SetBranchAddress( "pi_zero_resolved_PX_DTF" , &pi_zero_resolved_PX_DTF );
        tree -> SetBranchAddress( "pi_zero_resolved_PY_DTF" , &pi_zero_resolved_PY_DTF );
        tree -> SetBranchAddress( "pi_zero_resolved_PZ_DTF" , &pi_zero_resolved_PZ_DTF );
        tree -> SetBranchAddress( "pi_zero_resolved_PE_DTF" , &pi_zero_resolved_PE_DTF );
        tree -> SetBranchAddress( "pi_zero_resolved_PT_DTF" , &pi_zero_resolved_PT_DTF );
        tree -> SetBranchAddress( "pi_zero_resolved_ETA_DTF" , &pi_zero_resolved_ETA_DTF );
        tree -> SetBranchAddress( "pi_zero_resolved_PHI_DTF" , &pi_zero_resolved_PHI_DTF );

        tree -> SetBranchAddress( "gamma1_PX" , &gamma1_PX );
        tree -> SetBranchAddress( "gamma1_PY" , &gamma1_PY );
        tree -> SetBranchAddress( "gamma1_PZ" , &gamma1_PZ );
        tree -> SetBranchAddress( "gamma1_PE" , &gamma1_PE );
        tree -> SetBranchAddress( "gamma1_PT" , &gamma1_PT );
        tree -> SetBranchAddress( "gamma1_ETA" , &gamma1_ETA );
        tree -> SetBranchAddress( "gamma1_PHI" , &gamma1_PHI );

        tree -> SetBranchAddress( "gamma1_TRUE_PX" , &gamma1_TRUE_PX );
        tree -> SetBranchAddress( "gamma1_TRUE_PY" , &gamma1_TRUE_PY );
        tree -> SetBranchAddress( "gamma1_TRUE_PZ" , &gamma1_TRUE_PZ );
        tree -> SetBranchAddress( "gamma1_TRUE_PE" , &gamma1_TRUE_PE );
        tree -> SetBranchAddress( "gamma1_TRUE_PT" , &gamma1_TRUE_PT );
        tree -> SetBranchAddress( "gamma1_TRUE_ETA" , &gamma1_TRUE_ETA );
        tree -> SetBranchAddress( "gamma1_TRUE_PHI" , &gamma1_TRUE_PHI );

        tree -> SetBranchAddress( "gamma1_PX_DTF" , &gamma1_PX_DTF );
        tree -> SetBranchAddress( "gamma1_PY_DTF" , &gamma1_PY_DTF );
        tree -> SetBranchAddress( "gamma1_PZ_DTF" , &gamma1_PZ_DTF );
        tree -> SetBranchAddress( "gamma1_PE_DTF" , &gamma1_PE_DTF );
        tree -> SetBranchAddress( "gamma1_PT_DTF" , &gamma1_PT_DTF );
        tree -> SetBranchAddress( "gamma1_ETA_DTF" , &gamma1_ETA_DTF );
        tree -> SetBranchAddress( "gamma1_PHI_DTF" , &gamma1_PHI_DTF );

        tree -> SetBranchAddress( "gamma2_PX" , &gamma2_PX );
        tree -> SetBranchAddress( "gamma2_PY" , &gamma2_PY );
        tree -> SetBranchAddress( "gamma2_PZ" , &gamma2_PZ );
        tree -> SetBranchAddress( "gamma2_PE" , &gamma2_PE );
        tree -> SetBranchAddress( "gamma2_PT" , &gamma2_PT );
        tree -> SetBranchAddress( "gamma2_ETA" , &gamma2_ETA );
        tree -> SetBranchAddress( "gamma2_PHI" , &gamma2_PHI );

        tree -> SetBranchAddress( "gamma2_TRUE_PX" , &gamma2_TRUE_PX );
        tree -> SetBranchAddress( "gamma2_TRUE_PY" , &gamma2_TRUE_PY );
        tree -> SetBranchAddress( "gamma2_TRUE_PZ" , &gamma2_TRUE_PZ );
        tree -> SetBranchAddress( "gamma2_TRUE_PE" , &gamma2_TRUE_PE );
        tree -> SetBranchAddress( "gamma2_TRUE_PT" , &gamma2_TRUE_PT );
        tree -> SetBranchAddress( "gamma2_TRUE_ETA" , &gamma2_TRUE_ETA );
        tree -> SetBranchAddress( "gamma2_TRUE_PHI" , &gamma2_TRUE_PHI );

        tree -> SetBranchAddress( "gamma2_PX_DTF" , &gamma2_PX_DTF );
        tree -> SetBranchAddress( "gamma2_PY_DTF" , &gamma2_PY_DTF );
        tree -> SetBranchAddress( "gamma2_PZ_DTF" , &gamma2_PZ_DTF );
        tree -> SetBranchAddress( "gamma2_PE_DTF" , &gamma2_PE_DTF );
        tree -> SetBranchAddress( "gamma2_PT_DTF" , &gamma2_PT_DTF );
        tree -> SetBranchAddress( "gamma2_ETA_DTF" , &gamma2_ETA_DTF );
        tree -> SetBranchAddress( "gamma2_PHI_DTF" , &gamma2_PHI_DTF );


    }

    if(Kst2Kpluspi0Merged || Kst2Kpluspi0Resolved){
        tree -> SetBranchAddress( "K_plus_PX" , &K_plus_PX );
        tree -> SetBranchAddress( "K_plus_PY" , &K_plus_PY );
        tree -> SetBranchAddress( "K_plus_PZ" , &K_plus_PZ );
        tree -> SetBranchAddress( "K_plus_PE" , &K_plus_PE );
        tree -> SetBranchAddress( "K_plus_PT" , &K_plus_PT );
        tree -> SetBranchAddress( "K_plus_ETA" , &K_plus_ETA );
        tree -> SetBranchAddress( "K_plus_PHI" , &K_plus_PHI );

        tree -> SetBranchAddress( "K_plus_TRUE_PX" ,  &K_plus_TRUE_PX );
        tree -> SetBranchAddress( "K_plus_TRUE_PY" ,  &K_plus_TRUE_PY );
        tree -> SetBranchAddress( "K_plus_TRUE_PZ" ,  &K_plus_TRUE_PZ );
        tree -> SetBranchAddress( "K_plus_TRUE_PE" ,  &K_plus_TRUE_PE );
        tree -> SetBranchAddress( "K_plus_TRUE_PT" ,   &K_plus_TRUE_PT );
        tree -> SetBranchAddress( "K_plus_TRUE_ETA" ,   &K_plus_TRUE_ETA );
        tree -> SetBranchAddress( "K_plus_TRUE_PHI" ,   &K_plus_TRUE_PHI );

        tree -> SetBranchAddress( "K_plus_PX_DTF" , &K_plus_PX_DTF );
        tree -> SetBranchAddress( "K_plus_PY_DTF" , &K_plus_PY_DTF );
        tree -> SetBranchAddress( "K_plus_PZ_DTF" , &K_plus_PZ_DTF );
        tree -> SetBranchAddress( "K_plus_PE_DTF" , &K_plus_PE_DTF );
        tree -> SetBranchAddress( "K_plus_PT_DTF" , &K_plus_PT_DTF );
        tree -> SetBranchAddress( "K_plus_ETA_DTF" , &K_plus_ETA_DTF );
        tree -> SetBranchAddress( "K_plus_PHI_DTF" , &K_plus_PHI_DTF );
    }

    tree -> SetBranchAddress( "mu_minus_PX" , &mu_minus_PX );
    tree -> SetBranchAddress( "mu_minus_PY" , &mu_minus_PY );
    tree -> SetBranchAddress( "mu_minus_PZ" , &mu_minus_PZ );
    tree -> SetBranchAddress( "mu_minus_PE" , &mu_minus_PE );
    tree -> SetBranchAddress( "mu_minus_PT" , &mu_minus_PT );
    tree -> SetBranchAddress( "mu_minus_PT" , &mu_minus_PT );
    tree -> SetBranchAddress( "mu_minus_PT" , &mu_minus_PT );

    tree -> SetBranchAddress( "mu_minus_TRUE_PX" , &mu_minus_TRUE_PX );
    tree -> SetBranchAddress( "mu_minus_TRUE_PY" , &mu_minus_TRUE_PY );
    tree -> SetBranchAddress( "mu_minus_TRUE_PZ" , &mu_minus_TRUE_PZ );
    tree -> SetBranchAddress( "mu_minus_TRUE_PE" , &mu_minus_TRUE_PE );
    tree -> SetBranchAddress( "mu_minus_TRUE_PT" ,  &mu_minus_TRUE_PT );
    tree -> SetBranchAddress( "mu_minus_TRUE_ETA" ,  &mu_minus_TRUE_ETA );
    tree -> SetBranchAddress( "mu_minus_TRUE_PHI" ,  &mu_minus_TRUE_PHI );

    tree -> SetBranchAddress( "mu_minus_PX_DTF" , &mu_minus_PX_DTF );
    tree -> SetBranchAddress( "mu_minus_PY_DTF" , &mu_minus_PY_DTF );
    tree -> SetBranchAddress( "mu_minus_PZ_DTF" , &mu_minus_PZ_DTF );
    tree -> SetBranchAddress( "mu_minus_PE_DTF" , &mu_minus_PE_DTF );
    tree -> SetBranchAddress( "mu_minus_PT_DTF" , &mu_minus_PT_DTF );
    tree -> SetBranchAddress( "mu_minus_ETA_DTF" , &mu_minus_ETA_DTF );
    tree -> SetBranchAddress( "mu_minus_PHI_DTF" , &mu_minus_PHI_DTF );


    tree -> SetBranchAddress( "mu_plus_PX" , &mu_plus_PX );
    tree -> SetBranchAddress( "mu_plus_PY" , &mu_plus_PY );
    tree -> SetBranchAddress( "mu_plus_PZ" , &mu_plus_PZ );
    tree -> SetBranchAddress( "mu_plus_PE" , &mu_plus_PE );
    tree -> SetBranchAddress( "mu_plus_PT" , &mu_plus_PT );
    tree -> SetBranchAddress( "mu_plus_ETA" , &mu_plus_ETA );
    tree -> SetBranchAddress( "mu_plus_PHI" , &mu_plus_PHI );

    tree -> SetBranchAddress( "mu_plus_TRUE_PX" , &mu_plus_TRUE_PX );
    tree -> SetBranchAddress( "mu_plus_TRUE_PY" , &mu_plus_TRUE_PY );
    tree -> SetBranchAddress( "mu_plus_TRUE_PZ" , &mu_plus_TRUE_PZ );
    tree -> SetBranchAddress( "mu_plus_TRUE_PE" , &mu_plus_TRUE_PE );
    tree -> SetBranchAddress( "mu_plus_TRUE_PT" , &mu_plus_TRUE_PT );
    tree -> SetBranchAddress( "mu_plus_TRUE_ETA" , &mu_plus_TRUE_ETA );
    tree -> SetBranchAddress( "mu_plus_TRUE_PHI" , &mu_plus_TRUE_PHI );

    tree -> SetBranchAddress( "mu_plus_PX_DTF" , &mu_plus_PX_DTF );
    tree -> SetBranchAddress( "mu_plus_PY_DTF" , &mu_plus_PY_DTF);
    tree -> SetBranchAddress( "mu_plus_PZ_DTF" , &mu_plus_PZ_DTF );
    tree -> SetBranchAddress( "mu_plus_PE_DTF" , &mu_plus_PE_DTF );
    tree -> SetBranchAddress( "mu_plus_PT_DTF" , &mu_plus_PT_DTF );
    tree -> SetBranchAddress( "mu_plus_ETA_DTF" , &mu_plus_ETA_DTF );
    tree -> SetBranchAddress( "mu_plus_PHI_DTF" , &mu_plus_PHI_DTF );


    /* TODO
    tree -> SetBranchAddress( "B_plus_ThetaL_DTF" , &B_plus_ThetaL_DTF);
    tree -> SetBranchAddress( "B_plus_ThetaK_DTF" , &B_plus_ThetaK_DTF);
    tree -> SetBranchAddress( "B_plus_Phi_DTF" ,    &B_plus_Phi_DTF );
*/
    tree -> SetBranchAddress( "B_plus_ThetaL" , &B_plus_ThetaL);
    tree -> SetBranchAddress( "B_plus_ThetaK" , &B_plus_ThetaK);
    tree -> SetBranchAddress( "B_plus_Phi" ,    &B_plus_Phi );

    tree -> SetBranchAddress( "B_plus_TRUEThetaL" , &B_plus_TRUE_ThetaL);
    tree -> SetBranchAddress( "B_plus_TRUEThetaK" , &B_plus_TRUE_ThetaK);
    tree -> SetBranchAddress( "B_plus_TRUEPhi" ,    &B_plus_TRUE_Phi );


    tree -> SetBranchAddress( "open_angle" , &open_angle);
    tree -> SetBranchAddress( "open_angle_DTF" , &open_angle_DTF);
    tree -> SetBranchAddress( "TRUE_open_angle" , &TRUE_open_angle );


    //global variables
    tree -> SetBranchAddress( "B_plus_ENDVERTEX_Z", &B_plus_ENDVERTEX_Z);
    tree -> SetBranchAddress( "B_plus_ENDVERTEX_ZERR", &B_plus_ENDVERTEX_ZERR);
    tree -> SetBranchAddress( "B_plus_OWNPV_Z", &B_plus_OWNPV_Z);
    tree -> SetBranchAddress( "B_plus_OWNPV_ZERR", &B_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "B_plus_TRUEORIGINVERTEX_Z", &B_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "B_plus_TRUEENDVERTEX_Z", &B_plus_TRUEENDVERTEX_Z);
    tree -> SetBranchAddress( "B_plus_DTF_PV_Z", &B_plus_DTF_PV_Z);
    tree -> SetBranchAddress( "B_plus_TOPPV_Z", &B_plus_TOPPV_Z);//TODO: what is this?

    tree -> SetBranchAddress( "K_star_plus_ENDVERTEX_Z", &K_star_plus_ENDVERTEX_Z);
    tree -> SetBranchAddress( "K_star_plus_ENDVERTEX_ZERR", &K_star_plus_ENDVERTEX_ZERR);
    tree -> SetBranchAddress( "K_star_plus_OWNPV_Z", &K_star_plus_OWNPV_Z);
    tree -> SetBranchAddress( "K_star_plus_OWNPV_ZERR", &K_star_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "K_star_plus_TRUEORIGINVERTEX_Z", &K_star_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "K_star_plus_TRUEENDVERTEX_Z", &K_star_plus_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "K_plus_OWNPV_Z", &K_plus_OWNPV_Z);
    tree -> SetBranchAddress( "K_plus_OWNPV_ZERR", &K_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "K_plus_TRUEORIGINVERTEX_Z", &K_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "K_plus_TRUEENDVERTEX_Z", &K_plus_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "pi_zero_resolved_TRUEORIGINVERTEX_Z", &pi_zero_resolved_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "pi_zero_resolved_TRUEENDVERTEX_Z", &pi_zero_resolved_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "gamma1_TRUEORIGINVERTEX_Z", &gamma1_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "gamma1_TRUEENDVERTEX_Z", &gamma1_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "gamma2_TRUEORIGINVERTEX_Z", &gamma2_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "gamma2_TRUEENDVERTEX_Z", &gamma2_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "mu_plus_OWNPV_Z", &mu_plus_OWNPV_Z);
    tree -> SetBranchAddress( "mu_plus_OWNPV_ZERR", &mu_plus_OWNPV_ZERR);
    tree -> SetBranchAddress( "mu_plus_TRUEORIGINVERTEX_Z", &mu_plus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "mu_plus_TRUEENDVERTEX_Z", &mu_plus_TRUEENDVERTEX_Z);

    tree -> SetBranchAddress( "mu_minus_OWNPV_Z", &mu_minus_OWNPV_Z);
    tree -> SetBranchAddress( "mu_minus_OWNPV_ZERR", &mu_minus_OWNPV_ZERR);
    tree -> SetBranchAddress( "mu_minus_TRUEORIGINVERTEX_Z", &mu_minus_TRUEORIGINVERTEX_Z);
    tree -> SetBranchAddress( "mu_minus_TRUEENDVERTEX_Z", &mu_minus_TRUEENDVERTEX_Z);

    int N = tree->GetEntries();
    if(N == 0){
        std::cout << "[ERROR]\t\tNo events found!" << std::endl;
        return 0;
    }

    // Create outputfile
    TFile *output = 0;
    preselected ?  newName = "_resolution_preselected_histos" : newName ="_resolution_histos";
    if (truthMatched) newName = newName + "_TM";


    if(Kst2Kpluspi0Resolved){
        if(!MC){
            output = new TFile(Form("%s/resolution/data/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
        }
        else{
            if(ReferenceChannel){
                output = new TFile(Form("%s/resolution/MC/RefKplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else if(PHSP){
                output = new TFile(Form("%s/resolution/PHSP/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else{
                output = new TFile(Form("%s/resolution/MC/KplusPi0/%s%s/%s%s_pi0Resolved%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
        }
    }
    // Kst2Kpluspi0Merged case
    if(Kst2Kpluspi0Merged){
        if(!MC){
            output = new TFile(Form("%s/resolution/data/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
        }
        else{
            if(ReferenceChannel){
                output = new TFile(Form("%s/resolution/MC/RefKplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else if(PHSP){
                output = new TFile(Form("%s/resolution/PHSP/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else{
                output = new TFile(Form("%s/resolution/MC/KplusPi0/%s%s/%s%s_pi0Merged%s.root",path_to_output_KplusPizero.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
        }
    }
    // Kst2Kspiplus case
    if(Kst2Kspiplus){
        if(!MC){
            output = new TFile(Form("%s/resolution/data/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
        }
        else{
            if(ReferenceChannel){
                output = new TFile(Form("%s/resolution/MC/RefKshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else if(PHSP){
                output = new TFile(Form("%s/resolution/PHSP/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
            else{
                output = new TFile(Form("%s/resolution/MC/KshortPiplus/%s%s/%s%s_piplus%s.root",path_to_output_KshortPiplus.c_str(), year.c_str(),magnet.c_str(),year.c_str(),magnet.c_str(),newName.c_str()),"RECREATE");
            }
        }
    }

    //Initialize new tree
    output->cd();

    string tmpName = "";
    string tmpYaxis= "";
    double eta_low_edge = 1.4;
    double eta_high_edge = 5;
    int eta_bins = 36;///2;
    double eta_low_edge_B = 1.4;
    double eta_high_edge_B = 6;
    int eta_bins_B = 46;///2;
    int PVZ_bins = 40;
    double PVZ_low_edge = -90;
    double PVZ_high_edge = 180;
    double KstPt_bins = 50;
    double KstPt_low_edge = 800;
    double KstPt_high_edge = 5000;
    double angle_low_edge = -3.15;
    double angle_high_edge = 3.15;
    double angle_bins = 30;

    //////////////////////////////////
    /////                       //////
    ////     Basic eta histos    /////
    /////                       //////
    //////////////////////////////////
    ///
    //pi0

    tmpYaxis = "Counts / 0.2";

    //pi0
    TH1 * h_pi0_ETA_DTF,*h_pi0_ETA, *h_pi0_ETA_TRUE;
    init_1D(h_pi0_ETA_DTF,  "pi0",  "ETA", "DTF", "",   tmpYaxis,eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_pi0_ETA,      "pi0",  "ETA", "", "",      tmpYaxis,eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_pi0_ETA_TRUE, "pi0",  "ETA", "TRUE", "",  tmpYaxis,eta_bins, eta_low_edge, eta_high_edge);


    //K+
    TH1 *h_Kplus_ETA_DTF,*h_Kplus_ETA,*h_Kplus_ETA_TRUE;
    init_1D(h_Kplus_ETA_DTF,    "Kplus","ETA", "DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kplus_ETA,        "Kplus","ETA", "", "",      tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kplus_ETA_TRUE,   "Kplus","ETA", "TRUE", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K*
    TH1 *h_Kstar_ETA_DTF,*h_Kstar_ETA,*h_Kstar_ETA_TRUE;
    init_1D(h_Kstar_ETA_DTF,    "Kstar","ETA", "DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kstar_ETA,        "Kstar","ETA", "", "",      tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kstar_ETA_TRUE,   "Kstar","ETA", "TRUE", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //B+
    TH1 *h_Bplus_ETA_DTF,*h_Bplus_ETA,*h_Bplus_ETA_TRUE;
    init_1D(h_Bplus_ETA_DTF,    "Bplus","ETA", "DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    cout << h_Bplus_ETA_DTF->GetName() << endl;
    init_1D(h_Bplus_ETA,        "Bplus","ETA", "", "",      tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Bplus_ETA_TRUE,   "Bplus","ETA", "TRUE", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //if (Kst2Kpluspi0Resolved) tmpName = variableLatex("ETA") + "{" + particleLatex("Kplus") + particleLatex("pi0") + particleLatex("mu") +  particleLatex("mu") + "}";
    //else if (Kst2Kspiplus) tmpName = variableLatex("ETA") + "{" + particleLatex("Kshort") + particleLatex("piplus") + particleLatex("mu") +  particleLatex("mu") + "}";


    tmpYaxis = Form("Counts / %.3f",(6.3/angle_bins));
    //ThetaK
    TH1 *h_ThetaK_DTF,*h_ThetaK,*h_ThetaK_TRUE;
    init_1D(h_ThetaK_DTF,    "","ThetaK", "DTF", "",   tmpYaxis, angle_bins, 0, angle_high_edge);
    init_1D(h_ThetaK,        "","ThetaK", "", "",      tmpYaxis, angle_bins, 0, angle_high_edge);
    init_1D(h_ThetaK_TRUE,   "","ThetaK", "TRUE", "",  tmpYaxis, angle_bins, 0, angle_high_edge);

    //ThetaL
    TH1 *h_ThetaL_DTF,*h_ThetaL,*h_ThetaL_TRUE;
    init_1D(h_ThetaL_DTF,    "","ThetaL", "DTF", "",   tmpYaxis, angle_bins, 0, angle_high_edge);
    init_1D(h_ThetaL,        "","ThetaL", "", "",      tmpYaxis, angle_bins, 0, angle_high_edge);;
    init_1D(h_ThetaL_TRUE,   "","ThetaL", "TRUE", "",  tmpYaxis, angle_bins, 0, angle_high_edge);

    //Phi
    TH1 *h_Phi_DTF,*h_Phi,*h_Phi_TRUE;
    init_1D(h_Phi_DTF,    "","Phi", "DTF", "",   tmpYaxis, angle_bins, 0, angle_high_edge);
    init_1D(h_Phi,        "","Phi", "",  "",     tmpYaxis, angle_bins, 0, angle_high_edge);
    init_1D(h_Phi_TRUE,   "","Phi", "TRUE",  "", tmpYaxis, angle_bins, 0, angle_high_edge);

    tmpYaxis = Form("Counts / %.3f",(PVZ_high_edge-PVZ_low_edge/PVZ_bins));
    //PVZ
    TH1 *h_PVZ_DTF,*h_PVZ,*h_PVZ_TRUE;
    init_1D(h_PVZ_DTF,    "","PVZ", "DTF", "",   tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_1D(h_PVZ,        "","PVZ", "",  "",     tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_1D(h_PVZ_TRUE,   "","PVZ", "TRUE",  "", tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);

    //////////////////////////////////
    /////                       //////
    ////        Pull histos      /////
    /////                       //////
    //////////////////////////////////
    ///
    tmpYaxis = "#frac{REC - MC}{#sigma}";

    //pi0
    TH1 * h_pi0_ETA_DTF_pull,*h_pi0_ETA_pull;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #frac{REC - MC}{#sigma}";
    init_1D(h_pi0_ETA_DTF_pull,  "pi0",  "ETA", "PULL_DTF", "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_pi0_ETA_pull,      "pi0",  "ETA", "PULL",     "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K+
    TH1 *h_Kplus_ETA_DTF_pull,*h_Kplus_ETA_pull;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kplus")+"}" + " #frac{REC - MC}{#sigma}";
    init_1D(h_Kplus_ETA_DTF_pull,    "Kplus","ETA", "PULL_DTF", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kplus_ETA_pull,        "Kplus","ETA", "PULL",     "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K*
    TH1 *h_Kstar_ETA_DTF_pull,*h_Kstar_ETA_pull;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kstar")+"}" + " #frac{REC - MC}{#sigma}";
    init_1D(h_Kstar_ETA_DTF_pull,    "Kstar","ETA", "PULL_DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kstar_ETA_pull,        "Kstar","ETA", "PULL",     "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //B+
    TH1 *h_Bplus_ETA_DTF_pull,*h_Bplus_ETA_pull;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Bplus")+"}" + " #frac{REC - MC}{#sigma}";
    init_1D(h_Bplus_ETA_DTF_pull,    "Bplus","ETA", "PULL_DTF", "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);
    init_1D(h_Bplus_ETA_pull,        "Bplus","ETA", "PULL",     "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);


    //pi0 in Kstar_end_vertex
    TH1 * h_pi0_PVZ_DTF_pull,*h_pi0_PVZ_pull;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #frac{REC - MC}{#sigma}";
    init_1D(h_pi0_PVZ_DTF_pull,  "Bplus",  "PVZ", "PULL_DTF", "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_1D(h_pi0_PVZ_pull,      "Bplus",  "PVZ", "PULL",     "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);

    TH1 * h_pi0_KstPt_DTF_pull,*h_pi0_KstPt_pull;
    tmpYaxis = variableLatex("ETA") + "{"+particleLatex("pi0")+"}" + " #frac{REC - MC}{#sigma}";
    init_1D(h_pi0_KstPt_DTF_pull,  "pi0",  "KstPt", "PULL_DTF", "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_pi0_KstPt_pull,      "pi0",  "KstPt", "PULL", "",     tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //ThetaK in Kstar pT
    TH1 *h_ThetaK_KstPt_DTF_pull,*h_ThetaK_KstPt_pull;
    tmpYaxis = variableLatex("ThetaK") + " #frac{REC - MC}{#sigma}";
    init_1D(h_ThetaK_KstPt_DTF_pull,    "ThetaK","KstPt", "PULL_DTF", "",tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaK_KstPt_pull,        "ThetaK","KstPt", "PULL",     "",tmpYaxis,  KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //ThetaL in Kstar pT
    TH1 *h_ThetaL_KstPt_DTF_pull,*h_ThetaL_KstPt_pull;
    tmpYaxis = variableLatex("ThetaL") + " #frac{REC - MC}{#sigma}";
    init_1D(h_ThetaL_KstPt_DTF_pull,    "ThetaL","KstPt", "PULL_DTF",    "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaL_KstPt_pull,        "ThetaL","KstPt", "PULL",        "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //Phi in Kstar pT
    TH1 *h_Phi_KstPt_DTF_pull,*h_Phi_KstPt_pull;
    tmpYaxis = variableLatex("Phi") + " #frac{REC - MC}{#sigma}";
    init_1D(h_Phi_KstPt_DTF_pull,    "Phi","KstPt", "PULL_DTF",  "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_Phi_KstPt_pull,        "Phi","KstPt", "PULL",      "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //////////////////////////////////
    /////                       //////
    ////    Resolution histos    /////
    /////          2D           //////
    //////////////////////////////////

    //pi0
    TH2 *h_pi0_ETA_DTF_ref, *h_pi0_ETA_ref;
    string tmpZaxis = "Counts a.u.";
    tmpYaxis = "#mu_{REC - TRUE}";
    init_2D(h_pi0_ETA_DTF_ref,  "pi0","ETA","ETA", "RES_DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);
    init_2D(h_pi0_ETA_ref,      "pi0","ETA","ETA", "RES",     tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);

    //pi0 (B_plus_ENDVERTEX_Z) PVZ
    TH2 *h_pi0_PVZ_DTF_ref, *h_pi0_PVZ_ref;
    init_2D(h_pi0_PVZ_DTF_ref,  "pi0","PVZ","ETA", "RES_DTF", tmpYaxis, tmpZaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge, 100, -1, 1);
    init_2D(h_pi0_PVZ_ref,      "pi0","PVZ","ETA", "RES",     tmpYaxis, tmpZaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge, 100, -1, 1);

    //pi0 (K_star_plus_pT)
    TH2 *h_pi0_KstPt_DTF_ref, *h_pi0_KstPt_ref;
    init_2D(h_pi0_KstPt_DTF_ref,  "pi0","ETA","KstPt", "RES_DTF", tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 100, -1, 1);
    init_2D(h_pi0_KstPt_ref,      "pi0","ETA","KstPt", "RES",     tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 100, -1, 1);

    //K+
    TH2 *h_Kplus_ETA_DTF_ref, *h_Kplus_ETA_ref;
    init_2D(h_Kplus_ETA_DTF_ref,  "Kplus","ETA","ETA", "RES_DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);
    init_2D(h_Kplus_ETA_ref,      "Kplus","ETA","ETA", "RES",     tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);

    //K*
    TH2 *h_Kstar_ETA_DTF_ref, *h_Kstar_ETA_ref;
    init_2D(h_Kstar_ETA_DTF_ref,  "Kstar","ETA","ETA", "RES_DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);
    init_2D(h_Kstar_ETA_ref,      "Kstar","ETA","ETA", "RES",     tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);

    //B+
    TH2 *h_Bplus_ETA_DTF_ref, *h_Bplus_ETA_ref;
    init_2D(h_Bplus_ETA_DTF_ref,  "Bplus","ETA","ETA", "RES_DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);
    init_2D(h_Bplus_ETA_ref,      "Bplus","ETA","ETA", "RES",     tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge, 100, -1, 1);


    //ThetaK in Kstar pT
    TH2 *h_ThetaK_KstPt_DTF_ref,*h_ThetaK_KstPt_ref;
    init_2D(h_ThetaK_KstPt_DTF_ref,    "ThetaK","KstPt","ThetaK", "RES_DTF",tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 150, -2, 1);
    init_2D(h_ThetaK_KstPt_ref,        "ThetaK","KstPt","ThetaK", "RES",    tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 150, -2, 1);

    //ThetaL in Kstar pT
    TH2 *h_ThetaL_KstPt_DTF_ref,*h_ThetaL_KstPt_ref;
    init_2D(h_ThetaL_KstPt_DTF_ref,    "ThetaL","KstPt","ThetaL", "RES_DTF",tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 100, -1, 1);
    init_2D(h_ThetaL_KstPt_ref,        "ThetaL","KstPt","ThetaL", "RES",    tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 100, -1, 1);

    //Phi in Kstar pT
    TH2 *h_Phi_KstPt_DTF_ref,*h_Phi_KstPt_ref;
    init_2D(h_Phi_KstPt_DTF_ref,    "Phi","KstPt","Phi", "RES_DTF",  tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 100, -1, 1);
    init_2D(h_Phi_KstPt_ref,        "Phi","KstPt","Phi", "RES",      tmpYaxis, tmpZaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge, 100, -1, 1);

    //PVZ (B_plus_ENDVERTEX_Z)
    TH2 *h_PVZ_ref;
    init_2D(h_PVZ_ref,      "","PVZ","PVZ", "RES",     tmpYaxis, tmpZaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge, 100, -1, 1);

    //////////////////////////////////
    /////                       //////
    ////      Res+sys histos     /////
    /////          1D           //////
    //////////////////////////////////


    //pi0
    TH1 * h_pi0_ETA_DTF_res_sys,*h_pi0_ETA_res_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_pi0_ETA_DTF_res_sys,  "pi0",  "ETA", "RES_SYS_DTF", "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_pi0_ETA_res_sys,      "pi0",  "ETA", "RES_SYS",     "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K+
    TH1 *h_Kplus_ETA_DTF_res_sys,*h_Kplus_ETA_res_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kplus")+"}" + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_Kplus_ETA_DTF_res_sys,    "Kplus","ETA", "RES_SYS_DTF", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kplus_ETA_res_sys,        "Kplus","ETA", "RES_SYS",     "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K*
    TH1 *h_Kstar_ETA_DTF_res_sys,*h_Kstar_ETA_res_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kstar")+"}" + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_Kstar_ETA_DTF_res_sys,    "Kstar","ETA", "RES_SYS_DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kstar_ETA_res_sys,        "Kstar","ETA", "RES_SYS",     "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //B+
    TH1 *h_Bplus_ETA_DTF_res_sys,*h_Bplus_ETA_res_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Bplus")+"}" + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_Bplus_ETA_DTF_res_sys,    "Bplus","ETA", "RES_SYS_DTF", "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);
    init_1D(h_Bplus_ETA_res_sys,        "Bplus","ETA", "RES_SYS",     "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);


    //pi0 in Kstar_end_vertex
    TH1 * h_pi0_PVZ_DTF_res_sys,*h_pi0_PVZ_res_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_pi0_PVZ_DTF_res_sys,  "Bplus",  "PVZ", "RES_SYS_DTF", "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_1D(h_pi0_PVZ_res_sys,      "Bplus",  "PVZ", "RES_SYS",     "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);

    TH1 * h_pi0_KstPt_DTF_res_sys,*h_pi0_KstPt_res_sys;
    init_1D(h_pi0_KstPt_DTF_res_sys,  "",  "KstPt", "RES_SYS_DTF", "res_sys", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_pi0_KstPt_res_sys,      "",  "KstPt", "RES_SYS", "",     tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //ThetaK in Kstar pT
    TH1 *h_ThetaK_KstPt_DTF_res_sys,*h_ThetaK_KstPt_res_sys;
    tmpYaxis = variableLatex("ThetaK") + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_ThetaK_KstPt_DTF_res_sys,    "ThetaK","KstPt", "RES_SYS_DTF", "",tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaK_KstPt_res_sys,        "ThetaK","KstPt", "RES_SYS",     "",tmpYaxis,  KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //ThetaL in Kstar pT
    TH1 *h_ThetaL_KstPt_DTF_res_sys,*h_ThetaL_KstPt_res_sys;
    tmpYaxis = variableLatex("ThetaL") + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_ThetaL_KstPt_DTF_res_sys,    "ThetaL","KstPt", "RES_SYS_DTF",    "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaL_KstPt_res_sys,        "ThetaL","KstPt", "RES_SYS",        "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //Phi in Kstar pT
    TH1 *h_Phi_KstPt_DTF_res_sys,*h_Phi_KstPt_res_sys;
    tmpYaxis = variableLatex("Phi") + " #mu_{REC - TRUE} #pm #sigma_{REC - TRUE}";
    init_1D(h_Phi_KstPt_DTF_res_sys,    "Phi","KstPt", "RES_SYS_DTF",  "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_Phi_KstPt_res_sys,        "Phi","KstPt", "RES_SYS",      "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //////////////////////////////////
    /////                       //////
    ////    Resolution histos    /////
    /////          1D           //////
    //////////////////////////////////


    //pi0
    TH1 * h_pi0_ETA_DTF_res,*h_pi0_ETA_res;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #sigma_{REC - TRUE}";
    init_1D(h_pi0_ETA_DTF_res,  "pi0",  "ETA", "RES_DTF", "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_pi0_ETA_res,      "pi0",  "ETA", "RES",     "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K+
    TH1 *h_Kplus_ETA_DTF_res,*h_Kplus_ETA_res;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kplus")+"}" + " #sigma_{REC - TRUE}";
    init_1D(h_Kplus_ETA_DTF_res,    "Kplus","ETA", "RES_DTF", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kplus_ETA_res,        "Kplus","ETA", "RES",     "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K*
    TH1 *h_Kstar_ETA_DTF_res,*h_Kstar_ETA_res;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kstar")+"}" + " #sigma_{REC - TRUE}";
    init_1D(h_Kstar_ETA_DTF_res,    "Kstar","ETA", "RES_DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kstar_ETA_res,        "Kstar","ETA", "RES",     "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //B+
    TH1 *h_Bplus_ETA_DTF_res,*h_Bplus_ETA_res;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Bplus")+"}" + " #sigma_{REC - TRUE}";
    init_1D(h_Bplus_ETA_DTF_res,    "Bplus","ETA", "RES_DTF", "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);
    init_1D(h_Bplus_ETA_res,        "Bplus","ETA", "RES",     "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);


    //pi0 in Kstar_end_vertex
    TH1 * h_pi0_PVZ_DTF_res,*h_pi0_PVZ_res;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #sigma_{REC - TRUE}";
    init_1D(h_pi0_PVZ_DTF_res,  "Bplus",  "PVZ", "RES_DTF", "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_1D(h_pi0_PVZ_res,      "Bplus",  "PVZ", "RES",     "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);

    TH1 * h_pi0_KstPt_DTF_res,*h_pi0_KstPt_res;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #sigma_{REC - TRUE}";
    init_1D(h_pi0_KstPt_DTF_res,  "pi0",  "KstPt", "RES_DTF", "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_pi0_KstPt_res,      "pi0",  "KstPt", "RES", "",     tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //ThetaK in Kstar pT
    TH1 *h_ThetaK_KstPt_DTF_res,*h_ThetaK_KstPt_res;
    tmpYaxis = variableLatex("ThetaK")+ " #sigma_{REC - TRUE}";
    init_1D(h_ThetaK_KstPt_DTF_res,    "ThetaK","KstPt", "RES_DTF", "",tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaK_KstPt_res,        "ThetaK","KstPt", "RES",     "",tmpYaxis,  KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //ThetaL in Kstar pT
    TH1 *h_ThetaL_KstPt_DTF_res,*h_ThetaL_KstPt_res;
    tmpYaxis = variableLatex("ThetaL")+ " #sigma_{REC - TRUE}";
    init_1D(h_ThetaL_KstPt_DTF_res,    "ThetaL","KstPt", "RES_DTF",    "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaL_KstPt_res,        "ThetaL","KstPt", "RES",        "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //Phi in Kstar pT
    TH1 *h_Phi_KstPt_DTF_res,*h_Phi_KstPt_res;
    tmpYaxis = variableLatex("Phi")+ " #sigma_{REC - TRUE}";
    init_1D(h_Phi_KstPt_DTF_res,    "Phi","KstPt", "RES_DTF",  "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_Phi_KstPt_res,        "Phi","KstPt", "RES",      "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //////////////////////////////////
    /////                       //////
    ////   Systematics histos    /////
    /////          1D           //////
    //////////////////////////////////

    tmpYaxis = "#mu_{REC - TRUE}";
    //pi0
    //pi0
    TH1 * h_pi0_ETA_DTF_sys,*h_pi0_ETA_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_pi0_ETA_DTF_sys,  "pi0",  "ETA", "SYS_DTF", "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_pi0_ETA_sys,      "pi0",  "ETA", "SYS",     "", tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K+
    TH1 *h_Kplus_ETA_DTF_sys,*h_Kplus_ETA_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kplus")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_Kplus_ETA_DTF_sys,    "Kplus","ETA", "SYS_DTF", "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kplus_ETA_sys,        "Kplus","ETA", "SYS",     "",  tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K*
    TH1 *h_Kstar_ETA_DTF_sys,*h_Kstar_ETA_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Kstar")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_Kstar_ETA_DTF_sys,    "Kstar","ETA", "SYS_DTF", "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_1D(h_Kstar_ETA_sys,        "Kstar","ETA", "SYS",     "",   tmpYaxis, eta_bins, eta_low_edge, eta_high_edge);

    //B+
    TH1 *h_Bplus_ETA_DTF_sys,*h_Bplus_ETA_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("Bplus")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_Bplus_ETA_DTF_sys,    "Bplus","ETA", "SYS_DTF", "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);
    init_1D(h_Bplus_ETA_sys,        "Bplus","ETA", "SYS",     "",  tmpYaxis, eta_bins_B, eta_low_edge_B, eta_high_edge_B);


    //pi0 in Kstar_end_vertex
    TH1 * h_pi0_PVZ_DTF_sys,*h_pi0_PVZ_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_pi0_PVZ_DTF_sys,  "Bplus",  "PVZ", "SYS_DTF", "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_1D(h_pi0_PVZ_sys,      "Bplus",  "PVZ", "SYS",     "",  tmpYaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);

    TH1 * h_pi0_KstPt_DTF_sys,*h_pi0_KstPt_sys;
    tmpYaxis = variableLatex("ETA")+"{"+particleLatex("pi0")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_pi0_KstPt_DTF_sys,  "pi0",  "KstPt", "SYS_DTF", "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_pi0_KstPt_sys,      "pi0",  "KstPt", "SYS",     "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);


    //ThetaK in Kstar pT
    TH1 *h_ThetaK_KstPt_DTF_sys,*h_ThetaK_KstPt_sys;
    tmpYaxis = variableLatex("ThetaK")+ + " #mu_{REC - TRUE}";
    init_1D(h_ThetaK_KstPt_DTF_sys,    "ThetaK","KstPt", "SYS_DTF", "",tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaK_KstPt_sys,        "ThetaK","KstPt", "SYS",     "",tmpYaxis,  KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //ThetaL in Kstar pT
    TH1 *h_ThetaL_KstPt_DTF_sys,*h_ThetaL_KstPt_sys;
    tmpYaxis = variableLatex("ThetaL")+"{"+particleLatex("pi0")+"}" + " #mu_{REC - TRUE}";
    init_1D(h_ThetaL_KstPt_DTF_sys,    "ThetaL","KstPt", "SYS_DTF", "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_ThetaL_KstPt_sys,        "ThetaL","KstPt", "SYS",     "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);

    //Phi in Kstar pT
    TH1 *h_Phi_KstPt_DTF_sys,*h_Phi_KstPt_sys;
    tmpYaxis = variableLatex("Phi")+ + " #mu_{REC - TRUE}";
    init_1D(h_Phi_KstPt_DTF_sys,    "Phi","KstPt", "SYS_DTF",  "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);
    init_1D(h_Phi_KstPt_sys,        "Phi","KstPt", "SYS",      "", tmpYaxis, KstPt_bins, KstPt_low_edge, KstPt_high_edge);



    //////////////////////////////////
    /////                       //////
    ////     REC vs MC histos    /////
    /////                       //////
    //////////////////////////////////

    //pi0
    TH2 *h_pi0_ETA_DTF_rec_MC, *h_pi0_ETA_rec_MC;
    tmpZaxis = "Counts a.u.";
    tmpYaxis = "TRUE ";
    init_2D(h_pi0_ETA_DTF_rec_MC,  "pi0","ETA","ETA", "DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_2D(h_pi0_ETA_rec_MC,      "pi0","ETA","ETA", "",    tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K+
    TH2 *h_Kplus_ETA_DTF_rec_MC, *h_Kplus_ETA_rec_MC;
    init_2D(h_Kplus_ETA_DTF_rec_MC,  "Kplus","ETA","ETA", "DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_2D(h_Kplus_ETA_rec_MC,      "Kplus","ETA","ETA", "",    tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);

    //K*
    TH2 *h_Kstar_ETA_DTF_rec_MC, *h_Kstar_ETA_rec_MC;
    init_2D(h_Kstar_ETA_DTF_rec_MC,  "Kstar","ETA","ETA", "DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_2D(h_Kstar_ETA_rec_MC,      "Kstar","ETA","ETA", "",    tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);

    //B+
    TH2 *h_Bplus_ETA_DTF_rec_MC, *h_Bplus_ETA_rec_MC;
    init_2D(h_Bplus_ETA_DTF_rec_MC,  "Bplus","ETA","ETA", "DTF", tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);
    init_2D(h_Bplus_ETA_rec_MC,      "Bplus","ETA","ETA", "",    tmpYaxis, tmpZaxis, eta_bins, eta_low_edge, eta_high_edge);


    //ThetaK in Kstar pT
    TH2 *h_ThetaK_DTF_rec_MC,*h_ThetaK_rec_MC;
    init_2D(h_ThetaK_DTF_rec_MC,    "","ThetaK","ThetaK", "DTF",tmpYaxis, tmpZaxis, angle_bins, 0, angle_high_edge);
    init_2D(h_ThetaK_rec_MC,        "","ThetaK","ThetaK", "",   tmpYaxis, tmpZaxis, angle_bins, 0, angle_high_edge);

    //ThetaL in Kstar pT
    TH2 *h_ThetaL_DTF_rec_MC,*h_ThetaL_rec_MC;
    init_2D(h_ThetaL_DTF_rec_MC,    "","ThetaL","ThetaL", "DTF", tmpYaxis, tmpZaxis, angle_bins, 0, angle_high_edge);
    init_2D(h_ThetaL_rec_MC,        "","ThetaL","ThetaL", "",    tmpYaxis, tmpZaxis, angle_bins, 0, angle_high_edge);

    //Phi in Kstar pT
    TH2 *h_Phi_DTF_rec_MC,*h_Phi_rec_MC;
    init_2D(h_Phi_DTF_rec_MC,    "","Phi","Phi", "DTF",  tmpYaxis, tmpZaxis, angle_bins, 0, angle_high_edge);
    init_2D(h_Phi_rec_MC,        "","Phi","Phi", "",     tmpYaxis, tmpZaxis, angle_bins, 0, angle_high_edge);

    //PVZ
    TH2 *h_PVZ_DTF_rec_MC, *h_PVZ_rec_MC;
    init_2D(h_PVZ_DTF_rec_MC,  "Bplus", "PVZ", "PVZ", "DTF", tmpYaxis, tmpZaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);
    init_2D(h_PVZ_rec_MC,      "Bplus", "PVZ", "PVZ", "",    tmpYaxis, tmpZaxis, PVZ_bins, PVZ_low_edge, PVZ_high_edge);



    //////////////////////////////////
    /////                       //////
    ////     Loop over events    /////
    /////                       //////
    //////////////////////////////////
    for (int i=0; i<N; i++){

        //output the different counters!
        if ((0ul == (i % 10000ul) || i + 1 == N) && i != 0) {
            cout << "Read event " << i + 1 << "/" << N
                 << endl;
        }

        tree->GetEntry(i);

        // fill HISTOGRAMS //
        h_Bplus_ETA_DTF         ->Fill(B_plus_ETA_DTF);
        h_Bplus_ETA_TRUE        ->Fill(B_plus_TRUE_ETA);
        h_Bplus_ETA_DTF_rec_MC  ->Fill(B_plus_ETA_DTF,B_plus_TRUE_ETA);
        h_Bplus_ETA_DTF_ref     ->Fill(B_plus_ETA_DTF,B_plus_ETA_DTF-B_plus_TRUE_ETA);
        if (!OnlyDTF){
            h_Bplus_ETA             ->Fill(B_plus_ETA);
            h_Bplus_ETA_rec_MC      ->Fill(B_plus_ETA,B_plus_TRUE_ETA);
            h_Bplus_ETA_ref         ->Fill(B_plus_ETA,B_plus_ETA-B_plus_TRUE_ETA);
        }

        h_ThetaK_DTF    ->Fill(B_plus_ThetaK_DTF);
        h_ThetaK_TRUE   ->Fill(B_plus_TRUE_ThetaK);
        h_ThetaK_DTF_rec_MC  ->Fill(B_plus_ThetaK_DTF,B_plus_TRUE_ThetaK);
        h_ThetaK_KstPt_DTF_ref  ->Fill(K_star_plus_PT_DTF,B_plus_ThetaK_DTF-B_plus_TRUE_ThetaK);
        //  if (!OnlyDTF){
        h_ThetaK        ->Fill(B_plus_ThetaK);
        h_ThetaK_rec_MC      ->Fill(B_plus_ThetaK,B_plus_TRUE_ThetaK);
        h_ThetaK_KstPt_ref      ->Fill(K_star_plus_PT_DTF,B_plus_ThetaK-B_plus_TRUE_ThetaK);
        //  }

        h_ThetaL_DTF    ->Fill(B_plus_ThetaL_DTF);
        h_ThetaL_TRUE   ->Fill(B_plus_TRUE_ThetaL);
        h_ThetaL_DTF_rec_MC     ->Fill(B_plus_ThetaL_DTF,  B_plus_TRUE_ThetaL);
        h_ThetaL_KstPt_DTF_ref  ->Fill(K_star_plus_PT_DTF, B_plus_ThetaL-B_plus_TRUE_ThetaL);

        //  if (!OnlyDTF){
        h_ThetaL        ->Fill(B_plus_ThetaL);
        h_ThetaL_rec_MC ->Fill(B_plus_ThetaL,       B_plus_TRUE_ThetaL);
        h_ThetaL_KstPt_ref   ->Fill(K_star_plus_PT_DTF, B_plus_ThetaL-B_plus_TRUE_ThetaL);
        //  }

        h_Phi_DTF       ->Fill(B_plus_Phi_DTF);
        h_Phi_TRUE      ->Fill(B_plus_TRUE_Phi);
        h_Phi_DTF_rec_MC    ->Fill(B_plus_Phi_DTF,B_plus_TRUE_Phi);
        h_Phi_KstPt_DTF_ref ->Fill(K_star_plus_PT_DTF,  B_plus_Phi-B_plus_TRUE_Phi);

        //  if (!OnlyDTF){
        h_Phi           ->Fill(TMath::Abs(B_plus_Phi));
        h_Phi_rec_MC      ->Fill(TMath::Abs(B_plus_Phi),      TMath::Abs(B_plus_TRUE_Phi));
        h_Phi_KstPt_ref   ->Fill(K_star_plus_PT_DTF, TMath::Abs(B_plus_Phi)-TMath::Abs(B_plus_TRUE_Phi));
        //  }

        //--------------------------------------------------

        h_PVZ_TRUE      ->Fill(B_plus_TRUEENDVERTEX_Z);
        h_PVZ           ->Fill(B_plus_ENDVERTEX_Z);
        h_PVZ_rec_MC    ->Fill(B_plus_ENDVERTEX_Z,  B_plus_TRUEENDVERTEX_Z);
        h_PVZ_ref       ->Fill(B_plus_ENDVERTEX_Z,  B_plus_ENDVERTEX_Z-B_plus_TRUEENDVERTEX_Z);

        h_Kstar_ETA_DTF         ->Fill(K_star_plus_ETA_DTF);
        h_Kstar_ETA_TRUE        ->Fill(K_star_plus_TRUE_ETA);
        h_Kstar_ETA_DTF_rec_MC  ->Fill(K_star_plus_ETA_DTF, K_star_plus_TRUE_ETA);
        h_Kstar_ETA_DTF_ref     ->Fill(K_star_plus_ETA_DTF, K_star_plus_ETA_DTF-K_star_plus_TRUE_ETA);
        if (!OnlyDTF){
            h_Kstar_ETA             ->Fill(K_star_plus_ETA);
            h_Kstar_ETA_rec_MC      ->Fill(K_star_plus_ETA,     K_star_plus_TRUE_ETA);
            h_Kstar_ETA_ref         ->Fill(K_star_plus_ETA,     K_star_plus_ETA-K_star_plus_TRUE_ETA);
        }

        //--------------------------------------------------
        //--------------------------------------------------
        //--------------------------------------------------

        if(Kst2Kspiplus){
            // h_Kshort_ETA_DTF     ->Fill(K_short_ETA_DTF);
            // h_Kshort_ETA_TRUE    ->Fill(K_short_TRUE_ETA);
            // h_Kshort_ETA         ->Fill(K_short_ETA);
        }
        //--------------------------------------------------
        //--------------------------------------------------
        //--------------------------------------------------

        if (Kst2Kpluspi0Resolved){
            h_Kplus_ETA_DTF         ->Fill(K_plus_ETA_DTF);
            h_Kplus_ETA_TRUE        ->Fill(K_plus_TRUE_ETA);
            h_Kplus_ETA_DTF_rec_MC  ->Fill(K_plus_ETA_DTF,K_plus_TRUE_ETA);
            h_Kplus_ETA_DTF_ref     ->Fill(K_plus_ETA_DTF,K_plus_ETA_DTF-K_plus_TRUE_ETA);
            if (!OnlyDTF){
                h_Kplus_ETA             ->Fill(K_plus_ETA);
                h_Kplus_ETA_rec_MC      ->Fill(K_plus_ETA,K_plus_TRUE_ETA);
                h_Kplus_ETA_ref         ->Fill(K_plus_ETA,K_plus_ETA-K_plus_TRUE_ETA);
            }
            //--------------------------------------------------

            h_pi0_ETA_DTF           ->Fill(pi_zero_resolved_ETA_DTF);
            h_pi0_ETA_TRUE          ->Fill(pi_zero_resolved_TRUE_ETA);
            h_pi0_ETA_DTF_rec_MC    ->Fill(pi_zero_resolved_ETA_DTF,pi_zero_resolved_TRUE_ETA);
            h_pi0_ETA_DTF_ref       ->Fill(pi_zero_resolved_ETA_DTF,pi_zero_resolved_ETA_DTF-pi_zero_resolved_TRUE_ETA);
            h_pi0_PVZ_DTF_ref       ->Fill(B_plus_ENDVERTEX_Z,pi_zero_resolved_ETA_DTF-pi_zero_resolved_TRUE_ETA);
            h_pi0_KstPt_DTF_ref     ->Fill(K_star_plus_PT_DTF,pi_zero_resolved_ETA_DTF-pi_zero_resolved_TRUE_ETA);
            if (!OnlyDTF){
                h_pi0_ETA               ->Fill(pi_zero_resolved_ETA);
                h_pi0_ETA_rec_MC        ->Fill(pi_zero_resolved_ETA,pi_zero_resolved_TRUE_ETA);
                h_pi0_ETA_ref           ->Fill(pi_zero_resolved_ETA,pi_zero_resolved_ETA-pi_zero_resolved_TRUE_ETA);
                h_pi0_PVZ_ref           ->Fill(B_plus_ENDVERTEX_Z,pi_zero_resolved_ETA-pi_zero_resolved_TRUE_ETA);
                h_pi0_KstPt_ref         ->Fill(K_star_plus_PT,pi_zero_resolved_ETA-pi_zero_resolved_TRUE_ETA);
            }
        }


    }
    output->cd();


    //////////////////////////////////
    /////                       //////
    ////     Fill res histos     /////
    /////                       //////
    //////////////////////////////////
    ///
    TF1 *func = new TF1("func","gaus",-1,1);


    res_sys(h_Bplus_ETA_DTF_ref,    h_Bplus_ETA_DTF_res_sys, h_Bplus_ETA_DTF_sys,h_Bplus_ETA_DTF_res, func, eta_bins_B, output ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    res(h_Bplus_ETA_DTF_res_sys,h_Bplus_ETA_DTF_res,    eta_bins_B ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys(h_Bplus_ETA_DTF_res_sys,h_Bplus_ETA_DTF_sys,    eta_bins_B ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
   //  if (!OnlyDTF){
        res_sys(h_Bplus_ETA_ref,        h_Bplus_ETA_res_sys,     h_Bplus_ETA_sys, h_Bplus_ETA_res,        func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res(h_Bplus_ETA_res_sys,    h_Bplus_ETA_res,        eta_bins_B ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys(h_Bplus_ETA_res_sys,    h_Bplus_ETA_sys ,       eta_bins_B ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    // }

    res_sys(h_ThetaK_KstPt_DTF_ref,     h_ThetaK_KstPt_DTF_res_sys, h_ThetaK_KstPt_DTF_sys,  h_ThetaK_KstPt_DTF_res,   func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res(h_ThetaK_KstPt_DTF_res_sys,     h_ThetaK_KstPt_DTF_res, KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys(h_ThetaK_KstPt_DTF_res_sys,     h_ThetaK_KstPt_DTF_sys, KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    // if (!OnlyDTF){
        res_sys(h_ThetaK_KstPt_ref,         h_ThetaK_KstPt_res_sys,  h_ThetaK_KstPt_sys,   h_ThetaK_KstPt_res,         func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res(h_ThetaK_KstPt_res_sys,         h_ThetaK_KstPt_res,     KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys(h_ThetaK_KstPt_res_sys,         h_ThetaK_KstPt_sys,     KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    // }

    res_sys(h_ThetaL_KstPt_DTF_ref,     h_ThetaL_KstPt_DTF_res_sys, h_ThetaL_KstPt_DTF_sys, h_ThetaL_KstPt_DTF_res,    func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res(h_ThetaL_KstPt_DTF_res_sys,     h_ThetaL_KstPt_DTF_res, KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys(h_ThetaL_KstPt_DTF_res_sys,     h_ThetaL_KstPt_DTF_sys, KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
  //  if (!OnlyDTF){
        res_sys(h_ThetaL_KstPt_ref,         h_ThetaL_KstPt_res_sys,   h_ThetaL_KstPt_sys,  h_ThetaL_KstPt_res,       func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res(h_ThetaL_KstPt_res_sys,         h_ThetaL_KstPt_res,     KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys(h_ThetaL_KstPt_res_sys,         h_ThetaL_KstPt_sys,     KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
   // }


    res_sys(h_Phi_KstPt_DTF_ref,     h_Phi_KstPt_DTF_res_sys,   h_Phi_KstPt_DTF_sys,  h_Phi_KstPt_DTF_res,   func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res(h_Phi_KstPt_DTF_res_sys,     h_Phi_KstPt_DTF_res, KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys(h_Phi_KstPt_DTF_res_sys,     h_Phi_KstPt_DTF_sys, KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    if (!OnlyDTF){
        res_sys(h_Phi_KstPt_ref,         h_Phi_KstPt_res_sys,   h_Phi_KstPt_sys,  h_Phi_KstPt_res,       func, eta_bins_B, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res(h_Phi_KstPt_res_sys,         h_Phi_KstPt_res,     KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys(h_Phi_KstPt_res_sys,         h_Phi_KstPt_sys,     KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    }


    res_sys(h_Kstar_ETA_DTF_ref,h_Kstar_ETA_DTF_res_sys,h_Kstar_ETA_DTF_sys,h_Kstar_ETA_DTF_res,func, eta_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res(h_Kstar_ETA_DTF_res_sys,    h_Kstar_ETA_DTF_res,eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys(h_Kstar_ETA_DTF_res_sys,    h_Kstar_ETA_DTF_sys,eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    if (!OnlyDTF){
        res_sys(h_Kstar_ETA_ref,    h_Kstar_ETA_res_sys,  h_Kstar_ETA_sys,h_Kstar_ETA_res,  func, eta_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res(h_Kstar_ETA_res_sys,        h_Kstar_ETA_res,    eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys(h_Kstar_ETA_res_sys,        h_Kstar_ETA_sys,    eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    }

    res_sys(h_Kplus_ETA_DTF_ref,h_Kplus_ETA_DTF_res_sys,h_Kplus_ETA_DTF_sys,h_Kplus_ETA_DTF_res,func, eta_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res(h_Kplus_ETA_DTF_res_sys,    h_Kplus_ETA_DTF_res,eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys(h_Kplus_ETA_DTF_res_sys,    h_Kplus_ETA_DTF_sys,eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    if (!OnlyDTF){
        res_sys(h_Kplus_ETA_ref,    h_Kplus_ETA_res_sys,    h_Kplus_ETA_sys,    h_Kplus_ETA_res,    func, eta_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res(h_Kplus_ETA_res_sys,        h_Kplus_ETA_res,eta_bins     ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys(h_Kplus_ETA_res_sys,        h_Kplus_ETA_sys,eta_bins     ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    }

    res_sys(h_pi0_ETA_DTF_ref,  h_pi0_ETA_DTF_res_sys,  h_pi0_ETA_DTF_sys,  h_pi0_ETA_DTF_res,  func, eta_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res_sys(h_pi0_PVZ_DTF_ref,  h_pi0_PVZ_DTF_res_sys,  h_pi0_PVZ_DTF_sys,  h_pi0_PVZ_DTF_res,  func, PVZ_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res_sys(h_pi0_KstPt_DTF_ref,h_pi0_KstPt_DTF_res_sys,h_pi0_KstPt_DTF_sys,h_pi0_KstPt_DTF_res,func, KstPt_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    res( h_pi0_ETA_DTF_res_sys, h_pi0_ETA_DTF_res,  eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    res( h_pi0_PVZ_DTF_res_sys, h_pi0_PVZ_res,      PVZ_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    res( h_pi0_KstPt_DTF_res_sys, h_pi0_KstPt_DTF_res,      KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys( h_pi0_ETA_DTF_res_sys, h_pi0_ETA_DTF_sys,  eta_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys( h_pi0_PVZ_DTF_res_sys, h_pi0_PVZ_sys,      PVZ_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    sys( h_pi0_KstPt_DTF_res_sys, h_pi0_KstPt_DTF_sys,      KstPt_bins ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    if (!OnlyDTF){
        res_sys(h_pi0_ETA_ref,      h_pi0_ETA_res_sys,      h_pi0_ETA_sys,      h_pi0_ETA_res,      func, eta_bins,   output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res_sys(h_pi0_PVZ_ref,      h_pi0_PVZ_res_sys,      h_pi0_PVZ_sys,      h_pi0_PVZ_res,      func, PVZ_bins,   output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res_sys(h_pi0_KstPt_ref,    h_pi0_KstPt_res_sys,    h_pi0_KstPt_sys,    h_pi0_KstPt_res,    func, KstPt_bins, output, preselected,  year,  magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        res( h_pi0_ETA_res_sys,     h_pi0_ETA_res,          eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        res( h_pi0_PVZ_res_sys,     h_pi0_PVZ_res,          PVZ_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        res( h_pi0_KstPt_res_sys,   h_pi0_KstPt_res,        KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys( h_pi0_ETA_res_sys,     h_pi0_ETA_sys,          eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys( h_pi0_PVZ_res_sys,     h_pi0_PVZ_sys,          PVZ_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        sys( h_pi0_KstPt_res_sys,   h_pi0_KstPt_sys,        KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);

    }


    //////////////////////////////////
    /////                       //////
    ////    Fill pull histos     /////
    /////                       //////
    //////////////////////////////////

    pull(h_Bplus_ETA_DTF_res,       h_Bplus_ETA_DTF_pull,   eta_bins_B  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_ThetaK_KstPt_DTF_res_sys,h_ThetaK_KstPt_DTF_pull,KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_ThetaL_KstPt_DTF_res_sys,h_ThetaL_KstPt_DTF_pull,KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_Phi_KstPt_DTF_res_sys,   h_Phi_KstPt_DTF_pull,   KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_Kstar_ETA_DTF_res_sys,   h_Kstar_ETA_DTF_pull,   eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_Kplus_ETA_DTF_res_sys,   h_Kplus_ETA_DTF_pull,   eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_pi0_ETA_DTF_res_sys,     h_pi0_ETA_DTF_pull,     eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_pi0_PVZ_DTF_res_sys,     h_pi0_PVZ_DTF_pull,     PVZ_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
    pull(h_pi0_KstPt_DTF_res_sys,   h_pi0_KstPt_DTF_pull,   KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);

    if (!OnlyDTF){
        pull(h_Bplus_ETA_res,       h_Bplus_ETA_pull,       eta_bins_B  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_ThetaK_KstPt_res_sys,h_ThetaK_KstPt_pull,    KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_ThetaL_KstPt_res_sys,h_ThetaL_KstPt_pull,    KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_Phi_KstPt_res_sys,   h_Phi_KstPt_pull,       KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_Kstar_ETA_res_sys,   h_Kstar_ETA_pull,       eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_Kplus_ETA_res_sys,   h_Kplus_ETA_pull,       eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_pi0_ETA_res_sys,     h_pi0_ETA_pull,         eta_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_pi0_PVZ_res_sys,     h_pi0_PVZ_pull,         PVZ_bins    ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);
        pull(h_pi0_KstPt_res_sys,   h_pi0_KstPt_pull,       KstPt_bins  ,preselected, year, magnet, MC, ReferenceChannel, PHSP, truthMatched);

    }


    //////////////////////////////////
    /////                       //////
    ////    Save all histos      /////
    /////                       //////
    //////////////////////////////////

    h_Bplus_ETA_DTF     ->Write();
    h_Bplus_ETA_TRUE    ->Write();
    h_Bplus_ETA_DTF_ref ->Write();
    h_Bplus_ETA_DTF_rec_MC  ->Write();
    design_corr(h_Bplus_ETA_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_comparison(h_Bplus_ETA_DTF,h_Bplus_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    if (!OnlyDTF){
        h_Bplus_ETA         ->Write();
        h_Bplus_ETA_ref     ->Write();
        h_Bplus_ETA_rec_MC      ->Write();
        design_corr(h_Bplus_ETA_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_Bplus_ETA,h_Bplus_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    }

    //--------------------------
    h_ThetaK_DTF     ->Write();
    h_ThetaK_TRUE    ->Write();
    h_ThetaK_KstPt_DTF_ref ->Write();
    h_ThetaK_DTF_rec_MC  ->Write();
    design_corr(h_ThetaK_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_comparison(h_ThetaK_DTF,h_ThetaK_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);

     // if (!OnlyDTF){
        h_ThetaK         ->Write();
        h_ThetaK_KstPt_ref     ->Write();
        h_ThetaK_rec_MC      ->Write();
        design_corr(h_ThetaK_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_ThetaK,h_ThetaK_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
     // }

    //--------------------------
    h_ThetaL_DTF     ->Write();
    h_ThetaL_TRUE    ->Write();
    h_ThetaL_KstPt_DTF_ref ->Write();
    h_ThetaL_DTF_rec_MC  ->Write();
    design_corr(h_ThetaL_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_comparison(h_ThetaL_DTF,h_ThetaL_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);

     // if (!OnlyDTF){
        h_ThetaL         ->Write();
        h_ThetaL_KstPt_ref     ->Write();
        h_ThetaL_rec_MC      ->Write();
        design_corr(h_ThetaL_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_ThetaL,h_ThetaL_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    //  }
    //--------------------------

    h_Phi_DTF           ->Write();
    h_Phi_TRUE          ->Write();
    h_Phi_KstPt_DTF_ref ->Write();
    h_Phi_DTF_rec_MC    ->Write();
    design_corr(h_Phi_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_comparison(h_Phi_DTF,h_Phi_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);

   // if (!OnlyDTF){
        h_Phi         ->Write();
        h_Phi_KstPt_ref     ->Write();
        h_Phi_rec_MC      ->Write();
        design_corr(h_Phi_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_Phi,h_Phi_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    //  }
    //--------------------------

    h_Kstar_ETA_DTF         ->Write();
    h_Kstar_ETA_TRUE        ->Write();
    h_Kstar_ETA_DTF_ref     ->Write();
    h_Kstar_ETA_DTF_rec_MC  ->Write();
    design_corr(h_Kstar_ETA_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_comparison(h_Kstar_ETA_DTF,h_Kstar_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);

    if (!OnlyDTF){
        h_Kstar_ETA         ->Write();
        h_Kstar_ETA_ref     ->Write();
        h_Kstar_ETA_rec_MC  ->Write();
        design_corr(h_Kstar_ETA_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_Kstar_ETA,h_Kstar_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    }
    //--------------------------

    h_PVZ_TRUE      ->Write();
    h_PVZ           ->Write();
    h_PVZ_rec_MC    ->Write();
    h_PVZ_ref       ->Write();

    design_corr(h_PVZ_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_diff(h_PVZ_ref, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
    design_comparison(h_PVZ,h_PVZ_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);


   //--------------------------

    if (Kst2Kpluspi0Resolved){
        h_Kplus_ETA_DTF  ->Write();
        h_Kplus_ETA_TRUE ->Write();
        h_Kplus_ETA_DTF_ref     ->Write();
        h_Kplus_ETA_DTF_rec_MC  ->Write();
        design_corr(h_Kplus_ETA_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_Kplus_ETA_DTF,h_Kplus_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        if (!OnlyDTF){
            h_Kplus_ETA         ->Write();
            h_Kplus_ETA_ref     ->Write();
            h_Kplus_ETA_rec_MC  ->Write();
            design_corr(h_Kplus_ETA_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
            design_comparison(h_Kplus_ETA,h_Kplus_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        }
        //--------------------------

        h_pi0_ETA_DTF       ->Write();
        h_pi0_ETA_TRUE      ->Write();
        h_pi0_ETA_DTF_ref   ->Write();
        h_pi0_PVZ_ref       ->Write();
        h_pi0_ETA_DTF_rec_MC->Write();
        design_corr(h_pi0_ETA_DTF_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        design_comparison(h_pi0_ETA_DTF,h_pi0_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);

        if (!OnlyDTF){
            h_pi0_ETA           ->Write();
            h_pi0_ETA_ref       ->Write();
            h_pi0_ETA_rec_MC    ->Write();
            design_corr(h_pi0_ETA_rec_MC, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
            design_comparison(h_pi0_ETA,h_pi0_ETA_TRUE, preselected, year, magnet,  MC, ReferenceChannel, PHSP, truthMatched);
        }

    }
    //--------------------------

    output->Close();


    return 1;
}

int ResHistAllMC(Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if(Run != 1 && Run != 2){
        std::cout << "[ERROR]\t\tInvalid Run number given: " << Run  << ". Exit program!" << std::endl;
        return 0;
    }

    if ( ResHist((Run == 1 ? "2011" : "2015"),"down", true, false, false, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2011" : "2015"),"up"  , true, false, false, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2012" : "2016"),"down", true, false, false, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2012" : "2016"),"up"  , true, false, false, preselected, truthMatched) == 0) return 0;
    return 1;

}
int ResHistAllRefChannel(Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if(Run != 1 && Run != 2){
        std::cout << "[ERROR]\t\tInvalid Run number given: " << Run  << ". Exit program!" << std::endl;
        return 0;
    }
    if ( ResHist((Run == 1 ? "2011" : "2015"),"down", true, true, false, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2011" : "2015"),"up"  , true, true, false, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2012" : "2016"),"down", true, true, false, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2012" : "2016"),"up"  , true, true, false, preselected, truthMatched) == 0) return 0;
    return 1;

}
int ResHistAllPHSP(Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if(Run != 1 && Run != 2){
        std::cout << "[ERROR]\t\tInvalid Run number given: " << Run  << ". Exit program!" << std::endl;
        return 0;
    }
    if ( ResHist((Run == 1 ? "2011" : "2015"),"down", true, false, true, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2011" : "2015"),"up"  , true, false, true, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2012" : "2016"),"down", true, false, true, preselected, truthMatched) == 0) return 0;
    if ( ResHist((Run == 1 ? "2012" : "2016"),"up"  , true, false, true, preselected, truthMatched) == 0) return 0;
    return 1;

}
int ResHistAll( Int_t Run = 1, bool preselected = true, bool truthMatched = false) {

    if (ResHistAllMC          (Run, preselected, truthMatched)==0) return 0;
    if (ResHistAllRefChannel  (Run, preselected, truthMatched)==0) return 0;
    if (ResHistAllPHSP        (Run, preselected, truthMatched)==0) return 0;
    return 1;
}
int ResHistComplete( Int_t Run = 1){
    if (ResHistAllMC          (Run, 0, 0)==0) return 0;
    if (ResHistAllMC          (Run, 1, 0)==0) return 0;
    if (ResHistAllMC          (Run, 1, 1)==0) return 0;
    if (ResHistAllRefChannel  (Run, 0, 0)==0) return 0;
    if (ResHistAllRefChannel  (Run, 1, 0)==0) return 0;
    if (ResHistAllRefChannel  (Run, 1, 1)==0) return 0;
    if (ResHistAllPHSP        (Run, 0, 0)==0) return 0;
    if (ResHistAllPHSP        (Run, 1, 0)==0) return 0;
    if (ResHistAllPHSP        (Run, 1, 1)==0) return 0;
    return 1;
}
