# Reading a tree with two loops (and other questions)

Dear all

I’m writing a macro to do the combinatorial between two particles. I created a tree with all needed properties and now I want to read it but I have a problem in the macro within loops. The histo of invariant mass has the good number of entries but their value are wrong. (I have also a ntuple with the mass already calculated for crosscheck)

[code]TBranch *b_px = tree->GetBranch(“px”);
TBranch *b_py = tree->GetBranch(“py”);
TBranch *b_pz = tree->GetBranch(“pz”);
TBranch *b_energy = tree->GetBranch(“E”);
TBranch *b_charge = tree->GetBranch(“charge”);
TBranch *b_nMuTracks = tree->GetBranch(“nMuTracks”);

Int_t nentries = (Int_t)tree->GetEntries();

for(Int_t i=0; i<nentries; i=i+nMuTracks)
{
b_nMuTracks->GetEntry(i);

``````if (nMuTracks > 1)

{

for(Int_t j=i; j < i+nMuTracks-1; j++)
{

b_px->GetEntry(j);
b_py->GetEntry(j);
b_pz->GetEntry(j);
b_energy->GetEntry(j);
b_charge->GetEntry(j);

lvM1.SetPxPyPzE(px,py,pz,E);

charge1 = charge;

for(Int_t k=j+1; k < i+nMuTracks; k++)
{

b_px->GetEntry(k);
b_py->GetEntry(k);
b_pz->GetEntry(k);
b_energy->GetEntry(k);
b_charge->GetEntry(k);

lvM2.SetPxPyPzE(px,py,pz,E);

lvDiMu = lvM1+lvM2;
mDiMu = lvDiMu.M();

charge2 = charge;

if (charge1*charge2 == -1)
{
hInvMassPosNeg->Fill(mDiMu);
cout << mDiMu << endl;
}

}//End of the loop over the second muon

}//End of the loop over the first muon

}
``````

}//End of the loop over all muons[/code]

Hi,

When doing:```for(Int_t i=0; i<nentries; i=i+nMuTracks) { b_nMuTracks->GetEntry(i); ... for(Int_t k=j+1; k < i+nMuTracks; k++) { b_px->GetEntry(k);```
You are reading a completely different entry for the branch ‘px’ and now ‘px’ and nMuTrack are totally unrelated.

It looks like ‘px’ is an array inside each entry rather than a single value. I suggest that you study carefully at the header file mysel.h created by tree->MakeSelector(“mysel”) ; this will indicate to you how to declare the variable and how to set the addresses (and you may also actually use the file mysel.C and mysel.h to implement you processing ; see TTree::Process).

Cheers,
philippe.

But the important point is the lorentz vector. After build it with SetPxPyPzE(), there is no problem, I think.

A other question, I’m creating a class using the Event class example, for the track class, there is a line that I don’t understand:

Track *track = new(tracks[fNtrack++]) Track(random);

what is the meaning of Track(random); ? Is it useful for real event ?

[quote]But the important point is the lorentz vector. After build it with SetPxPyPzE(), there is no problem, I think.[/quote]Well the code using the lorentz vector looks fine but the data your put in is definitively wrong.

[quote]what is the meaning of Track(random);;[/quote]This is the way to ‘create/initialize’ the Track object. Indeed passing a random number to it is unlikely to be useful for a real event.

Philippe.

Thanks for your answer. I have written the class and I want to compile it. I modified the makefile in \$ROOTSYS/test in order to compile only my class EventMuon but I did a mistake. Here is the code of the modified makfile

``````include Makefile.arch
-include ../MyConfig.mk

#------------------------------------------------------------------------------

EVENTO        = EventMuon.\$(ObjSuf) EventMuonDict.\$(ObjSuf)
EVENTS        = EventMuon.\$(SrcSuf) EventMuonDict.\$(SrcSuf)
EVENTSO       = libEventMuon.\$(DllSuf)
EVENT         = EventMuon\$(ExeSuf)
ifeq (\$(PLATFORM),win32)
EVENTLIB      = libEventMuon.lib
else
EVENTLIB      = \$(shell pwd)/\$(EVENTSO)
endif

OBJS          = \$(EVENTO)

