Beispiel #1
0
def push_array():
    global array_template, current_array
    if array_template is None:
        array_template = arr('I')
        array_template.append(0)
        while len(array_template) < STATE_SIZE:
            array_template.extend(array_template)
        assert len(array_template) == STATE_SIZE
    result = arr('I', array_template)
    current_array = result
    arrays.append(result)
Beispiel #2
0
def _labels(data):
    orig = sys.gettrace()
    sys.settrace(None)
    try:
        labels = arr('I')
        for i in _range(len(data)):
            a = i << 4
            b = data[i]
            if b > 0:
                labels.append(a + 1)
            if b > 1:
                labels.append(a + 2)
            if b > 2:
                labels.append(a + 3)
            if b > 3:
                labels.append(a + 4)
            if b > 4:
                labels.append(a + 5)
            if b > 8:
                labels.append(a + 6)
            if b > 16:
                labels.append(a + 7)
            if b > 32:
                labels.append(a + 8)
            if b > 64:
                labels.append(a + 9)
            if b > 128:
                labels.append(a + 10)
        return labels
    finally:
        sys.settrace(orig)
Beispiel #3
0
    def minimize(self, final_fit=True, log_print_level=2):
        '''Do the minimization. This calls `Minuit`'s algorithms ``MIGRAD``
        for minimization and, if `final_fit` is `True`, also ``HESSE``
        for computing/checking the parameter error matrix.'''

        # Set the FCN again. This HAS to be done EVERY
        # time the minimize method is called because of
        # the implementation of SetFCN, which is not
        # object-oriented but sets a global pointer!!!
        logger.debug("Updating current FCN")
        self.__gMinuit.SetFCN(self.FCN_wrapper)

        # Run minimization algorithm (MIGRAD + HESSE)
        error_code = Long(0)

        prefix = "Minuit run on"  # set the timestamp prefix

        # insert timestamp
        self.out_file.write('\n')
        self.out_file.write('#'*(len(prefix)+4+20))
        self.out_file.write('\n')
        self.out_file.write("# %s " % (prefix,) +
                            strftime("%Y-%m-%d %H:%M:%S #\n", gmtime()))
        self.out_file.write('#'*(len(prefix)+4+20))
        self.out_file.write('\n\n')
        self.out_file.flush()

        # save the old stdout stream
        old_out_stream = os.dup(sys.stdout.fileno())
        os.dup2(self.out_file.fileno(), sys.stdout.fileno())

        self.__gMinuit.SetPrintLevel(log_print_level)  # set Minuit print level
        logger.debug("Running MIGRAD")
        self.__gMinuit.mnexcm("MIGRAD",
                              arr('d', [self.max_iterations, self.tolerance]),
                              2, error_code)
        if(final_fit):
            logger.debug("Running HESSE")
            self.__gMinuit.mnexcm("HESSE", arr('d', [self.max_iterations]), 1, error_code)
        # return to normal print level
        self.__gMinuit.SetPrintLevel(self.print_level)

        # restore the previous output stream
        os.dup2(old_out_stream, sys.stdout.fileno())
Beispiel #4
0
    def set_err(self, up_value=1.0):
        '''Sets the ``UP`` value for Minuit.

        *up_value* : float (optional, default: 1.0)
            This is the value by which `FCN` is expected to change.
        '''
        # Tell TMinuit to use an up-value of 1.0
        error_code = Long(0)
        # execute SET ERR command
        self.__gMinuit.mnexcm("SET ERR", arr('d', [up_value]), 1, error_code)
Beispiel #5
0
    def set_strategy(self, strategy_id=1):
        '''Sets the strategy Minuit.

        *strategy_id* : int (optional, default: 1 (optimized))
            Tells ``TMinuit`` to use a certain strategy. Refer to ``TMinuit``'s
            documentation for available strategies.
        '''
        error_code = Long(0)
        # execute SET STRATEGY command
        self.__gMinuit.mnexcm("SET STRATEGY",
                              arr('d', [strategy_id]), 1, error_code)
