~s-h-wilbur/maus/mybranch

« back to all changes in this revision

Viewing changes to bin/utilities/analyze_data_fast_turnaround/rootCan2pdf.py

  • Committer: Adam Dobbs
  • Date: 2016-12-19 15:18:10 UTC
  • mfrom: (659.2.53 release-candidate)
  • Revision ID: phuccj@gmail.com-20161219151810-d4d1v2fas3tsjw1t
Tags: MAUS-v2.7, MAUS-v2.7.0
MAUS-v2.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
This utility takes a ROOT file containing TCanvas objects 
 
3
and outputs the canvas images to a PDF file.
 
4
These ROOT files are typically produced by a program 
 
5
e.g. from online reconstruction
 
6
Usage is rootCan2pdf.py <run-number>
 
7
output is <run-number>imageData.pdf
 
8
 
 
9
When used with -r <run-number>, 
 
10
  the output file will be Run<run-number>imageData.pdf
 
11
When used with -f <input-root-file>, 
 
12
  the output file will be Run0imageData.pdf
 
13
 
 
14
"""
 
15
 
 
16
# pylint:disable=C0103, E1101, W0621, W0702
 
17
 
 
18
import ROOT
 
19
import os
 
20
import sys
 
21
import getopt
 
22
 
 
23
ROOT.gROOT.SetBatch(ROOT.kTRUE)
 
24
run_number = 0
 
25
 
 
26
def imageify(inFile, outFile):
 
27
    """
 
28
    inFile is a root file with a list of canvases
 
29
    outFile is a pdf output containing canvas images
 
30
    """
 
31
 
 
32
    if not os.path.isfile(inFile):
 
33
        sys.exit(1)
 
34
    f1 = ROOT.TFile.Open(inFile,"r")
 
35
    if not len(f1.GetListOfKeys()):
 
36
        print "++ No keys in root file ", inFile
 
37
        sys.exit(1)
 
38
 
 
39
    c1 = ROOT.TCanvas()
 
40
    c1.Print("tmpFile(", "pdf")
 
41
 
 
42
    try:
 
43
        for key in f1.GetListOfKeys():
 
44
            cl = ROOT.gROOT.GetClass(key.GetClassName())
 
45
            if not cl.InheritsFrom("TCanvas"):
 
46
                continue
 
47
            c = key.ReadObj()
 
48
            c.Draw()
 
49
            imgFile = str(run_number) + "_" + key.GetName() + ".png"
 
50
            c.Print(imgFile)
 
51
            c.Print("tmpFile", "pdf")
 
52
    except:
 
53
        print "No keys in root file"
 
54
        sys.exit(2)
 
55
    c1.Print("tmpFile)", "pdf")
 
56
    os.rename("tmpFile", outFile)
 
57
 
 
58
if __name__ == "__main__":
 
59
    filename = None
 
60
 
 
61
    try:
 
62
        (opts, args) = \
 
63
          getopt.getopt(sys.argv[1:], "r:f:v", ["run_number=", "file="])
 
64
    except:
 
65
        print "Unrecognized option", sys.argv[1]
 
66
        print 'Usage: %s [-r run_number ] [-f filename]' % (sys.argv[0])
 
67
        sys.exit(1)
 
68
    for o, a in opts:
 
69
        if o in ('-r', '--run_number'):
 
70
            if a:
 
71
                run_number = int(a)
 
72
                inFile = "Run"+str(run_number)+"imageData.root"
 
73
        elif o in ('-f', '--file'):
 
74
            if a:
 
75
                inFile = "%s" % a
 
76
        else:
 
77
            print "Unrecognized option ", o
 
78
            sys.exit(2)
 
79
    outFile = "Run"+str(run_number)+"imageData.pdf"
 
80
    imageify(inFile, outFile)
 
81
 
 
82
 
 
83