Exemplo n.º 1
0
    def __getitem__(self, idx):

        # convert to actual index: by default, it is idx, but not if event_list provided
        event_idx = self._event_list[idx]

        # If this is the first data loading, instantiate chains
        if not self._trees_ready:
            from ROOT import TChain
            for key in self._trees.keys():
                chain = TChain(key + '_tree')
                for f in self._files:
                    chain.AddFile(f)
                self._trees[key] = chain
            self._trees_ready = True
        # Move the event pointer
        for tree in self._trees.values():
            tree.GetEntry(event_idx)
        # Create data chunks
        result = {}
        for index, (parser, datatree_keys) in enumerate(self._data_parsers):
            data = [
                getattr(self._trees[key], key + '_branch')
                for key in datatree_keys
            ]
            name = self._data_keys[index]
            result[name] = parser(data)

        result['index'] = event_idx
        return result
Exemplo n.º 2
0
def singleParticle(event):
    particle_mcst_chain = TChain("particle_mcst_tree")
    particle_mcst_chain.AddFile("data/test_10k.root")
    particle_mcst_chain.GetEntry(event)
    cpp_object = particle_mcst_chain.particle_mcst_branch
    particles = []
    single = True
    showerParticles = [11, -11, 111, 22]
    for particle in cpp_object.as_vector():
        particles.append(particle.pdg_code())
    for sParticle in showerParticles:
        if particles.count(sParticle) > 1:
            single = False
            return False, 0
    sumCount = 0
    trackIDs = []

    for particle in particles:
        if particle == 22 or particle == 11 or particle == -11 or particle == 111:
            sumCount += 1
    for particle in cpp_object.as_vector():
        if abs(particle.pdg_code()) == 22 or abs(
                particle.pdg_code()) == 111 or abs(particle.pdg_code()) == 11:
            trackIDs.append(particle.track_id())
    singleEvent = single and (sumCount == 1)

    return singleEvent, trackIDs
Exemplo n.º 3
0
def mkChain(files, name="vtxOptTree"):
    from ROOT import TChain
    chain = TChain(name)
    for f in files:
        print "Adding file %s" % f
        chain.AddFile(f)
    return chain
Exemplo n.º 4
0
def load_chain_from_files(file_names, tree_name):
    """
    Load TTree with tree_name from multiple files in a TChain.

    @param file_names List with ROOT file names or basestring "*" is allowed
    @param tree_name TTree with this name is loaded from files
    @return TChain
    """
    file_name_list = None

    if isinstance(file_names, basestring):
        if '*' in file_names:
            file_name_list = glob.glob(file_names)
        else:
            file_name_list = [file_names]
    elif isinstance(file_names, list):
        file_name_list = file_names
    else:
        raise TypeError("%s is not a str or list of str" % file_names)

    data_chain = TChain(tree_name)
    added_files = 0
    for file in file_name_list:
        if not os.path.isfile(file):
            raise IOError("File %s does not exist." % file)

        added_files += data_chain.AddFile(file)

    return data_chain, added_files
Exemplo n.º 5
0
class BaseData(object):
  tree_name=''
  logger = logging.getLogger('root2hdf5.data_types.base')
  def __init__(self, _file):
    from ROOT import TChain
    self.event_index = 0
    self.chain = TChain(self.tree_name)
    self.chain.AddFile(_file)

  def convert(self):
    self.logger.info("Starting Conversion of: {}".format(self.tree_name))
    pbar = tqdm.tqdm(total=self.chain.GetEntries())
    for i in self:
      pbar.update(1)
    pbar.close()
    del pbar
    self.logger.info("Done.")

  def __iter__(self):
    return self

  def __next__(self):
    return self.next()

  def next(self):
    if self.event_index == self.chain.GetEntries():
      raise StopIteration()
    self.chain.GetEntry(self.event_index)
    branch = getattr(self.chain, self.tree_name.replace('tree','branch'))
    self.process_branch(branch)
    self.event_index += 1
    return self.event_index
Exemplo n.º 6
0
    def __init__(self,
                 data_schema,
                 data_dirs,
                 data_key=None,
                 limit_num_files=0):
        """
        Args: data_dirs ..... a list of data directories to find files (up to 10 files read from each dir)
              data_schema ... a dictionary of string <=> list of strings. The key is a unique name of a data chunk in a batch.
                              The list must be length >= 2: the first string names the parser function, and the rest of strings
                              identifies data keys in the input files.
              data_key ..... a string that is required to be present in the filename
              limit_num_files ... an integer limiting number of files to be taken per data directory 
        """

        # Create file list
        self._files = _list_files(data_dirs, data_key, limit_num_files)
        if len(self._files) > 10: print(len(self._files), 'files loaded')
        else:
            for f in self._files:
                print('Loading file:', f)

        # Instantiate parsers
        self._data_keys = []
        self._data_parsers = []
        self._trees = {}
        for key, value in data_schema.items():
            if len(value) < 2:
                print(
                    'iotools.datasets.schema contains a key %s with list length < 2!'
                    % key)
                raise ValueError
            if not hasattr(mlreco.iotools.parsers, value[0]):
                print('The specified parser name %s does not exist!' %
                      value[0])
            self._data_keys.append(key)
            self._data_parsers.append((getattr(mlreco.iotools.parsers,
                                               value[0]), value[1:]))
            for data_key in value[1:]:
                if data_key in self._trees: continue
                self._trees[data_key] = None
        self._data_keys.append('index')

        # Prepare TTrees and load files
        from ROOT import TChain
        self._entries = None
        for data_key in self._trees.keys():
            # Check data TTree exists, and entries are identical across >1 trees.
            # However do NOT register these TTrees in self._trees yet in order to support >1 workers by DataLoader
            print('Loading tree', data_key)
            chain = TChain(data_key + "_tree")
            for f in self._files:
                chain.AddFile(f)
            if self._entries is not None:
                assert (self._entries == chain.GetEntries())
            else:
                self._entries = chain.GetEntries()
        # Flag to identify if Trees are initialized or not
        self._trees_ready = False
