Exemplo n.º 1
0
def Measure_Integral(Fname1, Fname2, Title, XaxisT, low, high, freq, RootName):

    FNumber = int((high - low) / freq)

    LINK = [19]
    #    LINK=[15,16,17,18,19]
    #    LINKkChannel=[0,1,2,3,4,5]
    LINKkChannel = [2]
    for linkChannel in LINKkChannel:
        for link in LINK:
            xIntegral = array("d", xrange(0, FNumber))
            yIntegral = array("d", xrange(0, FNumber))
            yIntegralErrUp = array("d", xrange(0, FNumber))
            yIntegralErrDown = array("d", xrange(0, FNumber))
            xIntegral_RMS = array("d", xrange(0, FNumber))
            yIntegral_RMS = array("d", xrange(0, FNumber))
            yIntegral_RMSErrUp = array("d", xrange(0, FNumber))
            yIntegral_RMSErrDown = array("d", xrange(0, FNumber))
            xRatio = array("d", xrange(0, FNumber))
            yRatio = array("d", xrange(0, FNumber))
            xSingleEv = array("d", xrange(0, 40))
            ySingleEv = array("d", xrange(0, 40))
            num = -1
            for iAmp in range(low, high, freq):
                num += 1
                Fname = Fname1 + str(iAmp) + Fname2
                print "*************  -> initiating      The ", Fname, " and number of files exist= ", FNumber
                print "-------------------->  Now is doing ....  ", low, "  ____ ", Fname

                f = open(Fname)
                data = getData(f)
                #                tdc = getTDCValues(f)

                M = TH1F(Fname, Fname, 10000, 0, 1000000)
                x = array("d", xrange(0, 1001))
                y = array("d", xrange(0, 1001))

                for event in xrange(0, 995):

                    pedSum = 0
                    sigSum = 0
                    Signal = 0
                    Pedestal = 0
                    for BX in xrange(0, 40):

                        adcValue = data[event][link][BX][linkChannel]
                        if BX < 15: pedSum += adcValue
                        if BX > 19 and BX < 27: sigSum += adcValue
                        #                        if BX > 19 and  BX < 25: sigSum += adcValue
                        xSingleEv[BX] = BX
                        ySingleEv[BX] = adcValue

                    scanvas = MakeCanvas("mm", "nn", 800, 800)
                    GrSingleEv = TGraph(len(xSingleEv), xSingleEv, ySingleEv)
                    SFit = TF1("fit", "gaus", 19, 23)
                    SFit.SetParameter(0, 4000)
                    SFit.SetParameter(1, 20.9)
                    SFit.SetParLimits(1, 20, 22)
                    SFit.SetParameter(2, 1.5)
                    GrSingleEv.Draw("AC*")
                    GrSingleEv.Fit("fit", "R0")
                    SFit.Draw("same")
                    FitParam = SFit.GetParameters()
                    print "Gaus fit param 1, 2, 3= ", round(
                        FitParam[0], 4), round(FitParam[1],
                                               4), round(FitParam[2], 4)
                    #            scanvas.SaveAs("singleEv_"+str(iAmp)+"_"+str(event)+".pdf")

                    Pedestal = pedSum / 15.
                    y[event] = sigSum - Pedestal * 7
                    #                    y[event]= sigSum- Pedestal*5
                    M.Fill(y[event])

                histMean = M.GetMean()
                histRMS = M.GetStdDev()

                highVal = histMean + 4 * histRMS
                lowVal = histMean - 4 * histRMS
                highValAx = histMean + 6 * histRMS
                lowValAx = histMean - 6 * histRMS

                canvas = MakeCanvas("asdf", "asdf", 800, 800)
                canvas.Update()
                MyGr = TGraph(len(x), x, y)
                mfit = TF1("fit", "gaus", lowVal, highVal)
                M.Fit(mfit, "R0", "")
                FitParam = mfit.GetParameters()
                #        FitParErr=mfit.GetParError()

                integral = round(FitParam[1], 4)
                integralErr = round(mfit.GetParError(1), 4)
                if FitParam[1] / histMean < 0.9 or FitParam[1] / histMean > 1.1:
                    integral = histMean
                    integralErr = 0

                integral_RMS = round(FitParam[2], 4)
                integral_RMSErr = round(mfit.GetParError(2), 4)
                if round(FitParam[2], 4) / histRMS < 0.9 or round(
                        FitParam[2], 4) / histRMS > 1.1:
                    integral_RMS = histRMS
                    integral_RMSErr = 0

                print "iAmp=", iAmp, "   integral= ", integral, "   integral_RMS=", integral_RMS

                M.SetMarkerStyle(22)
                M.GetXaxis().SetRangeUser(lowValAx, highValAx)

                M.SetTitle(Title + " = " + str(iAmp))
                M.Draw("pe")
                mfit.Draw("same")
                fitInfo = TPaveText(.20, 0.7, .60, 0.9, "NDC")
                fitInfo.SetBorderSize(0)
                fitInfo.SetFillStyle(0)
                fitInfo.SetTextAlign(12)
                fitInfo.SetTextSize(0.03)
                fitInfo.SetTextColor(1)
                fitInfo.SetTextFont(62)
                fitInfo.AddText("Mean of Fit=" + str(round(FitParam[1], 1)))
                fitInfo.AddText("RMS of Fit =" + str(round(FitParam[2], 1)))
                fitInfo.Draw()
                canvas.SaveAs("HistoSingleRun_" + str(iAmp) + "_" + Title +
                              RootName + "_link" + str(link) + "_ch_" +
                              str(linkChannel) + ".pdf")

                xIntegral[num] = iAmp
                yIntegral[num] = integral
                yIntegralErrUp[num] = integral + integralErr
                yIntegralErrDown[num] = integral - integralErr

                xIntegral_RMS[num] = iAmp
                yIntegral_RMS[num] = integral_RMS
                yIntegral_RMSErrUp[num] = integral_RMS + integral_RMSErr
                yIntegral_RMSErrDown[num] = integral_RMS - integral_RMSErr

                xRatio[num] = iAmp
                yRatio[num] = integral_RMS / integral

            Graph_Integral = TGraph(len(xIntegral), xIntegral, yIntegral)
            Graph_IntegralErUp = TGraph(len(xIntegral), xIntegral,
                                        yIntegralErrUp)
            Graph_IntegralErDown = TGraph(len(xIntegral), xIntegral,
                                          yIntegralErrDown)

            canvas_Integral = MakeCanvas("can1", "can1", 800, 800)
            Graph_Integral.SetTitle("Pulse Integral vs. Pulse  " + Title)
            Graph_Integral.SetMarkerStyle(22)
            Graph_Integral.SetMarkerColor(3)
            Graph_Integral.SetMarkerSize(2)
            Graph_Integral.GetXaxis().SetTitle(XaxisT)
            Graph_Integral.Draw()
            Graph_IntegralErUp.Draw("same")
            Graph_IntegralErDown.Draw("same")
            canvas_Integral.SaveAs("Integral_" + Title + RootName + "_link" +
                                   str(link) + "_ch_" + str(linkChannel) +
                                   ".pdf")

            Graph_Integral_RMS = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                        yIntegral_RMS)
            Graph_Integral_RMSErUp = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                            yIntegral_RMSErrUp)
            Graph_Integral_RMSErDown = TGraph(len(xIntegral_RMS),
                                              xIntegral_RMS,
                                              yIntegral_RMSErrDown)

            canvas_Integral_RMS = MakeCanvas("can2", "can2", 800, 800)
            Graph_Integral_RMS.SetTitle("Pulse Integral RMS vs. Pulse  " +
                                        Title)
            Graph_Integral_RMS.SetMarkerStyle(23)
            Graph_Integral_RMS.SetMarkerColor(2)
            Graph_Integral_RMS.SetMarkerSize(2)
            Graph_Integral_RMS.GetXaxis().SetTitle(XaxisT)
            Graph_Integral_RMS.Draw()
            Graph_Integral_RMSErUp.Draw("same")
            Graph_Integral_RMSErDown.Draw("same")
            canvas_Integral_RMS.SaveAs("Integral_RMS_" + Title + RootName +
                                       "_link" + str(link) + "_ch_" +
                                       str(linkChannel) + ".pdf")

            Graph_Ratio = TGraph(len(xRatio), xRatio, yRatio)
            canvas_Ratio = MakeCanvas("can2", "can2", 800, 800)
            Graph_Ratio.SetTitle(
                "Ratio of Pulse Integral RMS and Pulse Integral  " + Title)
            Graph_Ratio.SetMarkerStyle(21)
            Graph_Ratio.SetMarkerColor(8)
            Graph_Ratio.SetMarkerSize(2)
            Graph_Ratio.GetXaxis().SetTitle(XaxisT)
            Graph_Ratio.Draw()
            canvas_Ratio.SaveAs("Ratio_" + Title + RootName + "_link" +
                                str(link) + "_ch_" + str(linkChannel) + ".pdf")

            OutFile = TFile(
                "outFile_" + RootName + "_link" + str(link) + "_ch_" +
                str(linkChannel) + ".root", "RECREATE")
            OutFile.WriteObject(Graph_Integral, "Graph_Integral")
            OutFile.WriteObject(Graph_Integral_RMS, "Graph_Integral_RMS")
            OutFile.WriteObject(Graph_Ratio, "Graph_Ratio")
            OutFile.Close()
