PyROOT & limit.C

I’m trying to convert the limit.C tutorial to python. I’m having a problem with types.

Heres the error:

[quote]Computing limits…
<main.TClass object at 0x00918CE0>
Traceback (most recent call last):
File “C:/RootWin/tutorials/limit.py”, line 66, in ?
myconfidence = ROOT.TLimit.ComputeLimit(mydatasource, numberOfTests)
TypeError: argument type Double_t (*) (Double_t, Double_t,Double_t) not handled[/quote]

Here’s the code in question:

[quote]mydatasource = ROOT.TLimitDataSource(signal,background,data)
myconfidence = ROOT.TLimit.ComputeLimit(mydatasource, 50000)
print "CLs : " << myconfidence.CLs()
print "CLsb : " << myconfidence.CLsb()[/quote]

And in case you’re interested, heres the entire code:

[quote]import sys, os, math
import ROOT

#void limit() {
#//------------------------------------*
#//-
#//- This program demonstrates the computation of 95 % C.L. limits.
#//- It uses a set of randomly created histograms.
#//-
##- Written by Christophe.Delaere@cern.ch on 21.08.02
#//-
##------------------------------------*

#// Create a new canvas.

c1 = ROOT.TCanvas(“c1”,“Dynamic Filling Example”,200,10,700,500)
c1.SetFillColor(42)

Create some histograms

background = ROOT.TH1D(“background”,“The expected background”,30,-4,4)
signal = ROOT.TH1D(“signal”,“the expected signal”,30,-4,4)
data = ROOT.TH1D(“data”,“some fake data points”,30,-4,4)
background.SetFillColor(48)
signal.SetFillColor(41)
data.SetMarkerStyle(21)
data.SetMarkerColor(ROOT.kBlue)
background.Sumw2() # needed for stat uncertainty
signal.Sumw2() # needed for stat uncertainty

Fill histograms randomly

r = ROOT.TRandom();
bg = 0.0
sig = 0.0
dt = 0.0

for i in range(2500):
bg = r.Gaus(0,1)
sig = r.Gaus(1,.2)
background.Fill(bg,0.02)
signal.Fill(sig,0.001)

for i in range(500):
dt = r.Gaus(0,1)
data.Fill(dt)

hs = ROOT.THStack(“hs”,“Signal and background compared to data…”)
hs.Add(background)
#hs.Add(signal)
#hs.Draw(“hist”)
#data.Draw(“PE1,Same”)
c1.Modified()
c1.Update()
c1.GetFrame().SetFillColor(21)
c1.GetFrame().SetBorderSize(6)
c1.GetFrame().SetBorderMode(-1)
c1.Modified()
c1.Update()
ROOT.gSystem.ProcessEvents()

Compute the limits

print "Computing limits… "
mydatasource = ROOT.TLimitDataSource(signal,background,data)
myconfidence = ROOT.TLimit.ComputeLimit(mydatasource, 50000)
print "CLs : " << myconfidence.CLs()
print "CLsb : " << myconfidence.CLsb()
print "CLb : " << myconfidence.CLb()
print “< CLs > : %s” % myconfidence.GetExpectedCLs_b()
print “< CLsb > : %s” % myconfidence.GetExpectedCLsb_b()
print “< CLb > : %s” % myconfidence.GetExpectedCLb_b()

Add stat uncertainty

#print "Computing limits with stat systematics… "
#mystatconfidence = ROOT.TLimit.ComputeLimit(mydatasource,50000,true)
#print "CLs : " << mystatconfidence.CLs()
#print "CLsb : " << mystatconfidence.CLsb()
#print "CLb : " << mystatconfidence.CLb()
#print “< CLs > : %s” % mystatconfidence.GetExpectedCLs_b()
#print “< CLsb > : %s” % mystatconfidence.GetExpectedCLsb_b()
#print “< CLb > : %s” % mystatconfidence.GetExpectedCLb_b()

Add some systematics

#print "Computing limits with systematics… "
#errorb = ROOT.TH1D(“errorb”,“errors on background”,1,0,1)
#errors = ROOT.TH1D(“errors”,“errors on signal”,1,0,1)
#names = ROOT.TObjArray()
#name1 = ROOT.TObjString(“bg uncertainty”)
#name2 = ROOT.TObjString(“sig uncertainty”)
#names.AddLast(name1)
#names.AddLast(name2)
#errorb.SetBinContent(0,0.05) # error source 1: 5%
#errorb.SetBinContent(1,0) # error source 2: 0%
#errors.SetBinContent(0,0) # error source 1: 0%
#errors.SetBinContent(1,0.01) # error source 2: 1%
#mynewdatasource = ROOT.TLimitDataSource()
#mynewdatasource.AddChannel(signal,background,data,errors,errorb,names)
##mynewconfidence = ROOT.TLimit.ComputeLimit(mynewdatasource,50000,true)
##print “CLs : %s” % mynewconfidence.CLs()
##print “CLsb : %s” % mynewconfidence.CLsb()
##print “CLb : %s” % mynewconfidence.CLb()
##print “< CLs > : %s” % mynewconfidence.GetExpectedCLs_b()
##print “< CLsb > : %s” % mynewconfidence.GetExpectedCLsb_b()
##print “< CLb > : %s” % mynewconfidence.GetExpectedCLb_b()

show canonical -2lnQ plots in a ROOT.canvas

- The histogram of -2lnQ for background hypothesis (full)

- The histogram of -2lnQ for signal and background hypothesis (dashed)

c3 = ROOT.TCanvas(“c2”)
myconfidence.Draw()[/quote]

Hi,

not sure what exactly you are doing … this bit: [quote]print "CLs : " << myconfidence.CLs()[/quote] for sure isn’t going to do what you’d expect. Replace the ‘<<’ with ‘,’ first.

As for [quote]mydatasource = ROOT.TLimitDataSource(signal,background,data) myconfidence = ROOT.TLimit.ComputeLimit(mydatasource, 50000)[/quote] the function pointer that it complains about has a default value, so it shouldn’t come up as a problem unless used (function pointers are not generally mapped; I’m not sure how I could do such a thing). When I try it myself (ROOT HEAD CVS), there are no problems.

Which version of ROOT are you using and on which platform do you work?

Btw., I’m leaving for CHEP and won’t have net access for the next 4 days or so.

Cheers,
Wim

I primarily develop on my laptop, Win. XP with root 4.04 and Python 2.3. I also run the programs on Linux at D0.

BTW, this is just an error on my part. I failed to convert this line from C++ to python.

Hi,

had a look at 4.04 … the code is such that with a missing handler, default or no, it will always refuse to initialize the dispatcher.

I really hope you can upgrade, but there’s also a way to cheat: as long as you won’t use the actual argument, it is possible to add your own dummy converter (this isn’t too clean, but you can use it until you upgrade).

Create a file called “workaround.C” and add these contents:[code]#include “DllImport.h”

#include
#include

namespace PyROOT {

class Converter;
typedef Converter* (*ConverterFactory_t) ();
typedef std::map< std::string, ConverterFactory_t > ConvFactories_t;
R__EXTERN ConvFactories_t gConvFactories;

}

namespace {

PyROOT::Converter* dummyFactory() { return 0; }

struct init {
init() {
PyROOT::gConvFactories[ “(unknown)” ] = dummyFactory;
}
} inti_;

}[/code]

Then, after your ‘import ROOT’, add:ROOT.gROOT.LoadMacro( 'workaround.C+' ) and things will “work” (not complain, that is).

HTHFN,
Wim

Thanks for your help. I’ll try upgrading first. I’m not sure about @D0.