Skip to content

Commit

Permalink
reformat python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lobis committed Jan 9, 2023
1 parent 5a2e94e commit 6c6fc35
Show file tree
Hide file tree
Showing 14 changed files with 789 additions and 581 deletions.
7 changes: 6 additions & 1 deletion pipeline/analysisProcesses/toRawSignal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

def load_REST():
import ROOT

ROOT.gSystem.Load("libRestFramework.so")
ROOT.gSystem.Load("libRestConnectors.so")
ROOT.gSystem.Load("libRestDetector.so")
Expand All @@ -17,14 +18,18 @@ def load_REST():

# Create a fake detector event


def generate_random_detector_event() -> ROOT.TRestDetectorSignalEvent:
event = ROOT.TRestDetectorSignalEvent()
time_range = (0.0, 100.0 * 1000) # ns
energy_range = (1.0, 500.0) # keV
for signal_id in range(4):
n_hits = np.random.randint(20, 50)
time = np.random.rand(n_hits) * (time_range[1] - time_range[0]) + time_range[0]
energy = np.random.rand(n_hits) * (energy_range[1] - energy_range[0]) + energy_range[0]
energy = (
np.random.rand(n_hits) * (energy_range[1] - energy_range[0])
+ energy_range[0]
)

for i in range(n_hits):
event.AddChargeToSignal(signal_id, time[i], energy[i])
Expand Down
12 changes: 7 additions & 5 deletions pipeline/trex/validateStreamer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import os, sys

os.system("restRoot -b -q StreamerOutput.C'(\"Hits_01928.root\")' | grep Process | grep TRest | grep version | wc -l > output.log 2>&1")
os.system(
"restRoot -b -q StreamerOutput.C'(\"Hits_01928.root\")' | grep Process | grep TRest | grep version | wc -l > output.log 2>&1"
)

with open('output.log') as f:
with open("output.log") as f:
lines = f.readlines()

for line in lines:
if( line.find("9") == 0):
print ("The number of processes inside the event data chain is 9. Succeed!")
if line.find("9") == 0:
print("The number of processes inside the event data chain is 9. Succeed!")
sys.exit(0)
else:
print ("The number of processes inside the event data chain is NOT 6! Fail!")
print("The number of processes inside the event data chain is NOT 6! Fail!")
sys.exit(1)

sys.exit(0)
2 changes: 1 addition & 1 deletion pipeline/validateMacros.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

os.system("restRoot -b -q -m 1 > error.log 2>&1")

with open('error.log') as f:
with open("error.log") as f:
lines = f.readlines()

for line in lines:
Expand Down
216 changes: 115 additions & 101 deletions pipeline/validateProcesses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# J. Galan - [email protected]
# 23 - Dec - 2019

#from __future__ import print_function
# from __future__ import print_function
import os
import sys
import re
Expand All @@ -14,59 +14,68 @@


def validateProcess(className):
print ("")
print ("++++ Validating process : " + className)
with open(className, 'r', encoding="utf-8") as file:
print("")
print("++++ Validating process : " + className)
with open(className, "r", encoding="utf-8") as file:
data = file.read()

data = data[data.find("::ProcessEvent"):]
data = data[data.find("::ProcessEvent") :]
data = removeCppComment(data)
data = getMethodDefinition(data)
#data = removeCppComment(data)
# data = removeCppComment(data)

# we shall find only "return fXXXEvent" and "return nullptr"
# other cases might be for example lambda func
returnPos = 0
while returnPos < len(data):
returnPos = data.find("RETURN ", returnPos)
if data.find("EVENT", returnPos) - returnPos > 25 or data.find("EVENT", returnPos) == -1:
returnPos = returnPos + 6;
if (
data.find("EVENT", returnPos) - returnPos > 25
or data.find("EVENT", returnPos) == -1
):
returnPos = returnPos + 6
else:
break
if data.find("NULL", returnPos) - returnPos > 25 or data.find("NULL", returnPos) == -1:
returnPos = returnPos + 6;
if (
data.find("NULL", returnPos) - returnPos > 25
or data.find("NULL", returnPos) == -1
):
returnPos = returnPos + 6
else:
break

print(returnPos);
print(returnPos)

map = getObservablePositions(data)

if returnPos != -1:
for i in map:
for i in map:
iObsPos = map[i]
if iObsPos > returnPos:
print (" - Process is likely to be returned before SetObservableValue!!")
print (" - FILE : " + className)
print(
" - Process is likely to be returned before SetObservableValue!!"
)
print(" - FILE : " + className)
sys.exit(1)
print ("OK")
print("OK")
return


def getObservablePositions(data):
obsposes = {}
pos = 0
str = "SETOBSERVABLEVALUE(\""
while(pos < len(data)):
pos1 = data.find(str,pos)
if(pos1 == -1):
break
str = 'SETOBSERVABLEVALUE("'
while pos < len(data):
pos1 = data.find(str, pos)
if pos1 == -1:
break
pos1 += len(str)
pos2 = data.find("\"",pos1)
if(pos2 == -1):
break
pos2 = data.find('"', pos1)
if pos2 == -1:
break

name = data[pos1:pos2]
if(not name in obsposes):
if not name in obsposes:
obsposes[name] = pos1

pos = pos2 + 1
Expand All @@ -78,104 +87,109 @@ def getMethodDefinition(text):