def Measure_Integral(AllRuns, Title, RootName):

    FNumber=len(AllRuns)
    
    xIntegral=array("d",xrange(0,FNumber))
    yIntegral=array("d",xrange(0,FNumber))
    yIntegralErrUp=array("d",xrange(0,FNumber))
    yIntegralErrDown=array("d",xrange(0,FNumber))
    xIntegral_RMS=array("d",xrange(0,FNumber))
    yIntegral_RMS=array("d",xrange(0,FNumber))
    yIntegral_RMSErrUp=array("d",xrange(0,FNumber))
    yIntegral_RMSErrDown=array("d",xrange(0,FNumber))
    xRatio=array("d",xrange(0,FNumber))
    yRatio=array("d",xrange(0,FNumber))
    xSingleEv=array("d",xrange(0,40))
    ySingleEv=array("d",xrange(0,40))
    
    OutFile=TFile(RootName,"RECREATE")
    num=-1
    iAmp=0
    for Fname in AllRuns:
        num+=1
        iAmp+=1

        f = open(Fname)
        data = getData(f)

        M=TH1F(Fname,Fname,600,0,120000)
        x = array("d", xrange(0,1001))
        y = array("d", xrange(0,1001))


        for event in xrange(0,995):
            pedSum=0
            sigSum=0
            Signal=0
            Pedestal=0
            for BX in xrange(0, 40):

                adcValue=data[event][link][BX][linkChannel]
                if BX < 15 : pedSum += adcValue
                if BX > 19 and  BX < 25: sigSum += adcValue
                xSingleEv[BX]=BX
                ySingleEv[BX]=adcValue
        
            scanvas = MakeCanvas("mm","nn",800,800)
            GrSingleEv=TGraph(len(xSingleEv),xSingleEv,ySingleEv)
            OutFile.WriteObject(GrSingleEv,"singleEv_"+str(iAmp)+"_"+str(event))
            SFit=TF1("fit", "gaus", 19,23)
            SFit.SetParameter(0, 4000)
            SFit.SetParameter(1, 20.9)
            SFit.SetParLimits(1, 20, 22)
            SFit.SetParameter(2, 1.5)
            GrSingleEv.Draw("AC*")
            GrSingleEv.Fit("fit","R0")
            SFit.Draw("same")
            FitParam=SFit.GetParameters()
            print "Gaus fit param 1, 2, 3= " , round(FitParam[0],4), round(FitParam[1],4), round(FitParam[2],4)
            scanvas.SaveAs("PLOT/singleEv_"+str(iAmp)+"_"+str(event)+".pdf")
            
            Pedestal=pedSum/15.
            y[event]= sigSum- Pedestal*5
            M.Fill(y[event])

        histMean= M.GetMean()
        histRMS= M.GetStdDev()

        highVal = histMean + 4 * histRMS
        lowVal = histMean - 4 * histRMS
        highValAx = histMean + 6 * histRMS
        lowValAx = histMean - 6 * histRMS

        canvas = MakeCanvas("asdf","asdf",800,800)
        canvas.Update()
        MyGr= TGraph(len(x), x,y)
        mfit=TF1("fit", "gaus", lowVal,highVal)
        M.Fit(mfit, "R0","")
        FitParam=mfit.GetParameters()