Exemplo n.º 7
0
 def Process_Files(file_arr):
     emp_arr = []
     i = 0
     for i in range(len(file_arr)):
         print "Sample_" + str(i) + " = " + file_arr[i]
         a = TChain("NominalFixed")
         a.AddFile(file_arr[i])
         emp_arr.append(a)
         i = i + 1
     return emp_arr  #Returns an array of TChain Root Objects
Exemplo n.º 8
0
def returnChain(filelist):

    files = filelist.split(',')

    chain = TChain('tree', 'tree')

    for inputfile in files:
        print('Adding ...', inputfile)
        chain.AddFile(inputfile)

    return chain
Exemplo n.º 9
0
def energy(ENTRY):
    image2d, label2d = show_event(ENTRY)
    unique_values, unique_counts = np.unique(label2d, return_counts=True)
    print('Label values:', unique_values)
    print('Label counts:', unique_counts)
    categories = ['Background', 'Shower', 'Track']
    fig, axes = plt.subplots(1,
                             len(unique_values),
                             figsize=(18, 12),
                             facecolor='w')
    xlim, ylim = get_view_range(image2d)

    for index, value in enumerate(unique_values):
        ax = axes[index]
        mask = (label2d == value)
        Image = image2d * mask
        ax.imshow(Image, interpolation='none', cmap='jet', origin='lower')
        print("Event", categories[index])
        print("Energy: {0:.2f} MeV".format(np.sum((Image) / 100)))
        ax.set_title(categories[index],
                     fontsize=20,
                     fontname='Georgia',
                     fontweight='bold')
        ax.set_xlim(xlim)
        ax.set_ylim(ylim)
        if index == 1:
            showerImage = np.array(Image)
            b, m, angle = plot_best_fit(showerImage)
            ax.set_title("{0} {1:.2f} deg".format(categories[index], angle),
                         fontsize=20,
                         fontname='Georgia',
                         fontweight='bold')
            print("Angle of the shower: {0:.2f}".format(angle))
            print("Energy of the shower: {0:.2f} MeV".format(
                np.sum((Image) / 100)))
            shower_energy = np.sum(Image / 100)
            x = np.array(range(0, len(showerImage[0])))
            y = x * m + b
            ax.plot(x, y)
    plt.show()

    particle_mcst_chain = TChain("particle_mcst_tree")
    particle_mcst_chain.AddFile("data/test_10k.root")
    particle_mcst_chain.GetEntry(ENTRY)
    cpp_object = particle_mcst_chain.particle_mcst_branch

    print('particle_mcst_tree contents:')
    energy_deposit = 0
    for particle in cpp_object.as_vector():
        if abs(particle.pdg_code()) == 22 or abs(particle.pdg_code()) == 11:
            energy_deposit += particle.energy_deposit()
        print(particle.dump())
    print("Energy Deposit: ", energy_deposit)
    return energy_deposit, shower_energy
Exemplo n.º 10
0
    def _connectInputs(self,
                       samples,
                       inputDir,
                       skipMissingFiles,
                       friendsDir=None,
                       skimListDir=None):
        listTrees = []
        for aFile in samples:
            tree = TChain(self._treeName)
            tree.AddFile(aFile)
            listTrees.append(tree)

        return listTrees
Exemplo n.º 11
0
    def initialize(self):
        from larcv import larcv
        from ROOT import TChain
        ch_data = TChain('sparse3d_%s_tree' % self._flags.DATA_KEY)
        ch_label = TChain('sparse3d_%s_tree' % self._flags.LABEL_KEY)
        for f in self._flags.INPUT_FILE:
            ch_data.AddFile(f)
            ch_label.AddFile(f)
        self._num_entries = ch_data.GetEntries()
        self._data = []
        self._label = []
        br_data, br_label = (None, None)
        event_fraction = 1. / self.num_entries() * 100.
        total_point = 0.
        for i in range(self.num_entries()):
            ch_data.GetEntry(i)
            ch_label.GetEntry(i)
            if br_data is None:
                br_data = getattr(ch_data,
                                  'sparse3d_%s_branch' % self._flags.DATA_KEY)
                br_label = getattr(
                    ch_label, 'sparse3d_%s_branch' % self._flags.LABEL_KEY)
            num_point = br_data.as_vector().size()
            np_data = np.zeros(shape=(num_point, 4), dtype=np.float32)
            np_label = np.zeros(shape=(num_point, 4), dtype=np.float32)
            larcv.fill_3d_pcloud(br_data, np_data)
            larcv.fill_3d_pcloud(br_label, np_label)
            self._data.append(np.expand_dims(np_data, 0))
            self._label.append(np.expand_dims(np_label, 0))

            total_point += np_data.size
            sys.stdout.write(
                'Processed %d%% ... %d MB\r' %
                (int(event_fraction * i), int(total_point * 4 * 2 / 1.e6)))
            sys.stdout.flush()
        sys.stdout.write('\n')
        sys.stdout.flush()
