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

Last change on this file since 13782 was 13737, checked in by Jens Buss, 13 years ago
possibility to define if output file will be updated or recreated
File size: 4.3 KB
Line 
1#include <iostream>
2#include "TObjArray.h"
3
4#include "rootfilehandler.h"
5#include <stdlib.h>
6using namespace std;
7
8/*
9 POSSIBLE IMPROVEMENT:
10
11 creat files on the heap and handover pointers
12
13 */
14
15
16
17
18void
19CreateRootFile(
20 TString loc_fname,
21 bool overwrite,
22 int verbosityLevel
23 )
24{
25 TFile* output_rootfile;
26
27 if(!overwrite)
28 {
29 output_rootfile = new TFile(loc_fname);
30 if (output_rootfile->IsZombie())
31 {
32 cout << "file does not exist...creating file" << endl;
33 delete output_rootfile;
34 output_rootfile = new TFile(loc_fname,"NEW");
35 }
36 else
37 {
38 cout << "file does exist and will be updated" << endl;
39 output_rootfile->OpenFile(loc_fname,"UPDATE");
40 }
41 }
42 else if(overwrite)
43 {
44 cout << "overwriting file" << endl;
45 output_rootfile = new TFile(loc_fname,"RECREATE");
46 }
47
48 if (output_rootfile->IsZombie())
49 {
50 cout << "Error opening file" << endl;
51 exit(-1);
52 }
53 else {
54 if (verbosityLevel > 1) cout << "creating root-file...successfull" << endl;
55 output_rootfile->pwd();
56 output_rootfile->Close();
57 delete output_rootfile;
58 }
59}
60//end of CreateRootFile
61//----------------------------------------------------------------------------
62
63
64TFile*
65ChooseRootFileToWrite(
66 TString loc_fname,
67 int verbosityLevel
68 )
69{
70 TFile* output_rootfile = new TFile(loc_fname,"UPDATE");
71 if (output_rootfile->IsZombie())
72 {
73 cout << "Error opening file" << endl;
74 exit(-1);
75 }
76 else
77 {
78 if (verbosityLevel > 1)
79 {
80 cout << "...opening root-file:"
81 << loc_fname
82 << " successfull" << endl;
83 }
84 }
85 return output_rootfile;
86}
87//end of WriteInRootFile
88//----------------------------------------------------------------------------
89
90
91void
92CloseRootFile(
93 TFile* output_rootfile
94 )
95{
96 if (output_rootfile->IsZombie())
97 {
98 cout << "Error closing file" << endl;
99 exit(-1);
100 } else {
101 output_rootfile->Close();
102 }
103 delete output_rootfile;
104}
105//end of CloseRootFile
106//----------------------------------------------------------------------------
107
108
109void
110SaveHistograms(
111 TString loc_fname,
112 TString subdirectory,
113 TObjArray* histList,
114 bool saveResults,
115 int verbosityLevel
116 )
117{
118 if (!saveResults) return;
119 if (verbosityLevel > 2) cout << endl
120 << "...saving pixel histograms to subdirectory: "
121 << subdirectory << endl ;
122
123 TFile* output_rootfile = NULL;
124 output_rootfile = ChooseRootFileToWrite( loc_fname, verbosityLevel);
125
126 if (verbosityLevel > 2) cout << endl
127 << "...writing histogram list to root file "
128 << endl ;
129 gFile->WriteTObject(histList, subdirectory); // write the histograms into one Key in the top level directory
130
131
132 if (verbosityLevel > 3) output_rootfile->ls();
133 CloseRootFile( output_rootfile ); // close the file
134 if (verbosityLevel > 2) cout << "...done" << endl;
135}
136// end of SaveHistograms
137//----------------------------------------------------------------------------
138
139
140TString
141CreateSubDirName(
142 int pixel
143 )
144{
145 TString path = "Pixel_";
146 path += pixel;
147// cout << "path " << path << endl ;
148 return path;
149}
150// end of CreateSubDirName
151//----------------------------------------------------------------------------
152
153
154TString
155CreateSubDirName(
156 TString title
157 )
158{
159 title += "_Pixel";
160// cout << "path " << path << endl ;
161 return title;
162}
163// end of CreateSubDirName
164//----------------------------------------------------------------------------
165
166TFile*
167OpenRootFile(
168 TString path,
169 TString filename,
170 int verbosityLevel
171 )
172{
173 if (verbosityLevel > 1)
174 {
175 cout << "...opening root-file: ";
176 }
177
178 path += filename;
179 TFile* file = TFile::Open( path );
180
181 if (verbosityLevel > 1)
182 {
183 cout << path ;
184 cout << " ...successfull" << endl;
185 }
186 return file;
187}
Note: See TracBrowser for help on using the repository browser.