Beispiel #6
0
    def fix_parameter(self, parameter_number):
        '''
        Fix parameter number <`parameter_number`>.

        **parameter_number** : int
            Number of the parameter to fix.
        '''
        error_code = Long(0)
        logger.info("Fixing parameter %d in Minuit" % (parameter_number,))
        # execute FIX command
        self.__gMinuit.mnexcm("FIX",
                              arr('d', [parameter_number+1]), 1, error_code)
Beispiel #7
0
    def release_parameter(self, parameter_number):
        '''
        Release parameter number <`parameter_number`>.

        **parameter_number** : int
            Number of the parameter to release.
        '''
        error_code = Long(0)
        logger.info("Releasing parameter %d in Minuit" % (parameter_number,))
        # execute RELEASE command
        self.__gMinuit.mnexcm("RELEASE",
                              arr('d', [parameter_number+1]), 1, error_code)
Beispiel #8
0
    def get_error_matrix(self):
        '''Retrieves the parameter error matrix from TMinuit.

        return : `numpy.matrix`
        '''

        # set up an array of type `double' to pass to TMinuit
        tmp_array = arr('d', [0.0]*(self.number_of_parameters**2))
        # get parameter covariance matrix from TMinuit
        self.__gMinuit.mnemat(tmp_array, self.number_of_parameters)
        # reshape into 2D array
        return np.asmatrix(
            np.reshape(
                tmp_array,
                (self.number_of_parameters, self.number_of_parameters)
            )
        )
Beispiel #9
0
    def minos_errors(self):
        '''
           Get (asymmetric) parameter uncertainties from MINOS
           algorithm. This calls `Minuit`'s algorithms ``MINOS``,
           which determines parameter uncertainties using profiling
           of the chi2 function.

           returns : tuple
             A tuple of [err+, err-, parabolic error, global correlation]
        '''

        # Set the FCN again. This HAS to be done EVERY
        # time the minimize method is called because of
        # the implementation of SetFCN, which is not
        # object-oriented but sets a global pointer!!!
        logger.debug("Updating current FCN")
        self.__gMinuit.SetFCN(self.FCN_wrapper)

        # save the old stdout stream
        old_out_stream = os.dup(sys.stdout.fileno())
        os.dup2(self.out_file.fileno(), sys.stdout.fileno())

        self.__gMinuit.SetPrintLevel(1)  # verboseness level 1 is sufficient
        logger.debug("Running MINOS")
        error_code = Long(0)
        self.__gMinuit.mnexcm("MINOS", arr('d', [self.max_iterations]), 1, error_code)

        # return to normal print level
        self.__gMinuit.SetPrintLevel(self.print_level)

        # restore the previous output stream
        os.dup2(old_out_stream, sys.stdout.fileno())

        output = []
        errpos=Double(0) # positive parameter error
        errneg=Double(0) # negative parameter error
        err=Double(0)    # parabolic error
        gcor=Double(0)   # global correlation coefficient

        for i in xrange(0, self.number_of_parameters):
            self.__gMinuit.mnerrs(i, errpos, errneg, err, gcor)
            output.append([float(errpos),float(errneg),float(err),float(gcor)])

        return output
Beispiel #10
0
    def minos_errors(self, log_print_level=1):
        '''
           Get (asymmetric) parameter uncertainties from MINOS
           algorithm. This calls `Minuit`'s algorithms ``MINOS``,
           which determines parameter uncertainties using profiling
           of the chi2 function.

           returns : tuple
             A tuple of [err+, err-, parabolic error, global correlation]
        '''

        # Set the FCN again. This HAS to be done EVERY
        # time the minimize method is called because of
        # the implementation of SetFCN, which is not
        # object-oriented but sets a global pointer!!!
        logger.debug("Updating current FCN")
        self.__gMinuit.SetFCN(self.FCN_wrapper)

        # redirect stdout stream
        _redirection_target = None
        if log_print_level >= 0:
            _redirection_target = self.out_file

        with redirect_stdout_to(_redirection_target):
            self.__gMinuit.SetPrintLevel(log_print_level)
            logger.debug("Running MINOS")
            error_code = Long(0)
            self.__gMinuit.mnexcm("MINOS", arr('d', [self.max_iterations]), 1, error_code)

            # return to normal print level
            self.__gMinuit.SetPrintLevel(self.print_level)


        output = []
        errpos=Double(0) # positive parameter error
        errneg=Double(0) # negative parameter error
        err=Double(0)    # parabolic error
        gcor=Double(0)   # global correlation coefficient

        for i in range(0, self.number_of_parameters):
            self.__gMinuit.mnerrs(i, errpos, errneg, err, gcor)
            output.append([float(errpos),float(errneg),float(err),float(gcor)])

        return output