Exemplo n.º 12
0
def mkChain(files, name="vtxOptTree", aliases={}):
    ### if len(files) == 1:
    ###     from ROOT import TFile
    ###     f = TFile.Open(files[0])
    ###     objs.append(f)
    ###     t=f.Get(name)
    ###     objs.append(t)
    ###     return t
    from ROOT import TChain
    chain = TChain(name)
    for f in files:
        print "Adding file %s" % f
        chain.AddFile(f)
    setAliases(chain, aliases)
    return chain
Exemplo n.º 13
0
def draw_contour(attribute_name, treename='fitted.root', dir=training_dir):
    # Create a new canvas, and customize it.
    #c1 = TCanvas( 'c1', 'Contour Plot for ', attribute_name, 200, 10, 700, 500 )
    c1 = TCanvas()
    c1.SetFillColor( 42 )
    c1.GetFrame().SetFillColor( 21 )
    c1.GetFrame().SetBorderSize( 6 )
    c1.GetFrame().SetBorderMode( -1 )

    # Open tree
    ttree = TChain('nominal', 'nominal')
    ttree.AddFile("{0}/{1}".format(dir, treename))

    # Draw and save contour plot
    ttree.Draw("{0}_true:{0}_fitted".format(attribute_name), "", "colz")
    c1.SaveAs("{0}/ContourPlots/{1}.jpg".format(dir, attribute_name))
Exemplo n.º 14
0
    def __getitem__(self, idx):
        # If this is the first data loading, instantiate chains
        if not self._trees_ready:
            from ROOT import TChain
            for key in self._trees.keys():
                chain = TChain(key + '_tree')
                for f in self._files:
                    chain.AddFile(f)
                self._trees[key] = chain
            self._trees_ready = True
        # Move the event pointer
        for tree in self._trees.values():
            tree.GetEntry(idx)
        # Create data chunks
        result = []
        for parser, data_keys in self._data_parsers:
            data = [
                getattr(self._trees[key], key + '_branch') for key in data_keys
            ]
            result.append(parser(data))

        result.append([idx])
        return tuple(result)
Exemplo n.º 15
0
def getRootChain(jobpar,treeName):

    import glob
    # print infiles
    InputRootFiles = glob.glob (jobpar.inputFiles)

    nStep1=0
    nStep1WithPU=0
    tr = TChain(treeName)    
    trFR = TChain("treeFriend")
    for rootfile in InputRootFiles:
        basename=os.path.basename(rootfile)
        print "Adding to chain: ",rootfile,rootfile.find("pnfs")
        if rootfile.find("pnfs")>-1:
            rootfile="dcache:" + rootfile

        ## print "XXX: ", rootfile

        f=TFile.Open(rootfile)
        h1=f.Get('Count')  # get histogram with Step 1 event count
        # print h.GetEntries()
        nStep1 = nStep1 + h1.GetBinContent(1)

        h2=f.Get('CountWithPU')  # get histogram with Step 1 event count
        # print h.GetEntries()
        nStep1WithPU = nStep1WithPU + h2.GetBinContent(1)
        f.Close()

        tr.AddFile(rootfile)

        ## now get friends
        FriendDir=os.path.join(os.path.dirname(rootfile),"NewFriends")
        if jobpar.isData:
            if os.environ.has_key('DATAFRIEND'):
                FriendDir=os.environ['DATAFRIEND']
        else:
            if os.environ.has_key('MCFRIEND'):
                FriendDir=os.environ['MCFRIEND']

        ## if jobpar.ApplyBJetRegressionSubjets:
        friendName=basename[:basename.find(".root")] + "_Friend.root"
        rootFriend=os.path.join(FriendDir,friendName)
            #if not os.path.isfile(rootFriend):
            #    print "Could not find Friend " + rootFriend+ " -- Exiting"
            #    sys.exit(1)

        print "Adding friend ",rootFriend
        trFR.AddFile(rootFriend)


    if (tr.GetEntries() != trFR.GetEntries()):
        print "Number of entries on Main and Friend chains differ -- exiting program"
        sys.exit(1)

    tr.AddFriend(trFR)
    SetOwnership( tr, False )

    print "Number of Step1 events: ", nStep1
    print "Number of Step1 events (pileup weighted): ", nStep1WithPU

    return tr, nStep1, nStep1WithPU
Exemplo n.º 16
0
gROOT.Macro(os.path.expanduser('~/rootlogon.C'))

style1 = Style()
style2 = Style(markerColor=2, lineColor=2)
style3 = Style(markerColor=3, lineColor=3)
style4 = Style(markerColor=4, lineColor=4)
style5 = Style(markerColor=5, lineColor=5)

basename = 'rfio:/castor/cern.ch/cms/store/cmst3/user/lenzip/CMG/SusyLP/WJetsToLNu_TuneD6T_7TeV-madgraph-tauola/Spring11-PU_S1_START311_V1G1-v1/CMGtuple/'
vfile = []
for i in range(6):
    vfile.append(basename + 'susy_tree_CMG_' + str(i) + '.root')

events = TChain('Events', 'Events')
for file in vfile:
    events.AddFile(file)
#file = TFile( sys.argv[1] )
#events = file.Get('Events')

from CMGTools.RootTools.cmgTuple import *

cmg = cmgTuple(events)

LPcanvas = TCanvas('LPcanvas', 'LP', 1000, 800)
LPcanvas.Divide(2, 3)

num1 = TH1F('num1', 'num1', 100, 0, 100)
num2 = TH1F('num2', 'num2', 100, 0, 100)

den1 = TH1F('den1', 'den1', 100, 0, 100)
den2 = TH1F('den2', 'den2', 100, 0, 100)
Exemplo n.º 17
0
import sys
from ROOT import TChain, larcv
larcv.load_pyutil
import cv2, numpy
import matplotlib.pyplot as plt

