1 |
// $Id: Analysis.cc,v 1.3 2008/06/05 07:54:21 loizides Exp $
|
2 |
|
3 |
#include "MitAna/TreeMod/interface/Analysis.h"
|
4 |
#include <Riostream.h>
|
5 |
#include <TFile.h>
|
6 |
#include <TList.h>
|
7 |
#include <TDSet.h>
|
8 |
#include <TChain.h>
|
9 |
#include <TObjString.h>
|
10 |
#include <TSystem.h>
|
11 |
#include <TProof.h>
|
12 |
#include <TROOT.h>
|
13 |
#include <TBrowser.h>
|
14 |
#include "MitAna/DataUtil/interface/Debug.h"
|
15 |
#include "MitAna/DataTree/interface/Names.h"
|
16 |
#include "MitAna/TAM/interface/TAMVirtualLoader.h"
|
17 |
#include "MitAna/TAM/interface/TAModule.h"
|
18 |
#include "MitAna/TAM/interface/TAMSelector.h"
|
19 |
|
20 |
ClassImp(mithep::Analysis)
|
21 |
|
22 |
using namespace mithep;
|
23 |
|
24 |
//__________________________________________________________________________________________________
|
25 |
Analysis::Analysis(Bool_t useproof)
|
26 |
: fUseProof(useproof),
|
27 |
fHierachy(kTRUE),
|
28 |
fState(kPristine),
|
29 |
fNFriends(0),
|
30 |
fList(new TList),
|
31 |
fOutput(0),
|
32 |
fPackages(new TList),
|
33 |
fLoaders(new TList),
|
34 |
fSuperMod(0),
|
35 |
fSelector(0),
|
36 |
fChain(0),
|
37 |
fSet(0),
|
38 |
fDeleteList(new TList),
|
39 |
fTreeName(Names::gkMitTreeName),
|
40 |
fCompLevel(2),
|
41 |
fProof(0)
|
42 |
{
|
43 |
// Default constructor.
|
44 |
|
45 |
fList->SetOwner();
|
46 |
fDeleteList->SetOwner();
|
47 |
|
48 |
/* default packages for PROOF upload */
|
49 |
fPackages->SetOwner();
|
50 |
// nothing to be done since we do not use par files (yet?)
|
51 |
}
|
52 |
|
53 |
//__________________________________________________________________________________________________
|
54 |
Analysis::~Analysis()
|
55 |
{
|
56 |
// Destructor.
|
57 |
|
58 |
if (fState == kInit || fState == kRun)
|
59 |
Terminate();
|
60 |
|
61 |
delete fList;
|
62 |
delete fDeleteList;
|
63 |
delete fLoaders;
|
64 |
delete fSelector;
|
65 |
delete fPackages;
|
66 |
fOutput = 0; // owned by TAM
|
67 |
fSuperMod = 0; // owned by user
|
68 |
|
69 |
delete fProof;
|
70 |
}
|
71 |
|
72 |
//__________________________________________________________________________________________________
|
73 |
Bool_t Analysis::AddFile(const char *pname)
|
74 |
{
|
75 |
// Add file with given name to the list of files to be processed.
|
76 |
// Using the token "|", you can specify an arbritray number
|
77 |
// of paths to tree files that will be concatenated as friend
|
78 |
// trees.
|
79 |
|
80 |
if (fState != kPristine) {
|
81 |
Error("AddFile", "Analysis already initialized");
|
82 |
return kFALSE;
|
83 |
}
|
84 |
|
85 |
TString pnamestr(pname);
|
86 |
TString tok("|");
|
87 |
TObjArray *arr = pnamestr.Tokenize(tok);
|
88 |
TString msg;
|
89 |
|
90 |
for(Int_t i=0; i<arr->GetEntries(); i++){
|
91 |
|
92 |
TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
|
93 |
if(!dummy) continue;
|
94 |
|
95 |
AddFile(dummy->GetName(),i);
|
96 |
if(i==0) msg=dummy->GetName();
|
97 |
else {
|
98 |
Info("AddFile", "Add file %s as friend to %s",
|
99 |
dummy->GetName(), msg.Data());
|
100 |
}
|
101 |
}
|
102 |
delete arr;
|
103 |
|
104 |
return kTRUE;
|
105 |
}
|
106 |
|
107 |
//________________________________________________________________________
|
108 |
void Analysis::AddFile(const char *pname, Int_t eventlist)
|
109 |
{
|
110 |
// Add file name to the event list specified by eventlist. The lists
|
111 |
// are used to hold filenames of different types of events. In case
|
112 |
// you dont want friend trees, just give no eventlist argument (default 0).
|
113 |
|
114 |
MitAssert("AddFile", pname != 0);
|
115 |
|
116 |
TList *l = 0;
|
117 |
if (eventlist >= 0 && eventlist < fNFriends) {
|
118 |
|
119 |
l = dynamic_cast<TList*>(fList->At(eventlist));
|
120 |
if (!l) {
|
121 |
Fatal("AddFile", "Requested list %d not found!", eventlist);
|
122 |
return;
|
123 |
}
|
124 |
|
125 |
} else if (eventlist == fNFriends) {
|
126 |
|
127 |
l = new TList;
|
128 |
l->SetOwner();
|
129 |
fList->Add(l);
|
130 |
fNFriends++;
|
131 |
|
132 |
} else if (eventlist < 0 || eventlist > fNFriends) {
|
133 |
Error("AddFile", "Specified list %d not in [0,%d]", eventlist, fNFriends);
|
134 |
return;
|
135 |
}
|
136 |
|
137 |
if(!IsValidName(pname)) return;
|
138 |
|
139 |
l->Add(new TObjString(pname));
|
140 |
|
141 |
MDB(kAnalysis, 2)
|
142 |
Info("AddFile", "Added %s to list of files.", pname);
|
143 |
}
|
144 |
|
145 |
//__________________________________________________________________________________________________
|
146 |
void Analysis::AddFile(const TObject *oname, Int_t eventlist)
|
147 |
{
|
148 |
// Add file name to the event list specified by eventlist. The lists
|
149 |
// are used to hold filenames of different types of events. In case
|
150 |
// you dont want mixing, just give no eventlist argument (default 0).
|
151 |
|
152 |
MitAssert("AddFile", oname != 0);
|
153 |
|
154 |
return AddFile(oname->GetName(), eventlist);
|
155 |
}
|
156 |
|
157 |
//__________________________________________________________________________________________________
|
158 |
void Analysis::AddList(TList *list, Int_t eventlist)
|
159 |
{
|
160 |
// Add file name to the event list specified by eventlist. The lists
|
161 |
// are used to hold filenames of different types of events. In case
|
162 |
// you dont want mixing, just give no eventlist argument (default 0).
|
163 |
|
164 |
MitAssert("AddList", list != 0);
|
165 |
|
166 |
TIter next(list);
|
167 |
while (TObject *obj = next())
|
168 |
AddFile(obj->GetName(), eventlist);
|
169 |
}
|
170 |
|
171 |
//__________________________________________________________________________________________________
|
172 |
void Analysis::AddLoader(TAMVirtualLoader *l)
|
173 |
{
|
174 |
// Add loader to the list of loaders.
|
175 |
|
176 |
fLoaders->Add(l);
|
177 |
}
|
178 |
|
179 |
//__________________________________________________________________________________________________
|
180 |
void Analysis::AddPackage(const char* name)
|
181 |
{
|
182 |
// Add package to the list of uploaded packages.
|
183 |
|
184 |
MitAssert("AddPackage", name != 0);
|
185 |
fPackages->Add(new TObjString(name));
|
186 |
}
|
187 |
|
188 |
//__________________________________________________________________________________________________
|
189 |
void Analysis::AddPackages(TList *list)
|
190 |
{
|
191 |
// Add list of packages to the list of uploaded packages.
|
192 |
|
193 |
MitAssert("AddPackage", list != 0);
|
194 |
|
195 |
TIter next(list);
|
196 |
while (TObject *obj = next()) {
|
197 |
fPackages->Add(new TObjString(obj->GetName()));
|
198 |
}
|
199 |
}
|
200 |
|
201 |
//__________________________________________________________________________________________________
|
202 |
Bool_t Analysis::Init()
|
203 |
{
|
204 |
// Setup the TDSet and TChain to be used for the analysis
|
205 |
// with or without PROOF. If more than one list of
|
206 |
// file names was given, friend trees are supported.
|
207 |
|
208 |
if (fState == kRun || fState == kInit) {
|
209 |
Error("Init", "Init in state %d is not possible! Call Terminate() first.",
|
210 |
Int_t(fState));
|
211 |
return kFALSE;
|
212 |
}
|
213 |
|
214 |
if (fNFriends <= 0) {
|
215 |
Error("Init", "List of friend lists is empty!");
|
216 |
return kFALSE;
|
217 |
}
|
218 |
|
219 |
if (!fSuperMod) {
|
220 |
Error("Init", "Top-level TAM module is NULL!");
|
221 |
return kFALSE;
|
222 |
}
|
223 |
|
224 |
if (fUseProof) { // first init our PROOF session
|
225 |
if (!InitProof()) return kFALSE;
|
226 |
}
|
227 |
|
228 |
// we do this here instead in Terminate() so that
|
229 |
// we can browse the output even after Terminate()
|
230 |
delete fSelector;
|
231 |
fSelector = 0;
|
232 |
|
233 |
fChain = new TChain(fTreeName);
|
234 |
fSet = new TDSet("TTree",fTreeName);
|
235 |
|
236 |
for(Int_t i=0; i<fNFriends; i++){
|
237 |
|
238 |
TList *l = dynamic_cast<TList*>(fList->At(i));
|
239 |
if (!l) {
|
240 |
Fatal("Init", "List %d not found!", i);
|
241 |
return kFALSE;
|
242 |
}
|
243 |
|
244 |
if (i == 0) {
|
245 |
TIter next(l);
|
246 |
while ( TObjString *obj = dynamic_cast<TObjString*>(next()) ) {
|
247 |
fChain->Add(obj->GetName());
|
248 |
fSet->Add(obj->GetName());
|
249 |
}
|
250 |
|
251 |
} else {
|
252 |
|
253 |
TChain *chain = new TChain(fTreeName);
|
254 |
TDSet *set = new TDSet("TTree",fTreeName);
|
255 |
|
256 |
TIter next(l);
|
257 |
while (TObjString *obj = dynamic_cast<TObjString*>(next())) {
|
258 |
chain->Add(obj->GetName());
|
259 |
set->Add(obj->GetName());
|
260 |
}
|
261 |
|
262 |
TString alias("TAMTREE_"); // aliases currently not used
|
263 |
alias+=i;
|
264 |
|
265 |
fChain->AddFriend(chain,alias.Data());
|
266 |
fSet->AddFriend(set,alias.Data());
|
267 |
|
268 |
fDeleteList->Add(chain);
|
269 |
fDeleteList->Add(set);
|
270 |
}
|
271 |
|
272 |
}
|
273 |
|
274 |
// if we had our default TAM plugin we would create it here
|
275 |
// TreeLoader *bl = new TreeLoader;
|
276 |
// fLoaders->Add(bl);
|
277 |
// fDeleteList->Add(bl);
|
278 |
|
279 |
if (fUseProof) {
|
280 |
|
281 |
fProof->AddInput(fSuperMod);
|
282 |
fLoaders->SetName("TAM_LOADERS");
|
283 |
fProof->AddInput(fLoaders);
|
284 |
|
285 |
} else {
|
286 |
|
287 |
// when not running Proof, we must make a selector
|
288 |
fSelector = new TAMSelector;
|
289 |
fSelector->AddInput(fSuperMod);
|
290 |
MDB(kAnalysis, 2)
|
291 |
fSelector->SetVerbosity(1);
|
292 |
|
293 |
// pass loaders to selector
|
294 |
TIter next(fLoaders);
|
295 |
while ( TAMVirtualLoader *l = dynamic_cast<TAMVirtualLoader*>(next()) )
|
296 |
fSelector->AddLoader(l);
|
297 |
}
|
298 |
|
299 |
fState = kInit;
|
300 |
return kTRUE;
|
301 |
}
|
302 |
|
303 |
//__________________________________________________________________________________________________
|
304 |
Bool_t Analysis::InitProof()
|
305 |
{
|
306 |
// Initialize PROOF connection.
|
307 |
|
308 |
if(fProof && fProof->IsValid())
|
309 |
return kTRUE;
|
310 |
|
311 |
delete fProof;
|
312 |
|
313 |
if (fMaster.Contains("rcf.bnl.gov")) {
|
314 |
for(Int_t i=0;i<5;i++) {
|
315 |
Warning("InitProof", "*** DID YOU RUN PROOF_KINIT? %d (5) ***", i);
|
316 |
gSystem->Sleep(1000);
|
317 |
}
|
318 |
}
|
319 |
|
320 |
MDB(kAnalysis, 1)
|
321 |
Info("InitProof", "Starting PROOF on master %s with config %s",
|
322 |
fMaster.Data(), fConfig.Data());
|
323 |
|
324 |
fProof = dynamic_cast<TProof*>(TProof::Open(fMaster, fConfig));
|
325 |
if (!fProof) {
|
326 |
Error("InitProof", "Could not start PROOF!");
|
327 |
return kFALSE;
|
328 |
}
|
329 |
|
330 |
MDB(kAnalysis, 3)
|
331 |
gROOT->Print();
|
332 |
|
333 |
//fProof->AddInput(new TNamed("PROOF_NewPacketizer",""));
|
334 |
|
335 |
Bool_t ret=kTRUE;
|
336 |
if (fPackages) {
|
337 |
// tell Proof what additional libraries we will need on each worker computer
|
338 |
ret = UploadPackages(fPackages);
|
339 |
}
|
340 |
|
341 |
return ret;
|
342 |
}
|
343 |
|
344 |
//__________________________________________________________________________________________________
|
345 |
void Analysis::Run()
|
346 |
{
|
347 |
// Run the analysis on the created file set.
|
348 |
|
349 |
if (fState == kPristine || fState == kRun) {
|
350 |
Error("Run", "Run in state %d is not possible! Call Init() first.",
|
351 |
Int_t(fState));
|
352 |
}
|
353 |
|
354 |
if (fUseProof) {
|
355 |
|
356 |
MDB(kAnalysis, 1)
|
357 |
Info("Run", "Start processing with PROOF...");
|
358 |
|
359 |
fSet->Process("TAMSelector");
|
360 |
|
361 |
} else {
|
362 |
|
363 |
MDB(kAnalysis, 1)
|
364 |
Info("Run", "Start processing (no PROOF)...");
|
365 |
|
366 |
fChain->Process(fSelector);
|
367 |
}
|
368 |
|
369 |
MDB(kAnalysis, 1)
|
370 |
Info("Run", "Processing complete!");
|
371 |
|
372 |
fState = kRun;
|
373 |
}
|
374 |
|
375 |
//__________________________________________________________________________________________________
|
376 |
Bool_t Analysis::Run(Bool_t browse)
|
377 |
{
|
378 |
// Execute analysis and open TBrowser if requested.
|
379 |
|
380 |
if (Init()) {
|
381 |
Run();
|
382 |
Terminate();
|
383 |
if(browse) {
|
384 |
new TBrowser;
|
385 |
}
|
386 |
return kTRUE;
|
387 |
}
|
388 |
|
389 |
Error("Execute", "Could not initialize analysis.");
|
390 |
return kFALSE;
|
391 |
}
|
392 |
|
393 |
//__________________________________________________________________________________________________
|
394 |
void Analysis::Terminate()
|
395 |
{
|
396 |
// Terminate current analysis run.
|
397 |
|
398 |
if (fState == kPristine || fState == kTerminate) {
|
399 |
Error("Terminate", "Terminate in state %d is not possible! Call Init() first.",
|
400 |
Int_t(fState));
|
401 |
return;
|
402 |
}
|
403 |
|
404 |
if (fState == kRun) {
|
405 |
|
406 |
if (fUseProof) {
|
407 |
// the output list from Proof can (in principal) contain other objects
|
408 |
// besides the module output hierarchy.
|
409 |
TList* outputlist = fProof->GetOutputList();
|
410 |
TIter nextOut(outputlist);
|
411 |
while (TObject *obj = nextOut()) {
|
412 |
if (obj->InheritsFrom(TAMOutput::Class())) {
|
413 |
fOutput = dynamic_cast<TList*>(obj);
|
414 |
break;
|
415 |
}
|
416 |
}
|
417 |
|
418 |
} else {
|
419 |
fOutput = fSelector->GetModOutput();
|
420 |
}
|
421 |
|
422 |
if (fOutput && !fAnaOutput.IsNull()) {
|
423 |
TDirectory::TContext context(0); // automatically restore gDirectory
|
424 |
|
425 |
std::auto_ptr<TFile> outf(TFile::Open(fAnaOutput,"recreate","", fCompLevel));
|
426 |
if (outf.get() == 0) {
|
427 |
Error("Terminate", "Could not open file %s for output!", fAnaOutput.Data());
|
428 |
} else {
|
429 |
MDB(kAnalysis, 1)
|
430 |
Info("Terminate", "Saving output to %s!", fAnaOutput.Data());
|
431 |
|
432 |
if (fHierachy)
|
433 |
fOutput->Write(fOutput->GetName(),TObject::kSingleKey);
|
434 |
else
|
435 |
fOutput->Write();
|
436 |
|
437 |
}
|
438 |
}
|
439 |
}
|
440 |
|
441 |
delete fChain;
|
442 |
delete fSet;
|
443 |
fDeleteList->Delete();
|
444 |
|
445 |
fState = kTerminate;
|
446 |
}
|
447 |
|
448 |
//__________________________________________________________________________________________________
|
449 |
Bool_t Analysis::UploadPackages(TList *packages)
|
450 |
{
|
451 |
// Upload list of par files to the server.
|
452 |
|
453 |
MitAssert("UploadPackages", packages != 0);
|
454 |
|
455 |
for(Int_t i=0; i < packages->GetEntries(); i++) {
|
456 |
|
457 |
TObject* objstr = packages->At(i);
|
458 |
if (!objstr){
|
459 |
Error("InitProof", "Problem at package number %d!", i);
|
460 |
return kFALSE;
|
461 |
}
|
462 |
|
463 |
TString packname = objstr->GetName();
|
464 |
Int_t en = 0;
|
465 |
if (packname.EndsWith("+")) {
|
466 |
en=1;
|
467 |
packname.Resize(packname.Length()-1);
|
468 |
}
|
469 |
|
470 |
ifstream ftest(gSystem->ExpandPathName(packname.Data()),ios_base::binary);
|
471 |
if (!ftest.good()){
|
472 |
Error("InitProof", "Could not open %s for upload!", packname.Data());
|
473 |
return kFALSE;
|
474 |
}
|
475 |
|
476 |
if(fProof->UploadPackage(packname)<0) {
|
477 |
Error("UploadPackage", "Upload for %s failed!", packname.Data());
|
478 |
return kFALSE;
|
479 |
}
|
480 |
|
481 |
if (en == 1) {
|
482 |
Int_t pos=packname.Last('/')+1;
|
483 |
if (pos) packname.Remove(0,pos);
|
484 |
if(fProof->EnablePackage(packname)<0) {
|
485 |
Error("UploadPackage", "Enabling for %s failed!", packname.Data());
|
486 |
return kFALSE;
|
487 |
}
|
488 |
}
|
489 |
}
|
490 |
|
491 |
return kTRUE;
|
492 |
}
|