Beispiel #11
0
def merge_arrays(x, y):
    result = arr('I')
    xi = 0
    yi = 0
    while xi < len(x) and yi < len(y):
        xv = x[xi]
        yv = y[yi]
        if xv < yv:
            result.append(xv)
            xi += 1
        elif xv > yv:
            result.append(yv)
            yi += 1
        else:
            result.append(xv)
            xi += 1
            yi += 1
    while xi < len(x):
        result.append(x[xi])
        xi += 1
    while yi < len(y):
        result.append(y[yi])
        yi += 1
    return result
#   arg2 can be specified as "bkg" or "sig" for background and signal modeling
#Example1(plot default efficiencies): python Step2_PlotAll.py
#Example2(systematic -- bkg modeling): python Step2_PlotAll.py /uscms/home/zhangjin/nobackup/ bkg
#Example3(systematic -- sig modeling): python Step2_PlotAll.py /uscms/home/zhangjin/nobackup/ sig
#Example2 and 3 are used for systematic calculation.
##################################################################################

from  ROOT import *
from  numpy import *
from Config import *
gROOT.SetStyle("Plain")
gStyle.SetPaintTextFormat("0.3g")
gStyle.SetOptStat(0)

from array import array as arr
Red = arr('d',[0.00, 0.00, 0.00, 1.00, 1.00])
Green = arr('d',[0.00, 0.10, 1.00, 1.00, 0.00])
Blue = arr('d',[1.00, 0.90, 0.00, 0.00, 0.00])
Length = arr('d',[0.00, 0.85, 0.90, 0.95, 1.00])
TColor.CreateGradientColorTable(5,Length,Red,Green,Blue,500)
gStyle.SetNumberContours(500)

import sys,os
if (sys.argv[0] == "python"): args=sys.argv[2:]
else: args=sys.argv[1:]

Prefix=""
Postfix=""
if len(args)>0:
    Prefix=args[0]
    if Prefix[-1] != "/":
Beispiel #13
0
global kor_matrix
kor_matrix=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
  kor_matrix[i][i] = error[i]*error[i]
  print 'error = ',error[i]

global kor_matrix_inv
kor_matrix_inv = inv(kor_matrix)

#print kor_matrix_inv


minuit = TMinuit(npar) #initialize TMinuit with a maximum of 2 params
minuit.SetFCN(fcn)
arglist = arr('d', 2*[0.01])
ierflg = Long(0)
arglist[0] = 1.

minuit.mnexcm("SET ERR", arglist, 1, ierflg)

# Set starting values and step sizes for parameters
vstart = [8., 1.]
step = [0.01 , 0.01]
minuit.mnparm(0, "y", vstart[0], step[0], 0,0,ierflg)
minuit.mnparm(1, "N", vstart[1], step[1], 0,0,ierflg)


# Now ready for minimization step
arglist[0] = 500
arglist[1] = 1.
Beispiel #14
0
,'f_s_mKK2'
,'delta_s_perp_mKK2'
,'f_s_mKK3'
,'delta_s_perp_mKK3'
,'f_s_mKK4'
,'delta_s_perp_mKK4'
,'f_s_mKK5'
,'delta_s_perp_mKK5'
]