#        FitParErr=mfit.GetParError()
        integral= round(FitParam[1],4)
        integral_RMS= round(FitParam[2],4)
        integralErr= round(mfit.GetParError(1),4)
        integral_RMSErr= round(mfit.GetParError(2),4)
        print "iAmp=", iAmp, "   integral= ", integral,  "   integral_RMS=", integral_RMS


        M.SetMarkerStyle(22)
        M.GetXaxis().SetRangeUser(lowValAx,highValAx)
        
        M.SetTitle(Title+" = "+str(iAmp))
        M.Draw("pe")
        mfit.Draw("same")
        fitInfo  =TPaveText(.20,0.7,.60,0.9, "NDC");
        fitInfo.SetBorderSize(   0 );
        fitInfo.SetFillStyle(    0 );
        fitInfo.SetTextAlign(   12 );
        fitInfo.SetTextSize ( 0.03 );
        fitInfo.SetTextColor(    1 );
        fitInfo.SetTextFont (   62 );
        fitInfo.AddText("Mean of Fit=" + str(round(FitParam[1],1)))
        fitInfo.AddText("RMS of Fit =" + str(round(FitParam[2],1)))
        fitInfo.Draw()
        canvas.SaveAs("HistoSingleRun_"+str(iAmp)+"_"+Title+RootName+".pdf")



        xIntegral[num]=iAmp
        yIntegral[num]=integral
        yIntegralErrUp[num]=integral+integralErr
        yIntegralErrDown[num]=integral-integralErr

        xIntegral_RMS[num]=iAmp
        yIntegral_RMS[num]=integral_RMS
        yIntegral_RMSErrUp[num]=integral_RMS+integral_RMSErr
        yIntegral_RMSErrDown[num]=integral_RMS-integral_RMSErr
        
        xRatio[num]=iAmp
        yRatio[num]=integral_RMS/integral

