From 61f3cb073cfaae2f3f5a98fb8bf3e80a5df63b16 Mon Sep 17 00:00:00 2001
From: mmusich <marco.musich@cern.ch>
Date: Fri, 28 May 2021 21:19:19 +0200
Subject: [PATCH] improve scripts and actually return failure if does not pass
 checks

---
 .../test/parseFwkJobReport.py                 | 31 ++++++----
 .../test/testPCLAlCaHarvesting.py             | 58 +++++++++++++++++--
 2 files changed, 75 insertions(+), 14 deletions(-)

diff --git a/Calibration/TkAlCaRecoProducers/test/parseFwkJobReport.py b/Calibration/TkAlCaRecoProducers/test/parseFwkJobReport.py
index abcbd44488868..6acd54e05cc61 100644
--- a/Calibration/TkAlCaRecoProducers/test/parseFwkJobReport.py
+++ b/Calibration/TkAlCaRecoProducers/test/parseFwkJobReport.py
@@ -1,12 +1,18 @@
 from __future__ import print_function
 import xml.etree.ElementTree as ET
-  
+import sys
+
 ## declare all constants here
 TARGET_LIST_OF_TAGS=['SiPixelQualityFromDbRcd_other', 'SiPixelQualityFromDbRcd_prompt', 'SiPixelQualityFromDbRcd_stuckTBM', 
-                     'SiStripApvGain_pcl', 'SiStripApvGainAAG_pcl', 'SiStripBadStrip_pcl', 'SiPixelAli_pcl']
+                     'SiStripApvGain_pcl', 'SiStripApvGainAAG_pcl',
+                     'SiStripBadStrip_pcl', 'SiPixelAli_pcl']
 TARGET_DQM_FILES=1
+TARGET_DQM_FILENAME='./DQM_V0001_R000325022__Express__PCLTest__ALCAPROMPT.root'
 TARGET_DB_FILES=7
+TARGET_DB_FILENAME='sqlite_file:promptCalibConditions.db'
+TOTAL_TARGET_FILES=TARGET_DQM_FILES+TARGET_DB_FILES
 
+#_____________________________________________________
 def parseXML(xmlfile):
   
     # create element tree object
@@ -15,22 +21,25 @@ def parseXML(xmlfile):
     # get root element
     root = tree.getroot()
 
-    if( len(root.findall('AnalysisFile'))!=8):
-        print("ERROR: not found enough AnalysisFile entries in the FrameworkJobReport.xml")
+    totAnaEntries=len(root.findall('AnalysisFile'))
+
+    if(totAnaEntries!=TOTAL_TARGET_FILES):
+        print("ERROR: found a not expected number (",totAnaEntries,") of AnalysisFile entries in the FrameworkJobReport.xml")
         return -1
 
     listOfInputTags=[]
 
     countDBfiles=0
     countDQMfiles=0
+
     # iterate news items
     for item in root.findall('AnalysisFile'):
         # iterate child elements of item
         for child in item:
             if(child.tag == 'FileName'):
-                if(child.text=='sqlite_file:promptCalibConditions.db'):
+                if(child.text==TARGET_DB_FILENAME):
                     countDBfiles+=1
-                elif(child.text=='./DQM_V0001_R000325022__Express__PCLTest__ALCAPROMPT.root'):
+                elif(child.text==TARGET_DQM_FILENAME):
                     countDQMfiles+=1
                 else:
                     pass
@@ -38,7 +47,7 @@ def parseXML(xmlfile):
                 listOfInputTags.append(child.attrib['Value'])
 
     if(countDBfiles!=TARGET_DB_FILES):
-        print("ERROR! Found a not expected number DB files,",countDBfiles)
+        print("ERROR! Found a not expected number of DB files",countDBfiles)
         return -1
 
     if(countDQMfiles!=TARGET_DQM_FILES):
@@ -52,22 +61,24 @@ def parseXML(xmlfile):
     
     return 0
 
+#_____________________________________________________
 def main():
     try:
         f = open("FrameworkJobReport.xml")
-        # Do something with the file
     except IOError:
         print("File not accessible")
