ATLAS Offline Software
Loading...
Searching...
No Matches
python.trfValidateRootFile Namespace Reference

Functions

 checkBranch (branch)
 checkTreeBasketWise (tree)
 checkTreeEventWise (tree, printInterval=150000)
 checkNTupleEventWise (ntuple, printInterval=150000)
 checkNTupleFieldWise (ntuple)
 checkDirectory (directory, the_type, requireTree, depth)
 checkFile (fileName, the_type, requireTree)
 checkNEvents (fileName, nEntries)
 usage ()
 main (argv)

Variables

 ROOT = RootUtils.import_root()
 msg = logging.getLogger(__name__)
 ch = logging.StreamHandler(sys.stdout)
 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 rc = main(sys.argv)

Function Documentation

◆ checkBranch()

python.trfValidateRootFile.checkBranch ( branch)

Definition at line 22 of file trfValidateRootFile.py.

22def checkBranch(branch):
23
24 msg.debug('Checking branch %s ...', branch.GetName())
25
26 nBaskets=branch.GetWriteBasket()
27
28 msg.debug('Checking %s baskets ...', nBaskets)
29
30 for iBasket in range(nBaskets):
31 basket=branch.GetBasket(iBasket)
32 if not basket:
33 msg.warning('Basket %s of branch %s is corrupted.', iBasket, branch.GetName() )
34 return 1
35
36 listOfSubBranches=branch.GetListOfBranches()
37 msg.debug('Checking %s subbranches ...', listOfSubBranches.GetEntries())
38 for subBranch in listOfSubBranches:
39 if checkBranch(subBranch)==1:
40 return 1
41
42 msg.debug('Branch %s looks ok.', branch.GetName())
43 return 0
44
45

◆ checkDirectory()

python.trfValidateRootFile.checkDirectory ( directory,
the_type,
requireTree,
depth )

Definition at line 144 of file trfValidateRootFile.py.

144def checkDirectory(directory, the_type, requireTree, depth):
145
146 from PyUtils import PoolFile
147 nentries = None
148 hasMetadata = False
149
150 msg.debug('Checking directory %s ...', directory.GetName())
151
152 listOfKeys=directory.GetListOfKeys()
153
154 msg.debug('Checking %s keys ... ', listOfKeys.GetEntries())
155
156 for key in listOfKeys:
157
158 msg.debug('Looking at key %s ...', key.GetName())
159 msg.debug('Key is of class %s.', key.GetClassName())
160
161 the_object=directory.Get(key.GetName())
162 if not the_object:
163 msg.warning("Can't get object of key %s.", key.GetName())
164 return 1
165
166 if requireTree and not isinstance(the_object, TTree):
167 msg.warning("Object of key %s is not of class TTree!", key.GetName())
168 return 1
169
170 if isinstance(the_object,TTree):
171
172 msg.debug('Checking tree %s ...', the_object.GetName())
173
174 if depth == 0:
175 if PoolFile.PoolOpts.TTreeNames.EventData == the_object.GetName():
176 nentries = the_object.GetEntries()
177 msg.debug(f' contains {nentries} events')
178 elif PoolFile.PoolOpts.TTreeNames.MetaData == the_object.GetName():
179 hasMetadata = True
180 msg.debug(' contains MetaData')
181
182 if the_type=='event':
183 if checkTreeEventWise(the_object)==1:
184 return 1
185 elif the_type=='basket':
186 if checkTreeBasketWise(the_object)==1:
187 return 1
188
189 msg.debug('Tree %s looks ok.', the_object.GetName())
190
191 if isRNTuple(the_object):
192
193 msg.debug('Checking ntuple of key %s ...', key.GetName())
194
195 try:
196 reader=RNTupleReader.Open(the_object)
197 except Exception as err:
198 msg.warning('Could not open ntuple %s: %s', the_object, err)
199 return 1
200
201 if depth == 0:
202 if PoolFile.PoolOpts.RNTupleNames.EventData == reader.GetDescriptor().GetName():
203 nentries = reader.GetNEntries()
204 msg.debug(f' contains {nentries} events')
205 elif PoolFile.PoolOpts.RNTupleNames.MetaData == reader.GetDescriptor().GetName():
206 hasMetadata = True
207 msg.debug(' contains MetaData')
208
209 if the_type=='event':
210 if checkNTupleEventWise(the_object)==1:
211 return 1
212 elif the_type=='basket':
213 if checkNTupleFieldWise(the_object)==1:
214 return 1
215
216 msg.debug('NTuple of key %s looks ok.', key.GetName())
217
218 if isinstance(the_object, TDirectory):
219 if checkDirectory(the_object, the_type, requireTree, depth + 1)==1:
220 return 1
221
222 # Only check if metadata object is available as in standard POOL files
223 if depth == 0 and hasMetadata and checkNEvents(directory.GetName(), nentries)==1:
224 return 1
225 else:
226 msg.debug('Directory %s looks ok.', directory.GetName())
227 return 0
228
229

◆ checkFile()

