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

Last change on this file since 13852 was 13787, checked in by Jens Buss, 13 years ago
changed all savelists from objarray to lists, add SetHostsPath function
File size: 5.5 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 gDirectory->pwd();
135 cout << histList;
136 for ( int idx = 0; idx < histList->GetSize(); idx++)
137 {
138 histList->At(idx)->Write();
139 }
140// gFile->WriteTObject(histList, subdirectory); // write the histograms into one Key in the top level directory
141 gFile->cd("..");
142
143 if (verbosityLevel > 3) output_rootfile->ls();
144 CloseRootFile( output_rootfile ); // close the file
145 if (verbosityLevel > 2) cout << "...done" << endl;
146}
147// end of SaveHistograms
148//----------------------------------------------------------------------------
149
150
151TString
152CreateSubDirName(
153 int pixel
154 )
155{
156 TString path = "Pixel_";
157 path += pixel;
158// cout << "path " << path << endl ;
159 return path;
160}
161// end of CreateSubDirName
162//----------------------------------------------------------------------------
163
164
165TString
166CreateSubDirName(
167 TString title
168 )
169{
170 title += "_Pixel";
171// cout << "path " << path << endl ;
172 return title;
173}
174// end of CreateSubDirName
175//----------------------------------------------------------------------------
176
177TFile*
178OpenRootFile(
179 TString path,
180 TString filename,
181 int verbosityLevel
182 )
183{
184 if (verbosityLevel > 1)
185 {
186 cout << "...opening root-file: ";
187 }
188 path += filename;
189 TFile* file = TFile::Open( path );
190
191 if (verbosityLevel > 1)
192 {
193 cout << path ;
194 cout << " ...successfull" << endl;
195 }
196 return file;
197}
198
199TString
200SetHostsPaths(
201 bool isInHomeDir,
202 TString path
203 )
204{
205 TString hostName = gSystem->HostName();
206 TString dataDirectory;
207 TString homeDirectory;
208
209 TString temp_filename;
210
211 if ( hostName.Contains("isdc") ) //IF ONE IS WORKING AT ISDC
212 {
213
214 dataDirectory = "/fact/";
215 homeDirectory = "/home_nfs/isdc/jbbuss/";
216 }
217 if ( hostName.Contains("hpc")||hostName.Contains("node") ) //IF ONE IS WORKING AT PHIDO
218 {
219
220 dataDirectory = "/fhgfs/groups/app/fact-construction/";
221 homeDirectory = "/home/jbuss/";
222 }
223 else
224 {
225 exit(-1);
226 }
227 if (!isInHomeDir)
228 {
229 temp_filename = dataDirectory;
230 temp_filename += path;
231 path = temp_filename;
232 }
233 else if (isInHomeDir)
234 {
235 temp_filename = homeDirectory;
236 temp_filename += path;
237 path = temp_filename;
238 }
239
240 return path;
241}
Note: See TracBrowser for help on using the repository browser.