CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions
chain.Chain Class Reference
Inheritance diagram for chain.Chain:

Public Member Functions

def __getattr__ (self, attr)
 
def __getitem__ (self, index)
 
def __init__ (self, input, tree_name=None)
 
def __iter__ (self)
 
def __len__ (self)
 

Public Attributes

 chain
 
 files
 

Private Member Functions

def _guessTreeName (self, pattern)
 

Detailed Description

Wrapper to TChain, with a python iterable interface.

Example of use:  #TODO make that a doctest / nose?
   from chain import Chain
   the_chain = Chain('../test/test_*.root', 'test_tree')
   event3 = the_chain[2]
   print event3.var1

   for event in the_chain:
       print event.var1

Definition at line 22 of file chain.py.

Constructor & Destructor Documentation

◆ __init__()

def chain.Chain.__init__ (   self,
  input,
  tree_name = None 
)
Create a chain.

Parameters:
  input     = either a list of files or a wildcard (e.g. 'subdir/*.root').
      In the latter case all files matching the pattern will be used
      to build the chain.
  tree_name = key of the tree in each file.
      if None and if each file contains only one TTree,
      this TTree is used.

Definition at line 35 of file chain.py.

35  def __init__(self, input, tree_name=None):
36  """
37  Create a chain.
38 
39  Parameters:
40  input = either a list of files or a wildcard (e.g. 'subdir/*.root').
41  In the latter case all files matching the pattern will be used
42  to build the chain.
43  tree_name = key of the tree in each file.
44  if None and if each file contains only one TTree,
45  this TTree is used.
46  """
47  self.files = input
48  if isinstance(input, str): # input is a pattern
49  self.files = glob.glob(input)
50  if len(self.files)==0:
51  raise ValueError('no matching file name: '+input)
52  else: # case of a list of files
53  if False in [
54  ((is_pfn(fnam) and os.path.isfile(fnam)) or
55  is_lfn(fnam)) or is_rootfn(fnam)
56  for fnam in self.files]:
57  err = 'at least one input file does not exist\n'
58  err += pprint.pformat(self.files)
59  raise ValueError(err)
60  if tree_name is None:
61  tree_name = self._guessTreeName(input)
62  self.chain = TChain(tree_name)
63  for file in self.files:
64  self.chain.Add(file)
65 

Member Function Documentation

◆ __getattr__()

def chain.Chain.__getattr__ (   self,
  attr 
)
All functions of the wrapped TChain are made available

Definition at line 92 of file chain.py.

92  def __getattr__(self, attr):
93  """
94  All functions of the wrapped TChain are made available
95  """
96  return getattr(self.chain, attr)
97 

References hgcalTriggerChains.HGCalTriggerChains.chain, edm::FileLocator::Rule.chain, and chain.Chain.chain.

◆ __getitem__()

def chain.Chain.__getitem__ (   self,
  index 
)
Returns the event at position index.

Definition at line 104 of file chain.py.

104  def __getitem__(self, index):
105  """
106  Returns the event at position index.
107  """
108  self.chain.GetEntry(index)
109  return self.chain
110 
111 

References hgcalTriggerChains.HGCalTriggerChains.chain, edm::FileLocator::Rule.chain, and chain.Chain.chain.

◆ __iter__()

def chain.Chain.__iter__ (   self)

Definition at line 98 of file chain.py.

98  def __iter__(self):
99  return iter(self.chain)
100 

References hgcalTriggerChains.HGCalTriggerChains.chain, edm::FileLocator::Rule.chain, and chain.Chain.chain.

◆ __len__()

def chain.Chain.__len__ (   self)

Definition at line 101 of file chain.py.

101  def __len__(self):
102  return int(self.chain.GetEntries())
103 

References hgcalTriggerChains.HGCalTriggerChains.chain, edm::FileLocator::Rule.chain, chain.Chain.chain, and createfilelist.int.

◆ _guessTreeName()

def chain.Chain._guessTreeName (   self,
  pattern 
)
private
Find the set of keys of all TTrees in all files matching pattern.
If the set contains only one key
  Returns: the TTree key
else raises ValueError.

Definition at line 66 of file chain.py.

66  def _guessTreeName(self, pattern):
67  """
68  Find the set of keys of all TTrees in all files matching pattern.
69  If the set contains only one key
70  Returns: the TTree key
71  else raises ValueError.
72  """
73  names = []
74  for fnam in self.files:
75  rfile = TFile(fnam)
76  for key in rfile.GetListOfKeys():
77  obj = rfile.Get(key.GetName())
78  if isinstance(obj, TTree):
79  names.append( key.GetName() )
80  thename = set(names)
81  if len(thename)==1:
82  return list(thename)[0]
83  else:
84  err = [
85  'several TTree keys in {pattern}:'.format(
86  pattern=pattern
87  ),
88  ','.join(thename)
89  ]
90  raise ValueError('\n'.join(err))
91 

References readConfig.fileINI.files, chain.Chain.files, MatrixReader.MatrixReader.files, MatrixUtil.InputInfo.files, JsonOutputProducer::JsonConfigurationBlock.files, and join().

Member Data Documentation

◆ chain

chain.Chain.chain

◆ files

chain.Chain.files
is_pfn
def is_pfn(fn)
Definition: chain.py:9
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
is_lfn
def is_lfn(fn)
Definition: chain.py:12
SiStripClientConfig_Tier0_cff.Add
Add
Definition: SiStripClientConfig_Tier0_cff.py:64
createfilelist.int
int
Definition: createfilelist.py:10
format
is_rootfn
def is_rootfn(fn)
Definition: chain.py:15