{
"cells": [
{
"cell_type": "markdown",
"id": "3eeaaf51",
"metadata": {},
"source": [
"# tree105_tree\n",
"This example illustrates how to make a Tree from variables or arrays\n",
"in a C struct - without a dictionary, by creating the branches for\n",
"builtin types (int, float, double) and arrays explicitly.\n",
"See tree106_tree.C for the same example using a class with dictionary\n",
"instead of a C-struct.\n",
"\n",
"In this example, we are mapping a C struct to one of the Geant3\n",
"common blocks /gctrak/. In the real life, this common will be filled\n",
"by Geant3 at each step and only the Tree Fill function should be called.\n",
"The example emulates the Geant3 step routines.\n",
"\n",
"to run the example, do:\n",
"```\n",
".x tree105_tree.C to execute with the Cling interpreter\n",
".x tree105_tree.C++ to execute with native compiler\n",
"```\n",
"\n",
"\n",
"\n",
"**Author:** Rene Brun \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:17 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8b75da83",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:59.394123Z",
"iopub.status.busy": "2026-05-19T20:17:59.394003Z",
"iopub.status.idle": "2026-05-19T20:17:59.402754Z",
"shell.execute_reply": "2026-05-19T20:17:59.402289Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TFile.h\"\n",
"#include \"TTree.h\"\n",
"#include \"TH2.h\"\n",
"#include \"TRandom.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TMath.h\"\n",
"\n",
"const Int_t MAXMEC = 30;\n",
"\n",
"typedef struct {\n",
" Float_t vect[7];\n",
" Float_t getot;\n",
" Float_t gekin;\n",
" Float_t vout[7];\n",
" Int_t nmec;\n",
" Int_t lmec[MAXMEC];\n",
" Int_t namec[MAXMEC];\n",
" Int_t nstep;\n",
" Int_t pid;\n",
" Float_t destep;\n",
" Float_t destel;\n",
" Float_t safety;\n",
" Float_t sleng;\n",
" Float_t step;\n",
" Float_t snext;\n",
" Float_t sfield;\n",
" Float_t tofg;\n",
" Float_t gekrat;\n",
" Float_t upwght;\n",
"} \n",
"ctrak_t;"
]
},
{
"cell_type": "markdown",
"id": "dd858109",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cf460348",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:59.403989Z",
"iopub.status.busy": "2026-05-19T20:17:59.403875Z",
"iopub.status.idle": "2026-05-19T20:17:59.407262Z",
"shell.execute_reply": "2026-05-19T20:17:59.406791Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void helixStep(Float_t step, Float_t *vect, Float_t *vout)\n",
"{\n",
" // extrapolate track in constant field\n",
" Float_t field = 20; // magnetic field in kilogauss\n",
" enum Evect {kX, kY, kZ, kPX, kPY, kPZ, kPP};\n",
" vout[kPP] = vect[kPP];\n",
" Float_t h4 = field*2.99792e-4;\n",
" Float_t rho = -h4/vect[kPP];\n",
" Float_t tet = rho*step;\n",
" Float_t tsint = tet*tet/6;\n",
" Float_t sintt = 1 - tsint;\n",
" Float_t sint = tet*sintt;\n",
" Float_t cos1t = tet/2;\n",
" Float_t f1 = step*sintt;\n",
" Float_t f2 = step*cos1t;\n",
" Float_t f3 = step*tsint*vect[kPZ];\n",
" Float_t f4 = -tet*cos1t;\n",
" Float_t f5 = sint;\n",
" Float_t f6 = tet*cos1t*vect[kPZ];\n",
" vout[kX] = vect[kX] + (f1*vect[kPX] - f2*vect[kPY]);\n",
" vout[kY] = vect[kY] + (f1*vect[kPY] + f2*vect[kPX]);\n",
" vout[kZ] = vect[kZ] + (f1*vect[kPZ] + f3);\n",
" vout[kPX] = vect[kPX] + (f4*vect[kPX] - f5*vect[kPY]);\n",
" vout[kPY] = vect[kPY] + (f4*vect[kPY] + f5*vect[kPX]);\n",
" vout[kPZ] = vect[kPZ] + (f4*vect[kPZ] + f6);\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "3b396b68",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "57193103",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:59.408440Z",
"iopub.status.busy": "2026-05-19T20:17:59.408328Z",
"iopub.status.idle": "2026-05-19T20:17:59.415510Z",
"shell.execute_reply": "2026-05-19T20:17:59.415117Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_46:9:4: error: unknown type name 'Gctrak_t'\n",
" Gctrak_t gstep;\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"void tree105_write()\n",
"{\n",
" // create a Tree file tree105.root\n",
"\n",
" // create the file, the Tree and a few branches with\n",
" // a subset of gctrak\n",
" TFile f(\"tree105.root\", \"recreate\");\n",
" TTree t2(\"t2\", \"a Tree with data from a fake Geant3\");\n",
" Gctrak_t gstep;\n",
" t2.Branch(\"vect\", gstep.vect, \"vect[7]/F\");\n",
" t2.Branch(\"getot\", &gstep.getot);\n",
" t2.Branch(\"gekin\", &gstep.gekin);\n",
" t2.Branch(\"nmec\", &gstep.nmec);\n",
" t2.Branch(\"lmec\", gstep.lmec, \"lmec[nmec]/I\");\n",
" t2.Branch(\"destep\", &gstep.destep);\n",
" t2.Branch(\"pid\", &gstep.pid);\n",
"\n",
" // Initialize particle parameters at first point\n",
" Float_t px, py, pz, p, charge = 0;\n",
" Float_t vout[7];\n",
" Float_t mass = 0.137;\n",
" Bool_t newParticle = kTRUE;\n",
" gstep.step = 0.1;\n",
" gstep.destep = 0;\n",
" gstep.nmec = 0;\n",
" gstep.pid = 0;\n",
"\n",
" // transport particles\n",
" for (Int_t i=0; i<10000; i++) {\n",
" // generate a new particle if necessary\n",
" if (newParticle) {\n",
" px = gRandom->Gaus(0, .02);\n",
" py = gRandom->Gaus(0, .02);\n",
" pz = gRandom->Gaus(0, .02);\n",
" p = TMath::Sqrt(px * px + py *py + pz * pz);\n",
" charge = 1;\n",
" if (gRandom->Rndm() < 0.5)\n",
" charge = -1;\n",
" gstep.pid += 1;\n",
" gstep.vect[0] = 0;\n",
" gstep.vect[1] = 0;\n",
" gstep.vect[2] = 0;\n",
" gstep.vect[3] = px / p;\n",
" gstep.vect[4] = py / p;\n",
" gstep.vect[5] = pz / p;\n",
" gstep.vect[6] = p * charge;\n",
" gstep.getot = TMath::Sqrt(p * p + mass * mass);\n",
" gstep.gekin = gstep.getot - mass;\n",
" newParticle = kFALSE;\n",
" }\n",
"\n",
" // fill the Tree with current step parameters\n",
" t2.Fill();\n",
"\n",
" // transport particle in magnetic field\n",
" helixStep(gstep.step, gstep.vect, vout); // make one step\n",
"\n",
" // apply energy loss\n",
" gstep.destep = gstep.step*gRandom->Gaus(0.0002, 0.00001);\n",
" gstep.gekin -= gstep.destep;\n",
" gstep.getot = gstep.gekin + mass;\n",
" gstep.vect[6] = charge*TMath::Sqrt(gstep.getot * gstep.getot - mass * mass);\n",
" gstep.vect[0] = vout[0];\n",
" gstep.vect[1] = vout[1];\n",
" gstep.vect[2] = vout[2];\n",
" gstep.vect[3] = vout[3];\n",
" gstep.vect[4] = vout[4];\n",
" gstep.vect[5] = vout[5];\n",
" gstep.nmec = (Int_t)(5 * gRandom->Rndm());\n",
" for (Int_t l=0; l 30)\n",
" newParticle = kTRUE;\n",
" }\n",
" // save the Tree header. The file will be automatically closed\n",
" // when going out of the function scope\n",
" t2.Write();\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "6855c0ac",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f2c8ccbd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:59.416697Z",
"iopub.status.busy": "2026-05-19T20:17:59.416565Z",
"iopub.status.idle": "2026-05-19T20:17:59.431829Z",
"shell.execute_reply": "2026-05-19T20:17:59.431300Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void tree105_read()\n",
"{\n",
" // read the Tree generated by tree105_write and fill one histogram\n",
" // we are only interested by the destep branch.\n",
"\n",
" // note that we create the TFile and TTree objects on the heap\n",
" // because we want to keep these objects alive when we leave\n",
" // this function.\n",
" auto f = TFile::Open(\"tree105.root\");\n",
" auto t2 = f->Get(\"t2\");\n",
" static Float_t destep;\n",
" TBranch *b_destep = t2->GetBranch(\"destep\");\n",
" b_destep->SetAddress(&destep);\n",
"\n",
" // create one histogram\n",
" auto hdestep = new TH1F(\"hdestep\", \"destep in Mev\", 100, 1e-5, 3e-5);\n",
"\n",
" // read only the destep branch for all entries\n",
" Long64_t nentries = t2->GetEntries();\n",
" for (Long64_t i=0; iGetEntry(i);\n",
" hdestep->Fill(destep);\n",
" }\n",
"\n",
" // we do not close the file\n",
" // we want to keep the generated histograms\n",
" // we fill a 3-d scatter plot with the particle step coordinates\n",
" auto c1 = new TCanvas(\"c1\", \"c1\", 600, 800);\n",
" c1->SetFillColor(42);\n",
" c1->Divide(1, 2);\n",
" c1->cd(1);\n",
" hdestep->SetFillColor(45);\n",
" hdestep->Fit(\"gaus\");\n",
" c1->cd(2);\n",
" gPad->SetFillColor(37);\n",
" t2->SetMarkerColor(kRed);\n",
" t2->Draw(\"vect[0]:vect[1]:vect[2]\");\n",
"\n",
" // Allow to use the TTree after the end of the function.\n",
" t2->ResetBranchAddresses();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3e0e4152",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:59.432995Z",
"iopub.status.busy": "2026-05-19T20:17:59.432879Z",
"iopub.status.idle": "2026-05-19T20:17:59.764979Z",
"shell.execute_reply": "2026-05-19T20:17:59.764511Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_49:2:3: error: use of undeclared identifier 'tree105_write'\n",
" (tree105_write())\n",
" ^\n",
"Error in : Error evaluating expression (tree105_write())\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"tree105_write();\n",
"tree105_read();"
]
},
{
"cell_type": "markdown",
"id": "767e9b76",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "fb334ff9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:17:59.766422Z",
"iopub.status.busy": "2026-05-19T20:17:59.766302Z",
"iopub.status.idle": "2026-05-19T20:17:59.971462Z",
"shell.execute_reply": "2026-05-19T20:17:59.970879Z"
}
},
"outputs": [],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"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
}