-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathProofSimpleFile.C
315 lines (276 loc) · 9.78 KB
/
ProofSimpleFile.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/// \file
/// \ingroup tutorial_ProofSimpleFile
///
/// Selector to fill a set of histograms and merging via file
///
/// \macro_code
///
/// \author Gerardo Ganis (gerardo.ganis@cern.ch)
#define ProofSimpleFile_cxx
#include "ProofSimpleFile.h"
#include <TCanvas.h>
#include <TFrame.h>
#include <TPaveText.h>
#include <TFormula.h>
#include <TF1.h>
#include <TH1F.h>
#include <TMath.h>
#include <TRandom3.h>
#include <TString.h>
#include <TStyle.h>
#include <TSystem.h>
#include <TParameter.h>
#include <TFile.h>
#include <TProofOutputFile.h>
//_____________________________________________________________________________
ProofSimpleFile::ProofSimpleFile()
{
// Constructor
fNhist = 16;
fHistTop = nullptr;
fHistDir = nullptr;
fRandom = nullptr;
fFile = nullptr;
fProofFile = nullptr;
fFileDir = nullptr;
}
//_____________________________________________________________________________
ProofSimpleFile::~ProofSimpleFile()
{
// Destructor
if (fRandom) delete fRandom;
}
//_____________________________________________________________________________
Int_t ProofSimpleFile::CreateHistoArrays()
{
// Create the histogram arrays
if (fNhist <= 0) {
Error("CreateHistoArrays", "fNhist must be positive!");
return -1;
}
// Histos array
fHistTop = new TH1F*[fNhist];
fHistDir = new TH1F*[fNhist];
// Done
return 0;
}
//_____________________________________________________________________________
void ProofSimpleFile::Begin(TTree * /*tree*/)
{
// The Begin() function is called at the start of the query.
// When running with PROOF Begin() is only called on the client.
// The tree argument is deprecated (on PROOF 0 is passed).
TString option = GetOption();
// Number of histograms (needed in terminate)
Ssiz_t iopt = kNPOS;
if (fInput->FindObject("ProofSimpleFile_NHist")) {
TParameter<Long_t> *p =
dynamic_cast<TParameter<Long_t>*>(fInput->FindObject("ProofSimpleFile_NHist"));
fNhist = (p) ? (Int_t) p->GetVal() : fNhist;
} else if ((iopt = option.Index("nhist=")) != kNPOS) {
TString s;
Ssiz_t from = iopt + strlen("nhist=");
if (option.Tokenize(s, from, ";") && s.IsDigit()) fNhist = s.Atoi();
}
}
//_____________________________________________________________________________
void ProofSimpleFile::SlaveBegin(TTree * /*tree*/)
{
// The SlaveBegin() function is called after the Begin() function.
// When running with PROOF SlaveBegin() is called on each slave server.
// The tree argument is deprecated (on PROOF 0 is passed).
TString option = GetOption();
// Number of histograms (needed in terminate)
Ssiz_t iopt = kNPOS;
if (fInput->FindObject("ProofSimpleFile_NHist")) {
TParameter<Long_t> *p =
dynamic_cast<TParameter<Long_t>*>(fInput->FindObject("ProofSimpleFile_NHist"));
fNhist = (p) ? (Int_t) p->GetVal() : fNhist;
} else if ((iopt = option.Index("nhist=")) != kNPOS) {
TString s;
Ssiz_t from = iopt + strlen("nhist=");
if (option.Tokenize(s, from, ";") && s.IsDigit()) fNhist = s.Atoi();
}
// The file for merging
fProofFile = new TProofOutputFile("SimpleFile.root", "M");
TNamed *out = (TNamed *) fInput->FindObject("PROOF_OUTPUTFILE");
if (out) fProofFile->SetOutputFileName(out->GetTitle());
TDirectory *savedir = gDirectory;
fFile = fProofFile->OpenFile("RECREATE");
if (fFile && fFile->IsZombie()) SafeDelete(fFile);
savedir->cd();
// Cannot continue
if (!fFile) {
TString amsg = TString::Format("ProofSimpleFile::SlaveBegin: could not create '%s':"
" instance is invalid!", fProofFile->GetName());
Abort(amsg, kAbortProcess);
return;
}
// Histos arrays
if (CreateHistoArrays() != 0) {
Abort("ProofSimpleFile::SlaveBegin: could not create histograms", kAbortProcess);
return;
}
// Create directory
if (!(fFileDir = fFile->mkdir("blue"))) {
Abort("ProofSimpleFile::SlaveBegin: could not create directory 'blue' in file!",
kAbortProcess);
return;
}
// Create the histograms
for (Int_t i=0; i < fNhist; i++) {
fHistTop[i] = new TH1F(Form("ht%d",i), Form("ht%d",i), 100, -3., 3.);
fHistTop[i]->SetFillColor(kRed);
fHistTop[i]->SetDirectory(fFile);
fHistDir[i] = new TH1F(Form("hd%d",i), Form("hd%d",i), 100, -3., 3.);
fHistDir[i]->SetFillColor(kBlue);
fHistDir[i]->SetDirectory(fFileDir);
}
// Set random seed
fRandom = new TRandom3(0);
}
//_____________________________________________________________________________
Bool_t ProofSimpleFile::Process(Long64_t)
{
// The Process() function is called for each entry in the tree (or possibly
// keyed object in the case of PROOF) to be processed. The entry argument
// specifies which entry in the currently loaded tree is to be processed.
// It can be passed to either ProofSimpleFile::GetEntry() or TBranch::GetEntry()
// to read either all or the required parts of the data. When processing
// keyed objects with PROOF, the object is already loaded and is available
// via the fObject pointer.
//
// This function should contain the "body" of the analysis. It can contain
// simple or elaborate selection criteria, run algorithms on the data
// of the event and typically fill histograms.
//
// The processing can be stopped by calling Abort().
//
// Use fStatus to set the return value of TTree::Process().
//
// The return value is currently not used.
for (Int_t i=0; i < fNhist; i++) {
if (fRandom && fHistTop[i] && fHistDir[i]) {
fHistTop[i]->Fill(fRandom->Gaus(0.,1.));
fHistDir[i]->Fill(fRandom->Gaus(0.,1.));
}
}
return kTRUE;
}
//_____________________________________________________________________________
void ProofSimpleFile::SlaveTerminate()
{
// The SlaveTerminate() function is called after all entries or objects
// have been processed. When running with PROOF SlaveTerminate() is called
// on each slave server.
// Write histos to file
if (fFile) {
Bool_t cleanup = kTRUE;
TDirectory *savedir = gDirectory;
fFile->cd();
for (Int_t i=0; i < fNhist; i++) {
if (fHistTop[i] && fHistTop[i]->GetEntries() > 0) {
fHistTop[i]->Write();
fHistTop[i]->SetDirectory(nullptr);
cleanup = kFALSE;
}
}
// Change to subdirectory
fFileDir->cd();
for (Int_t i=0; i < fNhist; i++) {
if (fHistDir[i] && fHistDir[i]->GetEntries() > 0) {
fHistDir[i]->Write();
fHistDir[i]->SetDirectory(nullptr);
cleanup = kFALSE;
}
}
gDirectory = savedir;
fFile->Close();
// Cleanup or register
if (cleanup) {
Info("SlaveTerminate", "nothing to save: just cleanup everything ...");
TUrl uf(*(fFile->GetEndpointUrl()));
SafeDelete(fFile);
gSystem->Unlink(uf.GetFile());
SafeDelete(fProofFile);
} else {
Info("SlaveTerminate", "objects saved into '%s%s': sending related TProofOutputFile ...",
fProofFile->GetFileName(), fProofFile->GetOptionsAnchor());
fProofFile->Print();
fOutput->Add(fProofFile);
}
}
}
//_____________________________________________________________________________
void ProofSimpleFile::Terminate()
{
// The Terminate() function is the last function to be called during
// a query. It always runs on the client, it can be used to present
// the results graphically or save the results to file.
// Get the histos from the file
if ((fProofFile =
dynamic_cast<TProofOutputFile*>(fOutput->FindObject("SimpleFile.root")))) {
TString outputFile(fProofFile->GetOutputFileName());
TString outputName(fProofFile->GetName());
outputName += ".root";
Printf("outputFile: %s", outputFile.Data());
// Read the ntuple from the file
if (!(fFile = TFile::Open(outputFile))) {
Error("Terminate", "could not open file: %s", outputFile.Data());
return;
}
} else {
Error("Terminate", "TProofOutputFile not found");
return;
}
// Histos arrays
if (CreateHistoArrays() != 0) {
Error("Terminate", "could not create histograms");
return;
}
// Top histos
PlotHistos(0);
// Dir histos
PlotHistos(1);
}
//_____________________________________________________________________________
void ProofSimpleFile::PlotHistos(Int_t opt)
{
// Plot the histograms ina dedicated canvas
// Create a canvas, with fNhist pads
if (opt == 0) {
TCanvas *c1 = new TCanvas("c1","ProofSimpleFile top dir canvas",200,10,700,700);
Int_t nside = (Int_t)TMath::Sqrt((Float_t)fNhist);
nside = (nside*nside < fNhist) ? nside+1 : nside;
c1->Divide(nside,nside,0,0);
for (Int_t i=0; i < fNhist; i++) {
fHistTop[i] = (TH1F *) fFile->Get(TString::Format("ht%d",i));
c1->cd(i+1);
if (fHistTop[i])
fHistTop[i]->Draw();
}
// Final update
c1->cd();
c1->Update();
} else if (opt == 1) {
TCanvas *c2 = new TCanvas("c2","ProofSimpleFile 'blue' sub-dir canvas",400,60,700,700);
Int_t nside = (Int_t)TMath::Sqrt((Float_t)fNhist);
nside = (nside*nside < fNhist) ? nside+1 : nside;
c2->Divide(nside,nside,0,0);
if ((fFileDir = (TDirectory *) fFile->Get("blue"))) {
for (Int_t i=0; i < fNhist; i++) {
fHistDir[i] = (TH1F *) fFileDir->Get(TString::Format("hd%d",i));
c2->cd(i+1);
if (fHistDir[i])
fHistDir[i]->Draw();
}
} else {
Error("PlotHistos", "directory 'blue' not found in output file");
}
// Final update
c2->cd();
c2->Update();
} else {
Error("PlotHistos", "unknown option: %d", opt);
}
}