Crombie Tools
HistWriter.py
Go to the documentation of this file.
1 """ @package CrombieTools.SkimmingTools.HistWriter
2 Submodule of CrombieTools.SkimmingTools.
3 Contains the constructor and default HistWriter object.
4 """
5 
6 from .. import Load
7 
8 newHistWriter = Load('HistWriter')
9 histWriter = newHistWriter()
10 
11 def MakeHist(inputFile, outputFile='', outputHist='', writer=histWriter):
12  """A convenience function for HistWriter assuming the text file is named something you like.
13 
14  @param inputFile is the text file where the histogram is written out.
15  @param outputFile is the location where the root histogram will be saved.
16  If left blank, the name is just the inputFile replaced with .txt changed to .root
17  @param outputHist is the name of the histogram saved. If left blank, it uses the
18  basename of the inputFile. (e.g. a/place/hist.txt -> 'hist').
19  @param writer specifies a HistWriter to use. This is probably rarely, if ever, needs to be changed.
20  """
21 
22  writer.SetFileName(outputFile)
23  writer.SetHistName(outputHist)
24 
25  import os.path as op
26 
27  if outputFile == '':
28  outputFile = op.splitext(inputFile)[0] + '.root'
29 
30  if outputHist == '':
31  outputHist = op.splitext(op.basename(inputFile))[0]
32 
33  # Check the configuration file to see if it's 2D
34  numX = 0
35  numY = 0
36  with open(inputFile, 'r') as check_input:
37  for line in check_input:
38  if not numX:
39  numX = len(line.split())
40  if numX == 2:
41  break
42  else:
43  numY += 1
44 
45  if numY:
46  writer.Make2DHist(inputFile, numX, numY)
47  else:
48  writer.MakeHist(inputFile)