#        xIntegral.append(iAmp)
#        yIntegral.append(integral)
#        xIntegral_RMS.append(iAmp)
#        yIntegral_RMS.append(integral_RMS)

    Graph_Integral= TGraph(len(xIntegral), xIntegral,yIntegral)
    Graph_IntegralErUp= TGraph(len(xIntegral), xIntegral,yIntegralErrUp)
    Graph_IntegralErDown= TGraph(len(xIntegral), xIntegral,yIntegralErrDown)
    
    canvas_Integral = MakeCanvas("can1","can1",800,800)
#    canvas_Integral.SetLogy()
    Graph_Integral.SetTitle("Pulse Integral Stability")
    Graph_Integral.SetMarkerStyle(22)
    Graph_Integral.SetMarkerColor(3)
    Graph_Integral.SetMarkerSize(2)
    Graph_Integral.GetXaxis().SetTitle("Day")
    Graph_Integral.GetYaxis().SetRangeUser(80000,85000)
    Graph_Integral.Draw()
#    Graph_IntegralErUp.Draw("same")
#    Graph_IntegralErDown.Draw("same")
    canvas_Integral.SaveAs("Integral_"+Title+RootName+".pdf")

    Graph_Integral_RMS= TGraph(len(xIntegral_RMS), xIntegral_RMS,yIntegral_RMS)
    Graph_Integral_RMSErUp= TGraph(len(xIntegral_RMS), xIntegral_RMS,yIntegral_RMSErrUp)
    Graph_Integral_RMSErDown= TGraph(len(xIntegral_RMS), xIntegral_RMS,yIntegral_RMSErrDown)

    canvas_Integral_RMS = MakeCanvas("can2","can2",800,800)
    Graph_Integral_RMS.SetTitle("Pulse Integral RMS vs. Pulse  "+Title)
    Graph_Integral_RMS.SetMarkerStyle(23)
    Graph_Integral_RMS.SetMarkerColor(2)
    Graph_Integral_RMS.SetMarkerSize(2)
    Graph_Integral.GetXaxis().SetTitle("Day")
    Graph_Integral_RMS.Draw()
#    Graph_Integral_RMSErUp.Draw("same")
#    Graph_Integral_RMSErDown.Draw("same")
    canvas_Integral_RMS.SaveAs("Integral_RMS_"+Title+RootName+".pdf")


    Graph_Ratio= TGraph(len(xRatio), xRatio,yRatio)
    canvas_Ratio = MakeCanvas("can2","can2",800,800)
    Graph_Ratio.SetTitle("Ratio of Pulse Integral RMS and Pulse Integral  "+Title)
    Graph_Ratio.SetMarkerStyle(21)
    Graph_Ratio.SetMarkerColor(8)
    Graph_Ratio.SetMarkerSize(2)
    Graph_Integral.GetXaxis().SetTitle("Day")
    Graph_Ratio.Draw()
    canvas_Ratio.SaveAs("Ratio_"+Title+RootName+".pdf")

#    OutFile=TFile(RootName,"RECREATE")
    OutFile.WriteObject(Graph_Integral,"Graph_Integral")
    OutFile.WriteObject(Graph_Integral_RMS,"Graph_Integral_RMS")
    OutFile.WriteObject(Graph_Ratio,"Graph_Ratio")
    OutFile.Close()
