def chktrack2(inputfile, outputfile, wcsim=True):
#   Int_t i, j;
#   Int_t nevents;
#   double cosx,cosy,cosz;
#   double p;
#   Int_t stat;
    kPi = ROOT.TMath.Pi();
  
#   TTree  *tn;
#   NeutVtx *nvtx;
#   NeutVect *nvect;

    ran = ROOT.TRandom3(0);

    f = ROOT.TFile(inputfile);
    tn = f.Get("nRooTracker"); # try to get NEUT tree
    if not tn:
        tn = f.Get("gRooTracker"); # try to get GENIE tree
    nevents = tn.GetEntries();
    fcomp = open(outputfile,"w");
    for j, entry in enumerate(tn):
        m_to_cm = 100.0
        GeV_to_MeV = 1000.0
        try:
            mode = entry.NEneutmode
        except AttributeError:
            mode = entry.G2NeutEvtCode
        vtx = entry.EvtVtx
    
        x = vtx[0] * m_to_cm
        y = vtx[1] * m_to_cm
        z = vtx[2] * m_to_cm
        t = vtx[3] * m_to_cm
    
        fcomp.write("$ begin\n");
        fcomp.write("$ nuance %d\n" % mode);
        fcomp.write("$ vertex %5.4f %5.4f %5.4f %5.4f\n" % (x, y, z, t) );

        npart = entry.StdHepN
        stdhepp4 = ROOT.getRooTrackerHepP4(entry.StdHepN, entry.StdHepP4)
        tracklist = []
        infoline_index = 0
        for i in xrange(npart):
            #get the information that we need
            pvec = stdhepp4.at(i)
            pid = entry.StdHepPdg[i]
            stdhepstatus = entry.StdHepStatus[i]
            #p = ROOT.TMath.Sqrt(
            #                    pvec.E()*(pvec.E()-part.fMass*part.fMass)
            #                    )
            pvec = ROOT.TLorentzVector(pvec[0] * GeV_to_MeV, 
                                       pvec[1] * GeV_to_MeV, 
                                       pvec[2] * GeV_to_MeV, 
                                       pvec[3] * GeV_to_MeV
                                       )
            p = pvec.P()
            if p == 0: 
                p = 0.000001;
            cosx = pvec.Px() / p;
            cosy = pvec.Py() / p;
            cosz = pvec.Pz() / p;

            if stdhepstatus==0:
                #initial state particle
                stat = -1;
            elif stdhepstatus == 1:
                #final state particle
                stat = 0
            else:
                #unknown (intermediate?)
                stat = -2;

            if (stat == -2):
                continue; # get rid of intermediate particles

            if stat == -1:
                infoline_index = i

            if pid > 10000:
                continue;
            tracklist.append("$ track %d %5.4f %5.4f %5.4f %5.4f %d\n" % (pid, pvec.E(), cosx, cosy, cosz, stat))
        if wcsim:
            #insert the info line into the tracklist
            infoline = "$ info 0 0 %d\n" % j
            tracklist.insert(infoline_index, infoline)
        for trk in tracklist:
            fcomp.write(trk)
        #fcomp.write(" $headerend %d\n" % j);
        fcomp.write("$ end %d\n" % j);
    fcomp.close()
    return
def _dump_single_event(tree, jj, ostr=sys.stdout):
        tree.GetEntry(jj)
        m_to_cm = 100.0
        GeV_to_MeV = 1000.0
        try:
            mode = tree.NEneutmode
        except AttributeError:
            mode = tree.G2NeutEvtCode
        vtx = tree.EvtVtx
    
        x = vtx[0] * m_to_cm
        y = vtx[1] * m_to_cm
        z = vtx[2] * m_to_cm
        t = vtx[3] * m_to_cm
        r = math.sqrt(x**2 + y**2)

        print >>ostr, "    Event number", jj
        print >>ostr, "Interaction code", mode
        print >>ostr, "          vertex x=%5.0fcm, y=%5.0fcm, r=%5.0fcm, z=%5.0fcm, t=%5.2f" % (x, y, r, z, t)
        #Loop over particles
        npart = tree.StdHepN
        stdhepp4 = ROOT.getRooTrackerHepP4(tree.StdHepN, tree.StdHepP4)
        header = ["index", "pdg", "particle", "status", "E [Mev]", "p [MeV]", "m [MeV]", "status name"]
        headerfmt = " | ".join(["{:<10s}"] * len(header))
        ifmt = "{:>10.0f}"
        ffmt = "{:>10.0f}"
        sfmt = "{:>10s}"
        rowfmt = " | ".join([ifmt, ifmt, sfmt, ifmt, ffmt, ffmt, ffmt, sfmt])
        header = headerfmt.format(*header)
        hline = "-"*len(header)
        print >>ostr,header
        print >>ostr, hline
        for i in xrange(npart):
            #get the information that we need
            pvec = stdhepp4.at(i)
            pid = tree.StdHepPdg[i]
            stdhepstatus = tree.StdHepStatus[i]
            #p = ROOT.TMath.Sqrt(
            #                    pvec.E()*(pvec.E()-part.fMass*part.fMass)
            #                    )
            pvec = ROOT.TLorentzVector(pvec[0] * GeV_to_MeV, 
                                       pvec[1] * GeV_to_MeV, 
                                       pvec[2] * GeV_to_MeV, 
                                       pvec[3] * GeV_to_MeV
                                       )
            if stdhepstatus==0:
                #initial state particle
                status_string = "initial";
            elif stdhepstatus == 1:
                #final state particle
                status_string = "final";
            else:
                #unknown (intermediate?)
                status_string = "inter";
            momentum = pvec.P()
            energy = pvec.E()
            mass = pvec.M()
            #print >>ostr, "particle pdg=%s, status=%s, E=%.0f, p=%.0f, m=%.0f (%s)" % (pid, stdhepstatus, energy, momentum, mass, status_string)
            row = [i, pid, pdg_to_string(pid), stdhepstatus, energy, momentum, mass, status_string]
            print >>ostr, rowfmt.format(*row)
        print >>ostr, hline
        return