def fnc_dscb(xx, pp): x = xx[0] N = pp[0] mu = pp[1] sig = pp[2] a1 = pp[3] p1 = pp[4] a2 = pp[5] p2 = pp[6] u = (x - mu) / sig A1 = TMath.Power(p1 / TMath.Abs(a1), p1) * TMath.Exp(-a1 * a1 / 2.0) A2 = TMath.Power(p2 / TMath.Abs(a2), p2) * TMath.Exp(-a2 * a2 / 2.0) B1 = p1 / TMath.Abs(a1) - TMath.Abs(a1) B2 = p2 / TMath.Abs(a2) - TMath.Abs(a2) result = N if (u < -a1): result *= A1 * TMath.Power(B1 - u, -p1) if (-a1 < u < a2): result *= TMath.Exp(-u * u / 2.0) if (u > a2): result *= A2 * TMath.Power(B2 + u, -p2) return result
def __call__(self, x, par): out = 0.0 gaus1 = 0.0 gaus2left = 0.0 gaus2right = 0.0 gaus1 = par[0] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[2] * par[2])) gaus2left = par[5] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[3] * par[3])) gaus2right = par[5] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[4] * par[4])) cc = par[2] * par[3] * par[4] if x[0] <= par[1] and cc > 0: out = gaus1 + gaus2left if x[0] > par[1] and cc > 0: out = gaus1 + gaus2right back = par[6] + par[7] * x[0] # add backg backout = out + back return backout
def __call__(self, x, par): x1 = par[0] * TMath.Power(x[0], -par[1]) x2 = TMath.Exp(-par[2] * x[0]) x3 = TMath.Exp(-par[3] * x[0] * x[0]) # x3=TMath.Power(x[0]*x[0],-par[1]) # x3=TMath.Exp(-par[3]*TMath.Sqrt(x[0])) # x3=par[3]*TMath.Gaus(x[0],par[4],par[5]) return x1 * x2 * x3
def get_lifetime_weight(tree, ctau, ctau_MC): if len(tree.Rhadron_properdecaytime) != 2: #print('vector size of Rhadron_properdecaytime is not 2. return weight=0.') return 0 dt1 = tree.Rhadron_properdecaytime[0] * 1e-9 # [ns]->[s] dt2 = tree.Rhadron_properdecaytime[1] * 1e-9 # [ns]->[s] tau = ctau / TMath.C() tau_MC = ctau_MC / TMath.C() weight_rhad1 = tau_MC / tau * TMath.Exp(dt1 / tau_MC - dt1 / tau) weight_rhad2 = tau_MC / tau * TMath.Exp(dt2 / tau_MC - dt2 / tau) return weight_rhad1 * weight_rhad2
def __call__(self, x, par): out = 0.0 gaus1 = 0.0 gaus2 = 0.0 gaus1 = par[0] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[2] * par[2])) gaus2 = par[4] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[3] * par[3])) cc = par[2] * par[3] if cc > 0: out = gaus1 + gaus2 return out
def __call__(self, x, par): out = 0 t = (x[0] - par[1]) / par[2] if (par[0] < 0): t = -t absAlpha = TMath.Abs(par[3]) if (t >= -absAlpha): out = par[0] * TMath.Exp(-0.5 * t * t) else: a = TMath.Power(par[4] / absAlpha, par[4]) * TMath.Exp( -0.5 * absAlpha * absAlpha) b = par[4] / absAlpha - absAlpha out = par[0] * (a / TMath.Power(b - t, par[4])) return out
def _ExpoIntegralNorm(self, x, par): ''' Exponential function normalized to its integral. See AliHFInvMassFitter::FitFunction4Bkg for more information. Parameters ---------- - x: function variable - par: function parameters par[0]: normalisation (integral of background) par[1]: expo slope ''' norm = par[0] * par[1] / (TMath.Exp(par[1] * self.maxMass) - TMath.Exp(par[1] * self.minMass)) return norm * TMath.Exp(par[1] * x[0])
def __init__(self, params): # https://github.com/rmanzoni/triggertools/blob/master/objects/FitFunctions.py#L153 alpha = params['alpha'] sigma = params['sigma'] norm = params['norm'] m0 = params['m_{0}'] n = params['n'] sqrtPi2 = sqrt(pi / 2) sqrt2 = sqrt(2.) absSig = abs(sigma) absAlpha = abs(alpha / absSig) a = (n / absAlpha)**n * TMath.Exp(-0.5 * absAlpha * absAlpha) b = absAlpha - n / absAlpha arg = absAlpha / sqrt2 erf = 1 if arg > 5. else -1 if arg < -5. else TMath.Erf(arg) leftArea = (1. + erf) * sqrtPi2 rightArea = (a * 1. / (absAlpha - b)**(n - 1)) / (n - 1) area = leftArea + rightArea self.norm = norm self.alpha = alpha self.sigma = sigma self.m0 = m0 self.nm1 = n - 1 self.absAlpha = absAlpha self.b = b self.sqrt2 = sqrt2 self.norm1 = norm * sqrtPi2 / area self.norm2 = norm * a / (1 - n) / area self.sigma1 = absSig * alpha / abs(alpha) self.A = norm * leftArea / area - self.norm2 / (absAlpha - b)**self.nm1
def __call__(self, x, par): x1 = par[0] * TMath.Power(x[0] - 60, -par[1]) # x2=TMath.Exp(-1.0*(TMath.Power(par[2]*(x[0]-60),2))) # x2=TMath.Exp(-par[2]*(x[0]-60))*TMath.Exp(-1.0*(TMath.Power(par[3]*(x[0]-60),2))) x2 = TMath.Exp(-1.0 * (TMath.Power(par[2] * (x[0] - 60), 2))) # x2=TMath.Exp(-par[2]*(x[0]-60)) return x1 * x2
def func_DDPIPI(x, par): ''' function for correlated breit wigner: e+e- --> DDpipi ''' xx = x[0] left = par[2] / xx numerator = sqrt(12.0*3.141592653589793*par[3]*par[4]) denominator = complex(xx*xx-par[2]*par[2], par[2]*par[3]) middle = numerator/denominator bw = left*middle return par[0] * pow(xx, -2) * TMath.Exp(-1 * par[1] * (xx - 4.015)) + pow(abs(bw), 2)
def __call__(self, x, par): out = 0.0 gaus1 = 0.0 gaus2left = 0.0 gaus2right = 0.0 gaus1 = par[0] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[2] * par[2])) gaus2left = par[5] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[3] * par[3])) gaus2right = par[5] * TMath.Exp( (-0.5) * (x[0] - par[1]) * (x[0] - par[1]) / (par[4] * par[4])) cc = par[2] * par[3] * par[4] if x[0] <= par[1] and cc > 0: out = gaus1 + gaus2left if x[0] > par[1] and cc > 0: out = gaus1 + gaus2right return out
def _ExpoPowIntegralNorm(self, x, par): ''' Exponential times power law function normalized to its integral for D* background. Parameters ---------- - x: function variable - par: function parameters par[0]: normalisation (integral of background) par[1]: expo slope ''' return par[0] * TMath.Sqrt(x[0] - self.mPi) * TMath.Exp( -1. * par[1] * (x[0] - self.mPi))
def ExpoPowLaw(x, par): ''' Exponential times power law function Parameters ---------- - x: function variable - par: function parameters par[0]: normalisation par[1]: mass (lowest possible value) par[2]: expo slope ''' return par[0] * TMath.Sqrt(x[0] - par[1]) * TMath.Exp(-1. * par[2] * (x[0] - par[1]))
def fitAlphaBeta(x, par): trise = par[0] tmax = par[1] a = par[2] amp = par[3] term1 = (x[0] - (tmax - trise)) / (trise + 0.001) term2 = (x[0] - tmax) / (trise + 0.001) pi = 3.14 f = 0 if (x[0] > (tmax - trise)): f = amp * pow(term1, a) * TMath.Exp(-a * term2) else: f = 0 return f
from tools.directory_manager import * from tools.sample_manager import * from tools.style_manager import * import argparse from ROOT import TFile, TH1, TCanvas, TH1F, THStack, TMath from ROOT import TLegend, TApplication, TRatioPlot, TPad ################################################################################ ## Initialisation stuff ################################################################################ nbin_time = 12 ntemoin = 20 temoin = TH1F("essaie", "essaie", ntemoin, 0, 10) for i in range(ntemoin): temoin.SetBinContent(i+1,TMath.Exp(i/3.14)) time_histo = TH1F("time", "time", nbin_time*ntemoin, 0, nbin_time) for i in range(nbin_time): for j in range(ntemoin): time_histo.SetBinContent(ntemoin*i + j+1, temoin.GetBinContent(j+1)) time_histo.Draw() exit = raw_input("Press key to quit : ")
def GenWeightProducer(sample, nGenPar, genParId, genMomParId, genParSt, genParP4): pt__ = 0 #print " inside gen weight " k2 = 1.0 ################# # WJets ################# if sample == "WJETS": goodLepID = [] for ig in range(nGenPar): PID = genParId[ig] momPID = genMomParId[ig] status = genParSt[ig] if ((abs(PID) != 11) & (abs(PID) != 12) & (abs(PID) != 13) & (abs(PID) != 14) & (abs(PID) != 15) & (abs(PID) != 16)): continue if (((status != 1) & (abs(PID) != 15)) | ((status != 2) & (abs(PID) == 15))): continue if ((abs(momPID) != 24) & (momPID != PID)): continue goodLepID.append(ig) if len(goodLepID) == 2: l4_thisLep = genParP4[goodLepID[0]] l4_thatLep = genParP4[goodLepID[1]] l4_z = l4_thisLep + l4_thatLep pt = l4_z.Pt() pt__ = pt k2 = -0.830041 + 7.93714 * TMath.Power(pt - (-877.978), (-0.213831)) ################# #ZJets ################# if sample == "ZJETS": goodLepID = [] for ig in range(nGenPar): PID = genParId[ig] momPID = genMomParId[ig] status = genParSt[ig] if ((abs(PID) != 12) & (abs(PID) != 14) & (abs(PID) != 16)): continue if (status != 1): continue if ((momPID != 23) & (momPID != PID)): continue goodLepID.append(ig) if len(goodLepID) == 2: l4_thisLep = genParP4[goodLepID[0]] l4_thatLep = genParP4[goodLepID[1]] l4_z = l4_thisLep + l4_thatLep pt = l4_z.Pt() k2 = -0.180805 + 6.04146 * TMath.Power(pt - (-759.098), (-0.242556)) ################# #TTBar ################# if (sample == "TT"): goodLepID = [] for ig in range(nGenPar): PID = genParId[ig] momPID = genMomParId[ig] status = genParSt[ig] if (abs(PID) == 6): goodLepID.append(ig) if (len(goodLepID) == 2): l4_thisLep = genParP4[goodLepID[0]] l4_thatLep = genParP4[goodLepID[1]] pt1 = TMath.Min(400.0, l4_thisLep.Pt()) pt2 = TMath.Min(400.0, l4_thatLep.Pt()) w1 = TMath.Exp(0.156 - 0.00137 * pt1) w2 = TMath.Exp(0.156 - 0.00137 * pt2) k2 = 1.001 * TMath.Sqrt(w1 * w2) if (sample == "all"): k2 = 1.0 return k2
def __call__(self, x, par): x1 = par[0] * TMath.Power(x[0], -par[1]) x2 = TMath.Exp(-par[2] * x[0]) x3 = TMath.Exp(-par[3] * (TMath.Power(x[0], 2))) return x1 * x2 * x3
def func_DDPIPI(x, par): ''' function for correlated breit wigner: e+e- --> DDpipi ''' xx = x[0] return par[0] * pow(xx, -2) * TMath.Exp( -1 * par[1] * (xx - 4.015)) + par[2] * TMath.Gaus(xx, par[3], par[4])
def __call__(self, x, par): x1 = par[0] * TMath.Power(x[0] - 60, -par[1]) x2 = TMath.Exp(-1.0 * (TMath.Power(par[2] * (x[0] - 60), 2))) out = x1 * x2 + par[3] * TMath.Gaus(x[0], par[4], par[5]) return out
def DiracFermiFunction(E, Z, m): beta = TMath.Sqrt(2 * E / m) eta = fine_structure_constant() * (Z + 1) / beta return 2 * TMath.Pi() * eta / (1 - TMath.Exp(-2 * TMath.Pi() * eta))
def integrand_init(interp, mass, Elow, Eup): print("\n\n--- INITIALIZING MASS:", mass, " Low Limit ", Elow, " , Up Lim: ", Eup, "\n") global inter_mean1 global inter_mean2 global inter_sigma1 global inter_sigma2 global inter_part global ene_on global integrand global normon global numtoton # integrand = [] for k in range(0, config.nsamples): # k= number of samples integrand.append( np.empty(shape=(numtoton[k], config.BinsIntegrand), dtype=config.ArrayDataType)) pbar = tqdm(total=numtoton[k] * config.BinsIntegrand) for i in range(0, numtoton[k]): energy = ene_on[k][i] mean1 = energy - inter_mean1[k]( energy) * energy # pEnBiasVsEnEst = ( E' - E ) / E' mean2 = energy - inter_mean1[k]( energy) * energy # pEnBiasVsEnEst = ( E' - E ) / E' sigma1 = inter_sigma1[k](energy) * energy sigma2 = inter_sigma2[k](energy) * energy part = inter_part[k](energy) # define the integration limits for true energy lowl = mean2 - config.SigmaLimits * sigma2 highl = mean2 + config.SigmaLimits * sigma2 if (lowl < config.energy_absmin): lowl = config.energy_absmin # define the bins in true energy binwidth = (highl - lowl) / config.BinsIntegrand for j in range(0, config.BinsIntegrand): entrue = lowl + (j + 0.5) * binwidth pbar.update(1) area = get_area(entrue, k) flux = 0. if (Elow < entrue < Eup): flux = get_phi(entrue, interp, mass) enmig = part * gaussian(entrue, mean1, sigma1) + ( 1. - part) * gaussian(entrue, mean2, sigma2) integrand[k][i][j] = flux * area * enmig * binwidth pbar.close() print("Starting Normalization\n") integrand_norm.append( np.empty(shape=(config.BinsIntegrandNorm, config.BinsIntegrand), dtype=config.ArrayDataType)) on_en_edges = [] binsize = TMath.Log(config.energy_upper_cut / config.energy_cut) / config.BinsIntegrandNorm for ebin in range(0, config.BinsIntegrandNorm + 1): on_en_edges.append(config.energy_cut * TMath.Exp(binsize * ebin)) for ebin in range(0, config.BinsIntegrandNorm): energy = math.sqrt(on_en_edges[ebin + 1] * on_en_edges[ebin]) mean1 = energy - inter_mean1[k]( energy) * energy # pEnBiasVsEnEst = ( E' - E ) / E' mean2 = energy - inter_mean1[k]( energy) * energy # pEnBiasVsEnEst = ( E' - E ) / E' sigma1 = inter_sigma1[k](energy) * energy sigma2 = inter_sigma2[k](energy) * energy part = inter_part[k](energy) # define the integration limits for true energy lowl = mean2 - config.SigmaLimits * sigma2 highl = mean2 + config.SigmaLimits * sigma2 if (lowl < config.energy_absmin): lowl = config.energy_absmin # define the bins in true energy binwidth = (highl - lowl) / config.BinsIntegrand for j in range(0, config.BinsIntegrand): entrue = lowl + (j + 0.5) * binwidth pbar.update(1) area = get_area(entrue, k) flux = 0. if ((Elow < entrue) and (entrue < Eup)): flux = get_phi(entrue, interp, mass) enmig = part * gaussian(entrue, mean1, sigma1) + ( 1. - part) * gaussian(entrue, mean2, sigma2) integrand_norm[k][ebin][j] = flux * area * enmig * ( on_en_edges[ebin + 1] - on_en_edges[ebin]) normon[k] = np.sum(integrand_norm[k]) print("Normalization=", normon[k]) # enmig.append(part*gaussian(entrue,mean1,sigma1)+(1.-part)*gaussian(entrue,mean2,sigma2)) # print ('mean1=', mean1, ' mean2= ', mean2, ' sigma1= ', sigma1, ' sigma2= ', sigma2, ' part=',part) print("--- INITIALIZATION FINISHED!\n") return
def __call__(self, x, par): x1 = par[0] * TMath.Power(x[0], -par[1]) x2 = TMath.Exp(-par[2] * x[0]) out = x1 * x2 + par[3] * TMath.Gaus(x[0], par[4], par[5]) return out
def UnormalizedSquarePoisson(y, l): ''' Return the probability density function for Y=sqrt(X) when X follows a Poisson distribution with param lambda ''' return 2 * y * TMath.Exp(-l) * TMath.Pow(l, y * y) / TMath.tgamma(y * y)
def GetTopPtReWeighting(self, pt1, pt2): w1 = TMath.Exp(0.0615 - 0.0005 * pt1); w2 = TMath.Exp(0.0615 - 0.0005 * pt2); return TMath.Sqrt(w1*w2)
dfFONLL['pt'] = dfFONLL.apply(lambda row: (row['ptmin'] + row['ptmax']) / 2) crossSecD = dfFONLL['central'].to_numpy().copy() ptCent = dfFONLL['pt'].to_numpy() for col in dfFONLL.columns: if 'pt' not in col: if args.Lc: dfFONLL[col] = dfFONLL.apply( lambda row: row[col] * (0.11 + 4.68735e-01 * TMath.Gaus(row['pt'], 1, 4.90037)), axis=1) if args.Lb: dfFONLL[col] = dfFONLL.apply( lambda row: row[col] * (7.93e-2 + TMath.Exp(-1.022 - 0.107 * (row['pt'] if row['pt'] > 4 else 4))), axis=1) crossSecLc = dfFONLL['central'].to_numpy().copy() gD, gLc = TGraph(0), TGraph(0) for iPt, (pt, d, lc) in enumerate(zip(ptCent, crossSecD, crossSecLc)): gD.SetPoint(iPt, pt, d) gLc.SetPoint(iPt, pt, lc) SetObjectStyle(gLc, color=kRed + 1, markerstyle=kOpenCircle) SetObjectStyle(gD, color=kAzure + 1, markerstyle=kFullDiamond) dfFONLL.to_csv(args.inFileName.replace('.txt', '_Mod.txt'), sep=' ', float_format='%.3f',
def GenWeightProducer(sample, nGenPar, genParId, genMomParId, genParSt, genParP4): pt__ = 0 #print " inside gen weight " k2 = 1.0 ################# # WJets ################# if sample == "WJETS": goodLepID = [] for ig in range(nGenPar): PID = genParId[ig] momPID = genMomParId[ig] status = genParSt[ig] #print "inside WJ loop pdgid", PID #print ("if status =", (abs(PID) != 11),( abs(PID) != 12),( abs(PID) != 13 ),( abs(PID) != 14),( abs(PID) != 15),( abs(PID) != 16)) #print "and of if status ", ( (abs(PID) != 11) & (abs(PID) != 12) & (abs(PID) != 13) & (abs(PID) != 14) & (abs(PID) != 15) & (abs(PID) != 16) ) if ((abs(PID) != 11) & (abs(PID) != 12) & (abs(PID) != 13) & (abs(PID) != 14) & (abs(PID) != 15) & (abs(PID) != 16)): continue #print "lepton found" if (((status != 1) & (abs(PID) != 15)) | ((status != 2) & (abs(PID) == 15))): continue #print "tau found" if ((abs(momPID) != 24) & (momPID != PID)): continue #print "W found" #print "aftrer WJ if statement" goodLepID.append(ig) #print "length = ",len(goodLepID) if len(goodLepID) == 2: l4_thisLep = genParP4[goodLepID[0]] l4_thatLep = genParP4[goodLepID[1]] l4_z = l4_thisLep + l4_thatLep pt = l4_z.Pt() pt__ = pt print " pt inside " k2 = -0.830041 + 7.93714 * TMath.Power(pt - (-877.978), (-0.213831)) ################# #ZJets ################# if sample == "ZJETS": print " inside zjets " goodLepID = [] for ig in range(nGenPar): # print " inside loop " PID = genParId[ig] momPID = genMomParId[ig] status = genParSt[ig] # print " after vars " if ((abs(PID) != 12) & (abs(PID) != 14) & (abs(PID) != 16)): continue if (status != 1): continue if ((momPID != 23) & (momPID != PID)): continue goodLepID.append(ig) if len(goodLepID) == 2: l4_thisLep = genParP4[goodLepID[0]] l4_thatLep = genParP4[goodLepID[1]] l4_z = l4_thisLep + l4_thatLep pt = l4_z.Pt() print " pt inside " k2 = -0.180805 + 6.04146 * TMath.Power(pt - (-759.098), (-0.242556)) ################# #TTBar ################# if (sample == "TT"): print " inside ttbar " goodLepID = [] for ig in range(nGenPar): print "inside TT loop " PID = genParId[ig] momPID = genMomParId[ig] status = genParSt[ig] if (abs(PID) == 6): goodLepID.append(ig) if (len(goodLepID) == 2): l4_thisLep = genParP4[goodLepID[0]] l4_thatLep = genParP4[goodLepID[1]] pt1 = TMath.Min(400.0, l4_thisLep.Pt()) pt2 = TMath.Min(400.0, l4_thatLep.Pt()) w1 = TMath.Exp(0.156 - 0.00137 * pt1) w2 = TMath.Exp(0.156 - 0.00137 * pt2) k2 = 1.001 * TMath.Sqrt(w1 * w2) if (sample == "all"): k2 = 1.0 return k2
def off_energy_distr(OffDir, NumBckgEnBins=40, EnCutHighBG=8000, EnCutLowBG=100): """ this function does: - read the OFF sample input files - """ global normoff global numtotoff global numsamples for k in range(0, numsamples): print("\n\n--- INITIALIZING VALUES from:", OffDir[k], "\n") DoHisto = TFile.Open(OffDir[k], "READ") print(DoHisto) noff = np.array([DoHisto.Get("fOffSample")]) numtotoff[k] = noff[0].GetEntries() print("The total number of entries in the normalization histogram is:", numtotoff[k]) off_en_edges = [] binsize = TMath.Log(EnCutHighBG / EnCutLowBG) / config.NumBckgEnBins for ebin in range(0, config.NumBckgEnBins + 1): off_en_edges.append(EnCutLowBG * TMath.Exp(binsize * ebin)) # create here offbins with the (larger) background limits offbins = array('d', off_en_edges) isto = ROOT.TH1F("integral", "integral", config.NumBckgEnBins, offbins) for i in range(0, numtotoff[k]): noff[0].GetEntry(i) energy = noff[0].GetArgs()[0] isto.Fill(energy) f = TFile('normalization_histogram_sample_{}.root'.format(k), "RECREATE") gramma = TCanvas("EOff_Histogram", 'EOFF_Histogram sample {}'.format(k)) gramma.SetCanvasSize(800, 800) gramma.SetWindowSize(1000, 1000) gramma.SetLogx() gramma.SetLogy() isto.SetXTitle("Log Eest") isto.SetYTitle("Log events") isto.Draw() ll = isto.GetXaxis().FindBin(EnCutLowBG) hh = isto.GetXaxis().FindBin(EnCutHighBG) line = TLine() line.SetLineColor(TColor.kRed) line.DrawLine(ll, isto.GetMinimum(), ll, isto.GetMaximum()) line.DrawLine(hh, isto.GetMinimum(), hh, isto.GetMaximum()) # gPad.Update() normoff[k] = isto.Integral(ll, hh) print("The integral from ", ll, " to ", hh, " of the normalization histogram is:", normoff[k]) latex = TLatex() latex.DrawLatex( ll, isto.GetMaximum() / 2, "The integral of the normalization histogram is: {}".format( normoff[k])) gramma.Write() f.Close() return