Exemplo n.º 3
0
def Measure_Integral(AllRuns, Title, RootName, WID):

    FNumber = len(AllRuns)

    xIntegral = array("d", xrange(0, FNumber))
    yIntegral = array("d", xrange(0, FNumber))
    yIntegralErrUp = array("d", xrange(0, FNumber))
    yIntegralErrDown = array("d", xrange(0, FNumber))
    xIntegral_RMS = array("d", xrange(0, FNumber))
    yIntegral_RMS = array("d", xrange(0, FNumber))
    yIntegral_RMSErrUp = array("d", xrange(0, FNumber))
    yIntegral_RMSErrDown = array("d", xrange(0, FNumber))
    xRatio = array("d", xrange(0, FNumber))
    yRatio = array("d", xrange(0, FNumber))
    xSingleEv = array("d", xrange(0, 40))
    ySingleEv = array("d", xrange(0, 40))

    OutFile = TFile(RootName, "RECREATE")
    num = -1
    iAmp = 0
    for Fname in AllRuns:
        num += 1
        iAmp += 1

        f = open(Fname)
        #        data = getData(f)
        tdc = getTDCValues(f)

        M = TH1F(Fname, Fname, 200, 0, 100)
        x = array("d", xrange(0, 1001))
        y = array("d", xrange(0, 1001))

        for event in xrange(0, 995):
            pedSum = 0
            sigSum = 0
            Signal = 0
            Pedestal = 0
            for BX in xrange(0, 40):
                #                print "[event][link][BX][linkChannel] = ", event,"  "  ,link,"  "  , BX ,"  "  ,linkChannel, "---->data[event][link][BX][linkChannel]", tdc[event][link][BX][linkChannel]
                #            print BX
                #            print event
                #            print "data=", data[event][link][BX][linkChannel]
                #                print "TDC=", tdc[event][link][BX][linkChannel]
                #            print "\n"
                #                print "@@@@@@@@------->     [event] ", event
                tdcValue = tdc[event][link][BX][linkChannel]
                #                print "@@@@@@@@------->     [event] ", event
                if (WID[num] < 10 and BX == 20
                        and tdc[event][link][BX][linkChannel] != 31.5):
                    sigSum += tdcValue
                if (WID[num] > 10 and BX == 21
                        and tdc[event][link][BX][linkChannel] != 31.5):
                    sigSum += tdcValue
#                    print "[event][link][BX][linkChannel] = ", event,"  "  ,link,"  "  , BX ,"  "  ,linkChannel, "---->data[event][link][BX][linkChannel]", tdc[event][link][BX][linkChannel]
#                if BX > 19 and  BX < 25: sigSum += adcValue
#                if BX > 18 and BX < 26: print "[event][link][BX][linkChannel] = ", event,"  "  ,link,"  "  , BX ,"  "  ,linkChannel, "---->data[event][link][BX][linkChannel]", data[event][link][BX][linkChannel]

#            print "---------------------> sigSum= ",sigSum
            y[event] = sigSum
            M.Fill(y[event])

        histMean = M.GetMean()
        histRMS = M.GetStdDev()

        highVal = histMean + 4 * histRMS
        lowVal = histMean - 4 * histRMS
        highValAx = histMean + 6 * histRMS
        lowValAx = histMean - 6 * histRMS

        canvas = MakeCanvas("asdf", "asdf", 800, 800)
        canvas.Update()
        MyGr = TGraph(len(x), x, y)
        mfit = TF1("fit", "gaus", lowVal, highVal)
        M.Fit(mfit, "R0", "")
        FitParam = mfit.GetParameters()
        #        FitParErr=mfit.GetParError()

        integral = histMean
        if FitParam[1] < 1.5 * histMean: integral = round(FitParam[1], 4)

        integral_RMS = histRMS
        if round(FitParam[2], 4) < 2 * histRMS:
            integral_RMS = round(FitParam[2], 4)

        integralErr = round(mfit.GetParError(1), 4)
        integral_RMSErr = round(mfit.GetParError(2), 4)
        print "iAmp=", iAmp, "   integral= ", integral, "   integral_RMS=", integral_RMS

        M.SetMarkerStyle(22)
        M.GetXaxis().SetRangeUser(lowValAx, highValAx)
        #        M.GetXaxis().SetRangeUser(lowValAx,highValAx)

        M.SetTitle(Title + " = " + str(iAmp))
        M.Draw("pe")
        mfit.Draw("same")
        fitInfo = TPaveText(.20, 0.7, .60, 0.9, "NDC")
        fitInfo.SetBorderSize(0)
        fitInfo.SetFillStyle(0)
        fitInfo.SetTextAlign(12)
        fitInfo.SetTextSize(0.03)
        fitInfo.SetTextColor(1)
        fitInfo.SetTextFont(62)
        fitInfo.AddText("Mean of Fit=" + str(round(FitParam[1], 1)))
        fitInfo.AddText("RMS of Fit =" + str(round(FitParam[2], 1)))
        fitInfo.Draw()
        canvas.SaveAs("HistoSingleRun_" + str(iAmp) + "_" + Title + "_TDC.pdf")

        XVAL = iAmp
        xIntegral[num] = XVAL
        yIntegral[num] = integral
        yIntegralErrUp[num] = integral + integralErr
        yIntegralErrDown[num] = integral - integralErr

        xIntegral_RMS[num] = XVAL
        yIntegral_RMS[num] = integral_RMS
        yIntegral_RMSErrUp[num] = integral_RMS + integral_RMSErr
        yIntegral_RMSErrDown[num] = integral_RMS - integral_RMSErr

        print "iAmp, integral=  ", iAmp, "  ", integral, "  XVAL= ", XVAL
        xRatio[num] = XVAL
        yRatio[num] = integral_RMS / integral


