cms-analysis / CombineHarvester

CMSSW package for the creation, editing and analysis of combine datacards and workspaces
cms-analysis.github.io/CombineHarvester/
15 stars 180 forks source link

Overload method for CombineHarvester::WriteDatacard with TFile #221

Closed IzaakWN closed 5 years ago

IzaakWN commented 5 years ago

Add a method to overload

CombineHarvester::WriteDatacard(std::string const& name, TFile& root_file)

for use in python. I noticed this was not possible yet, so I took inspiration from CombineHarvester/CombinePdfs/src/Python.cc::BuildRooMorphingPy.

You might want to double-check the use of pointers/references, as I am not an C++ expert, but these changes allowed me to do

harvester = CombineHarvester()
...
output = TFile("htt_mt.input.root", 'RECREATE')
for mass in masses:
  harvester.cp().mass([mass,'*']).WriteDatacard("htt_mt.datacard_M%s.root"%mass,outfile)
outfile.Close()

Where in the end, you have one datacard for each mass point, and one single root file with one directory per bin containing all common background histograms, as well as the signal histograms for each mass point.

ajgilbert commented 5 years ago

Hi Izaak, thanks for the PR - I think it's ok but will make a couple of small checks before merging. Btw, did you look at using the CardWriter class to write your datacards? It works in the python interface and can be used to create multiple txt cards + 1 ROOT file output.

cheers, Andrew

IzaakWN commented 5 years ago

Hi Andrew, thanks for the consideration! I was using the CardWriter class before in python, but the CombineHarvester.WriteDatacard method was more advertised in the documentation (e.g. Examples Part II), and it is more convenient for me to loop over different combination of channels and/or bins.