1 |
// $Id: Analysis.cc,v 1.1 2008/05/27 19:50:16 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 "MitAna/DataUtil/interface/Debug.h"
|
14 |
#include "MitAna/DataTree/interface/Names.h"
|
15 |
#include "MitAna/TAM/interface/TAMVirtualLoader.h"
|
16 |
#include "MitAna/TAM/interface/TAModule.h"
|
17 |
#include "MitAna/TAM/interface/TAMSelector.h"
|
18 |
|
19 |
ClassImp(mithep::Analysis)
|
20 |
|
21 |
using namespace mithep;
|
22 |
|
23 |
//__________________________________________________________________________________________________
|
24 |
Analysis::Analysis(Bool_t up) :
|
25 |
fUseProof(up),
|
26 |
fHierachy(kTRUE),
|
27 |
fState(kPristine),
|
28 |
fNFriends(0),
|
29 |
fList(new TList),
|
30 |
fOutput(0),
|
31 |
fPackages(new TList),
|
32 |
fLoaders(new TList),
|
33 |
fSuperMod(0),
|
34 |
fSelector(0),
|
35 |
fChain(0),
|
36 |
fSet(0),
|
37 |
fDeleteList(new TList),
|
38 |
fTreeName(Names::mittree),
|
39 |
fCompLevel(2),
|
40 |
fProof(0)
|
41 |
{
|
42 |
// Default constructor.
|
43 |
|
44 |
fList->SetOwner();
|
45 |
fDeleteList->SetOwner();
|
46 |
|
47 |
/* default packages for PROOF upload */
|
48 |
fPackages->SetOwner();
|
49 |
// nothing to be done since we do not use par files (yet?)
|
50 |
}
|
51 |
|
52 |
//__________________________________________________________________________________________________
|
53 |
Analysis::~Analysis()
|
54 |
{
|
55 |
// Destructor.
|
56 |
|
57 |
if (fState == kInit || fState == kRun)
|
58 |
Terminate();
|
59 |
|
60 |
delete fList;
|
61 |
delete fDeleteList;
|
62 |
delete fLoaders;
|
63 |
delete fSelector;
|
64 |
delete fPackages;
|
65 |
fOutput = 0; // owned by TAM
|
66 |
fSuperMod = 0; // owned by user
|
67 |
|
68 |
delete fProof;
|
69 |
}
|
70 |
|
71 |
//__________________________________________________________________________________________________
|
72 |
Bool_t Analysis::AddFile(const char *pname)
|
73 |
{
|
74 |
// Add file with given name to the list of files to be processed.
|
75 |
// Using the token "|", you can specify an arbritray number
|
76 |
// of paths to tree files that will be concatenated as friend
|
77 |
// trees.
|
78 |
|
79 |
if (fState != kPristine) {
|
80 |
Error("AddFile", "Analysis already initialized");
|
81 |
return kFALSE;
|
82 |
}
|
83 |
|
84 |
TString pnamestr(pname);
|
85 |
TString tok("|");
|
86 |
TObjArray *arr = pnamestr.Tokenize(tok);
|
87 |
TString msg;
|
88 |
|
89 |
for(Int_t i=0; i<arr->GetEntries(); i++){
|
90 |
|
91 |
TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
|
92 |
if(!dummy) continue;
|
93 |
|
94 |
AddFile(dummy->GetName(),i);
|
95 |
if(i==0) msg=dummy->GetName();
|
96 |
else {
|
97 |
Info("AddFile", "Add file %s as friend to %s",
|
98 |
dummy->GetName(), msg.Data());
|
99 |
}
|
100 |
}
|
101 |
delete arr;
|
102 |
|
103 |
return kTRUE;
|
104 |
}
|
105 |
|
106 |
//________________________________________________________________________
|
107 |
void Analysis::AddFile(const char *pname, Int_t eventlist)
|
108 |
{
|
109 |
// Add file name to the event list specified by eventlist. The lists
|
110 |
// are used to hold filenames of different types of events. In case
|
111 |
// you dont want friend trees, just give no eventlist argument (default 0).
|
112 |
|
113 |
MitAssert("AddFile", pname != 0);
|
114 |
|
115 |
TList *l = 0;
|
116 |
if (eventlist >= 0 && eventlist < fNFriends) {
|
117 |
|
118 |
l = dynamic_cast<TList*>(fList->At(eventlist));
|
119 |
if (!l) {
|
120 |
Fatal("AddFile", "Requested list %d not found!", eventlist);
|
121 |
return;
|
122 |
}
|
123 |
|
124 |
} else if (eventlist == fNFriends) {
|
125 |
|
126 |
l = new TList;
|
127 |
l->SetOwner();
|
128 |
fList->Add(l);
|
129 |
fNFriends++;
|
130 |
|
131 |
} else if (eventlist < 0 || eventlist > fNFriends) {
|
132 |
Error("AddFile", "Specified list %d not in [0,%d]", eventlist, fNFriends);
|
133 |
return;
|
134 |
}
|
135 |
|
136 |
if(!IsValidName(pname)) return;
|
137 |
|
138 |
l->Add(new TObjString(pname));
|
139 |
|
140 |
MDB(kAnalysis, 2)
|
141 |
Info("AddFile", "Added %s to list of files.", pname);
|
142 |
}
|
143 |
|
144 |
//__________________________________________________________________________________________________
|
145 |
void Analysis::AddFile(const TObject *oname, Int_t eventlist)
|
146 |
{
|
147 |
// Add file name to the event list specified by eventlist. The lists
|
148 |
// are used to hold filenames of different types of events. In case
|
149 |
// you dont want mixing, just give no eventlist argument (default 0).
|
150 |
|
151 |
MitAssert("AddFile", oname != 0);
|
152 |
|
153 |
return AddFile(oname->GetName(), eventlist);
|
154 |
}
|
155 |
|
156 |
//__________________________________________________________________________________________________
|
157 |
void Analysis::AddList(TList *list, Int_t eventlist)
|
158 |
{
|
159 |
// Add file name to the event list specified by eventlist. The lists
|
160 |
// are used to hold filenames of different types of events. In case
|
161 |
// you dont want mixing, just give no eventlist argument (default 0).
|
162 |
|
163 |
MitAssert("AddList", list != 0);
|
164 |
|
165 |
TIter next(list);
|
166 |
while (TObject *obj = next())
|
167 |
AddFile(obj->GetName(), eventlist);
|
168 |
}
|
169 |
|
170 |
//__________________________________________________________________________________________________
|
171 |
void Analysis::AddLoader(TAMVirtualLoader *l)
|
172 |
{
|
173 |
// Add loader to the list of loaders.
|
174 |
|
175 |
fLoaders->Add(l);
|
176 |
}
|
177 |
|
178 |
//__________________________________________________________________________________________________
|
179 |
void Analysis::AddPackage(const char* name)
|
180 |
{
|
181 |
// Add package to the list of uploaded packages.
|
182 |
|
183 |
MitAssert("AddPackage", name != 0);
|
184 |
fPackages->Add(new TObjString(name));
|
185 |
}
|
186 |
|
187 |
//__________________________________________________________________________________________________
|
188 |
void Analysis::AddPackages(TList *list)
|
189 |
{
|
190 |
// Add list of packages to the list of uploaded packages.
|
191 |
|
192 |
MitAssert("AddPackage", list != 0);
|
193 |
|
194 |
TIter next(list);
|
195 |
while (TObject *obj = next()) {
|
196 |
fPackages->Add(new TObjString(obj->GetName()));
|
197 |
}
|
198 |
}
|
199 |
|
200 |
//__________________________________________________________________________________________________
|
201 |
Bool_t Analysis::Init()
|
202 |
{
|
203 |
// Setup the TDSet and TChain to be used for the analysis
|
204 |
// with or without PROOF. If more than one list of
|
205 |
// file names was given, friend trees are supported.
|
206 |
|
207 |
if (fState == kRun || fState == kInit) {
|
208 |
Error("Init", "Init in state %d is not possible! Call Terminate() first.",
|
209 |
Int_t(fState));
|
210 |
return kFALSE;
|
211 |
}
|
212 |
|
213 |
if (fNFriends <= 0) {
|
214 |
Error("Init", "List of friend lists is empty!");
|
215 |
return kFALSE;
|
216 |
}
|
217 |
|
218 |
if (!fSuperMod) {
|
219 |
Error("Init", "Top-level TAM module is NULL!");
|
220 |
return kFALSE;
|
221 |
}
|
222 |
|
223 |
if (fUseProof) { // first init our PROOF session
|
224 |
if (!InitProof()) return kFALSE;
|
225 |
}
|
226 |
|
227 |
// we do this here instead in Terminate() so that
|
228 |
// we can browse the output even after Terminate()
|
229 |
delete fSelector;
|
230 |
fSelector = 0;
|
231 |
|
232 |
fChain = new TChain(fTreeName);
|
233 |
fSet = new TDSet("TTree",fTreeName);
|
234 |
|
235 |
for(Int_t i=0; i<fNFriends; i++){
|
236 |
|
237 |
TList *l = dynamic_cast<TList*>(fList->At(i));
|
238 |
if (!l) {
|
239 |
Fatal("Init", "List %d not found!", i);
|
240 |
return kFALSE;
|
241 |
}
|
242 |
|
243 |
if (i == 0) {
|
244 |
|
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 |
void Analysis::Terminate()
|
377 |
{
|
378 |
// Terminate current analysis run.
|
379 |
|
380 |
if (fState == kPristine || fState == kTerminate) {
|
381 |
Error("Terminate", "Terminate in state %d is not possible! Call Init() first.",
|
382 |
Int_t(fState));
|
383 |
return;
|
384 |
}
|
385 |
|
386 |
if (fState == kRun) {
|
387 |
|
388 |
if (fUseProof) {
|
389 |
// the output list from Proof can (in principal) contain other objects
|
390 |
// besides the module output hierarchy.
|
391 |
TList* outputlist = fProof->GetOutputList();
|
392 |
TIter nextOut(outputlist);
|
393 |
while (TObject *obj = nextOut()) {
|
394 |
if (obj->InheritsFrom(TAMOutput::Class())) {
|
395 |
fOutput = dynamic_cast<TList*>(obj);
|
396 |
break;
|
397 |
}
|
398 |
}
|
399 |
|
400 |
} else {
|
401 |
fOutput = fSelector->GetModOutput();
|
402 |
}
|
403 |
|
404 |
if (fOutput && !fAnaOutput.IsNull()) {
|
405 |
TDirectory::TContext context(0); // automatically restore gDirectory
|
406 |
|
407 |
std::auto_ptr<TFile> outf(TFile::Open(fAnaOutput,"recreate","", fCompLevel));
|
408 |
if (outf.get() == 0) {
|
409 |
Error("Terminate", "Could not open file %s for output!", fAnaOutput.Data());
|
410 |
} else {
|
411 |
MDB(kAnalysis, 1)
|
412 |
Info("Terminate", "Saving output to %s!", fAnaOutput.Data());
|
413 |
|
414 |
if (fHierachy)
|
415 |
fOutput->Write(fOutput->GetName(),TObject::kSingleKey);
|
416 |
else
|
417 |
fOutput->Write();
|
418 |
|
419 |
}
|
420 |
}
|
421 |
}
|
422 |
|
423 |
delete fChain;
|
424 |
delete fSet;
|
425 |
fDeleteList->Delete();
|
426 |
|
427 |
fState = kTerminate;
|
428 |
}
|
429 |
|
430 |
//__________________________________________________________________________________________________
|
431 |
Bool_t Analysis::UploadPackages(TList *packages)
|
432 |
{
|
433 |
// Upload list of par files to the server.
|
434 |
|
435 |
MitAssert("UploadPackages", packages != 0);
|
436 |
|
437 |
for(Int_t i=0; i < packages->GetEntries(); i++) {
|
438 |
|
439 |
TObject* objstr = packages->At(i);
|
440 |
if (!objstr){
|
441 |
Error("InitProof", "Problem at package number %d!", i);
|
442 |
return kFALSE;
|
443 |
}
|
444 |
|
445 |
TString packname = objstr->GetName();
|
446 |
Int_t en = 0;
|
447 |
if (packname.EndsWith("+")) {
|
448 |
en=1;
|
449 |
packname.Resize(packname.Length()-1);
|
450 |
}
|
451 |
|
452 |
ifstream ftest(gSystem->ExpandPathName(packname.Data()),ios_base::binary);
|
453 |
if (!ftest.good()){
|
454 |
Error("InitProof", "Could not open %s for upload!", packname.Data());
|
455 |
return kFALSE;
|
456 |
}
|
457 |
|
458 |
if(fProof->UploadPackage(packname)<0) {
|
459 |
Error("UploadPackage", "Upload for %s failed!", packname.Data());
|
460 |
return kFALSE;
|
461 |
}
|
462 |
|
463 |
if (en == 1) {
|
464 |
Int_t pos=packname.Last('/')+1;
|
465 |
if (pos) packname.Remove(0,pos);
|
466 |
if(fProof->EnablePackage(packname)<0) {
|
467 |
Error("UploadPackage", "Enabling for %s failed!", packname.Data());
|
468 |
return kFALSE;
|
469 |
}
|
470 |
}
|
471 |
}
|
472 |
|
473 |
return kTRUE;
|
474 |
}
|