#        xIntegral.append(iAmp)
#        yIntegral.append(integral)
#        xIntegral_RMS.append(iAmp)
#        yIntegral_RMS.append(integral_RMS)

    Graph_Integral = TGraph(len(xIntegral), xIntegral, yIntegral)
    Graph_IntegralErUp = TGraph(len(xIntegral), xIntegral, yIntegralErrUp)
    Graph_IntegralErDown = TGraph(len(xIntegral), xIntegral, yIntegralErrDown)

    canvas_Integral = MakeCanvas("can1", "can1", 800, 800)
    #    canvas_Integral.SetLogy()
    Graph_Integral.SetTitle("TDC Stability")
    Graph_Integral.SetMarkerStyle(22)
    Graph_Integral.SetMarkerColor(3)
    Graph_Integral.SetMarkerSize(2)
    Graph_Integral.GetXaxis().SetTitle("Day")
    Graph_Integral.GetYaxis().SetRangeUser(0, 40)
    print "%%%%%%%% Graph_Integral.GetMaximum()= ", TMath.MaxElement(
        len(xIntegral_RMS), Graph_Integral.GetY())
    #    Graph_Integral.SetMaximum(1.5)
    Graph_Integral.Draw()
    #    Graph_IntegralErUp.Draw("same")
    #    Graph_IntegralErDown.Draw("same")
    canvas_Integral.SaveAs("Integral_" + Title + "_TDC.pdf")

    Graph_Integral_RMS = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                yIntegral_RMS)
    Graph_Integral_RMSErUp = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                    yIntegral_RMSErrUp)
    Graph_Integral_RMSErDown = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                      yIntegral_RMSErrDown)

    canvas_Integral_RMS = MakeCanvas("can2", "can2", 800, 800)
    Graph_Integral_RMS.SetTitle("TDC RMS vs. Pulse  " + Title)
    Graph_Integral_RMS.SetMarkerStyle(23)
    Graph_Integral_RMS.SetMarkerColor(2)
    Graph_Integral_RMS.SetMarkerSize(2)
    Graph_Integral_RMS.GetXaxis().SetTitle("Day")
    Graph_Integral_RMS.GetYaxis().SetRangeUser(
        TMath.MinElement(len(xIntegral_RMS), Graph_Integral_RMS.GetY()) / 2,
        TMath.MaxElement(len(xIntegral_RMS), Graph_Integral_RMS.GetY()) * 1.5)
    Graph_Integral_RMS.Draw()
    #    Graph_Integral_RMSErUp.Draw("same")
    #    Graph_Integral_RMSErDown.Draw("same")
    canvas_Integral_RMS.SaveAs("Integral_RMS_" + Title + "_TDC.pdf")

    Graph_Ratio = TGraph(len(xRatio), xRatio, yRatio)
    canvas_Ratio = MakeCanvas("can2", "can2", 800, 800)
    Graph_Ratio.SetTitle("Ratio of TDC RMS and TDC  " + Title)
    Graph_Ratio.SetMarkerStyle(21)
    Graph_Ratio.SetMarkerColor(4)
    Graph_Ratio.SetMarkerSize(2)
    Graph_Ratio.GetXaxis().SetTitle("Day")
    Graph_Ratio.GetYaxis().SetRangeUser(
        TMath.MinElement(len(xIntegral_RMS), Graph_Ratio.GetY()) / 2,
        TMath.MaxElement(len(xIntegral_RMS), Graph_Ratio.GetY()) * 1.5)
    Graph_Ratio.Draw()
    canvas_Ratio.SaveAs("Ratio_" + Title + "_TDC.pdf")

    OutFile = TFile(RootName, "RECREATE")
    OutFile.WriteObject(Graph_Integral, "Graph_Integral")
    OutFile.WriteObject(Graph_Integral_RMS, "Graph_Integral_RMS")
    OutFile.WriteObject(Graph_Ratio, "Graph_Ratio")
    OutFile.Close()