ch1 = TChain("image2d_bnbnu_mc_int00_tree")
ch2 = TChain("image2d_segment_bnbnu_mc_tree")

ch1.AddFile(sys.argv[1])
ch2.AddFile(sys.argv[1])

ch1.GetEntry(0)
ch2.GetEntry(0)

img1_v = ch1.image2d_bnbnu_mc_int00_branch.Image2DArray()
img2_v = ch2.image2d_segment_bnbnu_mc_branch.Image2DArray()

for i in xrange(3):
    img1 = larcv.as_ndarray(img1_v[i])
    img2 = larcv.as_ndarray(img2_v[i])

    for col in xrange(len(img1)):
        for row in xrange(len(img1[col])):
            v1 = img1[col][row]
            if v1 < 5: v1 = 0
            if v1 > 5: v1 = 1
            v2 = img2[col][row]
            if v2 < 0: v2 = 0
            if v2 > 0: v2 = 1
            if not v1 == v2: print v1 - v2,
Exemplo n.º 18
0
#os.system('rm -r mynnmodels')
#os.system('mkdir mynnmodels')
TMVA.PyMethodBase.PyInitialize()
output = TFile.Open('GGFKiller.root', 'RECREATE')
factory = TMVA.Factory(
    'TMVAClassification', output,
    '!V:!Silent:Color:DrawProgressBar:Transformations=I:AnalysisType=Classification'
)
#Locate and add data files
file_VBF_HH_2016 = "../inputsamples/2016/SKIM_VBFHHTo4B_CV_1_C2V_1_C3_1_13TeV-madgraph.root"
file_VBF_HH_2017 = "../inputsamples/2017/SKIM_VBFHHTo4B_CV_1_C2V_1_C3_1_13TeV-madgraph.root"
file_GGF_HH_2016 = "../inputsamples/2016/SKIM_GluGluToHHTo4B_node_SM_13TeV-madgraph.root"
file_GGF_HH_2017 = "../inputsamples/2017/SKIM_GluGluToHHTo4B_node_SM_13TeV-madgraph_correctedcfg.root"
ch_sig = TChain("bbbbTree")
ch_bkg = TChain("bbbbTree")
ch_sig.AddFile(file_VBF_HH_2016)
ch_sig.AddFile(file_VBF_HH_2017)
#ch_sig.AddFile(file_VBF_HH_BSM_2016)
#ch_sig.AddFile(file_VBF_HH_BSM_2017)
ch_bkg.AddFile(file_GGF_HH_2016)
ch_bkg.AddFile(file_GGF_HH_2017)
#Load data to TMVA
dataloader = TMVA.DataLoader('GGFKiller')
dataloader.AddVariable("abs_H1_eta:=abs(H1_eta)")
dataloader.AddVariable("abs_H2_eta:=abs(H2_eta)")
dataloader.AddVariable("H1_pt")
dataloader.AddVariable("H2_pt")
dataloader.AddVariable("JJ_j1_pt")
dataloader.AddVariable("JJ_j2_pt")
dataloader.AddVariable("abs_JJ_eta:=abs(JJ_eta)")
dataloader.AddVariable("h1h2_deltaEta")
Exemplo n.º 19
0
    def initialize(self):
        self._last_entry = -1
        self._event_keys = []
        self._metas = []
        # configure the input
        from larcv import larcv
        from ROOT import TChain
        # set 2d vs. 3d functions
        as_numpy_array = None
        dtype_keyword = ''
        if self._flags.DATA_DIM == 3:
            as_numpy_voxels = larcv.fill_3d_voxels
            as_numpy_pcloud = larcv.fill_3d_pcloud
            dtype_keyword = 'sparse3d'
        elif self._flags.DATA_DIM == 2:
            as_numpy_voxels = larcv.fill_2d_voxels
            as_numpy_pcloud = larcv.fill_2d_pcloud
            dtype_keyword = 'sparse2d'
        else:
            print('larcv IO not implemented for data dimension',
                  self._flags.DATA_DIM)
            raise NotImplementedError

        ch_data = TChain('%s_%s_tree' % (dtype_keyword, self._flags.DATA_KEY))
        ch_label = None
        if self._flags.LABEL_KEY:
            ch_label = TChain('%s_%s_tree' %
                              (dtype_keyword, self._flags.LABEL_KEY))
        for f in self._flags.INPUT_FILE:
            ch_data.AddFile(f)
            if ch_label: ch_label.AddFile(f)

        self._voxel = []
        self._feature = []
        self._label = []
        br_data, br_label = (None, None)
        event_fraction = 1. / ch_data.GetEntries() * 100.
        total_point = 0.
        for i in range(ch_data.GetEntries()):
            if self._flags.LIMIT_NUM_SAMPLE > 0 and i > self._flags.LIMIT_NUM_SAMPLE:
                break
            ch_data.GetEntry(i)
            if ch_label: ch_label.GetEntry(i)
            if br_data is None:
                br_data = getattr(
                    ch_data,
                    '%s_%s_branch' % (dtype_keyword, self._flags.DATA_KEY))
                if ch_label:
                    br_label = getattr(
                        ch_label, '%s_%s_branch' %
                        (dtype_keyword, self._flags.LABEL_KEY))

            # HACK that should go away when unifying 2d and 3d data reps...
            data_tensor = br_data
            label_tensor = br_label
            if self._flags.DATA_DIM == 2:
                data_tensor = br_data.as_vector().front()
                label_tensor = br_label.as_vector().front()

            num_point = data_tensor.as_vector().size()
            if num_point < 10: continue

            self._event_keys.append(
                (br_data.run(), br_data.subrun(), br_data.event()))
            # HACK that should go away when unifying 2d and 3d data reps...
            if self._flags.DATA_DIM == 2:
                self._metas.append(larcv.ImageMeta(label_tensor.meta()))
            else:
                self._metas.append(larcv.Voxel3DMeta(br_data.meta()))

            np_voxel = np.zeros(shape=(num_point, self._flags.DATA_DIM),
                                dtype=np.int32)
            as_numpy_voxels(data_tensor, np_voxel)
            self._voxel.append(np_voxel)

            np_feature = np.zeros(shape=(num_point, 1), dtype=np.float32)
            as_numpy_pcloud(data_tensor, np_feature)
            self._feature.append(np_feature)

            if ch_label:
                np_label = np.zeros(shape=(num_point, 1), dtype=np.float32)
                as_numpy_pcloud(label_tensor, np_label)
                np_label = np_label.reshape([num_point])  # - 1.
                self._label.append(np_label)
            total_point += num_point
            sys.stdout.write(
                'Processed %d%% ... %d MB\r' %
                (int(event_fraction * i), int(total_point * 4 * 2 / 1.e6)))
            sys.stdout.flush()

        sys.stdout.write('\n')
        sys.stdout.flush()
        self._num_channels = self._voxel[-1].shape[-1]
        self._num_entries = len(self._voxel)
        # Output
        if self._flags.OUTPUT_FILE:
            import tempfile
            cfg = '''
IOManager: {
      Verbosity:   2
      Name:        "IOManager"
      IOMode:      1
      OutFileName: "%s"
      InputFiles:  []
      InputDirs:   []
      StoreOnlyType: []
      StoreOnlyName: []
    }
                  '''
            cfg = cfg % self._flags.OUTPUT_FILE
            cfg_file = tempfile.NamedTemporaryFile('w')
            cfg_file.write(cfg)
            cfg_file.flush()
            self._fout = larcv.IOManager(cfg_file.name)
            self._fout.initialize()
