def run(self, **kwargs): from ROOT import RooArgSet __check_req_kw__("Observables", kwargs) __check_req_kw__("Pdf", kwargs) __check_req_kw__("Sigmat", kwargs) __check_req_kw__("Time", kwargs) __check_req_kw__("SigmaGen", kwargs) sigma_gen = kwargs.pop("SigmaGen") observables = kwargs.pop("Observables") obs_set = RooArgSet(*observables) pdf = kwargs.pop("Pdf") sigmat = kwargs.pop("Sigmat") time = kwargs.pop("Time") gen_obs_set = RooArgSet(*observables) # Make another ArgSet to put the fit results in result_params = RooArgSet("result_params") from P2VV.RooFitWrappers import RealVar da = RealVar("da", Observable=True, MinMax=(0.01, 1.1)) dft = RealVar("dft", Observable=True, MinMax=(0.01, 1.1)) result_params.add(da._target_()) result_params.add(dft._target_()) transform = self.transform() if transform: trans_params = transform.gen_params(gen_obs_set) self._gen_params.extend(trans_params) for p in trans_params: result_params.add(p) # Some extra numbers of interest from ROOT import RooRealVar seed = RooRealVar("seed", "random seed", 0.0) result_params.add(seed) # The dataset to store the results from ROOT import RooDataSet self._data = RooDataSet("result_data", "result_data", result_params) data_params = self._data.get() from ROOT import RooRandom import struct, os # Reset pdf parameters to initial values. Note: this does not reset the estimated errors... args = dict(NumEvents=self.options().nevents) if "ProtoData" in kwargs: args["ProtoData"] = kwargs.pop("ProtoData") spec = pdf.prepareMultiGen(obs_set, **args) while self._data.numEntries() < self.options().ntoys: # Get a good random seed, set it and store it s = struct.unpack("I", os.urandom(4))[0] RooRandom.randomGenerator().SetSeed(s) seed.setVal(s) data = pdf.generate(spec) if self.transform(): old_data = data data = self.transform()(old_data) if not data: transform.set_params(data_params) self._data.add(data_params) continue from P2VV import Dilution d_ft = Dilution.dilution_ft(data, time, t_range=2, quiet=True) d_a = Dilution.signal_dilution_dg(data, sigmat, *sigma_gen) da.setVal(d_a[0]) da.setError(d_a[1] if d_a[1] != None else 0.0) dft.setVal(d_ft[0]) dft.setError(d_ft[1] if d_ft[1] != None else 0.0) if transform: transform.set_params(data_params) self._data.add(result_params) return self.data()
# generate data and compute moments in loop import sys from ROOT import RooRandom for it in range(nIters) : if it % 100 == 0 : print 'iteration %d' % it sys.stdout.flush() # generate data with (1 + x) / 2 RooRandom.randomGenerator().SetSeed( 100000 + it ) noWeightData.reset() weightData.reset() for evIt in range(nEvents) : weight = RooRandom.uniform() obs.setVal( 2. * weight - 1. ) if RooRandom.uniform() <= weight : noWeightData.add(obsSet) weightData.add( obsSet, weight ) # compute moments of functions moms.compute( noWeightData, ResetFirst = True, Verbose = False ) momsW.compute( weightData, ResetFirst = True, Verbose = False ) # update covariance sums momCoefs = moms.coefficients() momCoefsW = momsW.coefficients() for name1 in funcNames : for name2 in funcNames : covs[name1][name2] += ( momCoefs[name1][0] - funcMeans[name1] ) * ( momCoefs[name2][0] - funcMeans[name2] ) covsW[name1][name2] += ( momCoefsW[name1][0] - funcMeans[name1] ) * ( momCoefsW[name2][0] - funcMeans[name2] )
for key, comb in cutCombs.iteritems() : print '--> creating dataset for "%s" selection' % key DTT.Draw( '>>selList', ' && '.join( cuts[cutName] for cutName in comb ), 'entrylist' ) selList = gDirectory.Get('selList') print '%d selected events out of %d (%.1f%%)'\ % ( selList.GetN(), DTT.GetEntries(), float( selList.GetN() ) / float( DTT.GetEntries() ) * 100. ) dataSets[key] = RooDataSet( 'data_%s' % key, 'data_%s' % key, massSet ) evIt = -1 evInd = selList.Next() while evInd >= 0 : evIt += 1 if evIt % 100000 == 0 : print 'event %d' % evIt sys.stdout.flush() DTT.GetEntry(evInd) mass.setVal(DTT.B_s0_M) dataSets[key].add(massSet) evInd = selList.Next() print '%d events in dataset' % dataSets[key].numEntries() DTTFile.Close() from ROOT import TObject dataFile = TFile.Open( dataFilePath, 'RECREATE' ) for dataSet in dataSets.itervalues() : dataFile.Append(dataSet) dataFile.Write( dataFilePath, TObject.kOverwrite ) dataFile.Close() else : dataFile = TFile.Open(dataFilePath) dataSets = dict( [ ( key, dataFile.Get( 'data_' + key ) ) for key in cutKeys ] ) dataFile.Close()
tagDilAlt = 1. - 2. * evSet.getRealValue('tagomega_ss_nn') if tags[0] == tags[1] : tagDil = ( tagDil + tagDilAlt ) / ( 1 + tagDil * tagDilAlt ) else : tagDil = ( tagDil - tagDilAlt ) / ( 1 - tagDil * tagDilAlt ) if tagDil < 0. : tag = -tag tagDil = -tagDil elif tagCat[1] == 1 : tag = tags[1] tagDil = 1. - 2. * evSet.getRealValue('tagomega_ss_nn') if resDil * tagDil < dilutionCut : continue # set signal weight sigWeight.setVal( dataSet.weight() ) # set tagging observables dilution.setVal( resDil * tagDil ) asymCat.setIndex(tag) # calculate angular weight if applyAngWeights : ctk = evSet.getRealValue('helcosthetaK') ctl = evSet.getRealValue('helcosthetaL') phi = evSet.getRealValue('helphi') if applyAngWeights == 'ang' : angWeight.setVal( 2. - 5. * ( 1. - ctl**2 ) * sin(phi)**2 ) elif applyAngWeights == 'para_perp' : angWeight.setVal( ( 9. - 15. * ctk**2 ) * ( 1. - ctl**2 ) * sin(2. * phi) ) #angWeight.setVal( ( 1. - ctl**2 ) * sin(2. * phi) )