forked from ctolon/PythonInterfaceOOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runV0selector.py
executable file
·571 lines (478 loc) · 23.9 KB
/
runV0selector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-
# Copyright 2019-2020 CERN and copyright holders of ALICE O2.
# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
# All rights not expressly granted are reserved.
#
# This software is distributed under the terms of the GNU General Public
# License v3 (GPL Version 3), copied verbatim in the file "COPYING".
#
# In applying this license CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.
# \Author: [email protected]
# \Interface: [email protected]
# Orginal Task: https://github.com/AliceO2Group/O2Physics/blob/master/PWGDQ/Tasks/v0selector.cxx
import json
import sys
import logging
import logging.config
from logging import handlers
import os
import argparse
from extramodules.actionHandler import NoAction
from extramodules.actionHandler import ChoicesAction
from extramodules.debugOptions import DebugOptions
from extramodules.stringOperations import listToString
from commondeps.centralityTable import CentralityTable
from commondeps.eventSelection import EventSelectionTask
from commondeps.multiplicityTable import MultiplicityTable
from commondeps.pidTOFBase import TofEventTime
from commondeps.pidTOFBeta import TofPidBeta
from commondeps.pidTPCTOFFull import TpcTofPidFull
from commondeps.trackPropagation import TrackPropagation
from dqtasks.v0selector import V0selector
from pycacheRemover import PycacheRemover
"""
argcomplete - Bash tab completion for argparse
Documentation https://kislyuk.github.io/argcomplete/
Instalation Steps
pip install argcomplete
sudo activate-global-python-argcomplete
Only Works On Local not in O2
Activate libraries in below and activate #argcomplete.autocomplete(parser) line
"""
import argcomplete
from argcomplete.completers import ChoicesCompleter
###################################
# Interface Predefined Selections #
###################################
centralityTableParameters = [
"estRun2V0M",
"estRun2SPDtks",
"estRun2SPDcls",
"estRun2CL0",
"estRun2CL1",
"estFV0A",
"estFT0M",
"estFDDM",
"estNTPV"
]
# TODO: Add genname parameter
ft0Parameters = ["processFT0", "processNoFT0", "processOnlyFT0", "processRun2"]
pidParameters = [
"pid-el",
"pid-mu",
"pid-pi",
"pid-ka",
"pid-pr",
"pid-de",
"pid-tr",
"pid-he",
"pid-al"
]
booleanSelections = ["true", "false"]
clist = []
# Get system variables in alienv. In alienv we don't have cuts and signal library!!! We need discuss this thing
O2DPG_ROOT = os.environ.get("O2DPG_ROOT")
QUALITYCONTROL_ROOT = os.environ.get("QUALITYCONTROL_ROOT")
O2_ROOT = os.environ.get("O2_ROOT")
O2PHYSICS_ROOT = os.environ.get("O2PHYSICS_ROOT")
#################
# Init Workflow #
#################
class RunV0selector(object):
"""
This class is for managing the workflow by using the interface arguments from
all other Common dependencies and the v0selector Task's own arguments in a combined structure.
Args:
object (parser_args() object): runV0selector.py workflow
"""
def __init__(self,
parserRunV0selector=argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Arguments to pass"),
v0selector=V0selector(),
eventSelection=EventSelectionTask(),
centralityTable=CentralityTable(),
multiplicityTable=MultiplicityTable(),
tofEventTime=TofEventTime(),
tofPidBeta=TofPidBeta(),
tpcTofPidFull=TpcTofPidFull(),
trackPropagation=TrackPropagation(),
debugOptions=DebugOptions()
):
super(RunV0selector, self).__init__()
self.parserRunV0selector = parserRunV0selector
self.v0selector = v0selector
self.eventSelection = eventSelection
self.centralityTable = centralityTable
self.multiplicityTable = multiplicityTable
self.tofEventTime = tofEventTime
self.tofPidBeta = tofPidBeta
self.tpcTofPidFull = tpcTofPidFull
self.trackPropagation = trackPropagation
self.debugOptions = debugOptions
self.parserRunV0selector.register("action", "none", NoAction)
self.parserRunV0selector.register("action", "store_choice", ChoicesAction)
def addArguments(self):
"""
This function allows to add arguments for parser_args() function
"""
# Core Part
groupCoreSelections = self.parserRunV0selector.add_argument_group(title="Core configurations that must be configured")
groupCoreSelections.add_argument("cfgFileName", metavar="Config.json", default="config.json", help="config JSON file name")
groupTaskAdders = self.parserRunV0selector.add_argument_group(title="Additional Task Adding Options")
groupTaskAdders.add_argument("--add_mc_conv", help="Add the converter from mcparticle to mcparticle+001 (Adds your workflow o2-analysis-mc-converter task)", action="store_true")
groupTaskAdders.add_argument("--add_fdd_conv", help="Add the fdd converter (Adds your workflow o2-analysis-fdd-converter task)", action="store_true")
groupTaskAdders.add_argument("--add_track_prop", help="Add track propagation to the innermost layer (TPC or ITS) (Adds your workflow o2-analysis-track-propagation task)", action="store_true")
groupTaskAdders.add_argument("--add_weakdecay_ind", help="Add Converts V0 and cascade version 000 to 001 (Adds your workflow o2-analysis-weak-decay-indices task)", action="store_true")
# aod
groupDPLReader = self.parserRunV0selector.add_argument_group(title="Data processor options: internal-dpl-aod-reader")
groupDPLReader.add_argument("--aod", help="Add your AOD File with path", action="store", type=str)
# automation params
groupAutomations = self.parserRunV0selector.add_argument_group(title="Automation Parameters")
groupAutomations.add_argument("--onlySelect", help="If false JSON Overrider Interface If true JSON Additional Interface", action="store", default="true", type=str.lower, choices=booleanSelections).completer = ChoicesCompleter(booleanSelections)
groupAutomations.add_argument("--autoDummy", help="Dummy automize parameter (don't configure it, true is highly recomended for automation)", action="store", default="true", type=str.lower, choices=booleanSelections).completer = ChoicesCompleter(booleanSelections)
# helper lister commands
#groupAdditionalHelperCommands = self.parserRunV0selector.add_argument_group(title="Additional Helper Command Options")
#groupAdditionalHelperCommands.add_argument("--cutLister", help="List all of the analysis cuts from CutsLibrary.h", action="store_true")
def parseArgs(self):
"""
This function allows to save the obtained arguments to the parser_args() function
Returns:
Namespace: returns parse_args()
"""
argcomplete.autocomplete(self.parserRunV0selector, always_complete_options=False)
return self.parserRunV0selector.parse_args()
def mergeArgs(self):
"""
This function allows to merge parser_args argument information from different classes
"""
self.eventSelection.parserEventSelectionTask = self.parserRunV0selector
self.eventSelection.addArguments()
self.centralityTable.parserCentralityTable = self.parserRunV0selector
self.centralityTable.addArguments()
self.multiplicityTable.parserMultiplicityTable = self.parserRunV0selector
self.multiplicityTable.addArguments()
self.tofEventTime.parserTofEventTime = self.parserRunV0selector
self.tofEventTime.addArguments()
self.tofPidBeta.parserTofPidBeta = self.parserRunV0selector
self.tofPidBeta.addArguments()
self.tpcTofPidFull.parserTpcTofPidFull = self.parserRunV0selector
self.tpcTofPidFull.addArguments()
self.trackPropagation.parserTrackPropagation = self.parserRunV0selector
self.trackPropagation.addArguments()
self.debugOptions.parserDebugOptions = self.parserRunV0selector
self.debugOptions.addArguments()
self.v0selector.parserV0selector = self.parserRunV0selector
self.v0selector.addArguments()
self.addArguments()
# This function not work should be integrated instead of mergeArgs
"""
def mergeMultiArgs(self, *objects):
parser = self.parserRunV0selector
for object in objects:
object.parser = parser
object.addArguments()
self.addArguments()
"""
# init args manually
initArgs = RunV0selector()
initArgs.mergeArgs()
initArgs.parseArgs()
extrargs = initArgs.parseArgs()
configuredCommands = vars(extrargs) # for get extrargs
# Transcation management for forgettining assign a value to parameters
forgetParams = []
for key,value in configuredCommands.items():
if(value != None):
if (type(value) == type("string") or type(value) == type(clist)) and len(value) == 0:
forgetParams.append(key)
if len(forgetParams) > 0:
print("[ERROR] Your forget assign a value to for this parameters: ", forgetParams)
sys.exit()
# Debug Settings
if extrargs.debug and (not extrargs.logFile):
DEBUG_SELECTION = extrargs.debug
numeric_level = getattr(logging, DEBUG_SELECTION.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: %s" % DEBUG_SELECTION)
logging.basicConfig(format="[%(levelname)s] %(message)s", level=DEBUG_SELECTION)
if extrargs.logFile and extrargs.debug:
log = logging.getLogger("")
level = logging.getLevelName(extrargs.debug)
log.setLevel(level)
format = logging.Formatter("%(asctime)s - [%(levelname)s] %(message)s")
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(format)
log.addHandler(ch)
loggerFile = "v0selector.log"
if os.path.isfile(loggerFile):
os.remove(loggerFile)
fh = handlers.RotatingFileHandler(loggerFile, maxBytes=(1048576*5), backupCount=7, mode="w")
fh.setFormatter(format)
log.addHandler(fh)
######################
# PREFIX ADDING PART #
######################
# add prefix for extrargs.pid for pid selection
if extrargs.pid != None:
prefix_pid = "pid-"
extrargs.pid = [prefix_pid + sub for sub in extrargs.pid]
# add prefix for extrargs.FT0 for tof-event-time
if extrargs.FT0 != None:
prefix_process = "process"
extrargs.FT0 = prefix_process + extrargs.FT0
######################################################################################
commonDeps = [
"o2-analysis-timestamp",
"o2-analysis-event-selection",
"o2-analysis-multiplicity-table",
"o2-analysis-trackselection",
"o2-analysis-trackextension",
"o2-analysis-pid-tof-base",
"o2-analysis-pid-tof",
"o2-analysis-pid-tof-full",
"o2-analysis-pid-tof-beta",
"o2-analysis-pid-tpc-full"
]
# Make some checks on provided arguments
if len(sys.argv) < 2:
logging.error("Invalid syntax! The command line should look like this:")
logging.info(" ./runV0selector.py <yourConfig.json> --param value ...")
sys.exit()
# Load the configuration file provided as the first parameter
cfgControl = sys.argv[1] == extrargs.cfgFileName
config = {}
try:
if cfgControl:
with open(extrargs.cfgFileName) as configFile:
config = json.load(configFile)
else:
logging.error("Invalid syntax! After the script you must define your json configuration file!!! The command line should look like this:")
logging.info(" ./runV0selector.py<yourConfig.json> <-runData|-runMC> --param value ...")
sys.exit()
except FileNotFoundError:
isConfigJson = sys.argv[1].endswith(".json")
if not isConfigJson:
logging.error("Invalid syntax! After the script you must define your json configuration file!!! The command line should look like this:")
logging.info(" ./runV0selector.py <yourConfig.json> --param value ...")
sys.exit()
logging.error("Your JSON Config File found in path!!!")
sys.exit()
taskNameInConfig = "v0-selector"
taskNameInCommandLine = "o2-analysis-dq-v0-selector"
if not taskNameInConfig in config:
logging.error("%s Task to be run not found in the configuration file!", taskNameInConfig)
sys.exit()
# Check alienv
if O2PHYSICS_ROOT == None:
logging.error("You must load O2Physics with alienv")
sys.exit()
#############################
# Start Interface Processes #
#############################
logging.info("Only Select Configured as %s", extrargs.onlySelect)
if extrargs.onlySelect == "true":
logging.info("INTERFACE MODE : JSON Overrider")
if extrargs.onlySelect == "false":
logging.info("INTERFACE MODE : JSON Additional")
for key, value in config.items():
if type(value) == type(config):
for value, value2 in value.items():
# aod
if value =="aod-file" and extrargs.aod:
config[key][value] = extrargs.aod
logging.debug(" - [%s] %s : %s",key,value,extrargs.aod)
# QA Options
if value == "cfgWithQA" and extrargs.cfgWithQA:
config[key][value] = extrargs.cfgWithQA
logging.debug(" - [%s] %s : %s",key,value,extrargs.cfgWithQA)
# PID Selections
if (value in pidParameters) and extrargs.pid and key != "tof-pid":
if value in extrargs.pid:
value2 = "1"
config[key][value] = value2
logging.debug(" - [%s] %s : %s",key,value,value2)
elif extrargs.onlySelect == "true":
value2 = "-1"
config[key][value] = value2
logging.debug(" - [%s] %s : %s",key,value,value2)
# event-selection
if value == "syst" and extrargs.syst:
config[key][value] = extrargs.syst
logging.debug(" - [%s] %s : %s",key,value,extrargs.syst)
if value =="muonSelection" and extrargs.muonSelection:
config[key][value] = extrargs.muonSelection
logging.debug(" - [%s] %s : %s",key,value,extrargs.muonSelection)
if value == "customDeltaBC" and extrargs.customDeltaBC:
config[key][value] = extrargs.customDeltaBC
logging.debug(" - [%s] %s : %s",key,value,extrargs.customDeltaBC)
# v0-selector
if value =="d_bz_input" and extrargs.d_bz_input:
config[key][value] = extrargs.d_bz_input
logging.debug(" - [%s] %s : %s",key,value,extrargs.d_bz_input)
if value == "v0cospa" and extrargs.v0cospa:
config[key][value] = extrargs.v0cospa
logging.debug(" - [%s] %s : %s",key,value,extrargs.v0cospa)
if value == "dcav0dau" and extrargs.dcav0dau:
config[key][value] = extrargs.dcav0dau
logging.debug(" - [%s] %s : %s",key,value,extrargs.dcav0dau)
if value =="v0Rmin" and extrargs.v0Rmin:
config[key][value] = extrargs.v0Rmin
logging.debug(" - [%s] %s : %s",key,value,extrargs.v0Rmin)
if value == "v0Rmax" and extrargs.v0Rmax:
config[key][value] = extrargs.v0Rmax
logging.debug(" - [%s] %s : %s",key,value,extrargs.v0Rmax)
if value == "dcamin" and extrargs.dcamin:
config[key][value] = extrargs.dcamin
logging.debug(" - [%s] %s : %s",key,value,extrargs.dcamin)
if value == "dcamax" and extrargs.dcamax:
config[key][value] = extrargs.dcamax
logging.debug(" - [%s] %s : %s",key,value,extrargs.dcamax)
if value =="mincrossedrows" and extrargs.mincrossedrows:
config[key][value] = extrargs.mincrossedrows
logging.debug(" - [%s] %s : %s",key,value,extrargs.mincrossedrows)
if value == "maxchi2tpc" and extrargs.maxchi2tpc:
config[key][value] = extrargs.maxchi2tpc
logging.debug(" - [%s] %s : %s",key,value,extrargs.maxchi2tpc)
# centrality-table
if (value in centralityTableParameters) and extrargs.est:
if value in extrargs.est:
value2 = "1"
config[key][value] = value2
logging.debug(" - [%s] %s : %s",key,value,value2)
elif extrargs.onlySelect == "true":
value2 = "-1"
config[key][value] = value2
logging.debug(" - [%s] %s : %s",key,value,value2)
# multiplicity-table
if value == "doVertexZeq" and extrargs.isVertexZeq:
if extrargs.isVertexZeq == "true":
config[key][value] = "1"
config[key]["doDummyZeq"] = "0"
logging.debug(" - %s %s : 1",key,value)
logging.debug(" - [%s] doDummyZeq : 0",key)
if extrargs.isVertexZeq == "false":
config[key][value] = "0"
config[key]["doDummyZeq"] = "1"
logging.debug(" - %s %s : 0",key,value)
logging.debug(" - [%s] doDummyZeq : 1",key)
# tof-pid, tof-pid-full
if value == "processWSlice" and extrargs.isWSlice:
if extrargs.isWSlice == "true":
config[key][value] = "true"
config[key]["processWoSlice"] = "false"
logging.debug(" - %s %s : true",key,value)
logging.debug(" - [%s] processWoSlice : false",key)
if extrargs.isWSlice == "false":
config[key][value] = "false"
config[key]["processWoSlice"] = "true"
logging.debug(" - %s %s : false",key,value)
logging.debug(" - [%s] processWoSlice : true",key)
# tof-pid-beta
if value == "tof-expreso" and extrargs.tof_expreso:
config[key][value] = extrargs.tof_expreso
logging.debug(" - [%s] %s : %s",key,value,extrargs.tof_expreso)
# tof-event-time
if (value in ft0Parameters) and extrargs.FT0 and key == "tof-event-time":
if value == extrargs.FT0:
value2 = "true"
config[key][value] = value2
logging.debug(" - [%s] %s : %s",key,value,value2)
elif value != extrargs.FT0:
value2 = "false"
config[key][value] = value2
logging.debug(" - [%s] %s : %s",key,value,value2)
# AOD File Checker
if extrargs.aod != None:
argProvidedAod = extrargs.aod
textAodList = argProvidedAod.startswith("@")
endsWithRoot = argProvidedAod.endswith(".root")
endsWithTxt = argProvidedAod.endswith("txt") or argProvidedAod.endswith("text")
if textAodList and endsWithTxt:
argProvidedAod = argProvidedAod.replace("@","")
logging.info("You provided AO2D list as text file : %s",argProvidedAod)
if not os.path.isfile(argProvidedAod):
logging.error("%s File not found in path!!!", argProvidedAod)
sys.exit()
else:
logging.info("%s has valid File Format and Path, File Found", argProvidedAod)
elif endsWithRoot:
logging.info("You provided single AO2D as root file : %s",argProvidedAod)
if not os.path.isfile(argProvidedAod):
logging.error("%s File not found in path!!!", argProvidedAod)
sys.exit()
else:
logging.info("%s has valid File Format and Path, File Found", argProvidedAod)
else:
logging.error("%s Wrong formatted File, check your file!!!", argProvidedAod)
sys.exit()
#####################
# Deps Transcations #
#####################
# In extended tracks, o2-analysis-trackextension is not a valid dep for run 3
# More Information : https://aliceo2group.github.io/analysis-framework/docs/helperTasks/trackselection.html?highlight=some%20of%20the%20track%20parameters
"""
Some of the track parameters used in the track selection require additional calculation effort and are then stored in a table called TracksExtended
which is produced by either the o2-analysis-trackextension task (Run 2) or o2-analysis-track-propagation (Run 3).
The quantities contained in this table can also be directly used in the analysis.
"""
#if config["bc-selection-task"]["processRun3"] == "true":
#commonDeps.remove("o2-analysis-trackextension")
#logging.info("o2-analysis-trackextension is not valid dep for run 3, It will deleted from your workflow.")
###########################
# End Interface Processes #
###########################
# Write the updated configuration file into a temporary file
updatedConfigFileName = "tempConfigV0Selector.json"
with open(updatedConfigFileName,"w") as outputFile:
json.dump(config, outputFile ,indent=2)
# Check which dependencies need to be run
depsToRun = {}
for dep in commonDeps:
depsToRun[dep] = 1
commandToRun = taskNameInCommandLine + " --configuration json://" + updatedConfigFileName + " --severity error --shm-segment-size 12000000000 -b"
for dep in depsToRun.keys():
commandToRun += " | " + dep + " --configuration json://" + updatedConfigFileName + " -b"
logging.debug("%s added your workflow",dep)
if extrargs.add_mc_conv:
logging.debug("o2-analysis-mc-converter added your workflow")
commandToRun += " | o2-analysis-mc-converter --configuration json://" + updatedConfigFileName + " -b"
if extrargs.add_fdd_conv:
commandToRun += " | o2-analysis-fdd-converter --configuration json://" + updatedConfigFileName + " -b"
logging.debug("o2-analysis-fdd-converter added your workflow")
if extrargs.add_track_prop:
commandToRun += " | o2-analysis-track-propagation --configuration json://" + updatedConfigFileName + " -b"
logging.debug("o2-analysis-track-propagation added your workflow")
if extrargs.add_weakdecay_ind:
commandToRun += " | o2-analysis-weak-decay-indices --configuration json://" + updatedConfigFileName + " -b"
logging.debug("o2-analysis-weak-decay-indices added your workflow")
print("====================================================================================================================")
logging.info("Command to run:")
logging.info(commandToRun)
print("====================================================================================================================")
# Listing Added Commands
logging.info("Args provided configurations List")
print("====================================================================================================================")
for key,value in configuredCommands.items():
if(value != None):
if type(value) == type(clist):
listToString(value)
logging.info("--%s : %s ",key,value)
os.system(commandToRun)
# Pycache remove after running in O2
#getParrentDir = sys.path[-1]
# trying to insert to false directory
try:
#os.chdir(getParrentDir)
logging.info("Inserting inside for pycache remove: %s", os.getcwd())
# Caching the exception
except:
logging.error("Something wrong with specified\
directory. Exception- %s", sys.exc_info())
pycacheRemover = PycacheRemover()
pycacheRemover.__init__()
logging.info("pycaches removed succesfully")