1
2 """
3 daemon for merging ROOT histograms and generating graphs for iceprod datasets
4
5 copyright (c) 2007 the icecube collaboration
6
7 @version: $Revision: $
8 @date: $Date: $
9 @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu>
10 """
11
12 import sys,os
13 import time
14 import os.path
15 import getopt
16 import glob
17 import signal
18 from ROOT import *
19 import logging
20 from iceprod.core.metadata import *
21 from iceprod.core.xmlwriter import IceTrayXMLWriter
22 from os.path import expandvars
23
24 import iceprod.core.logger
25 logger = logging.getLogger('Histos')
26
27 -def makehistos( argv , outputpath ,dataset,exphisto=None):
28
29 targetName = os.path.join(outputpath,'soaphisto_%u.root' % dataset)
30 logger.info("Target file: %s" % targetName)
31 Target = TFile.Open( targetName, "RECREATE" );
32
33 Merge( Target, argv);
34 Draw( Target, outputpath ,exphisto);
35
36
37 -def Merge( target, sourcelist ):
38
39 path = "/"
40 first_source = TFile.Open( sourcelist[0] ) ;
41
42
43 norm = float(len(sourcelist))
44
45
46 for key in gDirectory.GetListOfKeys():
47
48
49 first_source.cd( path );
50 obj = key.ReadObj();
51
52 if obj.IsA().InheritsFrom( "TH1" ):
53
54 h1 = obj;
55
56
57 for next in sourcelist:
58 nextsource = TFile.Open( next ) ;
59 nextsource.cd( path );
60 h2 = gDirectory.Get( h1.GetName() );
61 if h2:
62 h1.Add( h2 );
63 del h2;
64 nextsource.Close();
65 h1.Scale(1.0/norm);
66
67 elif obj.IsA().InheritsFrom( "TDirectory" ):
68 logger.debug("Found subdirectory %s" % obj.GetName())
69
70 target.cd();
71 newdir = target.mkdir( obj.GetName(), obj.GetTitle() );
72
73
74 MergeRootfile( newdir, sourcelist );
75
76 else:
77
78 logger.warn("Unknown object type, name: %s , title: %s" %
79 (obj.GetName(), obj.GetTitle()))
80
81
82 if obj:
83 target.cd();
84 obj.Write( key.GetName() );
85
86 first_source.Close()
87
88 -def Draw( target ,outputpath, exphisto=None):
89
90 path = "/"
91 expfile = TFile.Open( exphisto ) ;
92
93
94 for key in target.GetListOfKeys():
95
96
97 obj = key.ReadObj();
98
99 if obj.IsA().InheritsFrom( "TH1" ):
100
101 c1 = TCanvas("c1", "c1",123,71,699,499);
102 h1 = obj;
103 h1.SetLineColor(4);
104 h1.SetLineWidth(1);
105 h2 = None;
106
107 expname = h1.GetName().strip();
108 logger.debug(expname)
109 if exphisto:
110 k2 = expfile.FindKey(expname)
111 if k2:
112 h2 = k2.ReadObj();
113 h2.SetLineColor(2);
114 h2.SetLineWidth(1);
115
116 c1.SetBorderSize(2);
117 c1.SetLogy(0);
118 h1.Draw();
119
120 if h2:
121 h2.Draw("same");
122 c1.SaveAs(os.path.join(outputpath,h1.GetName()+".gif"));
123
124 c1.SetLogy(1);
125 h1.Draw();
126 if h2:
127 h2.Draw("same");
128 c1.SaveAs(os.path.join(outputpath,h1.GetName()+"_log.gif"));
129
130 c1.SetLogy(0);
131 rat = h1.Clone();
132 rat.Divide(h1,h2,1.,1.,"B");
133 rat.SetName(h1.GetName()+"_ratio")
134 rat.Draw();
135 c1.SaveAs(os.path.join(outputpath,rat.GetName()+".gif"));
136
137 c1.SetLogy(1);
138 rat.Draw();
139 c1.SaveAs(os.path.join(outputpath,rat.GetName()+"_log.gif"));
140
141 elif obj.IsA().InheritsFrom( "TDirectory" ):
142 logger.debug("Found subdirectory %s"% obj.GetName())
143 target.cd();
144 newdir = target.mkdir( obj.GetName(), obj.GetTitle() );
145
146
147 Draw( newdir );
148
149 else:
150 logger.warn("Unknown object type, name: %s Title: %s"% (obj.GetName(), obj.GetTitle()))
151
152
155 self.cfg = cfg
156 self.i3db = i3db
157 self.resources = os.path.join(cfg.get('path','basedir'),"shared")
158 self.exphistos = cfg.get('path','exp_histos',raw=True)
159
160
161 - def MakeHistos(self,set,basepath,histopath,pattern="histo*.root"):
162 self.i3db.connect()
163 dataset = set["dataset_id"]
164 logger.info('getting path info for dataset %d ' % dataset)
165 path = basepath % set
166 histo_path = histopath % set
167 exp_histos = expandvars(self.exphistos % set)
168 logger.debug("exp_histos %s" % exp_histos)
169
170
171 if not os.path.exists(exp_histos):
172 exp_histos = None
173
174 if os.path.exists(path):
175
176 logger.info("generating histograms in %s" % path)
177
178 logger.info('looking in %s/*/%s' % (path,pattern))
179 rootfilelist = glob.glob('%s/*/%s' % (path,pattern))
180 if len(rootfilelist) > 0:
181 if not os.path.exists(histo_path):
182 os.makedirs(histo_path)
183 os.system("cp %s %s/index.php" % (os.path.join(self.resources,'histo.php'), histo_path))
184 os.system("cp %s %s/" % (os.path.join(self.resources,'histo.css'), histo_path ))
185 makehistos(rootfilelist,histo_path,dataset,exp_histos)
186 self.i3db.AddedHisto(dataset)
187 else:
188 logger.info('No root files found for dataset %d' % dataset)
189 else:
190 logger.error('%s path does not exist' % path)
191 return
192