Skip to content

Instantly share code, notes, and snippets.

@wavewave
Last active December 17, 2015 14:08
Show Gist options
  • Save wavewave/5621762 to your computer and use it in GitHub Desktop.
Save wavewave/5621762 to your computer and use it in GitHub Desktop.
hist2workspace (RooFit/RooStats) simplest example
<!--
//============================================================================
// Name : example.xml
//============================================================================
-->
<!--
Top-level configuration, details for the example channel are in example_channel.xml.
This is the input file to the executable.
Note: Config.dtd needs to be accessible. It can be found in ROOT release area.
The file system path is relative to location of this XML file, not the executable.
-->
<!DOCTYPE Combination SYSTEM 'HistFactorySchema.dtd'>
<Combination OutputFilePrefix="./result" >
<Input>./example_channel.xml</Input>
<Measurement Name="GaussExample" Lumi="1." LumiRelErr="0.1" >
<POI>sigoverbkg</POI>
<ParamSetting Const="True">Lumi alpha_syst1</ParamSetting>
<!-- don't need <ConstraintTerm> default is Gaussian with an exponential interpolation for normalization factors (LHC HCG default)-->
</Measurement>
</Combination>
<!--
Single channel configuration exampple.
The top level configuration XML is example.xml
NormalizedByTheory should be "True" (not "TRUE" or "true") for all non-data-driven backgrounds.
If you comment or remove the <Data> tag then it will use the expected data.
Histogram inputs should be in pb and in top-level xml the lumi should be in 1/pb
(The important thing is that they match... fb and 1/fb is also ok)
Note: Config.dtd needs to be accessible. It can be found in ROOT release area.
The file system path is relative to location of this XML file, not the executable.
-->
<!DOCTYPE Channel SYSTEM 'HistFactorySchema.dtd'>
<Channel Name="channel1" InputFile="./example.root" >
<!-- <Data HistoName="Channel1Data" HistoPath="" /> -->
<!-- Set the StatError type to Poisson. Can also be Gaussian -->
<StatErrorConfig RelErrorThreshold="0.05" ConstraintType="Poisson" />
<Sample Name="signal" HistoPath="" HistoName="signal">
<OverallSys Name="syst1" High="1.05" Low="0.95"/>
<NormFactor Name="sigoverbkg" Val="1" Low="0." High="3." />
</Sample>
<Sample Name="background" HistoPath="" HistoName="background">
<OverallSys Name="syst2" Low="0.95" High="1.05"/>
</Sample>
</Channel>
<!-- The top level combination spec -->
<!-- OutputFilePrefix: Prefix to the output root file to be created (inspection histograms) -->
<!-- Mode: Type of the analysis -->
<!ELEMENT Combination (Function*,Input+,Measurement*)>
<!ATTLIST Combination
OutputFilePrefix CDATA #REQUIRED
Mode CDATA #IMPLIED
>
<!-- Input files detailing the channels. One channel per file -->
<!ELEMENT Function EMPTY>
<!ATTLIST Function
Name CDATA #REQUIRED
Expression CDATA #REQUIRED
Dependents CDATA #REQUIRED
>
<!-- Input files detailing the channels. One channel per file -->
<!ELEMENT Input (#PCDATA) >
<!-- Configuration for each measurement -->
<!-- Name: to be used as the heading in the table -->
<!-- Lumi: the luminosity of the measurement -->
<!-- LumiRelErr: the relative error known for the lumi -->
<!-- BinLow: the lowest bin number used for the measurement (inclusive) -->
<!-- BinHigh: the highest bin number used for the measurement (exclusive) -->
<!-- Mode: type of the measurement (a closed list of ...) -->
<!-- ExportOnly: if "True" skip fit, only export model -->
<!ELEMENT Measurement (POI,ParamSetting*,ConstraintTerm*) >
<!ATTLIST Measurement
Name CDATA #REQUIRED
Lumi CDATA #REQUIRED
LumiRelErr CDATA #REQUIRED
BinLow CDATA #IMPLIED
BinHigh CDATA #IMPLIED
Mode CDATA #IMPLIED
ExportOnly CDATA #IMPLIED
>
<!-- Specify what you are measuring. Corresponds to the name specified in the construction
of the model in the channel setup. Typically the NormFactor for xsec measurements -->
<!ELEMENT POI (#PCDATA) >
<!-- Specify what parameters are fixed, or have particular value -->
<!-- Val: set the value of the parameter -->
<!-- Const: set this parameter constant -->
<!ELEMENT ParamSetting (#PCDATA)>
<!ATTLIST ParamSetting
Val CDATA #IMPLIED
Const CDATA #IMPLIED
>
<!-- Specify an alternative shape to use for given constraint terms (Gaussian is used if this is not specified) -->
<!-- Type: can be Gamma or Uniform -->
<!-- RelativeUncertainty: relative uncertainty on the shape -->
<!ELEMENT ConstraintTerm (#PCDATA)>
<!ATTLIST ConstraintTerm
Type CDATA #REQUIRED
RelativeUncertainty CDATA #IMPLIED
>
<!-- Top element for channels. InputFile, HistoName and HistoPath
can be set at this level in which case they will become defaul to
all subsequent elements. Otherwise they can be set in individual
subelements -->
<!ELEMENT Channel (Data*,StatErrorConfig*,Sample+)>
<!-- InputFile: input file where the input histogram can be found (use abs path) -->
<!-- HistoPath: the path (within the root file) where the histogram can be found -->
<!-- HistoName: the name of the histogram to be used for this (and following in not overridden) item -->
<!ATTLIST Channel
Name CDATA #REQUIRED
InputFile CDATA #IMPLIED
HistoPath CDATA #IMPLIED
HistoName CDATA #IMPLIED
>
<!-- Data to be fit. If you don't provide it, Asimov data will be created -->
<!-- InputFile: any item set here will override the configuration for the subelements.
For this element there is no sublemenents so the setting will only have local effects -->
<!ELEMENT Data EMPTY>
<!ATTLIST Data
InputFile CDATA #IMPLIED
HistoPath CDATA #IMPLIED
HistoName CDATA #IMPLIED
>
<!ELEMENT StatErrorConfig EMPTY>
<!ATTLIST StatErrorConfig
RelErrorThreshold CDATA #IMPLIED
ConstraintType CDATA #IMPLIED
>
<!-- Sample elements are made up of systematic variations -->
<!ELEMENT Sample (StatError | HistoSys | OverallSys | ShapeSys | NormFactor | ShapeFactor)*>
<!ATTLIST Sample
Name CDATA #REQUIRED
InputFile CDATA #IMPLIED
HistoName CDATA #IMPLIED
HistoPath CDATA #IMPLIED
NormalizeByTheory CDATA #IMPLIED
>
<!-- Systematics for which the variation is provided by histograms -->
<!ELEMENT StatError EMPTY>
<!ATTLIST StatError
Activate CDATA #REQUIRED
HistoName CDATA #IMPLIED
InputFile CDATA #IMPLIED
HistoPath CDATA #IMPLIED
>
<!ELEMENT HistoSys EMPTY>
<!ATTLIST HistoSys
Name CDATA #REQUIRED
InputFile CDATA #IMPLIED
HistoFileHigh CDATA #IMPLIED
HistoPathHigh CDATA #IMPLIED
HistoNameHigh CDATA #IMPLIED
HistoFileLow CDATA #IMPLIED
HistoPathLow CDATA #IMPLIED
HistoNameLow CDATA #IMPLIED
>
<!-- Systematics for which the variation is provided by simple overall scaling -->
<!ELEMENT OverallSys EMPTY>
<!ATTLIST OverallSys
Name CDATA #REQUIRED
High CDATA #REQUIRED
Low CDATA #REQUIRED
>
<!-- Systematics for which the variation is provided by simple overall scaling -->
<!ELEMENT ShapeSys EMPTY>
<!ATTLIST ShapeSys
Name CDATA #REQUIRED
HistoName CDATA #REQUIRED
HistoPath CDATA #IMPLIED
InputFile CDATA #IMPLIED
ConstraintType CDATA #IMPLIED
>
<!-- Scaling factor, which may be the parameter of interest for cross section measurements-->
<!ELEMENT NormFactor EMPTY>
<!ATTLIST NormFactor
Name CDATA #REQUIRED
Val CDATA #REQUIRED
High CDATA #REQUIRED
Low CDATA #REQUIRED
Const CDATA #IMPLIED
>
<!-- Systematics for which the variation is provided by simple overall scaling -->
<!ELEMENT ShapeFactor EMPTY>
<!ATTLIST ShapeFactor
Name CDATA #REQUIRED
>
-----------------------------------------------------------------------------
-- |
--
-- Copyright : (c) 2013 Ian-Woo Kim
--
-- License : GPL-3
-- Maintainer : [email protected]
-- Stability : experimental
-- Portability : GHC
--
-- Make a sample root file for hist2factory example
--
-----------------------------------------------------------------------------
import Control.Monad (when)
import System.Directory
--
import HROOT
main :: IO ()
main = do
-- clear
clearPreviousRun
-- prepare histogram
h_sig <- newTH1F "signal" "signal" 40 (-10.0) 10.0
h_sig2 <- newTH1F "test" "test" 40 (-10.0) 10.0
h_bkg <- newTH1F "background" "background" 40 (-10.0) 10.0
--
tRandom <- newTRandom 65535
let dist1 = gaus tRandom (-2) 2
dist2 = gaus tRandom 3 1
dist3 = gaus tRandom (-10) 10
let go n | n < 0 = return ()
| otherwise = do
histfill dist1 h_sig
histfill dist2 h_sig2
histfill dist3 h_bkg
go (n-1)
go 1000000
add h_sig h_sig2 1.0
-- write to a root file
tfile <- newTFile "example.root" "NEW" "" 1
write h_sig "" 0 0
write h_bkg "" 0 0
close tfile ""
-- clear resource
delete tfile
delete h_sig
delete h_sig2
delete h_bkg
--
return ()
-- | histogram fill using generator
histfill :: IO Double -> TH1F -> IO ()
histfill dist hist = do
x <- dist
fill1 hist x
return ()
-- | clear previous run (example.root)
clearPreviousRun :: IO ()
clearPreviousRun = doesFileExist "example.root" >>= flip when (removeFile "example.root")
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Applicative
import HROOT
import HROOT.RooFit as RooFit
import HROOT.RooFit.RooStats
main = do
myfile <- newTFile "result_combined_GaussExample_model.root" "READ" "" 1
wspace :: RooWorkspace <- downcastTObject <$> get myfile "combined"
mc :: ModelConfig <- downcastTObject <$> rooWorkspaceobj wspace "ModelConfig"
obs <- modelConfigGetObservables mc
x <- rooAbsCollectionfind (upcastRooAbsCollection obs) "obs_x_channel1"
pdf <- modelConfigGetPdf mc
x_set <- newRooArgSet "x_set"
RooFit.add x_set x 0
dat <- generate pdf x_set 1000000
h1 <- createHistogram_RooAbsData dat "obs_x_channel1"
c1 <- newTCanvas "test" "test" 640 480
draw h1 ""
saveAs c1 "resample.pdf" ""
-- delete c1
-- delete h1
TFile *myfile = new TFile("result_combined_GaussExample_model.root");
RooWorkspace *roo = (RooWorkspace*) myfile->Get("combined");
RooArgSet *obs = (RooArgSet*) roo->obj("obs_x_channel1");
obs->Print();
RooStats::ModelConfig *mc = (RooStats::ModelConfig*) roo->obj("ModelConfig");
RooAbsPdf *pdf1 = mc->RooStats::ModelConfig::GetPdf();
RooArgSet *obs1 = mc->RooStats::ModelConfig::GetObservables();
RooArgSet *newobs = new RooArgSet();
newobs->add(*(obs1->find("obs_x_channel1")));
RooRealVar *x = obs1->find("obs_x_channel1");
RooDataSet *dat = pdf1->generate((*newobs),1000000);
TH1* h1 = dat->createHistogram("obs_x_channel1");
h1->Draw();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment