ATLAS Offline Software
LArNewCalib_MergeDB.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 import os,sys
4 from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
5 from AthenaConfiguration.ComponentFactory import CompFactory
6 from IOVDbSvc.IOVDbSvcConfig import addFolders
7 
8 def DBConnectionFile(sqlitefile):
9  return "sqlite://;schema="+sqlitefile+";dbname=CONDBR2"
10 
11 def mergeDBCfg(flags, InputKeys=[], InputSQLiteFiles=[]):
12  result=ComponentAccumulator()
13 
14  print("mergeDBCfg",len(InputSQLiteFiles)," SQLite files: ",InputSQLiteFiles)
15  print(len(InputKeys)," InputKeys: ",InputKeys)
16 
17 
18  if not flags.LArCalib.isSC: GroupingType = "ExtendedSubDetector"
19  else: GroupingType = "SuperCells"
20 
21  if ( flags.LArCalib.Output.POOLFile ):
22  if os.path.exists(flags.LArCalib.Output.POOLFile):
23  os.remove(flags.LArCalib.Output.POOLFile)
24 
25  for key in InputKeys:
26 
27  print("Working for: ",key)
28  KeyOutput = key
29  FlagInput = key
30 
31  if 'Pedestal' == key or 'PhysWave' == key or 'CaliWave' or 'MphysOverMcal' == key or 'Ramp' == key or 'Params' in key:
32  KeyInput="LAr"+key
33  KeyOutput="LAr"+key
34 
35  if 'AutoCorr' in key:
36  KeyInput='LArAutoCorr'
37 
38  ofcphyskey=None
39  if "OFC" in key:
40  KeyInput = "LArOFC"
41  if "Cali" in key:
42  FlagInput = "OFCCali"
43 
44  if "Phys" in key:
45  ofcphyskeytmp = key.split("OFCPhys")[1]
46  if 'Mu' in ofcphyskeytmp:
47  ofcphyskey=ofcphyskeytmp.replace("Mu","")
48  else:
49  ofcphyskey=ofcphyskeytmp
50  FlagInput = "OFCPhys"
51 
52  shapekey=None
53  if "Shape" in key:
54  shapekey = key.split("Shape")[1]
55  FlagInput = "Shape"
56  KeyInput = "LArShape"
57 
58 
59  #if FlagInput in flags._flagdict.keys():
60  if [i for i in flags._flagdict.keys() if FlagInput in i]:
61  try:
62  Folder = flags._get('LArCalib.'+FlagInput+'.Folder')
63  except Exception as e:
64  print(e)
65  print(flags._flagdict)
66  sys.exit(-1)
67  else:
68  print('No folder in flags for '+FlagInput)
69  print(flags._flagdict.keys())
70  sys.exit(-2)
71 
72  if shapekey is not None:
73  if Folder.endswith("/"):
74  Folder+=shapekey
75  else:
76  Folder+="/"+shapekey
77 
78  if ofcphyskey is not None and ofcphyskey != "":
79  print("Folder: ",Folder," ofcphyskey: ",ofcphyskey)
80  if "4samples" in Folder:
81  Folder = Folder.replace("4samples", ofcphyskey)
82  elif Folder.endswith("/"):
83  Folder+=ofcphyskey
84  else:
85  Folder+="/"+ofcphyskey
86 
87  if "OFCPhys" in FlagInput:
88  print("Folder: ",Folder)
89  # Get the available tags
90  from PyCool import cool
91  dbSvc=cool.DatabaseSvcFactory.databaseService()
92  db_string=DBConnectionFile(InputSQLiteFiles[0])
93  try:
94  db=dbSvc.openDatabase(db_string)
95  except Exception as e:
96  print ('Problem opening database',e)
97  print(db_string)
98  sys.exit(-3)
99  cool_folder=db.getFolder(Folder)
100  tags = [ x for x in cool_folder.listTags() ]
101  if len(tags) == 1:
102  TagSpec = str(tags[0])
103  else:
104  if flags.LArCalib.OFC.Ncoll > 0 or 'Mu' in key:
105  TagSpec = [ str(tag) for tag in tags if "-mu-" in str(tag)][0]
106  else:
107  TagSpec = [ str(tag) for tag in tags if "-mu-" not in str(tag)][0]
108  else:
109  from LArCalibProcessing.utils import FolderTagResolver as FolderTagResover
110  FolderTagResover._globalTag=flags.IOVDb.GlobalTag
111  #rs=FolderTagResover(flags.LArCalib.Input.Database2)
112  # until not all folders in COOL:
113  rs=FolderTagResover(DBConnectionFile(InputSQLiteFiles[0]))
114  FolderTag = rs.getFolderTagSuffix(Folder)
115  del rs
116 
117  from LArCalibProcessing.LArCalibConfigFlags import LArCalibFolderTag
118  TagSpec = LArCalibFolderTag(Folder,FolderTag)
119 
120 
121  if 'Wave' in KeyInput:
122  OutputObjectSpec = KeyInput+"Container#"+KeyOutput+"#"+Folder
123  else:
124  OutputObjectSpec = KeyInput+"Complete#"+KeyOutput+"#"+Folder
125  OutputObjectSpecTag = TagSpec
126 
127  print("**",KeyInput,FlagInput,"folder **",Folder)
128  print("** TagSpec: ",TagSpec)
129  print("** keyOutput: ",KeyOutput)
130 
131  result.addEventAlgo(CompFactory.getComp('LArBlockCorrections')())
132 
133  result.addEventAlgo(CompFactory.getComp('LArBlockCorrections')())
134 
135  inkeys=[]
136  inobjs=[]
137 
138  for sqf in InputSQLiteFiles:
139  thiskey = KeyInput+str(InputSQLiteFiles.index(sqf))
140  if 'OFC' in key and 'Mu' in key:
141  thiskey+="Mu"
142  if 'OFC' in key and 'Cali' in key:
143  thiskey+="Cali"
144  inkeys.append(thiskey)
145  if 'Wave' in KeyInput:
146  inobjs.append(KeyInput+"Container#"+thiskey)
147  else:
148  inobjs.append(KeyInput+"Complete#"+thiskey)
149 
150  if 'Wave' in KeyInput or 'Params' in KeyInput:
151  result.merge(addFolders(flags,Folder,tag=TagSpec,detDb=sqf,modifiers="<key>"+thiskey+"</key>"))
152  else:
153  result.merge(addFolders(flags,Folder,tag=TagSpec,detDb=sqf,modifiers="<key>"+thiskey+"</key>", className=KeyInput+"Complete"))
154 
155 
156  if 'Wave' in KeyInput:
157  result.addCondAlgo(CompFactory.getComp('ForceLoadCondObj')(KeyOutput,ObjectList=inobjs))
158  result.addCondAlgo(CompFactory.getComp('LArConditionsMergerAlg<LArDAC2uAComplete,'+KeyInput+'Container>')(key+"Merger",
159  GroupingType = GroupingType, WriteKey = KeyOutput, DetStoreReadKeys = inkeys))
160  elif 'Params' in KeyInput:
161  result.addCondAlgo(CompFactory.getComp('ForceLoadCondObj')(KeyOutput,ObjectList=inobjs))
162  result.addCondAlgo(CompFactory.getComp('LArConditionsMergerAlg<LArDAC2uAComplete,'+KeyInput+'Complete>')(key+"Merger",
163  GroupingType = GroupingType, WriteKey = KeyOutput, DetStoreReadKeys = inkeys))
164  else:
165  result.addCondAlgo(CompFactory.getComp('LArConditionsMergerAlg<'+KeyInput+'Complete, LArPhysWaveContainer>')(key+"Merger",
166  GroupingType = GroupingType, WriteKey = KeyOutput, ReadKeys = inkeys))
167 
168  if ( flags.LArCalib.Output.POOLFile ):
169 
170  from RegistrationServices.OutputConditionsAlgConfig import OutputConditionsAlgCfg
171  result.merge(OutputConditionsAlgCfg(flags,name="OutputConditionsAlg"+KeyInput, outputFile=flags.LArCalib.Output.POOLFile,
172  ObjectList=[OutputObjectSpec], IOVTagList=[OutputObjectSpecTag],
173  Run1 = flags.LArCalib.IOVStart, Run2= flags.LArCalib.IOVEnd, WriteIOV=True))
174  pass
175 
176  result.addService(CompFactory.IOVRegistrationSvc(RecreateFolders = False))
177 
178  #MC Event selector since we have no input data file
179  from McEventSelector.McEventSelectorConfig import McEventSelectorCfg
180  result.merge(McEventSelectorCfg(flags,
181  RunNumber = flags.LArCalib.Input.RunNumbers[0],
182  EventsPerRun = 1,
183  FirstEvent = 1,
184  InitialTimeStamp = 0,
185  TimeStampInterval = 1))
186 
187  result.printConfig(withDetails=True, printDefaults=True)
188  return result
189 
190 
191 if __name__=="__main__":
192  import argparse
193 
194  def list_of_strings(arg):
195  return arg.split(',')
196 
197  # now process the CL options and assign defaults
198  parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
199  parser.add_argument('-r','--run', dest='run', default=str(0x7FFFFFFF), help='Run number to query input DB', type=str)
200  parser.add_argument('-i','--insqlite', dest='insql', default="", help='List of input sqlite files, comma separated, if empty all *.db files in current dir.', type=list_of_strings)
201  parser.add_argument('-k','--inkeys', dest='inkeys', default=["LArPedestal"], help='List of input keys to process', type=list_of_strings)
202  parser.add_argument('-o','--outsqlite', dest='outsql', default="freshConstants_merged.db", help='Output sqlite file', type=str)
203  parser.add_argument('-p','--poolfile', dest='poolfile', default="freshConstants_pp.pool.root", help='Output pool file', type=str)
204  parser.add_argument('-d','--dbname', dest='dbname', default="COOLOFL_LAR/CONDBR2", help='DB to query for tags', type=str)
205  parser.add_argument('--iovstart',dest="iovstart", default=0, help="IOV start (run-number)", type=int)
206  parser.add_argument('--isSC', dest='supercells', default=False, help='is SC data ?', action='store_true')
207  parser.add_argument('-c','--poolcat', dest='poolcat', default="freshConstants.xml", help='Catalog of POOL files', type=str)
208  parser.add_argument('--Ncoll',dest='Ncoll', default=60, help='Number of MinBias collision assumed for OFCs folder', type=int)
209  args = parser.parse_args()
210  if help in args and args.help is not None and args.help:
211  parser.print_help()
212  sys.exit(0)
213 
214  if len(args.insql)==0:
215  InputSQLiteFiles = [ db for db in os.listdir(".") if db.endswith(".db") ]
216  else:
217  InputSQLiteFiles = args.insql
218 
219  if len(InputSQLiteFiles) == 1: #Main readout case, copy first input to output
220  import shutil, os
221  shutil.copyfile(InputSQLiteFiles[0],args.outsql)
222 
223  #Import the flag-container that is the arguemnt to the configuration methods
224  from AthenaConfiguration.AllConfigFlags import initConfigFlags
225  from LArCalibProcessing.LArCalibConfigFlags import addLArCalibFlags
227  addLArCalibFlags(flags, args.supercells)
228 
229  #Now we set the flags as required for this particular job:
230  flags.LArCalib.Input.RunNumbers = [int(args.run),]
231  flags.LArCalib.Input.Database = args.insql
232  flags.LArCalib.Input.Database2 = args.dbname
233 
234  flags.LArCalib.Output.POOLFile = args.poolfile
235  #flags.LArCalib.Output.ROOTFile2 ="freshConstants2_pp.pool.root"
236  flags.IOVDb.DBConnection=DBConnectionFile(args.outsql)
237 
238  #The global tag we are working with
239  flags.IOVDb.GlobalTag = "LARCALIB-RUN2-00"
240 
241  flags.Input.Files=[]
242  flags.LArCalib.Input.Files = [ ]
243  flags.LArCalib.OFC.Ncoll = args.Ncoll
244  flags.LArCalib.IOVStart = args.iovstart
245 
246  flags.LAr.doAlign=False
247  flags.Input.RunNumbers=flags.LArCalib.Input.RunNumbers
248 
249  from AthenaConfiguration.TestDefaults import defaultGeometryTags
250  flags.GeoModel.AtlasVersion=defaultGeometryTags.RUN3
251 
252  flags.Debug.DumpDetStore=True
253  flags.Debug.DumpCondStore=True
254 
255  flags.lock()
256 
257  #Import the MainServices (boilerplate)
258  from AthenaConfiguration.MainServicesConfig import MainServicesCfg
259  cfg=MainServicesCfg(flags)
260  from LArGeoAlgsNV.LArGMConfig import LArGMCfg
261  cfg.merge(LArGMCfg(flags))
262  #from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
263  #cfg.merge(PoolReadCfg(flags))
264  from AthenaPoolCnvSvc.PoolCommonConfig import PoolSvcCfg
265  cfg.merge(PoolSvcCfg(flags))
266 
267  cfg.merge(mergeDBCfg(flags,args.inkeys, InputSQLiteFiles))
268 
269  cfg.getService("PoolSvc").ReadCatalog+=["xmlcatalog_file:%s"%args.poolcat,]
270  cfg.getService("PoolSvc").WriteCatalog="xmlcatalog_file:%s"%args.poolcat
271  cfg.getService("IOVDbSvc").DBInstance=""
272 
273  cfg.printConfig(withDetails=True, summariseProps=True, printDefaults=True)
274  #cfg.getService("StoreGateSvc").Dump=True
275  #cfg.getService("StoreGateSvc").OutputLevel=2
276 
277  cfg.run(1)
python.JetAnalysisCommon.ComponentAccumulator
ComponentAccumulator
Definition: JetAnalysisCommon.py:302
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.LArNewCalib_MergeDB.mergeDBCfg
def mergeDBCfg(flags, InputKeys=[], InputSQLiteFiles=[])
Definition: LArNewCalib_MergeDB.py:11
OutputConditionsAlgConfig.OutputConditionsAlgCfg
def OutputConditionsAlgCfg(flags, name="OutputConditionsAlg", outputFile='condobjs.root', **kwargs)
Definition: OutputConditionsAlgConfig.py:5
python.MainServicesConfig.MainServicesCfg
def MainServicesCfg(flags, LoopMgr='AthenaEventLoopMgr')
Definition: MainServicesConfig.py:256
python.McEventSelectorConfig.McEventSelectorCfg
def McEventSelectorCfg(flags, **kwargs)
Definition: McEventSelectorConfig.py:5
python.IOVDbSvcConfig.addFolders
def addFolders(flags, folderStrings, detDb=None, className=None, extensible=False, tag=None, db=None, modifiers='')
Definition: IOVDbSvcConfig.py:72
python.LArNewCalib_MergeDB.DBConnectionFile
def DBConnectionFile(sqlitefile)
Definition: LArNewCalib_MergeDB.py:8
LArGMConfig.LArGMCfg
def LArGMCfg(flags)
Definition: LArGMConfig.py:8
python.PoolCommonConfig.PoolSvcCfg
def PoolSvcCfg(flags, withCatalogs=False, **kwargs)
Definition: PoolCommonConfig.py:7
python.LArCalibConfigFlags.LArCalibFolderTag
def LArCalibFolderTag(folder, tag)
Definition: LArCalibConfigFlags.py:96
python.AllConfigFlags.initConfigFlags
def initConfigFlags()
Definition: AllConfigFlags.py:19
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
str
Definition: BTagTrackIpAccessor.cxx:11
python.LArCalibConfigFlags.addLArCalibFlags
def addLArCalibFlags(flags, isSC=False)
Definition: LArCalibConfigFlags.py:3
python.LArNewCalib_MergeDB.list_of_strings
def list_of_strings(arg)
Definition: LArNewCalib_MergeDB.py:194