コード例 #1
0
def main():
    """ Converts usxsuw output into a ROOT TH2F histogram """

    parser = argparse.ArgumentParser(
        description=main.__doc__,
        epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrbdx', type=str, help='usxsuw binary output')
    parser.add_argument('root',
                        type=str,
                        nargs='?',
                        help='output ROOT file name',
                        default="")
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        dest='verbose',
                        help='print what is being done')

    args = parser.parse_args()

    if not path.isfile(args.usrbdx):
        print("usrbdx2root: File %s does not exist." % args.usrbdx,
              file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrbdx, ".root")
    else:
        rootFileName = args.root

    b = Data.Usrbdx()
    b.readHeader(args.usrbdx)

    ND = len(b.detector)

    if args.verbose:
        b.sayHeader()
        print("\n%d tallies found:" % ND)
        for i in range(ND):
            b.say(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i))
        err = Data.unpackArray(b.readStat(i))
        det = b.detector[i]

        h = hist(det)

        for i in range(det.ne):
            for j in range(det.na):
                gbin = i + j * det.ne
                h.SetBinContent(i + 1, j + 1, val[gbin])
                h.SetBinError(i + 1, j + 1, err[gbin] * val[gbin])
        h.SetEntries(b.weight)
        h.Write()

    fout.Close()
コード例 #2
0
    def readHeader(self, filename):
        """ Reads the file header info
            Based on Data.Usrbdx
        """
        f = Data.Usrxxx.readHeader(self, filename)
        #        self.sayHeader()

        while True:
            data = fortran.read(f)
            if data is None: break
            size = len(data)
            #            print("size: ", size)

            if size == 14 and data.decode('utf8')[:10] == "STATISTICS":
                self.statpos = f.tell()
                for det in self.detector:
                    data = Data.unpackArray(fortran.read(f))
                    det.total = data[0]
                    det.totalerror = data[1]


#                    for j in range(6):
#                        fortran.skip(f)
                break

            if size != 50:
                raise IOError("Invalid USRTRACK/USRCOLL file %d " % size)

            header = struct.unpack("=i10siiififfif", data)

            det = Data.Detector()
            det.nb = header[0]
            det.name = header[1].decode(
                'utf8').strip()  # titutc - track/coll name
            det.type = header[
                2]  # itustc - type of binning: 1 - linear energy etc
            det.dist = header[3]  # idustc = distribution to be scored
            det.reg = header[4]  # nrustc = region
            det.volume = header[5]  # vusrtc = volume (cm**3) of the detector
            det.lowneu = header[6]  # llnutc = low energy neutron flag
            det.elow = header[7]  # etclow = minimum energy [GeV]
            det.ehigh = header[8]  # etchgh = maximum energy [GeV]
            det.ne = header[9]  # netcbn = number of energy intervals
            det.de = header[10]  # detcbn = energy bin width

            self.detector.append(det)

            if det.lowneu:
                data = fortran.read(f)
                det.ngroup = struct.unpack("=i", data[:4])[0]
                det.egroup = struct.unpack("=%df" % (det.ngroup + 1), data[4:])
                print("Low energy neutrons scored with %d groups" % det.ngroup)
            else:
                det.ngroup = 0
                det.egroup = []

            size = (det.ngroup + det.ne) * 4
            if size != fortran.skip(f):
                raise IOError("Invalid USRTRACK file")
        f.close()
コード例 #3
0
ファイル: ustsuw2root.py プロジェクト: kbat/mc-tools
def main():
    """ Converts ustsuw output into a ROOT TH1F histogram """

    parser = argparse.ArgumentParser(description=main.__doc__,
                                     epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrtrack', type=str, help='ustsuw binary output')
    parser.add_argument('root', type=str, nargs='?', help='output ROOT file name', default="")
    parser.add_argument('-v', '--verbose', action='store_true', default=False, dest='verbose', help='print what is being done')
    
    args = parser.parse_args()

    if not path.isfile(args.usrtrack):
        print("ustsuw2root: File %s does not exist." % args.usrtrack, file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrtrack,".root")
    else:
        rootFileName = args.root
    
    b = Usrtrack()
    b.readHeader(args.usrtrack)

    ND = len(b.detector)
    
    if args.verbose:
        #b.sayHeader()
        for i in range(ND):
            b.printHeader(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i,b.detector[i].lowneu))
        err = Data.unpackArray(b.readStat(i,b.detector[i].lowneu))

        det = b.detector[i]

        h = hist(det)
        hn = histN(det) # filled only if det.lowneu
            
        n = h.GetNbinsX()
        print(n, len(val), det.ne, val)

        for i in range(det.ne):
            h.SetBinContent(i+1, val[i])

        for i in range(det.ne):
            h.SetBinError(i+1,   err[n-i-1]*val[i])

        h.SetEntries(b.weight)
        h.Write()
        if det.lowneu:
            hn.Write()

    fout.Close()
