{
"cells": [
{
"cell_type": "markdown",
"id": "077c334a",
"metadata": {},
"source": [
"# df009_FromScratchVSTTree\n",
"Compare creation of a ROOT dataset with RDataFrame and TTree.\n",
"\n",
"This tutorial illustrates how much simpler it can be to use a\n",
"RDataFrame to create a dataset with respect to the usage\n",
"of the TTree interfaces.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Danilo Piparo (CERN) \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:06 PM."
]
},
{
"cell_type": "markdown",
"id": "61bd921a",
"metadata": {},
"source": [
" ##This is the classic way of creating a ROOT dataset\n",
"The steps are:\n",
"- Create a file\n",
"- Create a tree associated to the file\n",
"- Define the variables to write in the entries\n",
"- Define the branches associated to those variables\n",
"- Write the event loop to set the right value to the variables\n",
"- Call TTree::Fill to save the value of the variables\n",
"- Write the TTree\n",
"- Close the file\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "79932212",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:06:46.576485Z",
"iopub.status.busy": "2024-03-19T19:06:46.575771Z",
"iopub.status.idle": "2024-03-19T19:06:46.616885Z",
"shell.execute_reply": "2024-03-19T19:06:46.615716Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void classicWay()\n",
"{\n",
" TFile f(\"df009_FromScratchVSTTree_classic.root\", \"RECREATE\");\n",
" TTree t(\"treeName\", \"treeName\");\n",
" double b1;\n",
" int b2;\n",
" t.Branch(\"b1\", &b1);\n",
" t.Branch(\"b2\", &b2);\n",
" for (int i = 0; i < 10; ++i) {\n",
" b1 = i;\n",
" b2 = i * i;\n",
" t.Fill();\n",
" }\n",
" t.Write();\n",
" f.Close();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "c0385e37",
"metadata": {},
"source": [
" ##This is the RDF way of creating a ROOT dataset\n",
"Few lines are needed to achieve the same result.\n",
"Parallel creation of the TTree is not supported in the\n",
"classic method.\n",
"In this case the steps are:\n",
"- Create an empty RDataFrame\n",
"- If needed, define variables for the functions used to fill the branches\n",
"- Create new columns expressing their content with lambdas, functors, functions or strings\n",
"- Invoke the Snapshot action\n",
"\n",
"\n",
"Parallelism is not the only advantage. Starting from an existing dataset,\n",
"filtering it, enriching it with new columns, leaving aside some other columns, and\n",
"writing a new dataset become very easy to do.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "129a6202",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:06:46.622124Z",
"iopub.status.busy": "2024-03-19T19:06:46.621465Z",
"iopub.status.idle": "2024-03-19T19:06:46.915126Z",
"shell.execute_reply": "2024-03-19T19:06:46.913891Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_44:6:36: error: expected ';' after expression\n",
" .Define(\"b2\", \"(int) b1 * b1\") This can even be a string\n",
" ^\n",
" ;\n",
"input_line_44:6:38: error: unknown type name 'This'\n",
" .Define(\"b2\", \"(int) b1 * b1\") This can even be a string\n",
" ^\n",
"input_line_44:6:46: error: expected ';' at end of declaration\n",
" .Define(\"b2\", \"(int) b1 * b1\") This can even be a string\n",
" ^\n",
" ;\n"
]
}
],
"source": [
"%%cpp -d\n",
"void RDFWay()\n",
"{\n",
" ROOT::RDataFrame df(10);\n",
" auto b = 0.;\n",
" df.Define(\"b1\", [&b]() { return b++; })\n",
" .Define(\"b2\", \"(int) b1 * b1\") This can even be a string\n",
" .Snapshot(\"treeName\", \"df009_FromScratchVSTTree_df.root\");\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "20df6ba6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2024-03-19T19:06:46.927176Z",
"iopub.status.busy": "2024-03-19T19:06:46.926796Z",
"iopub.status.idle": "2024-03-19T19:06:47.940584Z",
"shell.execute_reply": "2024-03-19T19:06:47.939446Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"In module 'ROOTDataFrame':\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: error: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::RDefine' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/usr/include/c++/9/ext/new_allocator.h:146:23: note: used here\n",
" { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }\n",
" ^\n",
"In module 'ROOTDataFrame':\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:109:4: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::~RDefine' has internal linkage but is not defined [-Wundefined-internal]\n",
" ~RDefine() { fLoopManager->Deregister(this); }\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:111:9: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::InitSlot' has internal linkage but is not defined [-Wundefined-internal]\n",
" void InitSlot(TTreeReader *r, unsigned int slot) final\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:119:10: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::GetValuePtr' has internal linkage but is not defined [-Wundefined-internal]\n",
" void *GetValuePtr(unsigned int slot) final\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:136:26: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::GetTypeId' has internal linkage but is not defined [-Wundefined-internal]\n",
" const std::type_info &GetTypeId() const final { return typeid(ret_type); }\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:125:9: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::Update' has internal linkage but is not defined [-Wundefined-internal]\n",
" void Update(unsigned int slot, Long64_t entry) final\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:134:9: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::Update' has internal linkage but is not defined [-Wundefined-internal]\n",
" void Update(unsigned int /*slot*/, const ROOT::RDF::RSampleInfo &/*id*/) final {}\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:139:9: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::FinalizeSlot' has internal linkage but is not defined [-Wundefined-internal]\n",
" void FinalizeSlot(unsigned int slot) final\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:148:9: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::MakeVariations' has internal linkage but is not defined [-Wundefined-internal]\n",
" void MakeVariations(const std::vector &variations) final\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:168:17: warning: function 'ROOT::Detail::RDF::RDefine<(lambda), ROOT::Detail::RDF::ExtraArgsForDefine::None>::GetVariedDefine' has internal linkage but is not defined [-Wundefined-internal]\n",
" RDefineBase &GetVariedDefine(const std::string &variationName) final\n",
" ^\n",
"/home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/include/ROOT/RDF/RDefine.hxx:98:4: note: used here\n",
" RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,\n",
" ^\n"
]
}
],
"source": [
"classicWay();\n",
"RDFWay();"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}