source: fact/tools/rootmacros/PulseTemplates/rootfilehandler.C@ 14807

Last change on this file since 14807 was 14807, checked in by Jens Buss, 12 years ago
small bug in if else
File size: 6.1 KB
Line 
1#include <iostream>
2#include <string>
3#include "TObjArray.h"
4
5#include "rootfilehandler.h"
6#include <stdlib.h>
7using namespace std;
8
9/*
10 POSSIBLE IMPROVEMENT:
11
12 creat files on the heap and handover pointers
13
14 */
15
16
17
18
19void
20CreateRootFile(
21 TString loc_fname,
22 bool overwrite,
23 int verbosityLevel
24 )
25{
26 TFile* output_rootfile;
27
28 if(!overwrite)
29 {
30 output_rootfile = new TFile(loc_fname);
31 if (output_rootfile->IsZombie())
32 {
33 cout << "file does not exist...creating file" << endl;
34 delete output_rootfile;
35 output_rootfile = new TFile(loc_fname,"NEW");
36 }
37 else
38 {
39 cout << "file does exist and will be updated" << endl;
40 output_rootfile->OpenFile(loc_fname,"UPDATE");
41 }
42 }
43 else if(overwrite)
44 {
45 cout << "overwriting file" << endl;
46 output_rootfile = new TFile(loc_fname,"RECREATE");
47 }
48
49 if (output_rootfile->IsZombie())
50 {
51 cout << "Error opening file" << endl;
52 exit(-1);
53 }
54 else {
55 if (verbosityLevel > 1) cout << "creating root-file...successfull" << endl;
56 output_rootfile->pwd();
57 output_rootfile->Close();
58 delete output_rootfile;
59 }
60}
61//end of CreateRootFile
62//----------------------------------------------------------------------------
63
64
65TFile*
66ChooseRootFileToWrite(
67 TString loc_fname,
68 int verbosityLevel
69 )
70{
71 TFile* output_rootfile = new TFile(loc_fname,"UPDATE");
72 if (output_rootfile->IsZombie())
73 {
74 cout << "Error opening file" << endl;
75 exit(-1);
76 }
77 else
78 {
79 if (verbosityLevel > 1)
80 {
81 cout << "...opening root-file:"
82 << loc_fname
83 << " successfull" << endl;
84 }
85 }
86 return output_rootfile;
87}
88//end of WriteInRootFile
89//----------------------------------------------------------------------------
90
91
92void
93CloseRootFile(
94 TFile* output_rootfile
95 )
96{
97 if (output_rootfile->IsZombie())
98 {
99 cout << "Error closing file" << endl;
100 exit(-1);
101 } else {
102 output_rootfile->Close();
103 }
104 delete output_rootfile;
105}
106//end of CloseRootFile
107//----------------------------------------------------------------------------
108
109
110void
111SaveHistograms(
112 TString loc_fname,
113 TString subdirectory,
114 TList* histList,
115 bool saveResults,
116 int verbosityLevel
117 )
118{
119
120 if (!saveResults) return;
121 if (histList==NULL) return;
122 if (verbosityLevel > 2) cout << endl
123 << "...saving pixel histograms to subdirectory: "
124 << subdirectory << endl ;
125
126 TFile* output_rootfile = NULL;
127 output_rootfile = ChooseRootFileToWrite( loc_fname, verbosityLevel);
128
129 if (verbosityLevel > 2) cout << endl
130 << "...writing histogram list to root file "
131 << endl ;
132 gFile->mkdir(subdirectory.Data());
133 gFile->cd(subdirectory.Data());
134 if (verbosityLevel > 1)
135 {
136 gDirectory->pwd();
137 }
138// cout << histList;
139 for ( int idx = 0; idx < histList->GetSize(); idx++)
140 {
141 histList->At(idx)->Write();
142 }
143// gFile->WriteTObject(histList, subdirectory); // write the histograms into one Key in the top level directory
144 gFile->cd("..");
145
146 if (verbosityLevel > 3) output_rootfile->ls();
147 CloseRootFile( output_rootfile ); // close the file
148 if (verbosityLevel > 2) cout << "...done" << endl;
149}
150// end of SaveHistograms
151//----------------------------------------------------------------------------
152
153TString
154CreateSubDirName(
155 int pixel
156 )
157{
158 TString path = "Pixel_";
159 path += pixel;
160// cout << "path " << path << endl ;
161 return path;
162}
163// end of CreateSubDirName
164//----------------------------------------------------------------------------
165
166
167TString
168CreateSubDirName(
169 TString title
170 )
171{
172 title += "_Pixel";
173// cout << "path " << path << endl ;
174 return title;
175}
176// end of CreateSubDirName
177//----------------------------------------------------------------------------
178
179TFile*
180OpenRootFile(
181 TString path,
182 TString filename,
183 int verbosityLevel
184 )
185{
186 if (verbosityLevel > 1)
187 {
188 cout << "...opening root-file: ";
189 }
190 path += filename;
191 TFile* file = TFile::Open( path );
192
193 if (verbosityLevel > 1)
194 {
195 cout << path ;
196 cout << " ...successfull" << endl;
197 }
198 return file;
199}
200
201TFile*
202LoadRootFile(
203 TString path,
204 TString filename,
205 int verbosityLevel
206 )
207{
208 if (verbosityLevel > 1)
209 {
210 cout << "...loading root-file: ";
211 }
212 path += filename;
213 TFile* file = TFile::Open( path );
214
215 if (file->IsZombie())
216 {
217 cerr << "file does not exist" << endl;
218 delete file;
219 return NULL;
220 }
221 else
222 if (verbosityLevel > 1)
223 {
224 cout << path ;
225 cout << " ...successfull" << endl;
226 }
227 return file;
228}
229
230TString
231SetHostsPaths(
232 bool isInHomeDir,
233 TString path
234 )
235{
236 TString hostName = gSystem->HostName();
237 TString dataDirectory;
238 TString homeDirectory;
239
240 TString temp_filename;
241
242 if ( hostName.Contains("isdc") ) //IF ONE IS WORKING AT ISDC
243 {
244 cout << "computing site is ISDC" << endl;
245 dataDirectory = "/fact/";
246 homeDirectory = "/home_nfs/isdc/jbbuss/";
247 }
248 else if ( hostName.Contains("hpc")||hostName.Contains("node") ) //IF ONE IS WORKING AT PHIDO
249 {
250 cout << "computing site is PhiDO" << endl;
251 dataDirectory = "/fhgfs/groups/app/fact-construction/";
252 homeDirectory = "/fhgfs/users/jbuss/";
253 }
254 else
255 {
256 exit(-1);
257 }
258 if (!isInHomeDir)
259 {
260 temp_filename = dataDirectory;
261 temp_filename += path;
262 path = temp_filename;
263 }
264 else if (isInHomeDir)
265 {
266 temp_filename = homeDirectory;
267 temp_filename += path;
268 path = temp_filename;
269 }
270
271 return path;
272}
Note: See TracBrowser for help on using the repository browser.