+        sys.exit(1)
 
     # parse xml file
     result = parseXML('FrameworkJobReport.xml')
     if(result==0):
         print("All is fine with the world!")
+        sys.exit(0)
     else:
         print("Parsing the FwkJobReport results in failure!")
+        sys.exit(1)
 
-    return result
-
+#_____________________________________________________
 if __name__ == "__main__":
   
     # calling main function
diff --git a/Calibration/TkAlCaRecoProducers/test/testPCLAlCaHarvesting.py b/Calibration/TkAlCaRecoProducers/test/testPCLAlCaHarvesting.py
index 3d146a95c7fdb..6f9e7a31e1b96 100644
--- a/Calibration/TkAlCaRecoProducers/test/testPCLAlCaHarvesting.py
+++ b/Calibration/TkAlCaRecoProducers/test/testPCLAlCaHarvesting.py
@@ -1,5 +1,52 @@
-import FWCore.ParameterSet.Config as cms
+from __future__ import print_function
+import calendar
+import CondCore.Utilities.conddblib as conddb
+
+#___________________________________________________________________
+def findRunStopTime(run_number):
+    con = conddb.connect(url = conddb.make_url("pro"))
+    session = con.session()
+    RunInfo = session.get_dbtype(conddb.RunInfo)
+    bestRun = session.query(RunInfo.run_number,RunInfo.start_time, RunInfo.end_time).filter(RunInfo.run_number >= run_number).first()
+    if bestRun is None:
+        raise Exception("Run %s can't be matched with an existing run in the database." % run_number)
+
+    start= bestRun[1]
+    stop = bestRun[2]
+
+    bestRunStartTime = calendar.timegm( bestRun[1].utctimetuple() ) << 32
+    bestRunStopTime  = calendar.timegm( bestRun[2].utctimetuple() ) << 32
+
+    print("run start time:",start,"(",bestRunStartTime,")")
+    print("run stop time: ",stop,"(",bestRunStopTime,")")
+
+    return bestRunStopTime
+
+import optparse
+parser = optparse.OptionParser(usage = 'Usage: %prog [options] <file> [<file> ...]\n')
+parser.add_option('-G', '--inputGT',
+                  dest = 'inputGT',
+                  default = 'auto:run2_data',
+                  help = 'Global Tag to get conditions')
+
+parser.add_option('-r', '--inputRun',
+                  dest = 'inputRun',
+                  default = 325022,
+                  help = 'run to be used')
+
+parser.add_option('-t', '--inputTime',
+                  dest = 'inputTime',
+                  default = 6614916085915320320,
+                  help = 'time to be used')
+
+parser.add_option('-e', '--enableJobReport',
+                  dest = 'empty',
+                  default = None,
+                  help = 'unused')
+
+(options, arguments) = parser.parse_args()
 
+import FWCore.ParameterSet.Config as cms
 process = cms.Process('ALCAHARVEST')
 
 # import of standard configurations
@@ -12,11 +59,14 @@
 process.load('Configuration.StandardSequences.AlCaHarvesting_cff')
 process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')
 
+##
+## configure the source with an random run
+##
 process.source = cms.Source("EmptySource",
-                            firstRun = cms.untracked.uint32(325022),
+                            firstRun = cms.untracked.uint32(options.inputRun),
                             numberEventsInRun = cms.untracked.uint32(1),
                             numberEventsInLuminosityBlock = cms.untracked.uint32(1),
-                            firstTime = cms.untracked.uint64(6614916085915320320),
+                            firstTime = cms.untracked.uint64(options.inputTime),
                             timeBetweenEvents = cms.untracked.uint64(1)
                             )
 
@@ -38,7 +88,7 @@
 
 process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')
 from Configuration.AlCa.GlobalTag import GlobalTag
-process.GlobalTag = GlobalTag(process.GlobalTag, 'auto:run2_data', '')
+process.GlobalTag = GlobalTag(process.GlobalTag, options.inputGT, '')
 
 process.SiStripQuality  = cms.Path(process.ALCAHARVESTSiStripQuality)
 process.alcaSiStripQualityHarvester.CalibrationThreshold = cms.untracked.uint32(0)