-1
import sys
import ROOT
from progressbar import Bar, Percentage, ProgressBar
from time import time
from tools import duration, check_outfile_path

ECMS = 3.686
p4shw = ROOT.vector('double')()

def main ():     
    args = sys.argv[1:]

    if (len(args) < 2):
        print 'input error'

    infile = args[0]
    outfile = args[1]
    check_outfile_path(outfile)

    fin = ROOT.TFile(infile)
    t = fin.Get('ana')
    t.SetBranchAddress("p4shw", p4shw)
    entries = t.GetEntriesFast()

    fout = ROOT.TFile(outfile, "RECREATE")
    t_out = ROOT.TTree("ana","ana")
    rec_mass_gam1 = ROOT.vector('double')()
    rec_mass_gam2 = ROOT.vector('double')()
    t_out.Branch("rec_mass_gam1", rec_mass_gam1, "rec_mass_gam1/D")
    t_out.Branch("rec_mass_gam2", rec_mass_gam2, "rec_mass_gam2/D")

    pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=entries).start()
    time_start = time()
    print("checking error 2")
    cms_p4 = ROOT.TLorentzVector(0.011*ECMS, 0, 0, ECMS)
    print 'entries=', entries
    print("checking error 3")
    for k in range(entries):

        pbar.update(k+1)

        #t.GetEntry(k)
        print("indentent error checking")
        #exit()
        p4shw_gam1 = ROOT.TLorentzVector(t.p4shw[0],t.p4shw[1],t.p4shw[2],t.p4shw[3])
        p4shw_gam2 = ROOT.TLorentzVector(t.p4shw[4],t.p4shw[5],t.p4shw[6],t.p4shw[7])
        print("checking error 4")
        p4_shw_gam1 = cms_p4 - p4shw_gam1
        p4_shw_gam2 = cms_p4 - p4shw_gam2
        rec_mass_gam1 = p4_shw_gam1.M()
        rec_mass_gam2 = p4_shw_gam2.M()
        print("rec_mass_gam1", rec_mass_gam1)
        #exit()
        t_out.Fill()
        print("checking error 5")
    t_out.Write()
    fout.Close()
    pbar.finish()
    dur = duration(time()-time_start)
    sys.stdout.write(' \nDone in %s. \n' % dur)
    print("checking error 6")

if __name__ =='__main__':
    main()
pseyfert
  • 3,263
  • 3
  • 21
  • 47
  • What's the question? – ruohola Mar 06 '19 at 02:11
  • I'm unable to fill the branches. The tree with the name "ana" is there and also the branches are there, but there is no information saved in the branches. – user8288477 Mar 06 '19 at 02:14
  • the root tag should be root-framework (I can't submit the edit since the post itself fails the mostly-code test) – pseyfert Mar 21 '19 at 10:45
  • @user8288477 you say the output tree `ana` exists, branches are there, but no "information". What does that exactly mean? Does the tree have entries (when reading something along the lines of `ana->GetEntries()`)? Do the branches have entries (`ana->GetBranch("rec_mass_gam1")->GetEntries()`)? – pseyfert Mar 21 '19 at 10:53

1 Answers1

0

When I compare your code to this example, you're using ROOT.vector rather than array. When I do this change the branch gets filled as expected

#!/bin/python

import ROOT
from array import array


# doesn't work
def test1():
    t_out = ROOT.TTree("ana", "ana")
    rec_mass_gam1 = ROOT.vector('double')()
    t_out.Branch("rec_mass_gam1", rec_mass_gam1, "rec_mass_gam1/D")
    rec_mass_gam1 = 1337.
    t_out.Fill()
    t_out.Draw("rec_mass_gam1")


# works
def test2():
    t_out = ROOT.TTree("ana", "ana")
    rec_mass_gam1 = array('f', [0.])
    t_out.Branch("rec_mass_gam1", rec_mass_gam1, "rec_mass_gam1/F")
    rec_mass_gam1[0] = 1337.
    t_out.Fill()
    t_out.Draw("rec_mass_gam1")

When I run test1, I see that the tree and branch got filled, just not with the value I wanted. In the second example the desired value gets filled.

Now looking closer at what happens there's anyway a mistake in your script:

python doesn't treat rec_mass_gam1 = p4_shw_gam1.M() as "set the value of the vector-variable rec_mass_gam1 to the number that drops out of the M() method. It rather creates a new float-variable with the name rec_mass_gam1 and the original vector variable (which is used by the branch) remains unchanged.

I have to admit I don't know if there is a way to fill the branch with vector as well.

pseyfert
  • 3,263
  • 3
  • 21
  • 47
  • I'm sorry but this is an old question. I wanted to save "double type" information in a vector, which was totally inappropriate. I have changed it and saved the information into an array[] and it was correct then. Thank you for your feedback. – user8288477 Mar 22 '19 at 07:52