Ejemplo n.º 1
0
 def writefiles(self, listname, **kwargs):
     """Write filenames to text file for fast look up in future."""
     writeevts = kwargs.pop('nevts', False)  # also write nevents to file
     listname = repkey(listname,
                       ERA=self.era,
                       GROUP=self.group,
                       SAMPLE=self.name)
     print ">>> Write list to %r..." % (listname)
     ensuredir(os.path.dirname(listname))
     filenevts = self.getfilenevts(checkfiles=True, **
                                   kwargs) if writeevts else None
     treename = kwargs.pop('tree', 'Events')
     files = self.getfiles(**kwargs)
     with open(listname, 'w+') as lfile:
         for infile in files:
             if writeevts:
                 nevts = filenevts.get(infile, -1)
                 if nevts < 0:
                     LOG.warning(
                         "Did not find nevents of %s. Trying again..." %
                         (infile))
                     nevts = getnevents(infile, treename)
                 infile = "%s:%d" % (infile, nevts
                                     )  # write $FILENAM(:NEVTS)
             lfile.write(infile + '\n')
Ejemplo n.º 2
0
 def _getnevents(self,
                 das=True,
                 refresh=False,
                 tree='Events',
                 limit=-1,
                 checkfiles=False,
                 verb=0):
     """Get number of nanoAOD events from DAS (default), or from files on storage system (das=False)."""
     nevents = self.nevents
     filenevts = self.filenevts
     treename = tree
     if nevents <= 0 or refresh:
         if checkfiles or (self.storage and not das
                           ):  # get number of events from storage system
             files = self.getfiles(url=True,
                                   das=das,
                                   refresh=refresh,
                                   limit=limit,
                                   verb=verb)
             for fname in files:
                 nevts = getnevents(fname, treename)
                 filenevts[fname] = nevts  # cache
                 nevents += nevts
                 LOG.verb(
                     "_getnevents: Found %d events in %r." % (nevts, fname),
                     verb, 3)
         else:  # get number of events from DAS
             for daspath in self.paths:
                 nevents += getdasnevents(daspath,
                                          instance=self.instance,
                                          verb=verb - 1)
         if limit < 0:
             self.nevents = nevents
     return nevents, filenevts
Ejemplo n.º 3
0
 def _writefile(ofile,fname,prefix=""):
   """Help function to write individual files."""
   if writeevts: # add nevents at end of infile string
     nevts = filenevts.get(fname,-1) # retrieve from cache
     if nevts<0:
       LOG.warning("Did not find nevents of %s. Trying again..."%(fname))
       nevts = getnevents(fname,treename) # get nevents from file
     fname = "%s:%d"%(fname,nevts) # write $FILENAM(:NEVTS)
   ofile.write(prefix+fname+'\n')
Ejemplo n.º 4
0
def main(args):
    files = args.files
    tname = args.tree
    hname = args.hist
    bin = args.bin
    ntot = 0
    nfiles = len(files)
    for file in files:
        if hname:
            nevts = isvalid(file, hname,
                            bin)  # get nevents from TH1.GetBinContent(bin)
        else:
            nevts = getnevents(file,
                               tname)  # get nevents from TTree.GetEntries()
        ntot += nevts
        print ">>> %9d  %s" % (nevts, file)
    print ">>> %9d  total in %d files, average %.1f" % (ntot, nfiles,
                                                        float(ntot) / nfiles)