def Measure_Integral(Fname1, Fname2, Title, XaxisT, low, high, freq, RootName):

    FNumber = int((high - low) / freq)
    print "*************  -> initiating      The ", Fname1, " and number of files exist= ", FNumber

    #    xIntegral=[]
    #    yIntegral=[]
    #    xIntegral_RMS=[]
    #    yIntegral_RMS=[]

    #    LINK=[15]
    LINK = [15, 16, 17, 18, 19.20]
    #    LINKkChannel=[0,1,2,3,4,5]
    LINKkChannel = [4]
    for linkChannel in LINKkChannel:
        for link in LINK:
            xIntegral = array("d", xrange(0, FNumber))
            yIntegral = array("d", xrange(0, FNumber))
            yIntegralErrUp = array("d", xrange(0, FNumber))
            yIntegralErrDown = array("d", xrange(0, FNumber))
            xIntegral_RMS = array("d", xrange(0, FNumber))
            yIntegral_RMS = array("d", xrange(0, FNumber))
            yIntegral_RMSErrUp = array("d", xrange(0, FNumber))
            yIntegral_RMSErrDown = array("d", xrange(0, FNumber))
            xRatio = array("d", xrange(0, FNumber))
            yRatio = array("d", xrange(0, FNumber))
            num = -1
            for iAmp in range(low, high, freq):
                num += 1
                Fname = Fname1 + str(iAmp) + Fname2
                print "*************  -> initiating      The ", Fname, " and number of files exist= ", FNumber
                print "-------------------->  Now is doing ....  ", low, "  ____ ", Fname

                f = open(Fname)
                #        data = getData(f)
                tdc = getTDCValues(f)

                M = TH1F(Fname, Fname, 200, 0, 100)
                x = array("d", xrange(0, 1001))
                y = array("d", xrange(0, 1001))

                for event in xrange(0, 995):
                    tdcValue = 0
                    for BX in xrange(0, 40):

                        if (iAmp <= 10 and BX == 20
                                and tdc[event][link][BX][linkChannel] != 31.5):
                            tdcValue = tdc[event][link][BX][linkChannel]
                        if (iAmp > 10 and BX == 21
                                and tdc[event][link][BX][linkChannel] != 31.5):
                            tdcValue = tdc[event][link][BX][linkChannel]

                    y[event] = tdcValue
                    M.Fill(y[event])

                histMean = M.GetMean()
                histRMS = M.GetStdDev()

                highVal = histMean + 4 * histRMS
                lowVal = histMean - 4 * histRMS
                highValAx = histMean + 6 * histRMS
                lowValAx = histMean - 6 * histRMS

                canvas = MakeCanvas("can", "can", 800, 800)
                canvas.Update()
                MyGr = TGraph(len(x), x, y)
                mfit = TF1("fit", "gaus", lowVal, highVal)
                M.Fit(mfit, "R0", "")
                FitParam = mfit.GetParameters()
                #        FitParErr=mfit.GetParError()

                integral = histMean
                if FitParam[1] < 1.5 * histMean:
                    integral = round(FitParam[1], 4)

                integral_RMS = histRMS
                if round(FitParam[2], 4) < 2 * histRMS:
                    integral_RMS = round(FitParam[2], 4)

                integralErr = round(mfit.GetParError(1), 4)
                integral_RMSErr = round(mfit.GetParError(2), 4)
                print "iAmp=", iAmp, "   integral= ", integral, "   integral_RMS=", integral_RMS

                M.SetMarkerStyle(22)
                M.GetXaxis().SetRangeUser(0, 30)

                M.SetTitle("TDC v.s. Delay Setting (ns)")
                M.Draw("pe")
                mfit.Draw("same")
                fitInfo = TPaveText(.20, 0.7, .60, 0.9, "NDC")
                fitInfo.SetBorderSize(0)
                fitInfo.SetFillStyle(0)
                fitInfo.SetTextAlign(12)
                fitInfo.SetTextSize(0.03)
                fitInfo.SetTextColor(1)
                fitInfo.SetTextFont(62)
                fitInfo.AddText("Mean of Fit=" + str(round(FitParam[1], 1)))
                fitInfo.AddText("RMS of Fit =" + str(round(FitParam[2], 1)))
                fitInfo.AddText("Delay Setting =" + str(iAmp))
                fitInfo.AddText("link  =" + str(link) + "  channel=" +
                                str(linkChannel))
                fitInfo.Draw()
                canvas.SaveAs("outHistoSingleRun_" + RootName + str(iAmp) +
                              "_link" + str(link) + "_ch_" + str(linkChannel) +
                              ".pdf")

                XVAL = low + num * freq
                xIntegral[num] = XVAL
                yIntegral[num] = integral
                yIntegralErrUp[num] = integral + integralErr
                yIntegralErrDown[num] = integral - integralErr

                xIntegral_RMS[num] = XVAL
                yIntegral_RMS[num] = integral_RMS
                yIntegral_RMSErrUp[num] = integral_RMS + integral_RMSErr
                yIntegral_RMSErrDown[num] = integral_RMS - integral_RMSErr

                print "iAmp, integral=  ", iAmp, "  ", integral, "  XVAL= ", XVAL
