#include #include #include #include //Root #include "TMath.h" #include "TFile.h" #include "TH1.h" #include "TH2.h" #include void MinimalTreeMacro( ) { TTree* _tree_pars = nullptr; TFile* _file_pars = nullptr; int NParams = 10; double param_values[NParams]; for (unsigned int itoy = 0; itoy < 2; itoy++) { int N = 5; bool tree_spectra_created = 1; TH1 *pdfs[N]; int pdfsDim[] = {1, 1, 2, 2, 2}; TH1D *blank1Dhist = new TH1D(); TH2D *blank2Dhist = new TH2D(); for (int ih = 0; ih < N; ih++) { pdfs[ih] = nullptr; } if (!_file_pars) { _file_pars = new TFile("ParameterFile.root", "RECREATE"); tree_spectra_created = 0; } if (!_tree_pars) { _tree_pars = new TTree("marg_parameter_values", "Input parameters used for toys"); for (int ip = 0; ip < NParams; ip++) { std::string param_name = "Parameter " + std::to_string(ip); std::string branch_info = param_name + "/D"; _tree_pars->Branch(param_name.c_str(), &(param_values[ip]), branch_info.c_str()); } } for (int ip = 0; ip < NParams; ip++) { param_values[ip] = ip; } for (unsigned int idata = 0; idata < N; idata++) { // Determine if 1D or 2D if (pdfsDim[idata] == 1) { //If 1D hist TH1D *curr_pdf = blank1Dhist; pdfs[idata] = (TH1 *) curr_pdf; } else if (pdfsDim[idata] == 2) { //If 2D hist TH2D *curr_pdf = blank2Dhist; pdfs[idata] = (TH1 *) curr_pdf; } } //Setting up branch for spectra. N.B: This is done after the object type assignment above to avoid errors in setting up TH1 branches (abstract base). if (!tree_spectra_created) { for (unsigned int imodel = 0; imodel < N; imodel++) { std::string model_name = "Spectra " + std::to_string(imodel); _tree_pars->Branch(model_name.c_str(), &(pdfs[imodel]), 32000, 0); //This is the line creating the error. } tree_spectra_created = 1; } // If this is the final toy, write the tree to a new file _tree_pars->Fill(); } _file_pars->cd(); _tree_pars->Write(); std::vector best_fit_toy; best_fit_toy = {1.1, 1.2}; const char* BestFitToy = "chi2_best_fit_toy"; const char* OutputFile = "parameter_values_with_ChiSquareDataFrame.root"; const char* BaseTTree = "marg_parameter_values"; const char* ParameterFile = "ParameterFile.root"; ROOT::RDataFrame df(BaseTTree, ParameterFile); df.Define(BestFitToy, [best_fit_toy](ULong64_t idx){ return best_fit_toy.at(idx); }, {"rdfentry_"}) .Snapshot(BaseTTree, OutputFile); }