python.trfValidateRootFile.checkFile ( fileName,
the_type,
requireTree )

Definition at line 230 of file trfValidateRootFile.py.

230def checkFile(fileName, the_type, requireTree):
231
232 msg.info('Checking file %s ...', fileName)
233
234 enabledIMT = False
235 if not ROOT.ROOT.IsImplicitMTEnabled() and os.environ.keys() >= {'TRF_MULTITHREADED_VALIDATION', 'ATHENA_CORE_NUMBER'}:
236 if (nThreads := int(os.environ['ATHENA_CORE_NUMBER'])) >= 0:
237 msg.info(f"Setting the number of implicit ROOT threads to {nThreads}")
238 ROOT.ROOT.EnableImplicitMT(nThreads)
239 enabledIMT = True
240 else:
241 msg.warning(f"Ignored negative ATHENA_CORE_NUMBER ({nThreads})")
242
243 try:
244 file_handle=TFile.Open(fileName)
245 except OSError as err:
246 msg.error('Could not open file %s: %s', fileName, err)
247 return 1
248
249 if not file_handle:
250 msg.warning("Can't access file %s.", fileName)
251 return 1
252
253 if not file_handle.IsOpen():
254 msg.warning("Can't open file %s.", fileName)
255 return 1
256
257 if file_handle.IsZombie():
258 msg.warning("File %s is a zombie.", fileName)
259 file_handle.Close()
260 return 1
261
262 if file_handle.TestBit(TFile.kRecovered):
263 msg.warning("File %s needed to be recovered.", fileName)
264 file_handle.Close()
265 return 1
266
267 if checkDirectory(file_handle, the_type, requireTree, 0)==1:
268 msg.warning("File %s is corrupted.", fileName)
269 file_handle.Close()
270 return 1
271
272 file_handle.Close()
273 msg.info("File %s looks ok.", fileName)
274
275 if enabledIMT:
276 ROOT.ROOT.DisableImplicitMT()
277
278 return 0
279
280

◆ checkNEvents()

python.trfValidateRootFile.checkNEvents ( fileName,
nEntries )
Check consistency of number of events in file with metadata.

fileName   name of file to check consistency of
nEntries   number of events in fileName (e.g., obtained by examining event data object)
return     0 in case of consistency, 1 otherwise

Definition at line 281 of file trfValidateRootFile.py.

281def checkNEvents(fileName, nEntries):
282 """Check consistency of number of events in file with metadata.
283
284 fileName name of file to check consistency of
285 nEntries number of events in fileName (e.g., obtained by examining event data object)
286 return 0 in case of consistency, 1 otherwise
287 """
288 from PyUtils.MetaReader import read_metadata
289
290 msg.debug('Checking number of events in file %s ...', fileName)
291
292 meta = read_metadata(fileName, mode='lite')[fileName]
293 msg.debug(' according to metadata: {0}'.format(meta["nentries"]))
294 msg.debug(' according to event data: {0}'.format(nEntries))
295 if meta["nentries"] and nEntries and meta["nentries"] != nEntries \
296 or meta["nentries"] and not nEntries \
297 or not meta["nentries"] and nEntries:
298 msg.warning(f' number of events ({nEntries}) inconsistent with metadata ({meta["nentries"]}) in file {fileName!r}.')
299 return 1
300 else:
301 msg.debug(" looks ok.")
302 return 0
303

◆ checkNTupleEventWise()

python.trfValidateRootFile.checkNTupleEventWise ( ntuple,
printInterval = 150000 )

Definition at line 77 of file trfValidateRootFile.py.

77def checkNTupleEventWise(ntuple, printInterval = 150000):
78
79 try:
80 reader=RNTupleReader.Open(ntuple)
81 except Exception as err:
82 msg.warning('Could not open ntuple %s: %s', ntuple, err)
83 return 1
84
85 nEntries=reader.GetNEntries()
86
87 msg.debug('Checking %s entries ...', nEntries)
88
89 entry = reader.CreateEntry()
90 for i in range(nEntries):
91 try:
92 reader.LoadEntry(i, entry)
93 except Exception as err:
94 msg.warning('Event %s of ntuple %s is corrupted: %s', i, reader.GetDescriptor().GetName(), err)
95 return 1
96
97 # Show a sign of life for long validation jobs: ATLASJT-433
98 if (i%printInterval)==0 and i>0:
99 msg.info('Validated %s events so far ...', i)
100
101 return 0
102

◆ checkNTupleFieldWise()

python.trfValidateRootFile.checkNTupleFieldWise ( ntuple)
For each cluster, bulk read each top level field.

Definition at line 103 of file trfValidateRootFile.py.