#                xRatio[num]=XVAL
#                yRatio[num]=integral_RMS/integral

#        xIntegral.append(iAmp)
#        yIntegral.append(integral)
#        xIntegral_RMS.append(iAmp)
#        yIntegral_RMS.append(integral_RMS)

            Graph_TDC = TGraph(len(xIntegral), xIntegral, yIntegral)
            Graph_TDCErUp = TGraph(len(xIntegral), xIntegral, yIntegralErrUp)
            Graph_TDCErDown = TGraph(len(xIntegral), xIntegral,
                                     yIntegralErrDown)

            canvas_TDC = MakeCanvas("can1", "can1", 800, 800)
            Graph_TDC.SetTitle("")
            Graph_TDC.SetMarkerStyle(22)
            Graph_TDC.SetMarkerColor(3)
            Graph_TDC.SetMarkerSize(2)
            Graph_TDC.GetXaxis().SetTitle(XaxisT)
            Graph_TDC.GetYaxis().SetRangeUser(0, 30)
            Graph_TDC.GetYaxis().SetTitle("TDC [ns]")
            Graph_TDC.Draw()
            Graph_TDCErUp.Draw("same")
            Graph_TDCErDown.Draw("same")
            canvas_TDC.SaveAs("outHisto_" + RootName + "_link" + str(link) +
                              "_ch_" + str(linkChannel) + ".pdf")

            Graph_TDC_RMS = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                   yIntegral_RMS)
            Graph_TDC_RMSErUp = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                       yIntegral_RMSErrUp)
            Graph_TDC_RMSErDown = TGraph(len(xIntegral_RMS), xIntegral_RMS,
                                         yIntegral_RMSErrDown)

            canvas_TDC_RMS = MakeCanvas("can2", "can2", 800, 800)
            Graph_TDC_RMS.SetTitle("TDC RMS vs. Pulse  " + Title)
            Graph_TDC_RMS.SetMarkerStyle(23)
            Graph_TDC_RMS.SetMarkerColor(2)
            Graph_TDC_RMS.SetMarkerSize(2)
            Graph_TDC_RMS.GetXaxis().SetTitle(XaxisT)
            Graph_TDC_RMS.GetYaxis().SetRangeUser(0, 2)
            Graph_TDC_RMS.Draw()
            Graph_TDC_RMSErUp.Draw("same")
            Graph_TDC_RMSErDown.Draw("same")
            canvas_TDC_RMS.SaveAs("outHistoRMS_" + RootName + "_link" +
                                  str(link) + "_ch_" + str(linkChannel) +
                                  ".pdf")

            OutFile = TFile(
                "outFile_" + RootName + "_link" + str(link) + "_ch_" +
                str(linkChannel) + ".root", "RECREATE")
            OutFile.WriteObject(Graph_TDC, "Graph_TDC")
            OutFile.WriteObject(Graph_TDC_RMS, "Graph_TDC_RMS")
            OutFile.Close()