Exemplo n.º 20
0
def hist(sample, color, output, expected_Nevets):
    chain = TChain("events")

    #files = glob("/afs/cern.ch/work/a/axiong/public/FCCsoft/heppy/FCChhAnalyses/Zprime_jj/out_0_500/*.root")
    homedirectory = "/afs/cern.ch/work/a/axiong/public/FCCsoft/heppy/FCChhAnalyses/"
    # ...Zprime_jj... depends on the location of TreeProducer.py used, subject to change
    rootfile = "/heppy.FCChhAnalyses.Zprime_jj.TreeProducer.TreeProducer_1/tree.root"

    # Add all files to TChain
    for i in range(5):
        filename = homedirectory + sample + str(i) + rootfile
        file = TFile(filename)
        chain.AddFile(filename)
        print("root file from chunk" + str(i) + " is added")

    # declare histograms
    h1d_mjj = TH1F('jj_mass', '', 130, 0, 2000)
    h1d_pt1 = TH1F('jj_pt1', '', 130, 0, 1500)
    h1d_pt2 = TH1F('jj_pt2', '', 130, 0, 1000)
    h1d_eta1 = TH1F('jj_eta1', '', 25, -5.0, 5.0)
    h1d_eta2 = TH1F('jj_eta2', '', 25, -5.0, 5.0)
    h1d_chi = TH1F('jj_chi', '', 25, 0, 18)
    h1d_delR = TH1F('jj_delR', '', 25, -1, 6)
    h1d_ht = TH1F("ht", '', 100, 0, 3000)
    h1d_deleta = TH1F('deleta', '', 20, -4, 4)
    #mychain = chain.Get( 'events' )
    entries = chain.GetEntries()

    # Event loop
    for jentry in xrange(entries):
        ientry = chain.LoadTree(jentry)
        if ientry < 0:
            break
        nb = chain.GetEntry(jentry)
        if nb <= 0:
            continue

# feed data of two leading jets into TLorentzVector
        jet1 = TLorentzVector()
        jet1.SetPtEtaPhiE(chain.jet1_pt, chain.jet1_eta, chain.jet1_phi,
                          chain.jet1_e)
        jet2 = TLorentzVector()
        jet2.SetPtEtaPhiE(chain.jet2_pt, chain.jet2_eta, chain.jet2_phi,
                          chain.jet2_e)
        jet12 = jet1 + jet2

        # Fill the histograms
        h1d_mjj.Fill(jet12.M())
        h1d_pt1.Fill(chain.jet1_pt)
        h1d_pt2.Fill(chain.jet2_pt)
        h1d_eta1.Fill(chain.jet1_eta)
        h1d_eta2.Fill(chain.jet2_eta)

        dr = jet1.DeltaR(jet2)
        h1d_delR.Fill(dr)

        h1d_chi.Fill(np.exp(2 *
                            (np.absolute(chain.jet1_eta - chain.jet2_eta))))
        h1d_deleta.Fill(chain.jet1_eta - chain.jet2_eta)
        h1d_ht.Fill(chain.Ht)

    hist = [
        h1d_mjj, h1d_chi, h1d_delR, h1d_pt1, h1d_pt2, h1d_eta1, h1d_eta2,
        h1d_deleta, h1d_ht
    ]
    title = [
        "dijet mass distribution", "angular variable distribution",
        "angular seperation", "Lead pt distribution",
        "Sublead pt distribution", "Rapitity distribution",
        "Rapitity distribution 2", "rapidity difference", "Jet Pt sum"
    ]
    x_title = [
        "mjj (GeV)", "chi", 'del_R', "pt(GeV)", "pt(GeV)", "eta", "eta",
        "eta1-eta2", "Ht (GeV)"
    ]

    color = [color]
    Line_color = color * 9

    f = TFile(output, "recreate")

    for i in range(9):
        hist[i].SetTitle(title[i])
        hist[i].GetXaxis().SetTitle(x_title[i])
        hist[i].GetYaxis().SetTitle('# of events')
        scale = expected_Nevets / hist[i].Integral()
        hist[i].SetLineColor(Line_color[i])
        hist[i].Scale(scale)
        hist[i].Write()
    print "finished processing the sample"
    print ""
