Hi ROOTers,
I would like to read an ASCII file in which every event is stored as
ch_tag, ch_t , delta, plab1x, plab1y, plab1z, plab1e,…,plab4e
and save the events in a TTree with 7 different branches: ch1, ch2, deltaM, par1, par2, par3, par4, where par* are TLorentzVectors.
Here’s my code:
#include "Riostream.h"
void myFunc(TString decayName="4p") {
// example of macro to read data from an ascii file and
// create a root file with an histogram and an ntuple.
gROOT->Reset();
gSystem->Load("libPhysics");
ifstream in;
// we assume a file basic.dat in the current directory
// this file has 3 columns of float data
in.open(decayName+".dat");
Int_t nlines = 0;
Double_t ch_tag, ch_t, delta;
Double_t plab1x,plab1y,plab1z,plab1e;
Double_t plab2x,plab2y,plab2z,plab2e;
Double_t plab3x,plab3y,plab3z,plab3e;
Double_t plab4x,plab4y,plab4z,plab4e;
TLorentzVector *plab1 = new TLorentzVector();
TLorentzVector *plab2 = new TLorentzVector();
TLorentzVector *plab3 = new TLorentzVector();
TLorentzVector *plab4 = new TLorentzVector();
TFile *f = new TFile("test"+decayName+"s.root","RECREATE");
TTree *myTree = new TTree("myTree","data from "+decayName);
myTree->Branch("ch_tag.",&ch_tag,"ch_tag/D");
myTree->Branch("ch_t.",&ch_t,"ch_t/D");
myTree->Branch("delta.",&delta,"delta/D");
myTree->Branch("par1","TLorentzVector",&plab1,32000,0);
myTree->Branch("par2","TLorentzVector",&plab2,32000,0);
myTree->Branch("par3","TLorentzVector",&plab3,32000,0);
myTree->Branch("par4","TLorentzVector",&plab4,32000,0);
while (1) {
in >> ch_tag >> ch_t >> delta >>
plab1x >> plab1y >> plab1z >> plab1e >>
plab2x >> plab2y >> plab2z >> plab2e >>
plab3x >> plab3y >> plab3z >> plab3e >>
plab4x >> plab4y >> plab4z >> plab4e;
if (!in.good()) break;
plab1->SetPxPyPzE(plab1x,plab1y,plab1z,plab1e);
plab2->SetPxPyPzE(plab2x,plab2y,plab2z,plab2e);
plab3->SetPxPyPzE(plab3x,plab3y,plab3z,plab3e);
plab4->SetPxPyPzE(plab4x,plab4y,plab4z,plab4e);
myTree->Fill();
nlines++;
}
printf(" read %d events\n",nlines);
in.close();
f->Write();
}
in root I load the function
# .L myFunc.cc
# myFunc()
read # events
#
But when I look into the ntuple with a TBrowser, many Leaves into the par# branches are not readable, taking me to a Segmentation Violation.
Where am I wrong?