コード例 #4
0
ファイル: usbsuw2root.py プロジェクト: kbat/mc-tools
def main():
    """ Converts usbsuw output into a ROOT histogram """

    parser = argparse.ArgumentParser(description=main.__doc__,
                                     epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrbin', type=str, help='usrxxx binary output (produced by usbsuw)')
    parser.add_argument('root', type=str, nargs='?', help='output ROOT file name', default="")
    parser.add_argument('-v', '--verbose', action='store_true', default=False, dest='verbose', help='Print some output')
    
    args = parser.parse_args()

    if not path.isfile(args.usrbin):
        print("usrbin2root: File %s does not exist." % args.usrbin, file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrbin,".root")
    else:
        rootFileName = args.root
    
    b = Data.Usrbin()
    b.readHeader(args.usrbin)

    ND = len(b.detector)
    
    if args.verbose:
        b.sayHeader()
        print("\n%d tallies found:" % ND)
        for i in range(ND):
            b.say(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i))
        err = Data.unpackArray(b.readStat(i))
        bin = b.detector[i]

        title = fluka.particle.get(bin.score, "unknown")
        if bin.type % 10 in (0, 3, 4, 5, 6):  # Fluka Manual, pages 250-251
            title = title +  ";x [cm];y [cm];z [cm]"
        h = ROOT.TH3F(bin.name, title, bin.nx, bin.xlow, bin.xhigh, bin.ny, bin.ylow, bin.yhigh, bin.nz, bin.zlow, bin.zhigh)
        
        for i in range(bin.nx):
            for j in range(bin.ny):
                for k in range(bin.nz):
                    gbin = i + j * bin.nx + k * bin.nx * bin.ny
                    h.SetBinContent(i+1, j+1, k+1, val[gbin])
                    h.SetBinError(i+1, j+1, k+1, err[gbin]*val[gbin])
        h.SetEntries(b.weight)
        h.Write()

    fout.Close()
コード例 #5
0
ファイル: usxsuw2root.py プロジェクト: kbat/mc-tools
def main():
    """ Converts usxsuw output into a ROOT TH2F histogram """

    parser = argparse.ArgumentParser(description=main.__doc__,
                                     epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrbdx', type=str, help='usxsuw binary output')
    parser.add_argument('root', type=str, nargs='?', help='output ROOT file name', default="")
    parser.add_argument('-v', '--verbose', action='store_true', default=False, dest='verbose', help='print what is being done')
    
    args = parser.parse_args()

    if not path.isfile(args.usrbdx):
        print("usrbdx2root: File %s does not exist." % args.usrbdx, file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrbdx,".root")
    else:
        rootFileName = args.root
    
    b = Data.Usrbdx()
    b.readHeader(args.usrbdx)

    ND = len(b.detector)
    
    if args.verbose:
        b.sayHeader()
        print("\n%d tallies found:" % ND)
        for i in range(ND):
            b.say(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i))
        err = Data.unpackArray(b.readStat(i))
        det = b.detector[i]

        h = hist(det)
        
        for i in range(det.ne):
            for j in range(det.na):
                    gbin = i + j * det.ne
                    h.SetBinContent(i+1, j+1, val[gbin])
                    h.SetBinError(i+1, j+1, err[gbin]*val[gbin])
        h.SetEntries(b.weight)
        h.Write()

    fout.Close()
コード例 #6
0
ファイル: ustsuw2root.py プロジェクト: kbat/mc-tools
    def readHeader(self, filename):
        """ Reads the file header info 
            Based on Data.Usrbdx
        """
        f = Data.Usrxxx.readHeader(self, filename)
#        self.sayHeader()
        
        while True:
            data = fortran.read(f)
            if data is None: break
            size = len(data)
#            print("size: ", size)

            if size == 14 and data[:10] == "STATISTICS":
                self.statpos = f.tell()
                for det in self.detector:
                    data = Data.unpackArray(fortran.read(f))
                    det.total = data[0]
                    det.totalerror = data[1]