names = ['phis','lambda']
#names = ['gamma_act', 'd_gamma', 'ap_zero_2', 'ap_perp_2']

for i in range(num_params):
	minuit.mnparm(i, names[i],  0.0, 0.01, 0, 0, error_code)
minuit.mnexcm("migrad", arr('d', [6000, 0.01]), 2, error_code)
minuit.mnexcm("hesse", arr('d', [6000]), 1, error_code)

if (false): # for making NLL scans
   for i in range(num_params):
	c = ROOT.TCanvas()
	minuit.mnexcm("SCAN",  arr('d', [i, 20]), 2, error_code);
	gr = minuit.GetPlot()
	gr.Draw("AL")
	gr.GetXaxis().SetTitle(names[i])
	c.SaveAs(names[i]+".pdf")

#par0, par1 = ROOT.Double(0), ROOT.Double(0)
#par0e, par1e = ROOT.Double(0), ROOT.Double(0)
#minuit.GetParameter(0, par0, par0e)
#minuit.GetParameter(1, par1, par1e)
#Green = arr('d',[0.00, 0.10, 1.00, 1.00, 0.00])
#Blue = arr('d',[1.00, 0.90, 0.00, 0.00, 0.00])
#Length = arr('d',[0.00, 0.40, 0.60, 0.80, 1.00])
#TColor.CreateGradientColorTable(5,Length,Red,Green,Blue,500)

#Red = arr('d',[.00, 1.00, 1.00])
#Green = arr('d',[1.00, 1.00, 0.00])
#Blue = arr('d',[0.00, 0.00, 0.00])
#Length = arr('d',[0.00, 0.5, 1.00])
#TColor.CreateGradientColorTable(3,Length,Red,Green,Blue,500)
#Red = arr('d',[1.00, 1.00, 1.00, .00])
#Green = arr('d',[0., 0.00, 1.00, 1.00])
#Blue = arr('d',[1.00, 0.00, 0.00, 0.00])
#Length = arr('d',[0.00, 0.1, 0.55, 1.00])
#TColor.CreateGradientColorTable(4,Length,Red,Green,Blue,500)
Red = arr('d',[0.,1.00, 1.00, 1.00, .00])
Green = arr('d',[0.,0., 0.00, 1.00, 1.00])
Blue = arr('d',[1.0,1.00, 0.00, 0.00, 0.00])
Length = arr('d',[0.00, 0.4, 0.6, 0.8, 1.00])
TColor.CreateGradientColorTable(5,Length,Red,Green,Blue,500)

gStyle.SetNumberContours(500)

def unshitify(pave):
    pave.SetFillStyle(0)
    pave.SetBorderSize(0)

cms_label = ROOT.TPaveText(0.06, 0.84, 0.9, 1.0, "NDC")
unshitify(cms_label)
cms_label.SetTextSize(0.03)
cms_label.SetTextAlign(12)
from array import array as arr

def IPtoBinary(addr):
  return unpack("!L", socket.inet_aton(addr))[0]

version = 5
numFlows = int(raw_input("Enter number of flows: "))
uptime = int(raw_input("Enter device uptime (millis): "))
epochMillis = int(raw_input("Enter system clock in millis (since Epoch): "))
epochNano = int(raw_input("Residual nanoseconds (since Epoch): "))
totalFlowsSeen = int(raw_input("Total flows seen since boot: "))
engineType = int(raw_input("Engine type: "))
engineID = int(raw_input("Engine ID: "))
samplingInterval = int(raw_input("Sampling Interval: "))

pktPayload = arr("c", (24 + (numFlows * 48)) * "\0")

pack_into("!HHLLLLBBH", pktPayload, 0, version, numFlows, uptime, epochMillis, epochNano, totalFlowsSeen, engineType, engineID, samplingInterval)

offset = 24
  