Exemplo n.º 21
0
from ROOT import TChain,TH1D,TCanvas
import sys
fakefile = None
chain = TChain("tpcfifo_tree")

for infile in sys.argv[1:]:
    if ".txt" in infile:
        fakefile = infile
    else:
        chain.AddFile( infile )

# Read in the fake data pattern
fdata = []
if fakefile:
    f = open( fakefile, "r" )
    for line in f:
        fdata += line.strip().split()

    print "len(fdata): ", len(fdata)
    toffset = 2

# Output canvas
c = TCanvas('c','',600,500)
c.SetGridx(1)
c.SetGridy(1)

chain.GetEntries()

for x in xrange( chain.GetEntries() ):
    chain.GetEntry(x)
    wfs = chain.tpcfifo_branch
Exemplo n.º 22
0
file_QCD_1000_1500_2017 = "../../inputsamples/2017altjobs/SKIM_QCD_HT1000to1500_TuneCP5_13TeV-madgraph-pythia8.root"
file_QCD_1500_2000_2017 = "../../inputsamples/2017altjobs/SKIM_QCD_HT1500to2000_TuneCP5_13TeV-madgraph-pythia8.root"
file_QCD_2000_Inf_2017 = "../../inputsamples/2017altjobs/SKIM_QCD_HT2000toInf_TuneCP5_13TeV-madgraph-pythia8.root"
#Load 2018 SIMULATION
#file_QCD_200_300_2018   = "../inputsamples/2018/SKIM_QCD_HT200to300_TuneCP5_13TeV-madgraphMLM-pythia8.root"
file_QCD_300_500_2018 = "../../inputsamples/2018altjobs/SKIM_QCD_HT300to500_TuneCP5_13TeV-madgraphMLM-pythia8.root"
file_QCD_500_700_2018 = "../../inputsamples/2018altjobs/SKIM_QCD_HT500to700_TuneCP5_13TeV-madgraphMLM-pythia8.root"
file_QCD_700_1000_2018 = "../../inputsamples/2018altjobs/SKIM_QCD_HT700to1000_TuneCP5_13TeV-madgraphMLM-pythia8.root"
file_QCD_1000_1500_2018 = "../../inputsamples/2018altjobs/SKIM_QCD_HT1000to1500_TuneCP5_13TeV-madgraphMLM-pythia8.root"
file_QCD_1500_2000_2018 = "../../inputsamples/2018altjobs/SKIM_QCD_HT1500to2000_TuneCP5_13TeV-madgraphMLM-pythia8.root"
file_QCD_2000_Inf_2018 = "../../inputsamples/2018altjobs/SKIM_QCD_HT2000toInf_TuneCP5_13TeV-madgraphMLM-pythia8.root"
#COMBINE THE INFORMATION
ch_sig = TChain("bbbbTree")
ch_bkg = TChain("bbbbTree")

ch_sig.AddFile(file_GGF_HH_2016)
ch_sig.AddFile(file_GGF_HH_2017)
#ch_bkg.AddFile(file_QCD_200_300_2016)
#ch_bkg.AddFile(file_QCD_200_300_2017)
#ch_bkg.AddFile(file_QCD_200_300_2018)
ch_bkg.AddFile(file_QCD_300_500_2016)
ch_bkg.AddFile(file_QCD_300_500_2017)
ch_bkg.AddFile(file_QCD_300_500_2018)
ch_bkg.AddFile(file_QCD_500_700_2016)
ch_bkg.AddFile(file_QCD_500_700_2017)
ch_bkg.AddFile(file_QCD_500_700_2018)
ch_bkg.AddFile(file_QCD_700_1000_2016)
ch_bkg.AddFile(file_QCD_700_1000_2017)
ch_bkg.AddFile(file_QCD_700_1000_2018)
ch_bkg.AddFile(file_QCD_1000_1500_2016)
ch_bkg.AddFile(file_QCD_1000_1500_2017)
test_io_cfg = {
    'filler_name': 'TestIO',
    'verbosity': 0,
    'filler_cfg': TEST_IO_CONFIG
}
test_io.configure(test_io_cfg)  # configure
test_io.start_manager(TEST_BATCH_SIZE)  # start read thread
time.sleep(2)
test_io.next(store_entries=True, store_event_ids=True)

# Step 0.1: additional input for analysis
from ROOT import TChain
ana_chain = TChain("particle_mctruth_tree")
input_filelist = test_io._proc.pd().io().file_list()
for f in input_filelist:
    ana_chain.AddFile(f)
    print 'Will analyze', ana_chain.GetEntries(), 'entries...'

#
# Step 1: Define network
#
import tensorflow.contrib.slim as slim
import tensorflow.python.platform
from build_resnet import build

#
# Step 2: Build network + define loss & solver
#
# retrieve dimensions of data for network construction
dim_data = test_io.fetch_data('test_image0').dim()
# define place holders
Exemplo n.º 24
0
from larcv import larcv
larcv.IOManager
import matplotlib.pyplot as plt
from ROOT import TChain
import sys

IMAGE_PRODUCER = sys.argv[1]

img_tree_name = 'image2d_%s_tree' % IMAGE_PRODUCER
img_br_name = 'image2d_%s_branch' % IMAGE_PRODUCER
img_ch = TChain(img_tree_name)
img_ch.AddFile(sys.argv[2])

start = 0
cutoff = 0
if len(sys.argv) > 3:
    cutoff = int(sys.argv[3])
if len(sys.argv) > 4:
    start = int(sys.argv[3])
    cutoff = int(sys.argv[4])

for entry in xrange(img_ch.GetEntries()):
    if entry < start: continue
    img_ch.GetEntry(entry)
    img_br = None
    exec('img_br=img_ch.%s' % img_br_name)
    event_key = img_br.event_key()
    index = 0
    for img in img_br.Image2DArray():
        mat = larcv.as_ndarray(img)
        mat_display = plt.imshow(mat)
Exemplo n.º 25
0
    def __init__(self,
                 data_schema,
                 data_keys,
                 limit_num_files=0,
                 limit_num_samples=0,
                 event_list=None):
        """
        INPUTS:
            - data_dirs: A list of data directories to find files
                       (up to 10 files read from each dir)

            - data_schema: A dictionary of string <=> list of strings.
                The key is a unique name of a data chunk in a batch.
                The list must be length >= 2: the first string names
                the parser function, and the rest of strings identifies
                data keys in the input files.

            - data_key: a string that is required to be present in the filename

            - limit_num_files: an integer limiting number of files to be
                taken per data directory

            - limit_num_samples: an integer limiting number of samples
                to be taken per data

            - event_list: a list of integers to specify which
                event (ttree index) to process
        """

        # Create file list
        #self._files = _list_files(data_dirs,data_key,limit_num_files)
        self._files = []
        for key in data_keys:
            fs = glob.glob(key)
            for f in fs:
                self._files.append(f)
                if len(self._files) >= limit_num_files: break
            if len(self._files) >= limit_num_files: break

        if len(self._files) < 1:
            raise FileNotFoundError
        elif len(self._files) > 10:
            print(len(self._files), 'files loaded')
        else:
            for f in self._files:
                print('Loading file:', f)

        # Instantiate parsers
        self._data_keys = []
        self._data_parsers = []
        self._trees = {}
        for key, value in data_schema.items():
            if len(value) < 2:
                print('iotools.datasets.schema contains a key \
                    %s with list length < 2!' % key)
                raise ValueError
            if not hasattr(mlreco.iotools.parsers, value[0]):
                print('The specified parser name %s does not exist!' %
                      value[0])
            self._data_keys.append(key)
            self._data_parsers.append((getattr(mlreco.iotools.parsers,
                                               value[0]), value[1:]))
            for data_key in value[1:]:
                if data_key in self._trees: continue
                self._trees[data_key] = None
        self._data_keys.append('index')

        # Prepare TTrees and load files
        from ROOT import TChain
        self._entries = None
        for data_key in self._trees.keys():
            # Check data TTree exists and entries are identical across >1 trees.
            # However do NOT register these TTrees in self._trees
            # yet in order to support >1 workers by DataLoader
            print('Loading tree', data_key)
            chain = TChain(data_key + "_tree")
            for f in self._files:
                chain.AddFile(f)
            if self._entries is not None:
                assert (self._entries == chain.GetEntries())
            else:
                self._entries = chain.GetEntries()

        # If event list is provided, register
        if event_list is None:
            self._event_list = np.arange(0, self._entries)
        else:
            if isinstance(event_list, list):
                event_list = np.array(event_list).astype(np.int32)
            assert (len(event_list.shape) == 1)
            where = np.where(event_list >= self._entries)
            removed = event_list[where]
            if len(removed):
                print('WARNING: ignoring some of specified events in \
                    event_list as they do not exist in the sample.')
                print(removed)
            self._event_list = event_list[np.where(event_list < self._entries)]
            self._entries = len(self._event_list)

        # Set total sample size
        if limit_num_samples > 0 and self._entries > limit_num_samples:
            self._entries = limit_num_samples

        # Flag to identify if Trees are initialized or not
        self._trees_ready = False
Exemplo n.º 26
0
from ROOT import TChain, TCanvas, gStyle, TGraph, gROOT, TH2I
import numpy as np
import sys, re, time

gROOT.SetBatch(False)

chainSN = TChain("tpcfifo_tpcfifo_tree")

chainSN.AddFile(sys.argv[1])
print 'SN stream file ', chainSN.GetFile().GetName()

# Show title for histograms
gStyle.SetOptTitle(1)
gStyle.SetPadRightMargin(0.15)
gStyle.SetCanvasDefW(800)

maxUInt = 4294967295  # Hardcoded

# Get crate and run number from file name
crate = int(re.findall("\d+", sys.argv[1])[0])
print 'Crate ', crate
run = int(re.findall("\d+", sys.argv[1])[-2])
print 'Run ', run

chY = TCanvas()
chU = TCanvas()
chV = TCanvas()