#                    for j in range(6):
#                        fortran.skip(f)
                break

            if size != 50: raise IOError("Invalid USRTRACK/USRCOLL file")

            header = struct.unpack("=i10siiififfif", data)

            det = Data.Detector()
            det.nb = header[0]
            det.name = header[1].strip() # titutc - track/coll name
            det.type = header[2] # itustc - type of binning: 1 - linear energy etc
            det.dist = header[3] # idustc = distribution to be scored
            det.reg  = header[4] # nrustc = region
            det.volume = header[5] # vusrtc = volume (cm**3) of the detector
            det.lowneu = header[6] # llnutc = low energy neutron flag
            det.elow = header[7] # etclow = minimum energy [GeV]
            det.ehigh = header[8] # etchgh = maximum energy [GeV]
            det.ne = header[9] # netcbn = number of energy intervals
            det.de = header[10] # detcbn = energy bin width

            self.detector.append(det)

            if det.lowneu:
                data = fortran.read(f)
                det.ngroup = struct.unpack("=i",data[:4])[0]
                det.egroup = struct.unpack("=%df"%(det.ngroup+1), data[4:])
                print("Low energy neutrons scored with %d groups" % det.ngroup)
            else:
		det.ngroup = 0
		det.egroup = []

	    size  = (det.ngroup+det.ne) * 4
	    if size != fortran.skip(f):
		raise IOError("Invalid USRTRACK file")
        f.close()
コード例 #7
0
def main():
    """ Converts usbsuw output into a ROOT histogram """

    parser = argparse.ArgumentParser(
        description=main.__doc__,
        epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrbin',
                        type=str,
                        help='usrxxx binary output (produced by usbsuw)')
    parser.add_argument('root',
                        type=str,
                        nargs='?',
                        help='output ROOT file name',
                        default="")
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        dest='verbose',
                        help='Print some output')

    args = parser.parse_args()

    if not path.isfile(args.usrbin):
        print("usrbin2root: File %s does not exist." % args.usrbin,
              file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrbin, ".root")
    else:
        rootFileName = args.root

    b = Data.Usrbin()
    b.readHeader(args.usrbin)

    ND = len(b.detector)

    if args.verbose:
        b.sayHeader()
        print("\n%d tallies found:" % ND)
        for i in range(ND):
            b.say(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i))
        err = Data.unpackArray(b.readStat(i))
        det = b.detector[i]

        title = fluka.particle.get(det.score, "unknown")
        if det.type % 10 in (0, 3, 4, 5, 6):  # Fluka Manual, pages 250-251
            title = title + ";x [cm];y [cm];z [cm]"

        h = ROOT.TH3F(det.name, title, det.nx, det.xlow, det.xhigh, det.ny,
                      det.ylow, det.yhigh, det.nz, det.zlow, det.zhigh)

        for i in range(det.nx):
            for j in range(det.ny):
                for k in range(det.nz):
                    gbin = i + j * det.nx + k * det.nx * det.ny
                    h.SetBinContent(i + 1, j + 1, k + 1, val[gbin])
                    h.SetBinError(i + 1, j + 1, k + 1, err[gbin] * val[gbin])
        h.SetEntries(b.weight)
        h.Write()

    fout.Close()
コード例 #8
0
def main():
    """ Converts ustsuw output into a ROOT TH1F histogram """

    parser = argparse.ArgumentParser(
        description=main.__doc__,
        epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrtrack', type=str, help='ustsuw binary output')
    parser.add_argument('root',
                        type=str,
                        nargs='?',
                        help='output ROOT file name',
                        default="")
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        dest='verbose',
                        help='print what is being done')

    args = parser.parse_args()

    if not path.isfile(args.usrtrack):
        print("ustsuw2root: File %s does not exist." % args.usrtrack,
              file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrtrack, ".root")
    else:
        rootFileName = args.root

    b = Usrtrack()
    b.readHeader(args.usrtrack)

    ND = len(b.detector)

    if args.verbose:
        #b.sayHeader()
        for i in range(ND):
            b.printHeader(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        det = b.detector[i]
        val = Data.unpackArray(b.readData(i, det.lowneu))
        err = Data.unpackArray(b.readStat(i, det.lowneu))

        h = hist(det)
        hn = histN(det)  # filled only if det.lowneu

        n = h.GetNbinsX()
        assert n == det.ne, "n != det.ne"

        for i in range(n):
            h.SetBinContent(i + 1, val[i])
            h.SetBinError(i + 1, err[n - i - 1] * val[i])

        h.SetEntries(b.weight)
        h.Write()

        if det.lowneu:
            # val_lowneu = val[det.ne::][::-1]
            # err_lowneu = err[det.ne::][::-1]
            n = hn.GetNbinsX()
            assert n == det.ngroup, "n != det.ngroup"
            for i in range(n):
                hn.SetBinContent(i + 1, val[-i - 1])
                hn.SetBinError(i + 1, err[-i - 1] * val[-i - 1])

            hn.SetEntries(b.weight)
            hn.Write()

    fout.Close()
コード例 #9
0
ファイル: usrsuw2root.py プロジェクト: furutaka/mc-tools
def main():
    """Convert usrsuw output into a ROOT TH2F histogram

    """

    parser = argparse.ArgumentParser(
        description=main.__doc__,
        epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrsuw', type=str, help='usrsuw binary output')
    parser.add_argument('root',
                        type=str,
                        nargs='?',
                        help='output ROOT file name',
                        default="")
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        dest='verbose',
                        help='print what is being done')

    args = parser.parse_args()

    if not path.isfile(args.usrsuw):
        print("usrsuw2root: File %s does not exist." % args.usrsuw,
              file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrsuw, ".root")
    else:
        rootFileName = args.root

    b = Data.Resnuclei()
    b.readHeader(args.usrsuw)  # data file is closed here

    ND = len(b.detector)

    if args.verbose:
        b.sayHeader()
        print("\n%s %d %s found:" %
              ('*' * 20, ND, "estimator" if ND == 1 else "estimators"))
        for i in range(ND):
            b.say(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i))
        stat = b.readStat(i)
        total, A, errA, Z, errZ, err = map(Data.unpackArray, stat[:-1])

        iso = stat[-1]
        if iso:
            print("isomeric:", iso)

        det = b.detector[i]
        #        print(det.nb, det.name, det.type, det.region, det.mhigh, det.zhigh, det.nmzmin)

        h = hist(det)

        grA = graphA(det, A, errA)
        grZ = graphZ(det, Z, errZ)

        for z in range(1, det.zhigh + 1):
            for j in range(1, det.mhigh + 1):
                gbin = z - 1 + (j - 1) * (det.zhigh)
                if val[gbin] > 0.0:
                    a = j + det.nmzmin + 2 * z
                    h.SetBinContent(z, a, val[gbin])
                    h.SetBinError(z, a, err[gbin] * val[gbin])

        h.Write()
        grA.Write()  # same as h.ProjectionY()
        grZ.Write()  # same as h.ProjectionX()

    fout.Close()
