43constexpr char const* kTreeFileName =
"http://root.cern.ch/files/NanoAOD_DoubleMuon_CMS2011OpenData.root";
44constexpr char const* kNTupleFileName =
"ntpl004_dimuon.root";
47using ColNames_t = std::vector<std::string>;
52template <
typename... ColumnTypes_t>
53class RNTupleHelper :
public ROOT::Detail::RDF::RActionImpl<RNTupleHelper<ColumnTypes_t...>> {
55 using Result_t = RNTupleWriter;
57 using ColumnValues_t = std::tuple<std::shared_ptr<ColumnTypes_t>...>;
59 std::string fNTupleName;
60 std::string fRootFile;
62 ColumnValues_t fColumnValues;
63 static constexpr const auto fNColumns = std::tuple_size<ColumnValues_t>::value;
64 std::shared_ptr<RNTupleWriter> fNTuple;
67 template<std::size_t...
S>
68 void InitializeImpl(std::index_sequence<S...>) {
71 std::initializer_list<int> expander{
72 (std::get<S>(fColumnValues) = eventModel->MakeField<ColumnTypes_t>(fColNames[
S]), 0)...};
73 fNTuple = std::move(RNTupleWriter::Recreate(std::move(eventModel), fNTupleName, fRootFile));
76 template<std::size_t...
S>
77 void ExecImpl(std::index_sequence<S...>, ColumnTypes_t... values) {
80 std::initializer_list<int> expander{(*std::get<S>(fColumnValues) = values, 0)...};
85 : fNTupleName(ntupleName), fRootFile(rootFile), fColNames(colNames)
87 InitializeImpl(std::make_index_sequence<fNColumns>());
90 RNTupleHelper(RNTupleHelper&&) =
default;
91 RNTupleHelper(
const RNTupleHelper&) =
delete;
92 std::shared_ptr<RNTupleWriter> GetResultPtr()
const {
return fNTuple; }
102 void Exec(
unsigned int slot, ColumnTypes_t... values)
105 ExecImpl(std::make_index_sequence<fNColumns>(), values...);
107 if (++fCounter % 100000 == 0)
108 std::cout <<
"Wrote " << fCounter <<
" entries" << std::endl;
113 fNTuple->CommitCluster();
116 std::string GetActionName() {
return "RNTuple Writer"; }
122T InvariantMassStdVector(std::vector<T>&
pt, std::vector<T>& eta, std::vector<T>& phi, std::vector<T>& mass)
124 assert(
pt.size() == 2 && eta.size() == 2 && phi.size() == 2 && mass.size() == 2);
142 std::string typeList =
"<";
143 std::string columnList =
"{";
144 auto columnNames = df.GetColumnNames();
145 for (
auto name : columnNames) {
146 auto typeName = df.GetColumnType(
name);
148 if (typeName ==
"ULong64_t")
continue;
149 columnList +=
"\"" +
name +
"\",";
150 typeList += typeName +
",";
152 *columnList.rbegin() =
'}';
153 *typeList.rbegin() =
'>';
155 std::string code =
"{";
157 code +=
"auto df = std::make_unique<ROOT::RDataFrame>(\"Events\", \"" + std::string(kTreeFileName)
158 +
"\")->Range(0, 4000000);";
159 code +=
"ColNames_t colNames = " + columnList +
";";
160 code +=
"RNTupleHelper" + typeList +
" helper{\"Events\", \"" + std::string(kNTupleFileName) +
"\", colNames};";
161 code +=
"*df.Book" + typeList +
"(std::move(helper), colNames);";
168void ntpl004_dimuon() {
181 auto df_2mu = df.Filter(
"nMuon == 2",
"Events with exactly two muons");
182 auto df_os = df_2mu.Filter(
"Muon_charge[0] != Muon_charge[1]",
"Muons with opposite charge");
185 auto df_mass = df_os.Define(
"Dimuon_mass", InvariantMassStdVector<float>, {
"Muon_pt",
"Muon_eta",
"Muon_phi",
"Muon_mass"});
186 auto df_size = df_os.Define(
"eta_size",
"Muon_mass.size()");
189 auto h = df_mass.Histo1D({
"Dimuon_mass",
"Dimuon_mass", 30000, 0.25, 300},
"Dimuon_mass");
192 auto report = df_mass.Report();
196 auto c =
new TCanvas(
"c",
"", 800, 700);
197 c->SetLogx();
c->SetLogy();
200 h->GetXaxis()->SetTitle(
"m_{#mu#mu} (GeV)");
h->GetXaxis()->SetTitleSize(0.04);
201 h->GetYaxis()->SetTitle(
"N_{Events}");
h->GetYaxis()->SetTitleSize(0.04);
206 label.
DrawLatex(0.205, 0.775,
"#rho,#omega");
210 label.
DrawLatex(0.485, 0.700,
"Y(1,2,3S)");
213 label.
SetTextSize(0.030); label.
DrawLatex(0.630, 0.920,
"#sqrt{s} = 8 TeV, L_{int} = 11.6 fb^{-1}");
R__EXTERN TStyle * gStyle
R__EXTERN TSystem * gSystem
static std::unique_ptr< RNTupleModel > Create()
An RNTuple that is used to read data from storage.
An RNTuple that gets filled with entries (data) and writes them to storage.
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
A "std::vector"-like collection of values implementing handy operation to analyse them.
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
To draw Mathematical Formula.
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
Make a copy of this object with the new parameters And copy object attributes.
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
virtual void Print(Option_t *option="") const
Dump this text with its attributes.
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
basic_string_view< char > string_view
RDataFrame MakeNTupleDataFrame(std::string_view ntupleName, std::string_view fileName)
Vector1::Scalar InvariantMass(const Vector1 &v1, const Vector2 &v2)
return the invariant mass of two LorentzVector The only requirement on the LorentzVector is that they...
RooArgSet S(const RooAbsArg &v1)
void Initialize(Bool_t useTMVAStyle=kTRUE)