Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-10-15 08:20:49

0001 #ifndef ROOFIT_IMPORT_TTREE_H
0002 #define ROOFIT_IMPORT_TTREE_H
0003 
0004 // auto-creates RooRealVar of appropriate size to encompass all of the data
0005 RooRealVar make_var(const std::string& name, const std::string& title, TTree* tree)
0006 {
0007   double minval = tree->GetMinimum(name.c_str());
0008   double maxval = tree->GetMaximum(name.c_str());
0009   std::cout << "creating RooRealVar " << name << " (" << title << "), min " << minval << " max " << maxval << std::endl;
0010   return RooRealVar(name.c_str(),title.c_str(),minval,maxval);
0011 }
0012 
0013 // imports entire TTree into a RooDataSet, with all RooRealVars sized to accommodate ranges of all branch variables
0014 // this usually crashes for large TTrees, so use more selective methods if at all possible
0015 RooDataSet* import_full_TTree(const std::string& name, const std::string& title, TTree* tree, const std::string& cut)
0016 {
0017   RooArgList branches;
0018 
0019   TObjArray* branchlist = tree->GetListOfBranches();
0020   size_t nbranches = tree->GetNbranches();
0021   for(size_t i=0; i<nbranches; i++)
0022   {
0023     TBranch* branch = (TBranch*)branchlist->At(i);
0024     branches.add(make_var(branch->GetName(),branch->GetTitle(),tree));
0025   }
0026 
0027   return new RooDataSet(name,title,branches,RooFit::Import(*tree),RooFit::Cut(cut.c_str()));
0028 }
0029 
0030 // imports selected branch names of TTree into a RooDataSet, with all RooRealVArs sized to accommodate ranges of corresponding branches
0031 // include both the differential variables and the cut variables in the list of branches needed, else errors will happen
0032 RooDataSet* import_TTree(const std::string& name, const std::string& title, const std::vector<std::string> branchlist, TTree* tree, const std::string& cut)
0033 {
0034   RooArgList branches;
0035 
0036   for(const std::string& branchname : branchlist)
0037   {
0038     branches.add(make_var(branchname.c_str(),branchname.c_str(),tree));
0039   }
0040 
0041   return new RooDataSet(name,title,branches,RooFit::Import(*tree),RooFit::Cut(cut.c_str()));
0042 }
0043 
0044 #endif