コード例 #10
0
def main():
    """ Converts usxsuw output into a ROOT TH2F histogram """

    parser = argparse.ArgumentParser(description=main.__doc__,
                                     epilog="Homepage: https://github.com/kbat/mc-tools")
    parser.add_argument('usrbdx', type=str, help='usxsuw binary output')
    parser.add_argument('root', type=str, nargs='?', help='output ROOT file name', default="")
    parser.add_argument('-v', '--verbose', action='store_true', default=False, dest='verbose', help='print what is being done')

    args = parser.parse_args()

    if not path.isfile(args.usrbdx):
        print("usrbdx2root: File %s does not exist." % args.usrbdx, file=sys.stderr)
        return 1

    if args.root == "":
        rootFileName = "%s%s" % (args.usrbdx,".root")
    else:
        rootFileName = args.root

    b = Data.Usrbdx()
    b.readHeader(args.usrbdx) # data file closed here

    ND = len(b.detector)

    if args.verbose:
        b.sayHeader()
        print("\n%s %d %s found:" % ('*'*20, ND, "estimator" if ND==1 else "estimators"))
        for i in range(ND):
            b.say(i)
            print("")

    fout = ROOT.TFile(rootFileName, "recreate")
    for i in range(ND):
        val = Data.unpackArray(b.readData(i))
        err = Data.unpackArray(b.readStat(i))
        det = b.detector[i]
        lenval = len(val)

        assert lenval == len(err)

        h = hist(det)
        if det.lowneu and det.dist!=8:
            hn = histN(det)

#        print(det.name,det.lowneu,det.dist,"val:",len(val), det.ngroup, det.ne)
        if det.lowneu and det.dist==8: # 8 is NEUTRON
            lnval = val[-det.ngroup*det.na:][::-1]
            lnerr = err[-det.ngroup*det.na:][::-1]
            v = ()
            e = ()
            for a in range(det.na,0,-1):
                i = det.ngroup*(a-1)
                j = det.ngroup*(a)
                v += lnval[i:j]
                e += lnerr[i:j]

                i=(det.na-a)*det.ne
                j=(det.na-a+1)*det.ne
                v += val[i:j]
                e += err[i:j]

            val = v
            err = e


        assert lenval == len(val), "%d != %d" % (lenval, len(val))
        assert lenval == len(err), "%d != %d" % (lenval, len(err))


        nebins = getNEbins(det)
        for i in range(nebins):
            for j in range(det.na):
                gbin = i + j * nebins
                h.SetBinContent(i+1, j+1, val[gbin])
                h.SetBinError(i+1, j+1, val[gbin]*err[gbin]*15.91549*det.na) # still wrong if det.na>1

        # if (det.name=="pBackN"):
        #     for j in range(det.na):
        #         print(j)
        #         for i in range(nebins):
        #             print("%.3E %.3E %.3E" % (h.GetXaxis().GetBinLowEdge(i+1),
        #                                       h.GetXaxis().GetBinLowEdge(i+2), h.GetBinContent(i+1,j+1)))

        h.SetEntries(b.weight)
        h.Write()

        if det.lowneu and det.dist!=8:
            hn.Write()

    fout.Close()