Newer
Older
TB_Chris / TbUT / scripts / TbUTAutomaticAnalyzer / .svn / text-base / TbUTAnalyzer.py.svn-base
  1. __author__ = 'ja'
  2. from xml.dom.minidom import parse
  3. import xml.dom.minidom
  4. import os
  5. from tempfile import mkstemp
  6. from shutil import move
  7. from os import remove, close
  8.  
  9. class TbUTAnalyzer():
  10.  
  11. def __init__(self):
  12. self.dirToAnalyse="BoardA4"
  13. self.pathToDataInEos="/afs/cern.ch/user/a/adendek/eos/lhcb/testbeam/ut/OfficialData/July2015/"
  14. self.scanType='BiasScan' # one of 'AngleScan' 'BiasScan'
  15. self.TbUTPath=os.path.expandvars("$KEPLERROOT")+"/../TbUT/"
  16.  
  17. def _performAnalysis(self, inputFilename):
  18. print( "work on: " +inputFilename)
  19. self.__changeOptionFile(inputFilename)
  20. os.chdir(self.TbUTPath)
  21. os.system("gaudirun.py options/TbUTRun.py")
  22. os.system("gaudirun.py options/TbUTRun.py")
  23. self._moveOutputRootFilesIntoDirectory(inputFilename)
  24. ###
  25. # add your stuffs here!
  26. ###
  27.  
  28.  
  29. def runAnalysis(self):
  30. self._createOutputDir()
  31. xmlRoot = xml.dom.minidom.parse('July2015Database.xml')
  32. for directory in xmlRoot.getElementsByTagName('directory'):
  33. if (directory.getAttribute("name")) in self.dirToAnalyse:
  34. biasScan=directory.getElementsByTagName(self.scanType)
  35. for run in biasScan[0].getElementsByTagName("file"):
  36. dataPath=self.pathToDataInEos+ self.dirToAnalyse+"/RawData/"+run.firstChild.nodeValue
  37. self._performAnalysis(dataPath)
  38.  
  39.  
  40. def _createOutputDir(self):
  41. self.outDirName=self.TbUTPath+"study_"+self.dirToAnalyse+"_"+self.scanType
  42. if not os.path.exists(self.outDirName):
  43. os.mkdir(self.outDirName)
  44.  
  45. def __changeOptionFile(self,inputFileName):
  46. optionFilePath=self.TbUTPath+"options/TbUTRun.py"
  47. fh, abs_path = mkstemp()
  48. with open(abs_path,'w') as new_file:
  49. with open(optionFilePath) as old_file:
  50. for line in old_file:
  51. if not "app.inputData" in line:
  52. new_file.write(line)
  53. else:
  54. new_file.write("app.inputData='"+inputFileName+"'\n")
  55. close(fh)
  56. #Remove original file
  57. remove(optionFilePath)
  58. #Move new file
  59. move(abs_path, optionFilePath)
  60.  
  61. def _moveOutputRootFilesIntoDirectory(self, inputFilename):
  62. outputName=inputFilename[:-4]+".root"
  63. outputName=outputName[outputName.rfind("/")+1:]
  64. move(outputName,self.outDirName+"/"+outputName)
  65.  
  66. outputTupleName=inputFilename[:-4]+"_Tuple.root"
  67. outputTupleName=outputTupleName[outputTupleName.rfind("/")+1:]
  68. move(outputTupleName,self.outDirName+"/"+outputTupleName)
  69.  
  70.  
  71. if __name__=='__main__':
  72. analyzer = TbUTAnalyzer()
  73. analyzer.runAnalysis()