14 """ Create a simple model and run statistical tests
16 This script can be used to make simple statistical using histfactory.
17 It takes values for signal, background, and data as input, and
18 can optionally take uncertainties on signal or background.
19 The model is created and saved to an output ROOT file, and
20 the model can be fit if requested.
27 desc =
" ".join(main.__doc__.split())
30 parser = optparse.OptionParser( description = desc, version = vers, usage =
"%prog [options]" )
32 parser.add_option(
"-s",
"--signal", dest =
"signal",
33 action =
"store", type =
"float", default=
None,
34 help =
"Expected Signal" )
36 parser.add_option(
"-b",
"--background", dest =
"background",
37 action =
"store", type =
"float", default=
None,
38 help =
"Expected Background" )
40 parser.add_option(
"-d",
"--data", dest =
"data",
41 action =
"store", type =
"float", default=
None,
42 help =
"Measured data" )
44 parser.add_option(
"--signal-uncertainty", dest =
"signal_uncertainty",
45 action =
"store", type =
"float", default=
None,
46 help =
"Uncertainty on the signal rate, as a fraction. --signal-uncertainty=.05 means a 5% uncertainty." )
48 parser.add_option(
"--background-uncertainty", dest =
"background_uncertainty",
49 action =
"store", type =
"float", default=
None,
50 help =
"Uncertainty on the background rate, as a fraction, not a percentage. --background-uncertainty=.05 means a 5% uncertainty." )
52 parser.add_option(
"--output-prefix", dest =
"output_prefix",
53 action =
"store", type =
"string", default=
"Measurement",
54 help =
"Prefix for output files when using export. Can include directories (ie 'MyDirectory/MyPrefix')" )
56 parser.add_option(
"-e",
"--export", dest =
"export",
57 action =
"store_true", default=
False,
58 help =
"Make output plots, graphs, and save the workspace." )
61 ( options, unknown ) = parser.parse_args()
65 FORMAT =
'Py:%(name)-25s %(levelname)-8s %(message)s'
67 logging.basicConfig( format = FORMAT )
69 logger = logging.getLogger(
"makeQuickMeasurement" )
71 logger.setLevel( logging.INFO )
75 logger.warning(
"Options(s) not recognised: [" +
",".join( unknown ) +
"]" )
78 if options.signal ==
None:
79 logger.error(
"You have to define a value for expacted signal (use --signal)" )
82 if options.background ==
None:
83 logger.error(
"You have to define a value for expacted background (use --background)" )
86 if options.data ==
None:
87 logger.error(
"You have to define a value for measured data (use --data)" )
95 MakeSimpleMeasurement( signal_val=options.signal, background_val=options.background, data_val=options.data,
96 signal_uncertainty=options.signal_uncertainty, background_uncertainty=options.background_uncertainty,
97 Export=options.export, output_prefix=options.output_prefix)
101def MakeSimpleMeasurement( signal_val, background_val, data_val, signal_uncertainty=None, background_uncertainty=None,
102 Export=False, output_prefix="Measurement"):
103 """ Make a simple measurement using HistFactory
105 Take in simple values for signal, background data,
106 and potentially uncertainty on signal and background
113 print(
"It seems that pyROOT isn't properly configured")
119 meas = ROOT.RooStats.HistFactory.Measurement(
"meas",
"meas")
120 meas.SetOutputFilePrefix( output_prefix )
121 meas.SetPOI(
"SigXsecOverSM" )
129 meas.SetLumiRelErr( 0.10 )
130 meas.AddConstantParam(
"Lumi")
135 meas.SetExportOnly(
False )
140 chan = ROOT.RooStats.HistFactory.Channel(
"channel" )
141 chan.SetData( data_val )
144 signal = ROOT.RooStats.HistFactory.Sample(
"signal" )
145 signal.SetNormalizeByTheory(
False )
146 signal.SetValue( signal_val )
152 upper_bound = 3*math.ceil( (data_val - background_val) / signal_val )
153 upper_bound = max(upper_bound, 3)
154 signal.AddNormFactor(
"SigXsecOverSM", 1, 0, upper_bound )
157 if signal_uncertainty !=
None:
158 uncertainty_up = 1.0 + signal_uncertainty
159 uncertainty_down = 1.0 - signal_uncertainty
160 signal.AddOverallSys(
"signal_uncertainty", uncertainty_down, uncertainty_up )
163 chan.AddSample( signal )
166 background = ROOT.RooStats.HistFactory.Sample(
"background" )
167 background.SetNormalizeByTheory(
False )
168 background.SetValue( background_val )
171 if background_uncertainty !=
None:
172 uncertainty_up = 1.0 + background_uncertainty
173 uncertainty_down = 1.0 - background_uncertainty
174 background.AddOverallSys(
"background_uncertainty", uncertainty_down, uncertainty_up )
177 chan.AddSample( background )
181 meas.AddChannel( chan )
185 workspace = ROOT.RooStats.HistFactory.MakeModelAndMeasurementFast( meas )
189 factory = ROOT.RooStats.HistFactory.HistoToWorkspaceFactoryFast()
190 workspace = factory.MakeCombinedModel( meas )
192 ROOT.RooStats.HistFactory.FitModel( workspace )
198if __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
main()
Create a simple model and run statistical tests
MakeSimpleMeasurement(signal_val, background_val, data_val, signal_uncertainty=None, background_uncertainty=None, Export=False, output_prefix="Measurement")
Make a simple measurement using HistFactory.