counter = 1
start = initPos + 1
while(counter > 0):
pos1 = text.find("{",start)
pos2 = text.find("}",start)
while counter > 0:
pos1 = text.find("{", start)
pos2 = text.find("}", start)

endPosition = pos2 + 1

if(pos1 != -1 and pos2 != -1):
if(pos1 < pos2):
if pos1 != -1 and pos2 != -1:
if pos1 < pos2:
counter = counter + 1
start = pos1 + 1
if(pos2 < pos1):
if pos2 < pos1:
counter = counter - 1
start = pos2 + 1
elif pos1 != -1:
print ("Big error!!")
print("Big error!!")
else:
counter = counter - 1
start = pos2 + 1
counter = counter - 1
start = pos2 + 1

return text[initPos:endPosition].upper()


def removeCppComment(strInput) :
state = 0;
strOutput = ''
strRemoved = ''

for c in strInput :
if state == 0 and c == '/' : # ex. [/]
state = 1
elif state == 1 and c == '*' : # ex. [/*]
state = 2
elif state == 1 and c == '/' : # ex. [#]
state = 4
elif state == 1 : # ex. [<secure/_stdio.h> or 5/3]
state = 0

elif state == 3 and c == '*': # ex. [/*he**]
state = 3
elif state == 2 and c == '*': # ex. [/*he*]
state = 3
elif state == 2: # ex. [/*heh]
state = 2

elif state == 3 and c == '/': # ex. [/*heh*/]
state = 0
elif state == 3: # ex. [/*heh*e]
state = 2

elif state == 4 and c == '\\': # ex. [//hehe\]
state = 9
elif state == 9 and c == '\\': # ex. [//hehe\\\\\]
state = 9
elif state == 9: # ex. [//hehe\<enter> or //hehe\a]
state = 4
elif state == 4 and c == '\n': # ex. [//hehe<enter>]
state = 0

elif state == 0 and c == '\'': # ex. [']
state = 5
elif state == 5 and c == '\\': # ex. ['\]
state = 6
elif state == 6: # ex. ['\n or '\' or '\t etc.]
state = 5
elif state == 5 and c == '\'': # ex. ['\n' or '\'' or '\t' ect.]
state = 0

elif state == 0 and c == '\"': # ex. ["]
state = 7
elif state == 7 and c == '\\': # ex. ["\]
state = 8
elif state == 8: # ex. ["\n or "\" or "\t ect.]
state = 7
elif state == 7 and c == '\"': # ex. ["\n" or "\"" or "\t" ect.]
state = 0

if (state == 0 and c != '/') or state == 5 or\
state == 6 or state == 7 or state == 8 :
strOutput += c
else:
# removed chareters
strRemoved += c
def removeCppComment(strInput):
state = 0
strOutput = ""
strRemoved = ""

for c in strInput:
if state == 0 and c == "/": # ex. [/]
state = 1
elif state == 1 and c == "*": # ex. [/*]
state = 2
elif state == 1 and c == "/": # ex. [#]
state = 4
elif state == 1: # ex. [<secure/_stdio.h> or 5/3]
state = 0

elif state == 3 and c == "*": # ex. [/*he**]
state = 3
elif state == 2 and c == "*": # ex. [/*he*]
state = 3
elif state == 2: # ex. [/*heh]
state = 2

elif state == 3 and c == "/": # ex. [/*heh*/]
state = 0
elif state == 3: # ex. [/*heh*e]
state = 2

elif state == 4 and c == "\\": # ex. [//hehe\]
state = 9
elif state == 9 and c == "\\": # ex. [//hehe\\\\\]
state = 9
elif state == 9: # ex. [//hehe\<enter> or //hehe\a]
state = 4
elif state == 4 and c == "\n": # ex. [//hehe<enter>]
state = 0

elif state == 0 and c == "'": # ex. [']
state = 5
elif state == 5 and c == "\\": # ex. ['\]
state = 6
elif state == 6: # ex. ['\n or '\' or '\t etc.]
state = 5
elif state == 5 and c == "'": # ex. ['\n' or '\'' or '\t' ect.]
state = 0

elif state == 0 and c == '"': # ex. ["]
state = 7
elif state == 7 and c == "\\": # ex. ["\]
state = 8
elif state == 8: # ex. ["\n or "\" or "\t ect.]
state = 7
elif state == 7 and c == '"': # ex. ["\n" or "\"" or "\t" ect.]
state = 0

if (
(state == 0 and c != "/")
or state == 5
or state == 6
or state == 7
or state == 8
):
strOutput += c
else:
# removed chareters
strRemoved += c

return strOutput
return strOutput


files = []

# r=root, d=directories, f = files
for r, d, f in os.walk(sys.argv[1]):
for file in f:
if file.endswith('Process.cxx'):
print("Validating " + file )
# files.append(os.path.join(r, file))
validateProcess(os.path.join(r, file))
for file in f:
if file.endswith("Process.cxx"):
print("Validating " + file)
# files.append(os.path.join(r, file))
validateProcess(os.path.join(r, file))

#for f in files:
# for f in files:
# print(f)

#validateProcess(sys.argv[1]);
# validateProcess(sys.argv[1]);
sys.exit(0)
Loading

0 comments on commit 6c6fc35

Please sign in to comment.