11from __future__
import print_function
15 """ Create a simple model and run statistical tests
17 This script can be used to make simple statistical using histfactory.
18 It takes values for signal, background, and data as input, and
19 can optionally take uncertainties on signal or background.
20 The model is created and saved to an output ROOT file, and
21 the model can be fit if requested.
28 desc =
" ".join(main.__doc__.split())
31 parser = optparse.OptionParser( description = desc, version = vers, usage =
"%prog [options]" )
33 parser.add_option(
"-s",
"--signal", dest =
"signal",
34 action =
"store", type =
"float", default=
None,
35 help =
"Expected Signal" )
37 parser.add_option(
"-b",
"--background", dest =
"background",
38 action =
"store", type =
"float", default=
None,
39 help =
"Expected Background" )
41 parser.add_option(
"-d",
"--data", dest =
"data",
42 action =
"store", type =
"float", default=
None,
43 help =
"Measured data" )
45 parser.add_option(
"--signal-uncertainty", dest =
"signal_uncertainty",
46 action =
"store", type =
"float", default=
None,
47 help =
"Uncertainty on the signal rate, as a fraction. --signal-uncertainty=.05 means a 5% uncertainty." )
49 parser.add_option(
"--background-uncertainty", dest =
"background_uncertainty",
50 action =
"store", type =
"float", default=
None,
51 help =
"Uncertainty on the background rate, as a fraction, not a percentage. --background-uncertainty=.05 means a 5% uncertainty." )
53 parser.add_option(
"--output-prefix", dest =
"output_prefix",
54 action =
"store", type =
"string", default=
"Measurement",
55 help =
"Prefix for output files when using export. Can include directories (ie 'MyDirectory/MyPrefix')" )
57 parser.add_option(
"-e",
"--export", dest =
"export",
58 action =
"store_true", default=
False,
59 help =
"Make output plots, graphs, and save the workspace." )
62 ( options, unknown ) = parser.parse_args()
66 FORMAT =
'Py:%(name)-25s %(levelname)-8s %(message)s'
68 logging.basicConfig( format = FORMAT )
70 logger = logging.getLogger(
"makeQuickMeasurement" )
72 logger.setLevel( logging.INFO )
76 logger.warning(
"Options(s) not recognised: [" +
",".join( unknown ) +
"]" )
79 if options.signal ==
None:
80 logger.error(
"You have to define a value for expacted signal (use --signal)" )
83 if options.background ==
None:
84 logger.error(
"You have to define a value for expacted background (use --background)" )
87 if options.data ==
None:
88 logger.error(
"You have to define a value for measured data (use --data)" )
96 MakeSimpleMeasurement( signal_val=options.signal, background_val=options.background, data_val=options.data,
97 signal_uncertainty=options.signal_uncertainty, background_uncertainty=options.background_uncertainty,
98 Export=options.export, output_prefix=options.output_prefix)
102def MakeSimpleMeasurement( signal_val, background_val, data_val, signal_uncertainty=None, background_uncertainty=None,
103 Export=False, output_prefix="Measurement"):
104 """ Make a simple measurement using HistFactory
106 Take in simple values for signal, background data,
107 and potentially uncertainty on signal and background
114 print(
"It seems that pyROOT isn't properly configured")
120 meas = ROOT.RooStats.HistFactory.Measurement(
"meas",
"meas")
121 meas.SetOutputFilePrefix( output_prefix )
122 meas.SetPOI(
"SigXsecOverSM" )
130 meas.SetLumiRelErr( 0.10 )
131 meas.AddConstantParam(
"Lumi")
136 meas.SetExportOnly(
False )
141 chan = ROOT.RooStats.HistFactory.Channel(
"channel" )
142 chan.SetData( data_val )
145 signal = ROOT.RooStats.HistFactory.Sample(
"signal" )
146 signal.SetNormalizeByTheory(
False )
147 signal.SetValue( signal_val )
153 upper_bound = 3*math.ceil( (data_val - background_val) / signal_val )
154 upper_bound = max(upper_bound, 3)
155 signal.AddNormFactor(
"SigXsecOverSM", 1, 0, upper_bound )
158 if signal_uncertainty !=
None:
159 uncertainty_up = 1.0 + signal_uncertainty
160 uncertainty_down = 1.0 - signal_uncertainty
161 signal.AddOverallSys(
"signal_uncertainty", uncertainty_down, uncertainty_up )
164 chan.AddSample( signal )
167 background = ROOT.RooStats.HistFactory.Sample(
"background" )
168 background.SetNormalizeByTheory(
False )
169 background.SetValue( background_val )
172 if background_uncertainty !=
None:
173 uncertainty_up = 1.0 + background_uncertainty
174 uncertainty_down = 1.0 - background_uncertainty
175 background.AddOverallSys(
"background_uncertainty", uncertainty_down, uncertainty_up )
178 chan.AddSample( background )
182 meas.AddChannel( chan )
186 workspace = ROOT.RooStats.HistFactory.MakeModelAndMeasurementFast( meas )
190 factory = ROOT.RooStats.HistFactory.HistoToWorkspaceFactoryFast()
191 workspace = factory.MakeCombinedModel( meas )
193 ROOT.RooStats.HistFactory.FitModel( workspace )
199if __name__ ==
"__main__":
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
MakeSimpleMeasurement(signal_val, background_val, data_val, signal_uncertainty=None, background_uncertainty=None, Export=False, output_prefix="Measurement")