def _print(c, p, e, l): plot.seterror(e) if not e: e = 0x0 plot.set_ErrorBand(e) c.Clear() p.Draw(c,1,ratio,errband) c.Modified() c.Update() hwwtools.ensuredir(opts.output) outbasename = os.path.join(opts.output,'fitshapes_mH%d_%s_%s' % (opt.mass,bin,mode)) if l: outbasename += '_' + l c.Print(outbasename+'.pdf') c.Print(outbasename+'.png')
def _print(c, p, e, l): plot.seterror(e) if not e: e = 0x0 plot.set_ErrorBand(e) c.Clear() p.Draw(c, 1, ratio, errband) c.Modified() c.Update() hwwtools.ensuredir(opts.output) outbasename = os.path.join( opts.output, 'fitshapes_mH%d_%s_%s' % (opt.mass, bin, mode)) if l: outbasename += '_' + l c.Print(outbasename + '.pdf') c.Print(outbasename + '.png')
siname = 'hww-{lumi:.2f}fb.mH{mass}.{channel}_shape' # masses = [m for m in hwwinfo.masses if m <= 250] if opt.mass == 0 else [opt.mass] masses = [m for m in opt.mass if m <= 250] dcdir = opt.input + '/datacards' shdir = dcdir + '/shapes' rndm = ROOT.TRandom3() for k in xrange(opt.ntoys): print '==> Instance', k toypath = 'toys/instance_%d/' % k toydcpath = toypath + 'datacards' toyshpath = toypath + 'datacards/shapes' hwwtools.ensuredir(toydcpath) hwwtools.ensuredir(toydcpath + '/pseudo') if os.path.exists(toyshpath): os.unlink(toyshpath) os.symlink(os.path.abspath(shdir), toyshpath) for chan in opt.chans: # the file for signal injection must be unique for all masses # let's take 125 as a reference refname = siname.format(lumi=opt.lumi, mass=125, channel=chan) refpath = os.path.join(shdir, refname + '.root') reffile = ROOT.TFile.Open(refpath) data_si = reffile.Get('histo_Data') if not data_si.__nonzero__(): reffile.Close()
def fitAndPlot( dcpath, opts ): ''' 1. read the datacard 2. convert to ws 3. run combine 4. open get the mlfit rootfile 1-4 don't need to know the content of the card, only to check that there are shapes inside ''' remass = re.compile('mH(\d*)') m = remass.search(dcpath) if not m: raise ValueError('Mass not found! Name your datacards properly!') print 'Mass',m.group(1) opt.mass = int(m.group(1)) shapepath = os.path.join(os.getenv('CMSSW_BASE'),'src/HWWAnalysis/ShapeAnalysis') print 'Shape directory is',shapepath ROOT.gInterpreter.ExecuteMacro(shapepath+'/macros/LatinoStyle2.C') # 1. load the datacard dcfile = open(dcpath,'r') class DCOptions: pass options = DCOptions() options.stat = False options.bin = True options.noJMax = False options.nuisancesToExclude = [] options.nuisancesToRescale = [] options.fileName = dcpath options.out = None options.cexpr = False options.fixpars = False options.libs = [] options.verbose = 0 options.poisson = 0 options.mass = opt.mass DC = parseCard(dcfile, options) if not DC.hasShapes: print 'This datacard has no shapes!' print dcpath sys.exit(-1) # if len(DC.bins) != 1: # raise ValueError('Only 1 bin datacards supported at the moment: '+', '.join(DC.bins)) # 2. convert to ws wspath = os.path.splitext(dcpath)[0]+'_workspace.root' logging.debug('Working with workspace %s',wspath) print " wspath = ", wspath mkws = (not os.path.exists(wspath) or os.path.getmtime(wspath) < os.path.getmtime(dcpath) or opts.clean) print " mkws = ", mkws print " not os.path.exists(wspath) = ", not os.path.exists(wspath) if os.path.exists(wspath) : print " os.path.getmtime(wspath) = ", os.path.getmtime(wspath) print " os.path.getmtime(dcpath) = ", os.path.getmtime(dcpath) print " opts.clean = ", opts.clean if mkws: # workspace + parameters = shapes print 'Making the workspace...', sys.stdout.flush() if opts.combineFolder != None : print " will run text2workspace in :", opts.combineFolder curentDir = os.getcwd() os.chdir( '%s' % (opts.combineFolder) ) #os.system( 'cd %s ' % (opts.combineFolder) ) #os.getenv('CMSSW_BASE') print "( 'cd %s ' % (opts.combineFolder) ) = ", ( 'cd %s ' % (opts.combineFolder) ) print " pwd = " os.system( 'pwd' ) #os.system( 'cmsenv' ) os.system( 'eval `scramv1 runtime -sh`' ) #os.system( 'cd - ' ) os.chdir( curentDir ) print " opts.modelName = ", opts.modelName os.system( 'text2workspace.py %s -o %s %s' % (dcpath,wspath,opts.modelName) ) print 'done.' ROOT.gSystem.Load('libHiggsAnalysisCombinedLimit') wsfile = ROOT.TFile.Open(wspath) if not wsfile.__nonzero__(): raise IOError('Could not open '+wspath) w = wsfile.Get('w') w.saveSnapshot('clean',w.allVars()) # run combine if requested if opt.usefit: mlfpath = opt.usefit print '-'*80 print 'Using results in',mlfpath print '-'*80 if not os.path.exists(mlfpath): raise IOError('Fit result file %s not found.' % mlfpath ) else: # 3.0 prepare the temp direcotry import tempfile mlfdir = opt.tmpdir if opt.tmpdir else tempfile.mkdtemp(prefix='mlfit_') hwwtools.ensuredir(mlfdir) # 3.1 go to the tmp dir here = os.getcwd() os.chdir(mlfdir) # 3.2 mlcmd = ' ' if opts.injectionSignal == False : mlcmd = 'combine -M MaxLikelihoodFit --saveNormalizations '+os.path.join(here,wspath) else : mlcmd = 'combine --expectSignal=1 -t -1 -M MaxLikelihoodFit --saveNormalizations '+os.path.join(here,wspath) logging.debug(mlcmd) print 'do: ',mlcmd print 'Fitting the workspace...', sys.stdout.flush() if opts.fit: os.system(mlcmd) os.chdir(here) print 'done.' # 3.3 set the max-like fit results path mlfpath = os.path.join(mlfdir,'mlfit.root') # 4. open the output and get the normalizations mlffile = ROOT.TFile.Open(mlfpath) if not mlffile.__nonzero__(): raise IOError('Could not open '+mlfpath) model_s = w.pdf('model_s') model_b = w.pdf('model_b') res_s = mlffile.Get('fit_s') res_b = mlffile.Get('fit_b') sig_fit = ( model_s, res_s.floatParsFinal(), mlffile.Get('norm_fit_s'), ) bkg_fit = ( model_b, res_b.floatParsFinal(), mlffile.Get('norm_fit_b'), ) print 'List of bins found',', '.join(DC.bins) bin = DC.bins[0] modes = odict.OrderedDict([ ('init',(model_s,res_s.floatParsInit(),None)), #(None, None, model_s) ('bkg' ,bkg_fit), ('sig' ,sig_fit), ]) # experimental MB = ShapeBuilder(DC, options) allshapes = {} nuisancemap = {} for mode,fit in modes.iteritems(): print 'Analysing model:',mode logging.debug('Plotting %s', fit) allshapes[mode] = {} for bin in DC.bins: print ' - Bin:',bin coroner = Coroner(bin, DC, MB, w, fit) coroner.errmode = opt.errmode shapes,errs = coroner.perform() nuisancemap[bin] = coroner.nuisances() if opts.output: printshapes(shapes, errs, mode, opts, bin, DC.signals, DC.processes) allshapes[mode][bin] = (shapes,errs) if opts.dump: logging.debug('Dumping histograms to %s',opts.dump) dumpdir = os.path.dirname(opt.dump) # open rootfile if dumpdir: hwwtools.ensuredir(dumpdir) dump = ROOT.TFile.Open(opts.dump,'recreate') here = ROOT.gDirectory.func() dump.cd() idir = dump.mkdir('info') idir.cd() # save the list of nuisances nuisances = ROOT.TObjArray() #ROOT.std.vector('string')() for (n,nf,pf,a,e) in DC.systs: nuisances.Add( ROOT.TObjString(n) ) nuisances.Write('nuisances', ROOT.TObject.kSingleKey) # save the list of processes processes = ROOT.TObjArray() #ROOT.std.vector('string')() for p in DC.processes: processes.Add( ROOT.TObjString(p) ) processes.Write('processes', ROOT.TObject.kSingleKey) # save the list of signals signals = ROOT.TObjArray() #ROOT.std.vector('string')() for s in DC.signals: signals.Add( ROOT.TObjString(s) ) signals.Write('signals', ROOT.TObject.kSingleKey) # save the list of nuisances per bin nuisbybin = ROOT.TMap() for bin,nuis in nuisancemap.iteritems(): tnuis = ROOT.TObjArray() for n in nuis: tnuis.Add( ROOT.TObjString(n) ) nuisbybin.Add(ROOT.TObjString(bin),tnuis) nuisbybin.Write('map_binnuisances', ROOT.TObject.kSingleKey) for mode,allbins in allshapes.iteritems(): # make the main directory mdir = dump.mkdir(mode) mdir.cd() # info directory idir = mdir.mkdir('info') idir.cd() # save the fit parameters model,pars,norms = modes[mode] pars.Write('parameters') # save the list of signals # save the bin plots for bin,(shapes,errs) in allbins.iteritems(): # bin directory bdir = mdir.mkdir(bin) bdir.cd() for s in shapes.itervalues(): # print s s.Write() for p,nugs in errs.iteritems(): dp = bdir.mkdir(p) dp.cd() for g in nugs.itervalues(): g.Write() bdir.cd() try: modelall = errs['model']['all'].Clone('model_errs') modelall.SetTitle('model_errs') modelall.Write() except: logging.warn('Error graph model:err not found') mdir.cd() dump.Write() dump.Close() here.cd()
siname = 'hww-{lumi:.2f}fb.mH{mass}.{channel}_shape' # masses = [m for m in hwwinfo.masses if m <= 250] if opt.mass == 0 else [opt.mass] masses = [m for m in opt.mass if m <= 250] dcdir = opt.input+'/datacards' shdir = dcdir+'/shapes' rndm = ROOT.TRandom3() for k in xrange(opt.ntoys): print '==> Instance',k toypath = 'toys/instance_%d/' % k toydcpath = toypath+'datacards' toyshpath = toypath+'datacards/shapes' hwwtools.ensuredir( toydcpath ) hwwtools.ensuredir( toydcpath+'/pseudo' ) if os.path.exists(toyshpath): os.unlink(toyshpath) os.symlink(os.path.abspath(shdir),toyshpath) for chan in opt.chans: # the file for signal injection must be unique for all masses # let's take 125 as a reference refname = siname.format(lumi=opt.lumi, mass=125, channel=chan) refpath = os.path.join(shdir,refname+'.root') reffile = ROOT.TFile.Open(refpath) data_si = reffile.Get('histo_Data') if not data_si.__nonzero__(): reffile.Close()
def fitAndPlot(dcpath, opts): ''' 1. read the datacard 2. convert to ws 3. run combine 4. open get the mlfit rootfile 1-4 don't need to know the content of the card, only to check that there are shapes inside ''' remass = re.compile('mH(\d*)') m = remass.search(dcpath) if not m: raise ValueError('Mass not found! Name your datacards properly!') print 'Mass', m.group(1) opt.mass = int(m.group(1)) shapepath = os.path.join(os.getenv('CMSSW_BASE'), 'src/HWWAnalysis/ShapeAnalysis') print 'Shape directory is', shapepath ROOT.gInterpreter.ExecuteMacro(shapepath + '/macros/LatinoStyle2.C') # 1. load the datacard dcfile = open(dcpath, 'r') class DCOptions: pass options = DCOptions() options.stat = False options.bin = True options.noJMax = False options.nuisancesToExclude = [] options.nuisancesToRescale = [] options.fileName = dcpath options.out = None options.cexpr = False options.fixpars = False options.libs = [] options.verbose = 0 options.poisson = 0 options.mass = opt.mass DC = parseCard(dcfile, options) if not DC.hasShapes: print 'This datacard has no shapes!' print dcpath sys.exit(-1) # if len(DC.bins) != 1: # raise ValueError('Only 1 bin datacards supported at the moment: '+', '.join(DC.bins)) # 2. convert to ws wspath = os.path.splitext(dcpath)[0] + '_workspace.root' logging.debug('Working with workspace %s', wspath) mkws = (not os.path.exists(wspath) or os.path.getmtime(wspath) < os.path.getmtime(dcpath) or opts.clean) if mkws: # workspace + parameters = shapes print 'Making the workspace...', sys.stdout.flush() os.system('text2workspace.py %s -o %s' % (dcpath, wspath)) print 'done.' ROOT.gSystem.Load('libHiggsAnalysisCombinedLimit') wsfile = ROOT.TFile.Open(wspath) if not wsfile.__nonzero__(): raise IOError('Could not open ' + wspath) w = wsfile.Get('w') w.saveSnapshot('clean', w.allVars()) # run combine if requested if opt.usefit: mlfpath = opt.usefit print '-' * 80 print 'Using results in', mlfpath print '-' * 80 if not os.path.exists(mlfpath): raise IOError('Fit result file %s not found.' % mlfpath) else: # 3.0 prepare the temp direcotry import tempfile mlfdir = opt.tmpdir if opt.tmpdir else tempfile.mkdtemp( prefix='mlfit_') hwwtools.ensuredir(mlfdir) # 3.1 go to the tmp dir here = os.getcwd() os.chdir(mlfdir) # 3.2 mlcmd = 'combine -M MaxLikelihoodFit --saveNormalizations ' + os.path.join( here, wspath) logging.debug(mlcmd) print 'Fitting the workspace...', sys.stdout.flush() if opts.fit: os.system(mlcmd) os.chdir(here) print 'done.' # 3.3 set the max-like fit results path mlfpath = os.path.join(mlfdir, 'mlfit.root') # 4. open the output and get the normalizations mlffile = ROOT.TFile.Open(mlfpath) if not mlffile.__nonzero__(): raise IOError('Could not open ' + mlfpath) model_s = w.pdf('model_s') model_b = w.pdf('model_b') res_s = mlffile.Get('fit_s') res_b = mlffile.Get('fit_b') sig_fit = ( model_s, res_s.floatParsFinal(), mlffile.Get('norm_fit_s'), ) bkg_fit = ( model_b, res_b.floatParsFinal(), mlffile.Get('norm_fit_b'), ) print 'List of bins found', ', '.join(DC.bins) bin = DC.bins[0] modes = odict.OrderedDict([ ('init', (model_s, res_s.floatParsInit(), None)), #(None, None, model_s) ('bkg', bkg_fit), ('sig', sig_fit), ]) # experimental MB = ShapeBuilder(DC, options) allshapes = {} nuisancemap = {} for mode, fit in modes.iteritems(): print 'Analysing model:', mode logging.debug('Plotting %s', fit) allshapes[mode] = {} for bin in DC.bins: print ' - Bin:', bin coroner = Coroner(bin, DC, MB, w, fit) coroner.errmode = opt.errmode shapes, errs = coroner.perform() nuisancemap[bin] = coroner.nuisances() if opts.output: printshapes(shapes, errs, mode, opts, bin, DC.signals, DC.processes) allshapes[mode][bin] = (shapes, errs) if opts.dump: logging.debug('Dumping histograms to %s', opts.dump) dumpdir = os.path.dirname(opt.dump) # open rootfile if dumpdir: hwwtools.ensuredir(dumpdir) dump = ROOT.TFile.Open(opts.dump, 'recreate') here = ROOT.gDirectory.func() dump.cd() idir = dump.mkdir('info') idir.cd() # save the list of nuisances nuisances = ROOT.TObjArray() #ROOT.std.vector('string')() for (n, nf, pf, a, e) in DC.systs: nuisances.Add(ROOT.TObjString(n)) nuisances.Write('nuisances', ROOT.TObject.kSingleKey) # save the list of processes processes = ROOT.TObjArray() #ROOT.std.vector('string')() for p in DC.processes: processes.Add(ROOT.TObjString(p)) processes.Write('processes', ROOT.TObject.kSingleKey) # save the list of signals signals = ROOT.TObjArray() #ROOT.std.vector('string')() for s in DC.signals: signals.Add(ROOT.TObjString(s)) signals.Write('signals', ROOT.TObject.kSingleKey) # save the list of nuisances per bin nuisbybin = ROOT.TMap() for bin, nuis in nuisancemap.iteritems(): tnuis = ROOT.TObjArray() for n in nuis: tnuis.Add(ROOT.TObjString(n)) nuisbybin.Add(ROOT.TObjString(bin), tnuis) nuisbybin.Write('map_binnuisances', ROOT.TObject.kSingleKey) for mode, allbins in allshapes.iteritems(): # make the main directory mdir = dump.mkdir(mode) mdir.cd() # info directory idir = mdir.mkdir('info') idir.cd() # save the fit parameters model, pars, norms = modes[mode] pars.Write('parameters') # save the list of signals # save the bin plots for bin, (shapes, errs) in allbins.iteritems(): # bin directory bdir = mdir.mkdir(bin) bdir.cd() for s in shapes.itervalues(): # print s s.Write() for p, nugs in errs.iteritems(): dp = bdir.mkdir(p) dp.cd() for g in nugs.itervalues(): g.Write() bdir.cd() try: modelall = errs['model']['all'].Clone('model_errs') modelall.SetTitle('model_errs') modelall.Write() except: logging.warn('Error graph model:err not found') mdir.cd() dump.Write() dump.Close() here.cd()