for i in range(numFlows):
  print "=== Flow #"+str(i+1)+" ===" 
  srcIP = IPtoBinary(raw_input("Source IP: "))
  dstIP = IPtoBinary(raw_input("Destination IP: "))
  nextIP = IPtoBinary(raw_input("IP of next hop: "))
  ifIn = int(raw_input("SNMP Input Index: "))
  ifOut = int(raw_input("SNMP Output Index: "))
  numPkts = int(raw_input("Number of Packets in Flow: "))
  l3bytes = int(raw_input("Total Layer 3 Bytes: "))
  flowStart = int(raw_input("Flow Start Time: "))
Beispiel #17
0
    def get_profile(self, parid, n_points=21):
        '''
        Returns a list of points (2-tuples) the profile
        the :math:`\\chi^2`  of the TMinuit fit.


        **parid** : int
            ID of the parameter to be displayed on the `x`-axis.

        *n_points* : int (optional)
            number of points used for profile. Default is 21.

        *returns* : two arrays, par. values and corresp. :math:`\\chi^2`
            containing ``n_points`` sampled profile points.
        '''

        self.out_file.write('\n')
        # entry in log-file
        self.out_file.write('\n')
        self.out_file.write('#'*(2+26))
        self.out_file.write('\n')
        self.out_file.write("# Profile for parameter %2d #\n" % (parid))
        self.out_file.write('#'*(2+26))
        self.out_file.write('\n\n')
        self.out_file.flush()

        # redirect stdout stream
        _redirection_target = None
        ## -- disable redirection completely, for now
        ##if log_print_level >= 0:
        ##    _redirection_target = self.out_file

        with redirect_stdout_to(_redirection_target):
            pv = []
            chi2 = []
            error_code = Long(0)
            self.__gMinuit.mnexcm("SET PRINT",
                     arr('d', [0.0]), 1, error_code)  # no printout

            # first, make sure we are at minimum, i.e. re-minimize
            self.minimize(final_fit=True, log_print_level=0)
            minuit_id = Double(parid + 1) # Minuit parameter numbers start with 1

            # retrieve information about parameter with id=parid
            pmin = Double(0)
            perr = Double(0)
            self.__gMinuit.GetParameter(parid, pmin, perr)  # retrieve fitresult

            # fix parameter parid ...
            self.__gMinuit.mnexcm("FIX",
                                    arr('d', [minuit_id]),
                                    1, error_code)
            # ... and scan parameter values, minimizing at each point
            for v in np.linspace(pmin - 3.*perr, pmin + 3.*perr, n_points):
                pv.append(v)
                self.__gMinuit.mnexcm("SET PAR",
                     arr('d', [minuit_id, Double(v)]),
                                   2, error_code)
                self.__gMinuit.mnexcm("MIGRAD",
                     arr('d', [self.max_iterations, self.tolerance]),
                                   2, error_code)
                chi2.append(self.get_fit_info('fcn'))

            # release parameter to back to initial value and release
            self.__gMinuit.mnexcm("SET PAR",
                                  arr('d', [minuit_id, Double(pmin)]),
                                   2, error_code)
            self.__gMinuit.mnexcm("RELEASE",
                                    arr('d', [minuit_id]),
                                    1, error_code)

        return pv, chi2
# Green = arr('d',[0.00, 0.10, 1.00, 1.00, 0.00])
# Blue = arr('d',[1.00, 0.90, 0.00, 0.00, 0.00])
# Length = arr('d',[0.00, 0.40, 0.60, 0.80, 1.00])
# TColor.CreateGradientColorTable(5,Length,Red,Green,Blue,500)

