{ "cells": [ { "cell_type": "markdown", "id": "0de85b9b", "metadata": {}, "source": [ "# mathcoreVectorIO\n", "Example of I/O of a GenVector Lorentz Vectors in a Tree and comparison with legacy TLorentzVector.\n", "\n", "A ROOT tree is written and read in both using either a XYZTVector or a TLorentzVector.\n", "\n", " To execute the macro type in:\n", "\n", "```cpp\n", "root[0] .x mathcoreVectorIO.C\n", "```\n", "\n", "\n", "\n", "\n", "**Author:** Lorenzo Moneta \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:25 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "7875b9d8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:57.586379Z", "iopub.status.busy": "2026-05-19T20:25:57.586261Z", "iopub.status.idle": "2026-05-19T20:25:57.597915Z", "shell.execute_reply": "2026-05-19T20:25:57.597351Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TRandom2.h\"\n", "#include \"TStopwatch.h\"\n", "#include \"TSystem.h\"\n", "#include \"TFile.h\"\n", "#include \"TTree.h\"\n", "#include \"TH1D.h\"\n", "#include \"TCanvas.h\"\n", "\n", "#include \n", "\n", "#include \"TLorentzVector.h\"\n", "\n", "#include \"Math/Vector4D.h\"\n", "\n", "using namespace ROOT::Math;" ] }, { "cell_type": "markdown", "id": "f6d92e1a", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "9e2072e6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:57.599234Z", "iopub.status.busy": "2026-05-19T20:25:57.599119Z", "iopub.status.idle": "2026-05-19T20:25:57.630334Z", "shell.execute_reply": "2026-05-19T20:25:57.629651Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void write(int n) {\n", " TRandom2 R;\n", " TStopwatch timer;\n", "\n", "\n", " R.SetSeed(1);\n", " timer.Start();\n", " double s = 0;\n", " for (int i = 0; i < n; ++i) {\n", " s += R.Gaus(0,10);\n", " s += R.Gaus(0,10);\n", " s += R.Gaus(0,10);\n", " s += R.Gaus(100,10);\n", " }\n", "\n", " timer.Stop();\n", " std::cout << s/double(n) << std::endl;\n", " std::cout << \" Time for Random gen \" << timer.RealTime() << \" \" << timer.CpuTime() << std::endl;\n", "\n", "\n", " TFile f1(\"mathcoreVectorIO_1.root\",\"RECREATE\");\n", "\n", " // create tree\n", " TTree t1(\"t1\",\"Tree with new LorentzVector\");\n", "\n", " XYZTVector *v1 = new XYZTVector();\n", " t1.Branch(\"LV branch\",\"ROOT::Math::XYZTVector\",&v1);\n", "\n", " R.SetSeed(1);\n", " timer.Start();\n", " for (int i = 0; i < n; ++i) {\n", " double Px = R.Gaus(0,10);\n", " double Py = R.Gaus(0,10);\n", " double Pz = R.Gaus(0,10);\n", " double E = R.Gaus(100,10);\n", " v1->SetCoordinates(Px,Py,Pz,E);\n", " t1.Fill();\n", " }\n", "\n", " f1.Write();\n", " timer.Stop();\n", " std::cout << \" Time for new Vector \" << timer.RealTime() << \" \" << timer.CpuTime() << std::endl;\n", "\n", " t1.Print();\n", "\n", " // create tree with old LV\n", "\n", " TFile f2(\"mathcoreVectorIO_2.root\",\"RECREATE\");\n", " TTree t2(\"t2\",\"Tree with TLorentzVector\");\n", "\n", " TLorentzVector * v2 = new TLorentzVector();\n", " TLorentzVector::Class()->IgnoreTObjectStreamer();\n", " TVector3::Class()->IgnoreTObjectStreamer();\n", "\n", " t2.Branch(\"TLV branch\",\"TLorentzVector\",&v2,16000,2);\n", "\n", " R.SetSeed(1);\n", " timer.Start();\n", " for (int i = 0; i < n; ++i) {\n", " double Px = R.Gaus(0,10);\n", " double Py = R.Gaus(0,10);\n", " double Pz = R.Gaus(0,10);\n", " double E = R.Gaus(100,10);\n", " v2->SetPxPyPzE(Px,Py,Pz,E);\n", " t2.Fill();\n", " }\n", "\n", " f2.Write();\n", " timer.Stop();\n", " std::cout << \" Time for old Vector \" << timer.RealTime() << \" \" << timer.CpuTime() << endl;\n", " t2.Print();\n", "}" ] }, { "cell_type": "markdown", "id": "20581fcb", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "bbe515b6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:57.631718Z", "iopub.status.busy": "2026-05-19T20:25:57.631567Z", "iopub.status.idle": "2026-05-19T20:25:57.646882Z", "shell.execute_reply": "2026-05-19T20:25:57.646226Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void read() {\n", "\n", " TRandom R;\n", " TStopwatch timer;\n", "\n", " TFile f1(\"mathcoreVectorIO_1.root\");\n", "\n", " // create tree\n", " TTree *t1 = (TTree*)f1.Get(\"t1\");\n", "\n", " XYZTVector *v1 = 0;\n", " t1->SetBranchAddress(\"LV branch\",&v1);\n", "\n", " timer.Start();\n", " int n = (int) t1->GetEntries();\n", " std::cout << \" Tree Entries \" << n << std::endl;\n", " double etot=0;\n", " for (int i = 0; i < n; ++i) {\n", " t1->GetEntry(i);\n", " etot += v1->Px();\n", " etot += v1->Py();\n", " etot += v1->Pz();\n", " etot += v1->E();\n", " }\n", " timer.Stop();\n", " std::cout << \" Time for new Vector \" << timer.RealTime() << \" \" << timer.CpuTime() << std::endl;\n", "\n", " std::cout << \" TOT average : n = \" << n << \"\\t \" << etot/double(n) << endl;\n", "\n", " // create tree with old LV\n", " TFile f2(\"mathcoreVectorIO_2.root\");\n", " TTree *t2 = (TTree*)f2.Get(\"t2\");\n", "\n", " TLorentzVector * v2 = 0;\n", " t2->SetBranchAddress(\"TLV branch\",&v2);\n", "\n", " timer.Start();\n", " n = (int) t2->GetEntries();\n", " std::cout << \" Tree Entries \" << n << std::endl;\n", " etot = 0;\n", " for (int i = 0; i < n; ++i) {\n", " t2->GetEntry(i);\n", " etot += v2->Px();\n", " etot += v2->Py();\n", " etot += v2->Pz();\n", " etot += v2->E();\n", " }\n", "\n", " timer.Stop();\n", " std::cout << \" Time for old Vector \" << timer.RealTime() << \" \" << timer.CpuTime() << endl;\n", " std::cout << \" TOT average:\\t\" << etot/double(n) << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "6b8cef41", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:25:57.648164Z", "iopub.status.busy": "2026-05-19T20:25:57.648037Z", "iopub.status.idle": "2026-05-19T20:25:58.267539Z", "shell.execute_reply": "2026-05-19T20:25:58.267169Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "99.8767\n", " Time for Random gen 0.00770187 0.01\n", " Time for new Vector 0.102948 0.1\n", "******************************************************************************\n", "*Tree :t1 : Tree with new LorentzVector *\n", "*Entries : 100000 : Total = 3214176 bytes File Size = 2884578 *\n", "* : : Tree compression factor = 1.11 *\n", "******************************************************************************\n", "*Branch :LV branch *\n", "*Entries : 100000 : BranchElement (see below) *\n", "*............................................................................*\n", "*Br 0 :fCoordinates : *\n", "*Entries : 100000 : Total Size= 4720 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 1 :fCoordinates.fX : Double_t *\n", "*Entries : 100000 : Total Size= 803057 bytes File Size = 726683 *\n", "*Baskets : 26 : Basket Size= 32000 bytes Compression= 1.10 *\n", "*............................................................................*\n", "*Br 2 :fCoordinates.fY : Double_t *\n", "*Entries : 100000 : Total Size= 803057 bytes File Size = 727173 *\n", "*Baskets : 26 : Basket Size= 32000 bytes Compression= 1.10 *\n", "*............................................................................*\n", "*Br 3 :fCoordinates.fZ : Double_t *\n", "*Entries : 100000 : Total Size= 803057 bytes File Size = 727013 *\n", "*Baskets : 26 : Basket Size= 32000 bytes Compression= 1.10 *\n", "*............................................................................*\n", "*Br 4 :fCoordinates.fT : Double_t *\n", "*Entries : 100000 : Total Size= 803057 bytes File Size = 702081 *\n", "*Baskets : 26 : Basket Size= 32000 bytes Compression= 1.14 *\n", "*............................................................................*\n", " Time for old Vector 0.075057 0.07\n", "******************************************************************************\n", "*Tree :t2 : Tree with TLorentzVector *\n", "*Entries : 100000 : Total = 4835755 bytes File Size = 3437842 *\n", "* : : Tree compression factor = 1.41 *\n", "******************************************************************************\n", "*Br 0 :TLV branch : TLorentzVector *\n", "*Entries : 100000 : Total Size= 4835322 bytes File Size = 3434427 *\n", "*Baskets : 327 : Basket Size= 16000 bytes Compression= 1.41 *\n", "*............................................................................*\n", " Tree Entries 100000\n", " Time for new Vector 0.0189521 0.02\n", " TOT average : n = 100000\t 99.8767\n", " Tree Entries 100000\n", " Time for old Vector 0.0201199 0.02\n", " TOT average:\t99.8767\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning in : TLorentzVector cannot be split, resetting splitlevel to 0\n" ] } ], "source": [ "int nEvents = 100000;\n", "write(nEvents);\n", "read();" ] } ], "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 }