Problem with fill RooDataSet with data from an array in TTree with pyRoot

Hi all,

I have a TTree with quite a few branches, and this one particular which I need to use is an array, but I’m only interested in the first element of the array, in this case it’s B_M[0].
I know that it’s still not possible to import data from an array in TTree to RooDataSet. So I get the data entry by entry and add them to RooDataSet. But it doesn’t work. As when I plot the data set, it gives 0 entry at all point except point which get all the entries (see the first plot).

This is the code I use right now:

f       = TFile("myfile.root")
tree    = f.Get("MyTree")

mass = array('d',[0])
branch = tree.GetBranch("B_M")
branch.SetAddress(mass)

numEntry = tree.GetEntries()
m        = RooRealVar("B_M","mass",5300., 5450.)
m_arg    = RooArgSet(m)
data     = RooDataSet("data", "data", m_arg)

for i in range(numEntry):
    tree.GetEvent(i)
    m = mass[0]
    data.add(m_arg, 1.0)

But when I tried the same thing in interactive ROOT, it works normally (see the second plot)

    TFile f("tree.root");
    TTree *datatree = (TTree*)(f.Get("MyTree"));
    TBranch *branch = tree->GetBranch("B_M");
    numEntry = branch->GetEntries();
    Float_t mass[100];
    branch->SetAddress(mass);
    RooRealVar m("B_M","mass",5300., 5450.);
    RooArgSet m_arg(m,"m_args");
    RooDataSet * dataset = new RooDataSet("dataset","dataset",m_arg);
    for(int i=0;i<datatree->GetEntries();i++){
        tree->GetEvent(i);
        m = mass[0];
        dataset->add(m_arg,1.0);
        }

Could anyone please help me with this?

Thanks,
Ryan

P/s: I’m using python 2.7.10 and root 6.08.06

I couldn’t upload 2 images at the same time. So this is the one I got when I used interactive root

m.__assign__(mass[0])

Hi wlav, thank you for the suggestion.
I’ve tried it but it still give me an error.

Traceback (most recent call last):                                                                                                                   |
  File "fit_data.py", line 29, in <module>
    m.__assign__(mass[0])
TypeError: RooRealVar& RooRealVar::operator=(const RooRealVar&) =>
    could not convert argument 1

Cheers
Ryan

Hum, seems that RooRealVar pulls in the base class assignment from RooAbsRealLValue:

  using RooAbsRealLValue::operator= ;

but still has its own (auto-generated?) assignment operator? Well, call the former explicitly then:

RooAbsRealLValue.__assign__(m, mass[0])

(Aside, I see differences of m_arg and m_args between the .cxx and .py. Don’t know if it matters.)

And oh, the assignment operator calls only public methods, so you could do the same:

RooAbsArg& RooAbsRealLValue::operator=(Double_t newValue)
{
  Double_t clipValue ;
  // Clip
  inRange(newValue,0,&clipValue) ;
  setVal(clipValue) ;

  return *this ;
}

no it was just a typo in my script.

Let me give it a try.

Thank you

Hi,
It works now, I changed the for loop to:

for i in range(numEntry):
    Branch.GetEntry(i)
    RooAbsRealLValue.__assign__(m, mass[0])
    data.add(m_arg, 1.0)

Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.