ATLAS Offline Software
Loading...
Searching...
No Matches
IDPVMtoIDTPMConverter.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
4
6
7import argparse, ROOT, os, sys
8from InDetTrackPerfMon.ConfigUtils import custom_find_datafile
9from AthenaCommon.Logging import logging
10log = logging.getLogger( "IDPVMtoIDTPMConverter.py" )
11
12# Parsing arguments
13parser = argparse.ArgumentParser( description = "IDPVMtoIDTPMConverter.py options:" )
14parser.add_argument( "-i", "--inputFile", help="IDPVM input file", required=True )
15parser.add_argument( "-c", "--config", help="config file", default="InDetTrackPerfMon/IDPVM_to_IDTPM_map_skeleton.txt" )
16parser.add_argument( "-o", "--outputFile", help="Name of the output IDPVM file converted in IDTPM format", default="IDPVMtoIDTPM.root" )
17parser.add_argument( "-d", "--debug", help='set debug level printout', action='store_true', default=False )
18parser.add_argument( "--doTightPrimary", help='also include tight-primary plots in map', action='store_true', default=False )
19# TODO - add here other flags for specific use cases
20MyArgs = parser.parse_args()
21
22if MyArgs.debug : log.setLevel( logging.DEBUG )
23
24
25inputPath = custom_find_datafile( MyArgs.inputFile )
26if not inputPath :
27 log.error( f"Input file {MyArgs.inputFile} not found" )
28 sys.exit(1)
29
30
31configFileName = custom_find_datafile( MyArgs.config )
32if not configFileName :
33 log.error( f"Config txt {MyArgs.config} not found" )
34 sys.exit(1)
35
36
37namingMap_base = {}
38with open( configFileName, 'r' ) as configFile :
39 for line in configFile.readlines() :
40 # skip empty lines or those starting with an empty space or #
41 if not line.strip() : continue
42 if line.startswith(" ") or line.startswith("#") : continue
43 # parsing line
44 parsed = line.strip().split()
45 htype = parsed[0]
46 hIDPVM = parsed[1]
47 hIDTPM = parsed[2]
48 if hIDPVM=="NONE" or hIDTPM=="NONE" : continue
49 # filling map
50 namingMap_base.update( { hIDPVM : { "IDTPMname" : hIDTPM, "type" : htype } } )
51
52
53expandMap = []
54if MyArgs.doTightPrimary : expandMap.append( { "IDPVMrepl" : "SquirrelPlots/TightPrimary/",
55 "IDTPMrepl" : "TrkAnaOffl_TightPrimary" } )
56# TODO - add here corresponding lines for other flags for specific use cases
57
58namingMap = namingMap_base.copy()
59for expitem in expandMap :
60 namingMap_add = {}
61 for hname, hdict in namingMap_base.items() :
62 newhname = hname.replace( "SquirrelPlots/", expitem["IDPVMrepl"] )
63 newhdict = hdict.copy()
64 newhdict["IDTPMname"] = newhdict["IDTPMname"].replace( "TrkAnaOffl", expitem["IDTPMrepl"] )
65 namingMap_add.update( { newhname : newhdict } )
66 namingMap.update( namingMap_add )
67log.info( "Naming map successfully created" )
68
69
70def getall(d, basepath=""):
71 "Generator function to recurse into a ROOT file/dir and yield (path, obj) pairs"
72 for key in d.GetListOfKeys():
73 kname = key.GetName()
74
75 if key.GetClassName() == "TTree":
76 continue
77 if key.IsFolder():
78 for i in getall(d.Get(kname), basepath+kname+"/"):
79 yield i
80 else:
81 yield basepath+kname, d.Get(kname)
82
83inFile = ROOT.TFile.Open( inputPath, "READ" )
84outFile = ROOT.TFile.Open( MyArgs.outputFile, "RECREATE" )
85
86for keyname, keyobj in getall( inFile ) :
87 if not keyobj : continue
88 keytype = keyobj.ClassName()
89
90
91 if keyname not in namingMap :
92 log.debug( f"WARNING: plot {keyname} not found. Skipping" )
93 continue
94
95
96 mapHtype = namingMap[ keyname ]["type"]
97 newHname = namingMap[ keyname ]["IDTPMname"]
98
99
100 if keytype != mapHtype :
101 log.warning( f"histogram {keyname} is {keytype}, but {mapHtype} requested. Skipping." )
102 continue
103
104
105 inFile.cd()
106 keyobj.SetDirectory(0)
107
108
109 outFile.cd()
110 newHname_path = os.path.dirname( newHname )
111 newHname_obj = os.path.basename( newHname )
112 if( not outFile.GetDirectory( newHname_path ) ):
113 outFile.mkdir( newHname_path, newHname_path )
114 outFile.cd( newHname_path )
115 keyobj.SetName( newHname_obj )
116 keyobj.Write()
117
118log.info( f"Output file {MyArgs.outputFile} successfully written. All done!" )
119inFile.Close()
120outFile.Close()
if(pathvar)
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:312
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179
getall(d, basepath="")
now opening and looping input IDPVM file