# Red = arr('d',[.00, 1.00, 1.00])
# Green = arr('d',[1.00, 1.00, 0.00])
# Blue = arr('d',[0.00, 0.00, 0.00])
# Length = arr('d',[0.00, 0.5, 1.00])
# TColor.CreateGradientColorTable(3,Length,Red,Green,Blue,500)
# Red = arr('d',[1.00, 1.00, 1.00, .00])
# Green = arr('d',[0., 0.00, 1.00, 1.00])
# Blue = arr('d',[1.00, 0.00, 0.00, 0.00])
# Length = arr('d',[0.00, 0.1, 0.55, 1.00])
# TColor.CreateGradientColorTable(4,Length,Red,Green,Blue,500)
Red = arr("d", [0.0, 1.00, 1.00, 1.00, 0.00])
Green = arr("d", [0.0, 0.0, 0.00, 1.00, 1.00])
Blue = arr("d", [1.0, 1.00, 0.00, 0.00, 0.00])
Length = arr("d", [0.00, 0.4, 0.6, 0.8, 1.00])
TColor.CreateGradientColorTable(5, Length, Red, Green, Blue, 500)

gStyle.SetNumberContours(500)


def unshitify(pave):
    pave.SetFillStyle(0)
    pave.SetBorderSize(0)


cms_label = ROOT.TPaveText(0.06, 0.84, 0.9, 1.0, "NDC")
unshitify(cms_label)
#Example1(plot default efficiencies): python Step2_PlotAll.py
#Example2(systematic -- bkg modeling): python Step2_PlotAll.py /uscms/home/zhangjin/nobackup/ bkg
#Example3(systematic -- sig modeling): python Step2_PlotAll.py /uscms/home/zhangjin/nobackup/ sig
#Example4(systematic -- MCTruth     ): python Step2_PlotAll.py /uscms/home/zhangjin/nobackup/ mc
#Example2-4 are used for systematic calculation.
##################################################################################

from  ROOT import *
from  numpy import *
from Config import *
gROOT.SetStyle("Plain")
gStyle.SetPaintTextFormat("0.3g")
gStyle.SetOptStat(0)

from array import array as arr
Red = arr('d',[0.00, 0.00, 0.00, 1.00, 1.00])
Green = arr('d',[0.00, 0.10, 1.00, 1.00, 0.00])
Blue = arr('d',[1.00, 0.90, 0.00, 0.00, 0.00])
Length = arr('d',[0.00, 0.40, 0.60, 0.80, 1.00])
TColor.CreateGradientColorTable(5,Length,Red,Green,Blue,500)
gStyle.SetNumberContours(500)

import sys,os,re
if (sys.argv[0] == "python"): args=sys.argv[2:]
else: args=sys.argv[1:]

Prefix=""
Postfix=""
TagProbeFitResult=TagProbeFitResult.split("/")[-1]
ResultPlotsFileName=ResultPlotsFileName.split("/")[-1]
if len(args)>0:
for i in range(len(error_w1)):
  g1 += error_w1[i]**2
  g2 += error_w2[i]**2

s = error_w1[3]*error_w2[3]
t = error_w1[4]*error_w2[4]

global kor_matirx
kor_matirx = np.array([[g1, s+t], [ s+t, g2]])
kor_matirx_inv = inv(kor_matirx)
print kor_matirx

# --> set up MINUIT
myMinuit = TMinuit(npar)  # initialize TMinuit with maximum of npar parameters
myMinuit.SetFCN(fcn)      # set function to minimize
arglist = arr('d', 2*[0.01]) # set error definition 
ierflg = Long(0)             
arglist[0] = 1.              # 1 sigma is Delta chi^2 = 1
myMinuit.mnexcm("SET ERR", arglist ,1,ierflg)


# --> Set starting values and step sizes for parameters
# Define the parameters for the fit
myMinuit.mnparm(1, "mean", 400, 0.001, 0,0,ierflg)

arglist[0] = 6000 # Number of calls to FCN before giving up.
arglist[1] = 0.3  # Tolerance
myMinuit.mnexcm("MIGRAD", arglist ,2,ierflg)  # execute the minimisation

# --> check TMinuit status 
amin, edm, errdef = Double(0.), Double(0.), Double(0.)
Beispiel #21
0
 def _hesse(self, max_calls=6000):
     # need to set the FCN explicitly before every call
     self._get_gMinuit().SetFCN(self._minuit_fcn)
     error_code = Long(0)
     self._get_gMinuit().mnexcm("HESSE", arr('d', [max_calls]), 1, error_code)