PROGRAMS      = \$(EVENT)
#------------------------------------------------------------------------------

.SUFFIXES: .\$(SrcSuf) .\$(ObjSuf) .\$(DllSuf)

all:            \$(PROGRAMS)

\$(EVENTSO):     \$(EVENTO)
ifeq (\$(ARCH),aix)
/usr/ibmcxx/bin/makeC++SharedLib \$(OutPutOpt) \$@ \$(LIBS) -p 0 \$^
else
ifeq (\$(ARCH),aix5)
/usr/vacpp/bin/makeC++SharedLib \$(OutPutOpt) \$@ \$(LIBS) -p 0 \$^
else
ifeq (\$(PLATFORM),macosx)
# We need to make both the .dylib and the .so
\$(LD) \$(SOFLAGS)\$@ \$(LDFLAGS) \$^ \$(OutPutOpt) \$@
ifneq (\$(subst \$(MACOSX_MINOR),,1234),1234)
ifeq (\$(MACOSX_MINOR),4)
ln -sf \$@ \$(subst .\$(DllSuf),.so,\$@)
else
\$(LD) -bundle -undefined \$(UNDEFOPT) \$(LDFLAGS) \$^ \
\$(OutPutOpt) \$(subst .\$(DllSuf),.so,\$@)
endif
endif
else
ifeq (\$(PLATFORM),win32)
bindexplib \$* \$^ > \$*.def
lib -nologo -MACHINE:IX86 \$^ -def:\$*.def \
\$(OutPutOpt)\$(EVENTLIB)
\$(LD) \$(SOFLAGS) \$(LDFLAGS) \$^ \$*.exp \$(LIBS) \
\$(OutPutOpt)\$@
\$(MT_DLL)
else
\$(LD) \$(SOFLAGS) \$(LDFLAGS) \$^ \$(OutPutOpt) \$@ \$(EXPLLINKLIBS)
endif
endif
endif
endif
@echo "\$@ done"

\$(EVENT):       \$(EVENTSO)
\$(LD) \$(LDFLAGS) \$(EVENTO) \$(LIBS) \$(OutPutOpt)\$@
\$(MT_EXE)
@echo "\$@ done"

clean:
@rm -f \$(OBJS) core

distclean:      clean
@rm -f \$(PROGRAMS) \$(EVENTSO) \$(EVENTLIB) *Dict.* *.def *.exp \
*.root *.ps *.so *.lib *.dll *.d *.log .def so_locations \
files/*
@rm -rf cxx_repository
-@cd RootShower && \$(MAKE) distclean
-@cd rhtml && \$(MAKE) distclean
-@cd RootIDE && \$(MAKE) distclean

.SUFFIXES: .\$(SrcSuf)

###

EventMuon.\$(ObjSuf): EventMuon.h

@echo "Generating dictionary \$@..."
\$(ROOTCINT) -f \$@ -c \$^

.\$(SrcSuf).\$(ObjSuf):
\$(CXX)  \$(CXXFLAGS) -c \$<
``````

The error is

No rule to make the target« EventMuonLinkDef.h » needed for « EventMuonDict.cxx ». Stop

Hi,

Philippe.

Hi

There was a error in the file, I didn’t change “Event” to “EventMuon” it’s ok now but there an other error:

```g++ -O2 -m64 EventMuon.o EventMuonDict.o -L/home/bruno/Alice/v4-19-11-AN/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -o EventMuon /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status make: *** [EventMuon] Erreur 1```

I have finally a file libEventMuon.so, so I don’t understand this error message.

Hi,

From the example, you are not linking in MainEvent.cxx

Philippe.

I don’t want MainEvent.cxx. I will use EventMuon in a tree on real data.

[quote]I don’t want MainEvent.cxx. I will use EventMuon in a tree on real data[/quote]humm okay Then what entity are you trying to build with the link line you shown? You probably do not need it and I am guessing you are attempting to build a shared library in which case, check the rules of libEvent.so

Cheers,
Philippe.

I removed

EVENT = EventMuon\$(ExeSuf)
PROGRAMS = \$(EVENT)
all: \$(PROGRAMS)

Now It seems ok.