ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/entryCount.C
Revision: 1.1
Committed: Sat Sep 6 18:52:41 2008 UTC (16 years, 8 months ago) by anderson
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Auto-counts entries in histograms in a file

File Contents

# Content
1 /*************************************************
2 Counts the number of entries in a histogram in
3 a root file.
4
5 Can be used in three ways, depending on what is
6 given:
7
8 1) A root file name:
9 List the entries in all the histograms in the file
10
11 2) A root file name & a histogram name:
12 Just list the entries in one histgram from one file
13
14 3) A histogram name:
15 Run over all root files in a directory and
16 find the number of entries in a given histogram.
17
18 Michael B. Anderson
19 April 15, 2008
20 *************************************************/
21
22 #include <TFile.h>
23 #include <TObjString.h>
24 #include <iomanip.h>
25
26 // Figure out if we were given
27 // a root file name or a histogram name
28 // and call the correct function.
29 void entryCount(TString argument) {
30
31 if ( argument.Contains(".root") ) {
32 countEntriesInFile(argument);
33 } else {
34 entryCountInAllFiles(argument);
35 }
36 }
37
38
39 // List the entries in all the histograms in the file
40 void countEntriesInFile(TString fileName) {
41
42 // Open the File
43 TFile f(fileName);
44
45 TH1 *h;
46 TTree *t;
47
48 TKey *key;
49 TIter nextkey(gDirectory->GetListOfKeys());
50
51 cout << setw(29) << " Object " << " entries" << endl;
52 cout << setw(29) << "---------" << " -------" << endl;
53
54 while (key = (TKey*)nextkey()) {
55
56 obj = key->ReadObj();
57
58 if (obj->IsA()->InheritsFrom("TH1")) {
59
60 h = (TH1*)obj;
61 TString histName = h->GetName();
62 Long64_t countOfEntries = h->GetEntries();
63
64 cout << "TH1 : " << setw(20) << histName << " " << countOfEntries << endl;
65
66 } else if (obj->IsA()->InheritsFrom("TNtuple")) {
67
68 t = (TTree*)obj;
69 TString histName = t->GetName();
70 Long64_t countOfEntries = t->GetEntries();
71
72 cout << "TNtuple: " << setw(20) << histName << " " << countOfEntries << endl;
73
74 }
75
76 } // End of loop over all keys in file
77 f.Close();
78 }
79
80
81 // Just list the entries in one histgram from one file
82 void entryCount(TString fileName, TString histName) {
83 // Open the File
84 TFile f(fileName);
85
86 TObject *obj = f.Get(histName);
87
88 if ( !obj ) {
89 cout << fileName << " does not contain " << histName << endl;
90 return;
91 }
92
93 if (obj->IsA()->InheritsFrom("TH1")) {
94
95 h = (TH1*)obj;
96 TString histName = h->GetName();
97 Long64_t countOfEntries = h->GetEntries();
98
99 cout << "TH1 : " << setw(20) << histName << " " << countOfEntries << endl;
100
101 } else if (obj->IsA()->InheritsFrom("TNtuple")) {
102
103 t = (TTree*)obj;
104 TString histName = t->GetName();
105 Long64_t countOfEntries = t->GetEntries();
106
107 cout << "TNtuple: " << setw(20) << histName << " " << countOfEntries << endl;
108
109 }
110 }
111
112
113 // Run over all root files in a directory and
114 // find the number of entries in a given histogram.
115 void entryCountInAllFiles(TString histName) {
116
117 TString dirname = "."; // . = this directory
118 TString pattern = ".root"; // match only root files
119
120 cout << "Files in " << dirname << " that match '" << pattern << "'" << endl;
121 cout << "Entries in histograms that match '" << histName << "'" << endl;
122
123 void *dir = gSystem->OpenDirectory(dirname);
124
125 if (!dir) {
126 cerr << "couldn't open directory" << dirname << endl;
127 return 0;
128 }
129
130 TObjArray* fileList = new TObjArray;
131 const char* file;
132 TString fileName;
133 TRegexp regexp(pattern);
134
135 Int_t maxFileNameLength = 10;
136
137 while ( (file = gSystem->GetDirEntry(dir)) ) {
138
139 fileName = file;
140
141 if (!fileName.CompareTo(".") || !fileName.CompareTo("..")) continue;
142
143 // Check if the filename matches the regexp
144 if (fileName.Index(regexp) != kNPOS) {
145
146 // if it does, add to list
147 fileList->Add(new TObjString(fileName));
148 if ( fileName.Length() > maxFileNameLength ) {
149 maxFileNameLength = fileName.Length();
150 }
151
152 }
153
154 }
155
156 gSystem->FreeDirectory(dir);
157
158
159
160 TIter next(fileList);
161 TObjString* fn;
162
163 cout << setw(maxFileNameLength+1) << "Filename" << " entries" << endl;
164 cout << setw(maxFileNameLength+1) << "--------" << " -------" << endl;
165
166 while ( fn = (TObjString*)next() ) {
167
168 TString s = fn->GetString();
169 TFile* f = TFile::Open( s );
170
171 if ( !f ) continue;
172
173 TObject *obj = f->Get(histName);
174
175 if ( !obj ) {
176 cout << setw(maxFileNameLength+1) << s << " does not contain " << histName << endl;
177 continue;
178 }
179
180 // ********************************************
181 // ** Change what you want in here to do
182 // whatever to the files.
183 if (obj->IsA()->InheritsFrom("TH1")) {
184
185 h = (TH1*)obj;
186 TString histName = h->GetName();
187 Long64_t countOfEntries = h->GetEntries();
188
189 cout << setw(maxFileNameLength+1) << s << " " << countOfEntries << endl;
190
191 } else if (obj->IsA()->InheritsFrom("TNtuple")) {
192 t = (TTree*)obj;
193 TString histName = t->GetName();
194 Long64_t countOfEntries = t->GetEntries();
195
196 cout << setw(maxFileNameLength+1) << s << " " << countOfEntries << endl;
197 }
198 // ********************************************
199
200
201 f->Close();
202 }
203 }
204