# Loop over SN events ( = one frame for each of the 64 channels )
#for ientrySN in xrange( 0, chainSN.GetEntries() ):
# Loop from last to first to view first events with a higher chance of having baseline established
Exemplo n.º 27
0
class MPID_Dataset(Dataset):
    def __init__(self,
                 input_file,
                 mctruth_tree,
                 image_tree,
                 device,
                 plane=2,
                 augment=False,
                 verbose=False):
        self.plane = plane
        self.augment = augment
        self.verbose = verbose
        self.particle_mctruth_chain = TChain(mctruth_tree)
        self.particle_mctruth_chain.AddFile(input_file)

        self.particle_image_chain = TChain(image_tree)
        self.particle_image_chain.AddFile(input_file)
        if (device):
            self.device = device
        else:
            self.device = "cpu"

    def __getitem__(self, ENTRY):
        # Reading Image

        #print ("open ENTRY @ {}".format(ENTRY))

        self.particle_image_chain.GetEntry(ENTRY)
        self.this_image_cpp_object = self.particle_image_chain.sparse2d_wire_branch
        self.this_image = larcv.as_ndarray(
            self.this_image_cpp_object.as_vector()[self.plane])
        # Image Thresholding
        self.this_image = image_modify(self.this_image)

        #print (self.this_image)
        #print ("sum, ")
        #if (np.sum(self.this_image) < 9000):
        #    ENTRY+

        if self.augment:
            if random.randint(0, 1):
                #if True:
                #if (self.verbose): print ("flipped")
                self.this_image = np.fliplr(self.this_image)
            if random.randint(0, 1):
                #if True:
                #if (self.verbose): print ("transposed")
                self.this_image = self.this_image.transpose(1, 0)
        self.this_image = torch.from_numpy(self.this_image.copy())
        #        self.this_image=torch.tensor(self.this_image, device=self.device).float()

        self.this_image = self.this_image.clone().detach()

        # Reading Truth Info
        self.particle_mctruth_chain.GetEntry(ENTRY)
        self.this_mctruth_cpp_object = self.particle_mctruth_chain.particle_mctruth_branch
        self.this_mctruth = torch.zeros([5])

        for particle in self.this_mctruth_cpp_object.as_vector():
            if (particle.pdg_code() == 11):
                self.this_mctruth[0] = 1
            if (particle.pdg_code() == 22):
                self.this_mctruth[1] = 1
            if (particle.pdg_code() == 13):
                self.this_mctruth[2] = 1
            if (particle.pdg_code() == 211 or particle.pdg_code() == -211):
                self.this_mctruth[3] = 1
            if (particle.pdg_code() == 2212):
                self.this_mctruth[4] = 1

        return (self.this_image, self.this_mctruth)

    def __len__(self):
        assert self.particle_image_chain.GetEntries(
        ) == self.particle_mctruth_chain.GetEntries()
        return self.particle_image_chain.GetEntries()
Exemplo n.º 28
0
import sys
from ROOT import TChain, larcv
larcv.load_pyutil
import cv2, numpy
import matplotlib.pyplot as plt

ch = TChain("image2d_segment_bnbnu_mc_tree")

ch.AddFile(sys.argv[1])

ch.GetEntry(0)

img_v = ch.image2d_segment_bnbnu_mc_branch.Image2DArray()

for i in xrange(3):
    img = larcv.as_ndarray(img_v[i])

    for col in xrange(len(img)):
        for row in xrange(len(img[col])):
            if img[col][row]: print '%-3d %-3d' % (row, col), img[col][row]
        print
    print
    plt.imshow(img)
    aho = input()
Exemplo n.º 29
0
from larcv import larcv
larcv.IOManager
from ROOT import TChain
import sys

PIXEL2D_PRODUCER = sys.argv[1]

pixel_tree_name = 'pixel2d_%s_tree' % PIXEL2D_PRODUCER
pixel_ch = TChain(pixel_tree_name)
pixel_ch.AddFile(sys.argv[2])

for entry in xrange(pixel_ch.GetEntries()):

    pixel_br = None
    pixel_ch.GetEntry(entry)

    exec('pixel_br=pixel_ch.pixel2d_%s_branch' % PIXEL2D_PRODUCER)
    print pixel_br

    print 'Pixel...', PIXEL2D_PRODUCER
    for p in xrange(0, 3):
        print "  plane=", p,
        bb_array = pixel_br.Pixel2DClusterArray(p)
        for pc in bb_array:
            print "  cluster num pixels=", pc.size()
            for ipixel2d in xrange(0, pc.size()):
                pixel2d = pc.at(ipixel2d)
                print pixel2d.X(), pixel2d.Y()
    print
Exemplo n.º 30
0
        "\n--- ############################################################# ---"
    )
    tree = TChain()
    with open(args.inputTrees) as f_list:
        data_list = f_list.read()
    lines = data_list.splitlines()
    tree = TChain()
    tree1 = TChain()
    add_files = []
    for iLine, line in enumerate(lines):
        #print("InputTree: ",line)
        if line[0] != "#":
            #print('add_files:',add_files)
            add_files.append(line)
    print("add_files = ", add_files[0])
    tree.AddFile(add_files[0])
    tree1.AddFile(add_files[1])

    #tree_list = []
    #for index in range(len(add_files)):
    #  print(add_files[index]
    #  tree_list.append(tree.AddFile(add_files[index]))
    print(
        "--- ############################################################# ---\n"
    )

    sel = ''
    sel_noWeight = ''
    if not args.selections:
        sel = 'weight*(Leading_Photon_pt/CMS_hgg_mass>0.35 && Subleading_Photon_pt/CMS_hgg_mass>0.25 && passbVeto==1 && ExOneLep==1 && N_goodJets>=1)'
        sel_noWeight = 'Leading_Photon_pt/CMS_hgg_mass>0.35 && Subleading_Photon_pt/CMS_hgg_mass>0.25 && passbVeto==1 && ExOneLep==1 && N_goodJets>=1'