103def checkNTupleFieldWise(ntuple):
104 """For each cluster, bulk read each top level field.
105 """
106 from ROOT import RException
107
108 try:
109 reader=RNTupleReader.Open(ntuple)
110 except Exception as err:
111 msg.warning('Could not open ntuple %r: %r', ntuple, err)
112 return 1
113
114 try:
115 descriptor = reader.GetDescriptor()
116 msg.debug(f"ntupleName={descriptor.GetName()}")
117
118 model = reader.GetModel()
119 fieldZero = model.GetConstFieldZero()
120 subFields = fieldZero.GetConstSubfields()
121 msg.debug(f"Top level fields number {subFields.size()}")
122 for clusterDescriptor in descriptor.GetClusterIterable():
123 size = int(clusterDescriptor.GetNEntries())
124 if msg.isEnabledFor(logging.DEBUG):
125 msg.debug(f" cluster #{clusterDescriptor.GetId()}"
126 f" firstEntryIndex={clusterDescriptor.GetFirstEntryIndex()}"
127 f" nEntries={size}")
128 clusterRange = ROOT.RNTupleLocalRange(clusterDescriptor.GetId(), 0, size)
129 for field in subFields:
130 if msg.isEnabledFor(logging.DEBUG):
131 msg.debug(f"fieldName={field.GetFieldName()} typeName={field.GetTypeName()}")
132 bulk = model.CreateBulk(field.GetFieldName())
133 values = bulk.ReadBulk(clusterRange)
134 if msg.isEnabledFor(logging.DEBUG):
135 msg.debug(f" values array at {values}")
136
137 except RException as err:
138 from traceback import format_exception
139 msg.error("Exception reading ntuple %r\n%s", ntuple, "".join(format_exception(err)))
140 return 1
141
142 return 0
143

◆ checkTreeBasketWise()

python.trfValidateRootFile.checkTreeBasketWise ( tree)

Definition at line 46 of file trfValidateRootFile.py.

46def checkTreeBasketWise(tree):
47
48 listOfBranches=tree.GetListOfBranches()
49
50 msg.debug('Checking %s branches ...', listOfBranches.GetEntries())
51
52 for branch in listOfBranches:
53 if checkBranch(branch)==1:
54 msg.warning('Tree %s is corrupted (branch %s ).', tree.GetName(), branch.GetName())
55 return 1
56
57 return 0
58
59

◆ checkTreeEventWise()

python.trfValidateRootFile.checkTreeEventWise ( tree,
printInterval = 150000 )

Definition at line 60 of file trfValidateRootFile.py.

60def checkTreeEventWise(tree, printInterval = 150000):
61
62 nEntries=tree.GetEntries()
63
64 msg.debug('Checking %s entries ...', nEntries)
65
66 for i in range(nEntries):
67 if tree.GetEntry(i)<0:
68 msg.warning('Event %s of tree %s is corrupted.', i, tree.GetName())
69 return 1
70
71 # Show a sign of life for long validation jobs: ATLASJT-433
72 if (i%printInterval)==0 and i>0:
73 msg.info('Validated %s events so far ...', i)
74
75 return 0
76

◆ main()

python.trfValidateRootFile.main ( argv)

Definition at line 313 of file trfValidateRootFile.py.

313def main(argv):
314
315 clock=TStopwatch()
316
317 argc=len(argv)
318
319 if (argc!=5):
320 return usage()
321
322 fileName=argv[1]
323 the_type=argv[2]
324 requireTree=argv[3]
325 verbosity=argv[4]
326
327
328 if the_type!="event" and the_type!="basket":
329 return usage()
330
331 if requireTree=="true":
332 requireTree=True
333 elif requireTree=="false":
334 requireTree=False
335 else:
336 return usage()
337
338 if verbosity=="on":
339 msg.setLevel(logging.DEBUG)
340 elif verbosity=="off":
341 msg.setLevel(logging.INFO)
342 else:
343 return usage()
344
345 rc=checkFile(fileName,the_type, requireTree)
346 msg.debug('Returning %s', rc)
347
348 clock.Stop()
349 clock.Print()
350
351 return rc
352
353
StatusCode usage()
int main()
Definition hello.cxx:18

◆ usage()

python.trfValidateRootFile.usage ( )

Definition at line 304 of file trfValidateRootFile.py.

304def usage():
305 print("Usage: validate filename type requireTree verbosity")
306 print("'type' must be either 'event' or 'basket'")
307 print("'requireTree' must be either 'true' or 'false'")
308 print("'verbosity' must be either 'on' or 'off'")
309
310 return 2
311
312
void print(char *figname, TCanvas *c1)

Variable Documentation

◆ ch

python.trfValidateRootFile.ch = logging.StreamHandler(sys.stdout)

Definition at line 356 of file trfValidateRootFile.py.

◆ formatter

python.trfValidateRootFile.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

Definition at line 357 of file trfValidateRootFile.py.

◆ msg

python.trfValidateRootFile.msg = logging.getLogger(__name__)

Definition at line 20 of file trfValidateRootFile.py.

◆ rc

python.trfValidateRootFile.rc = main(sys.argv)

Definition at line 361 of file trfValidateRootFile.py.

◆ ROOT

python.trfValidateRootFile.ROOT = RootUtils.import_root()

Definition at line 15 of file trfValidateRootFile.py.