{
"cells": [
{
"cell_type": "markdown",
"id": "86368e9d",
"metadata": {},
"source": [
"# mathcoreVectorFloatIO\n",
"Macro illustrating I/O with Lorentz Vectors of floats.\n",
"\n",
"The dictionary for LorentzVector of float is not in the libMathCore, therefore\n",
"is generated when parsed the file with CLING.\n",
"\n",
" To run this macro you must do\n",
"\n",
"```cpp\n",
"root[0] .L mathcoreVectorFloatIO.C+\n",
"root[1] runIt();\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": "493d032b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:55.244361Z",
"iopub.status.busy": "2026-05-19T20:25:55.244248Z",
"iopub.status.idle": "2026-05-19T20:25:55.255749Z",
"shell.execute_reply": "2026-05-19T20:25:55.255233Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"TRandom.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",
"\n",
"\n",
"using namespace ROOT::Math;"
]
},
{
"cell_type": "markdown",
"id": "a5a555c3",
"metadata": {},
"source": [
" Now the dictionary contains the vector's with float types\n",
"No need to force dictionary generation\n",
"You need to run ACLIC with old ROOT version\n",
"and uncomment these lines below\n",
"fdef __ROOTCLING__\n",
"ragma link C++ class ROOT::Math::PxPyPzE4D+;\n",
"ragma link C++ class ROOT::Math::LorentzVector >+;\n",
"ragma link C++ typedef ROOT::Math::XYZTVectorF;\n",
"ndif\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "adfb6098",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:55.257035Z",
"iopub.status.busy": "2026-05-19T20:25:55.256918Z",
"iopub.status.idle": "2026-05-19T20:25:55.279457Z",
"shell.execute_reply": "2026-05-19T20:25:55.278925Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void write(int n) {\n",
"\n",
" TRandom R;\n",
" TStopwatch timer;\n",
"\n",
" TFile f1(\"mathcoreVectorIO_F.root\",\"RECREATE\");\n",
"\n",
" // create tree\n",
" TTree t1(\"t1\",\"Tree with new Float LorentzVector\");\n",
"\n",
" XYZTVectorF *v1 = new XYZTVectorF();\n",
" t1.Branch(\"LV branch\",\"ROOT::Math::XYZTVectorF\",&v1);\n",
"\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 Float Vector \" << timer.RealTime() << \" \" << timer.CpuTime() << std::endl;\n",
" t1.Print();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1c6623af",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8020d2e2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:55.280755Z",
"iopub.status.busy": "2026-05-19T20:25:55.280642Z",
"iopub.status.idle": "2026-05-19T20:25:55.289935Z",
"shell.execute_reply": "2026-05-19T20:25:55.289425Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void read() {\n",
"\n",
" TRandom R;\n",
" TStopwatch timer;\n",
"\n",
" TFile f1(\"mathcoreVectorIO_F.root\");\n",
"\n",
" // create tree\n",
" TTree *t1 = (TTree*)f1.Get(\"t1\");\n",
"\n",
" XYZTVectorF *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->E();\n",
" }\n",
"\n",
" timer.Stop();\n",
" std::cout << \" Time for new Float Vector \" << timer.RealTime() << \" \" << timer.CpuTime() << std::endl;\n",
" std::cout << \" E average\" << n<< \" \" << etot << \" \" << etot/double(n) << endl;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "5f441425",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "059c6bfd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:55.291187Z",
"iopub.status.busy": "2026-05-19T20:25:55.291074Z",
"iopub.status.idle": "2026-05-19T20:25:55.295323Z",
"shell.execute_reply": "2026-05-19T20:25:55.294854Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void runIt() {\n",
"\n",
"#if defined(__CLING__) && !defined(__ROOTCLING__)\n",
" gSystem->Load(\"libMathCore\");\n",
" gSystem->Load(\"libPhysics\");\n",
" using namespace ROOT::Math ;\n",
"\n",
" cout << \"This tutorial can run only using ACliC, you must run it by doing: \" << endl;\n",
" cout << \"\\t .L tutorials/math/mathcoreVectorFloatIO.C+\" << endl;\n",
" cout << \"\\t runIt()\" << endl;\n",
"#endif\n",
" int nEvents = 100000;\n",
" write(nEvents);\n",
" read();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "71675f8c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:25:55.296488Z",
"iopub.status.busy": "2026-05-19T20:25:55.296375Z",
"iopub.status.idle": "2026-05-19T20:25:55.609948Z",
"shell.execute_reply": "2026-05-19T20:25:55.609382Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This tutorial can run only using ACliC, you must run it by doing: \n",
"\t .L tutorials/math/mathcoreVectorFloatIO.C+\n",
"\t runIt()\n"
]
}
],
"source": [
"#if defined(__CLING__) && !defined(__ROOTCLING__)\n",
"gSystem->Load(\"libMathCore\");\n",
"gSystem->Load(\"libPhysics\");\n",
"using namespace ROOT::Math ;\n",
"\n",
"cout << \"This tutorial can run only using ACliC, you must run it by doing: \" << endl;\n",
"cout << \"\\t .L tutorials/math/mathcoreVectorFloatIO.C+\" << endl;\n",
"cout << \"\\t runIt()\" << endl;\n",
"\n",
"#endif"
]
}
],
"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
}