forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEventIter.cxx
1340 lines (1150 loc) · 37.6 KB
/
TEventIter.cxx
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/proofplayer:$Id$
// Author: Maarten Ballintijn 07/01/02
// Modified: Long Tran-Thanh 04/09/07 (Addition of TEventIterUnit)
/*************************************************************************
* Copyright (C) 1995-2001, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/** \class TEventIter
\ingroup proofkernel
Special iterator class used in TProofPlayer to iterate over events
or objects in the packets.
*/
#include "TEnv.h"
#include "TEventIter.h"
#include "TFriendElement.h"
#include "TCollection.h"
#include "TDSet.h"
#include "TFile.h"
#include "TKey.h"
#include "TProofDebug.h"
#include "TSelector.h"
#include "TTimeStamp.h"
#include "TTree.h"
#include "TTreeCache.h"
#include "TTreeCacheUnzip.h"
#include "TVirtualPerfStats.h"
#include "TEventList.h"
#include "TEntryList.h"
#include "TList.h"
#include "TMap.h"
#include "TObjString.h"
#include "TRegexp.h"
#include "TProofServ.h"
#include "TSystem.h"
#include "TError.h"
#if defined(R__MACOSX)
#include "fcntl.h"
#endif
ClassImp(TEventIter);
////////////////////////////////////////////////////////////////////////////////
/// Default constructor
TEventIter::TEventIter()
{
fDSet = 0;
fElem = 0;
fFile = 0;
fDir = 0;
fSel = 0;
fFirst = 0;
fCur = -1;
fNum = 0;
fStop = kFALSE;
fOldBytesRead = 0;
fEventList = 0;
fEventListPos = 0;
fEntryList = 0;
fEntryListPos = 0;
fElemFirst = 0;
fElemNum = 0;
fElemCur = -1;
ResetBit(TEventIter::kData);
if ((fPackets = new TList)) {
TString n("ProcessedPackets_");
if (gProofServ) n += gProofServ->GetOrdinal();
fPackets->SetName(n);
Info("TEventIter", "fPackets list '%s' created", n.Data());
} else {
Warning("TEventIter", "fPackets list could not be created");
}
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor
TEventIter::TEventIter(TDSet *dset, TSelector *sel, Long64_t first, Long64_t num)
: fDSet(dset), fSel(sel)
{
fElem = 0;
fFile = 0;
fDir = 0;
fFirst = first;
fCur = -1;
fNum = num;
fStop = kFALSE;
fEventList = 0;
fEventListPos = 0;
fEntryList = 0;
fEntryListPos = 0;
fOldBytesRead = 0;
fElemFirst = 0;
fElemNum = 0;
fElemCur = -1;
ResetBit(TEventIter::kData);
if ((fPackets = new TList)) {
TString n("ProcessedPackets_");
if (gProofServ) n += gProofServ->GetOrdinal();
fPackets->SetName(n);
Info("TEventIter", "fPackets list '%s' created", n.Data());
} else {
Warning("TEventIter", "fPackets list could not be created");
}
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor
TEventIter::~TEventIter()
{
if (fPackets) {
fPackets->SetOwner(kTRUE);
SafeDelete(fPackets);
}
delete fFile;
}
////////////////////////////////////////////////////////////////////////////////
/// Invalidated the current packet (if any) by setting the TDSetElement::kCorrupted bit
void TEventIter::InvalidatePacket()
{
if (fElem) fElem->SetBit(TDSetElement::kCorrupted);
}
////////////////////////////////////////////////////////////////////////////////
/// Set flag to stop the process
void TEventIter::StopProcess(Bool_t /*abort*/)
{
fStop = kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Create and instance of the appropriate iterator
TEventIter *TEventIter::Create(TDSet *dset, TSelector *sel, Long64_t first, Long64_t num)
{
if (dset->TestBit(TDSet::kEmpty)) {
return new TEventIterUnit(dset, sel, num);
} else if (dset->IsTree()) {
return new TEventIterTree(dset, sel, first, num);
} else {
return new TEventIterObj(dset, sel, first, num);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Load directory
Int_t TEventIter::LoadDir()
{
Int_t ret = 0;
// Check Filename
if ( fFile == 0 || fFilename != fElem->GetFileName() ) {
fDir = 0;
delete fFile; fFile = 0;
fFilename = fElem->GetFileName();
TDirectory *dirsave = gDirectory;
Double_t start = 0;
if (gPerfStats) start = TTimeStamp();
// Take into acoount possible prefixes
TFile::EFileType typ = TFile::kDefault;
TString fname = gEnv->GetValue("Path.Localroot","");
if (!fname.IsNull())
typ = TFile::GetType(fFilename, "", &fname);
if (typ != TFile::kLocal)
fname = fFilename;
fFile = TFile::Open(fname);
if (gPerfStats) {
gPerfStats->FileOpenEvent(fFile, fFilename, start);
fOldBytesRead = 0;
}
if (dirsave) dirsave->cd();
if (!fFile || fFile->IsZombie() ) {
if (fFile)
Error("Process","Cannot open file: %s (%s)",
fFilename.Data(), strerror(fFile->GetErrno()) );
else
Error("Process","Cannot open file: %s (errno unavailable)",
fFilename.Data());
// cleanup ?
return -1;
}
PDB(kLoop,2) Info("LoadDir","Opening file: %s", fFilename.Data() );
ret = 1;
}
// Check Directory
if ( fDir == 0 || fPath != fElem->GetDirectory() ) {
TDirectory *dirsave = gDirectory;
fPath = fElem->GetDirectory();
if ( !fFile->cd(fPath) ) {
Error("Process","Cannot cd to: %s",
fPath.Data() );
return -1;
}
PDB(kLoop,2) Info("Process","Cd to: %s", fPath.Data() );
fDir = gDirectory;
if (dirsave) dirsave->cd();
ret = 1;
}
return ret;
}
//______________________________________________________________________________
Long64_t TEventIter::GetEntryNumber(Long64_t next)
{
// Get the entry number, taking into account event/entry lists
Long64_t entry = next;
// Set entry number; if data iteration we may need to test the entry or event lists
if (TestBit(TEventIter::kData)) {
if (fEntryList){
entry = fEntryList->GetEntry(next);
} else if (fEventList) {
entry = fEventList->GetEntry(next);
}
}
// Pre-event processing
PreProcessEvent(entry);
// Done
return entry;
}
//------------------------------------------------------------------------
ClassImp(TEventIterUnit);
////////////////////////////////////////////////////////////////////////////////
/// Default constructor
TEventIterUnit::TEventIterUnit()
{
fDSet = 0;
fElem = 0;
fSel = 0;
fNum = 0;
fCurrent = 0;
fStop = kFALSE;
fOldBytesRead = 0; // Measures the bytes written
}
////////////////////////////////////////////////////////////////////////////////
/// Main constructor
TEventIterUnit::TEventIterUnit(TDSet* dset, TSelector *sel, Long64_t num)
{
fDSet = dset;
fElem = 0;
fSel = sel;
fNum = num;
fCurrent = 0;
fStop = kFALSE;
fOldBytesRead = 0; // Measures the bytes written
}
////////////////////////////////////////////////////////////////////////////////
/// Get loop range
Int_t TEventIterUnit::GetNextPacket(Long64_t &fst, Long64_t &num)
{
if (gPerfStats) {
Long64_t totBytesWritten = TFile::GetFileBytesWritten();
Long64_t bytesWritten = totBytesWritten - fOldBytesRead;
PDB(kLoop, 2) Info("GetNextPacket", "bytes written: %lld", bytesWritten);
gPerfStats->SetBytesRead(bytesWritten);
fOldBytesRead = totBytesWritten;
}
if (fDSet->TestBit(TDSet::kIsLocal)) {
if (fElem) {
if (fPackets) {
fPackets->Add(fElem);
PDB(kLoop, 2)
Info("GetNextEvent", "packet added to list (sz: %d)", fPackets->GetSize());
fElem = 0;
} else {
SafeDelete(fElem);
}
return -1;
} else {
fElem = new TDSetElement("", "", "", 0, fNum);
fElem->SetBit(TDSetElement::kEmpty);
}
} else {
if (fPackets && fElem) {
fPackets->Add(fElem);
PDB(kLoop, 2)
Info("GetNextEvent", "packet added to list (sz: %d)", fPackets->GetSize());
fElem = 0;
} else {
SafeDelete(fElem);
}
if (!(fElem = fDSet->Next()))
return -1;
}
fElem->SetBit(TDSetElement::kNewPacket);
if (!fElem->TestBit(TDSetElement::kEmpty)) {
Error("GetNextPacket", "data element must be set to kEmtpy");
return -1;
}
// Set output
num = fElem->GetNum();
if (num == 0) return -1;
fst = fElem->GetFirst();
// Done
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Get next event
Long64_t TEventIterUnit::GetNextEvent()
{
if (fStop || fNum == 0)
return -1;
if (fElem) fElem->ResetBit(TDSetElement::kNewPacket);
while (fElem == 0 || fCurrent == 0) {
if (gPerfStats) {
Long64_t totBytesWritten = TFile::GetFileBytesWritten();
Long64_t bytesWritten = totBytesWritten - fOldBytesRead;
PDB(kLoop, 2) Info("GetNextEvent", "bytes written: %lld", bytesWritten);
gPerfStats->SetBytesRead(bytesWritten);
fOldBytesRead = totBytesWritten;
}
if (fDSet->TestBit(TDSet::kIsLocal)) {
if (fElem) {
if (fPackets) {
fPackets->Add(fElem);
PDB(kLoop, 2)
Info("GetNextEvent", "packet added to list (sz: %d)", fPackets->GetSize());
fElem = 0;
} else {
SafeDelete(fElem);
}
return -1;
} else {
fElem = new TDSetElement("", "", "", 0, fNum);
fElem->SetBit(TDSetElement::kEmpty);
}
} else {
if (fPackets && fElem) {
fPackets->Add(fElem);
PDB(kLoop, 2)
Info("GetNextEvent", "packet added to list (sz: %d)", fPackets->GetSize());
fElem = 0;
} else {
SafeDelete(fElem);
}
if (!(fElem = fDSet->Next()))
return -1;
}
fElem->SetBit(TDSetElement::kNewPacket);
if (!fElem->TestBit(TDSetElement::kEmpty)) {
Error("GetNextEvent", "data element must be set to kEmtpy");
return -1;
}
fNum = fElem->GetNum();
if (!(fCurrent = fNum)) {
fNum = 0;
return -1;
}
fFirst = fElem->GetFirst();
}
Long64_t event = fNum - fCurrent + fFirst ;
--fCurrent;
return event;
}
//------------------------------------------------------------------------
ClassImp(TEventIterObj);
////////////////////////////////////////////////////////////////////////////////
/// Default ctor.
TEventIterObj::TEventIterObj()
{
fKeys = 0;
fNextKey = 0;
fObj = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor
TEventIterObj::TEventIterObj(TDSet *dset, TSelector *sel, Long64_t first, Long64_t num)
: TEventIter(dset,sel,first,num)
{
fClassName = dset->GetType();
fKeys = 0;
fNextKey = 0;
fObj = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor
TEventIterObj::~TEventIterObj()
{
// delete fKeys ?
delete fNextKey;
delete fObj;
}
////////////////////////////////////////////////////////////////////////////////
/// Get loop range
Int_t TEventIterObj::GetNextPacket(Long64_t &first, Long64_t &num)
{
SafeDelete(fElem);
if (fStop || fNum == 0) return -1;
while (fElem == 0 || fCur < fFirst-1) {
if (gPerfStats && fFile) {
Long64_t bytesRead = fFile->GetBytesRead();
gPerfStats->SetBytesRead(bytesRead - fOldBytesRead);
fOldBytesRead = bytesRead;
}
if (fElem) {
// Save it to the list of processed packets
if (fPackets) {
fPackets->Add(fElem);
fElem = 0;
} else {
SafeDelete(fElem);
}
}
fElem = fDSet->Next(fKeys->GetSize());
if (fElem && fElem->GetEntryList()) {
Error("GetNextPacket", "entry- or event-list not available");
return -1;
}
if ( fElem == 0 ) {
fNum = 0;
return -1;
}
fElem->SetBit(TDSetElement::kNewPacket);
Int_t r = LoadDir();
if ( r == -1 ) {
// Error has been reported
fNum = 0;
return -1;
} else if ( r == 1 ) {
// New file and/or directory
fKeys = fDir->GetListOfKeys();
fNextKey = new TIter(fKeys);
}
// Validate values for this element
fElemFirst = fElem->GetFirst();
fElemNum = fElem->GetNum();
if (fElem->GetEntryList()) {
if (!(fEntryList = dynamic_cast<TEntryList *>(fElem->GetEntryList())))
fEventList = dynamic_cast<TEventList *>(fElem->GetEntryList());
}
fEventListPos = 0;
if (fEntryList)
fElemNum = fEntryList->GetEntriesToProcess();
else if (fEventList)
fElemNum = fEventList->GetN();
Long64_t tnum = fKeys->GetSize();
if ( fElemFirst > tnum ) {
Error("GetNextPacket","First (%lld) higher then number of keys (%lld) in %s",
fElemFirst, tnum, fElem->GetName());
fNum = 0;
return -1;
}
if ( fElemNum == -1 ) {
fElemNum = tnum - fElemFirst;
} else if ( fElemFirst+fElemNum > tnum ) {
Error("GetNextPacket","Num (%lld) + First (%lld) larger then number of keys (%lld) in %s",
fElemNum, fElemFirst, tnum, fElem->GetDirectory());
fElemNum = tnum - fElemFirst;
}
// Skip this element completely?
if ( fCur + fElemNum < fFirst ) {
fCur += fElemNum;
continue;
}
// Position within this element
fNextKey->Reset();
for(fElemCur = -1; fElemCur < fElemFirst-1 ; fElemCur++, fNextKey->Next()) { }
}
first = ++fElemCur;
num = fElemNum;
// Done
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// To be executed before by TProofPlayer calling TSelector::Process
void TEventIterObj::PreProcessEvent(Long64_t)
{
--fNum;
++fCur;
TKey *key = (TKey*) fNextKey->Next();
TDirectory *dirsave = gDirectory;
fDir->cd();
fObj = key->ReadObj();
if (dirsave) dirsave->cd();
fSel->SetObject(fObj);
}
////////////////////////////////////////////////////////////////////////////////
/// Get next event
Long64_t TEventIterObj::GetNextEvent()
{
if (fStop || fNum == 0) return -1;
if (fElem) fElem->ResetBit(TDSetElement::kNewPacket);
while ( fElem == 0 || fElemNum == 0 || fCur < fFirst-1 ) {
if (gPerfStats && fFile) {
Long64_t bytesRead = fFile->GetBytesRead();
gPerfStats->SetBytesRead(bytesRead - fOldBytesRead);
fOldBytesRead = bytesRead;
}
if (fElem) {
// Save it to the list of processed packets
if (fPackets) {
fPackets->Add(fElem);
fElem = 0;
} else {
SafeDelete(fElem);
}
}
fElem = fDSet->Next(fKeys->GetSize());
if (fElem && fElem->GetEntryList()) {
Error("GetNextEvent", "Entry- or event-list not available");
return -1;
}
if ( fElem == 0 ) {
fNum = 0;
return -1;
}
fElem->SetBit(TDSetElement::kNewPacket);
Int_t r = LoadDir();
if ( r == -1 ) {
// Error has been reported
fNum = 0;
return -1;
} else if ( r == 1 ) {
// New file and/or directory
fKeys = fDir->GetListOfKeys();
fNextKey = new TIter(fKeys);
}
// Validate values for this element
fElemFirst = fElem->GetFirst();
fElemNum = fElem->GetNum();
if (fElem->GetEntryList()) {
if (!(fEntryList = dynamic_cast<TEntryList *>(fElem->GetEntryList())))
fEventList = dynamic_cast<TEventList *>(fElem->GetEntryList());
}
fEventListPos = 0;
if (fEntryList)
fElemNum = fEntryList->GetEntriesToProcess();
else if (fEventList)
fElemNum = fEventList->GetN();
Long64_t num = fKeys->GetSize();
if ( fElemFirst > num ) {
Error("GetNextEvent","First (%lld) higher then number of keys (%lld) in %s",
fElemFirst, num, fElem->GetName());
fNum = 0;
return -1;
}
if ( fElemNum == -1 ) {
fElemNum = num - fElemFirst;
} else if ( fElemFirst+fElemNum > num ) {
Error("GetNextEvent","Num (%lld) + First (%lld) larger then number of keys (%lld) in %s",
fElemNum, fElemFirst, num, fElem->GetDirectory());
fElemNum = num - fElemFirst;
}
// Skip this element completely?
if ( fCur + fElemNum < fFirst ) {
fCur += fElemNum;
continue;
}
// Position within this element. TODO: more efficient?
fNextKey->Reset();
for(fElemCur = -1; fElemCur < fElemFirst-1 ; fElemCur++, fNextKey->Next()) { }
}
--fElemNum;
++fElemCur;
// Pre-event processing
PreProcessEvent(fElemCur);
return fElemCur;
}
//------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// Default ctor.
TEventIterTree::TFileTree::TFileTree(const char *name, TFile *f, Bool_t islocal)
: TNamed(name, ""), fUsed(kFALSE), fIsLocal(islocal), fFile(f)
{
fTrees = new TList;
fTrees->SetOwner();
}
////////////////////////////////////////////////////////////////////////////////
/// Default dtor.
TEventIterTree::TFileTree::~TFileTree()
{
// Avoid destroying the cache; must be placed before deleting the trees
TTree *tree = (TTree *)fTrees->First();
while (tree) {
fFile->SetCacheRead(0, tree);
tree = (TTree *)fTrees->After(tree);
}
SafeDelete(fTrees);
SafeDelete(fFile);
}
ClassImp(TEventIterTree);
////////////////////////////////////////////////////////////////////////////////
/// Default ctor.
TEventIterTree::TEventIterTree()
{
fTree = 0;
fTreeCache = 0;
fUseTreeCache = 1;
fCacheSize = -1;
fTreeCacheIsLearning = kTRUE;
fFileTrees = 0;
fUseParallelUnzip = 0;
fDontCacheFiles = kFALSE;
SetBit(TEventIter::kData);
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor
TEventIterTree::TEventIterTree(TDSet *dset, TSelector *sel, Long64_t first, Long64_t num)
: TEventIter(dset,sel,first,num)
{
fTreeName = dset->GetObjName();
fTree = 0;
fTreeCache = 0;
fTreeCacheIsLearning = kTRUE;
fFileTrees = new TList;
fFileTrees->SetOwner();
fUseTreeCache = gEnv->GetValue("ProofPlayer.UseTreeCache", 1);
fCacheSize = gEnv->GetValue("ProofPlayer.CacheSize", -1);
fUseParallelUnzip = gEnv->GetValue("ProofPlayer.UseParallelUnzip", 0);
if (fUseParallelUnzip) {
TTreeCacheUnzip::SetParallelUnzip(TTreeCacheUnzip::kEnable);
} else {
TTreeCacheUnzip::SetParallelUnzip(TTreeCacheUnzip::kDisable);
}
fDontCacheFiles = gEnv->GetValue("ProofPlayer.DontCacheFiles", 0);
SetBit(TEventIter::kData);
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor
TEventIterTree::~TEventIterTree()
{
// Delete the tree cache ...
SafeDelete(fTreeCache);
// ... and the remaining open files
SafeDelete(fFileTrees);
}
////////////////////////////////////////////////////////////////////////////////
/// Return the size in bytes of the cache, if any
/// Return -1 if not used
Long64_t TEventIterTree::GetCacheSize()
{
if (fUseTreeCache) return fCacheSize;
return -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Return the number of entries in the learning phase
Int_t TEventIterTree::GetLearnEntries()
{
return TTreeCache::GetLearnEntries();
}
////////////////////////////////////////////////////////////////////////////////
/// Create a Tree for the main TDSetElement and for all the friends.
/// Returns the main tree or 0 in case of an error.
TTree* TEventIterTree::GetTrees(TDSetElement *elem)
{
// Reset used flags
TIter nxft(fFileTrees);
TFileTree *ft = 0;
while ((ft = (TFileTree *)nxft()))
ft->fUsed = kFALSE;
Bool_t localfile = kFALSE;
TTree* main = Load(elem, localfile);
if (main && main != fTree) {
// Set the file cache
if (fUseTreeCache) {
TFile *curfile = main->GetCurrentFile();
if (curfile) {
if (!fTreeCache) {
main->SetCacheSize(fCacheSize);
fTreeCache = (TTreeCache *)curfile->GetCacheRead(main);
if (fCacheSize < 0) fCacheSize = main->GetCacheSize();
} else {
fTreeCache->ResetCache();
curfile->SetCacheRead(fTreeCache, main);
fTreeCache->UpdateBranches(main);
}
if (fTreeCache) {
fTreeCacheIsLearning = fTreeCache->IsLearning();
if (fTreeCacheIsLearning)
Info("GetTrees","the tree cache is in learning phase");
}
} else {
Warning("GetTrees", "default tree does nto have a file attached: corruption? Tree cache untouched");
}
} else {
// Disable the cache
main->SetCacheSize(0);
}
}
Bool_t loc = kFALSE;
// Also the friends
TList *friends = elem->GetListOfFriends();
if (friends) {
TIter nxf(friends);
TDSetElement *dse = 0;
while ((dse = (TDSetElement *) nxf())) {
// The alias, if any, is in the element name options ('friend_alias=<alias>|')
TUrl uf(dse->GetName());
TString uo(uf.GetOptions()), alias;
Int_t from = kNPOS;
if ((from = uo.Index("friend_alias=")) != kNPOS) {
from += strlen("friend_alias=");
if (!uo.Tokenize(alias, from, "|"))
Warning("GetTrees", "empty 'friend_alias' found for tree friend");
// The options may be used for other things, so remove the internal strings once decoded
uo.ReplaceAll(TString::Format("friend_alias=%s|", alias.Data()), "");
uf.SetOptions(uo);
dse->SetName(uf.GetUrl());
}
TTree *friendTree = Load(dse, loc, dse->GetObjName());
if (friendTree && main) {
// Make sure it has not yet been added
Bool_t addfriend = kTRUE;
TList *frnds = main->GetListOfFriends();
if (frnds) {
TIter xnxf(frnds);
TFriendElement *fe = 0;
while ((fe = (TFriendElement *) xnxf())) {
if (fe->GetTree() == friendTree) {
addfriend = kFALSE;
break;
}
}
}
if (addfriend) {
if (alias.IsNull())
main->AddFriend(friendTree);
else
main->AddFriend(friendTree, alias);
}
} else {
return 0;
}
}
}
// Remove instances not used
nxft.Reset();
while ((ft = (TFileTree *)nxft())) {
if (!(ft->fUsed)) {
fFileTrees->Remove(ft);
delete ft;
}
}
// Done, successfully or not
return main;
}
////////////////////////////////////////////////////////////////////////////////
/// Load a tree from s TDSetElement
TTree* TEventIterTree::Load(TDSetElement *e, Bool_t &localfile, const char *objname)
{
if (!e) {
Error("Load", "undefined element");
return (TTree *)0;
}
const char *fn = e->GetFileName();
const char *dn = e->GetDirectory();
const char *tn = 0;
if (objname && strlen(objname) > 0) {
tn = objname;
} else {
tn = (fDSet->GetObjName() && strlen(fDSet->GetObjName()) > 0)
? fDSet->GetObjName() : e->GetObjName();
if (!tn || (tn && strlen(tn) <= 0)) tn = "*";
}
PDB(kLoop,2)
Info("Load","loading: fn:'%s' dn:'%s' tn:'%s'", fn, dn, tn);
TFile *f = 0;
// Check if the file is already open
TString names(fn);
TString name;
Ssiz_t from = 0;
TFileTree *ft = 0;
while (names.Tokenize(name,from,"|")) {
TString key(TUrl(name).GetFileAndOptions());
if ((ft = (TFileTree *) fFileTrees->FindObject(key.Data()))) {
f = ft->fFile;
break;
}
}
// Open the file, if needed
if (!f) {
TFile::EFileType typ = TFile::kDefault;
TString fname = gEnv->GetValue("Path.Localroot","");
if (!fname.IsNull())
typ = TFile::GetType(fn, "", &fname);
if (typ != TFile::kLocal) {
fname = fn;
} else {
localfile = kTRUE;
}
// Open the file
f = TFile::Open(fname);
if (!f) {
Error("Load","file '%s' ('%s') could not be open", fn, fname.Data());
return (TTree *)0;
}
#if defined(R__MACOSX)
// If requested set the no cache mode
if (fDontCacheFiles && localfile) {
fcntl(f->GetFd(), F_NOCACHE, 1);
}
#endif
// Create TFileTree instance in the list
ft = new TFileTree(TUrl(f->GetName()).GetFileAndOptions(), f, localfile);
fFileTrees->Add(ft);
} else {
// Fill locality boolean
localfile = ft->fIsLocal;
PDB(kLoop,2)
Info("Load","file '%s' already open (local:%d)", fn, localfile);
}
// Check if the tree is already loaded
if (ft && ft->fTrees->GetSize() > 0) {
TTree *t = 0;
if (!strcmp(tn, "*"))
t = (TTree *) ft->fTrees->First();
else
t = (TTree *) ft->fTrees->FindObject(tn);
if (t) {
ft->fUsed = kTRUE;
return t;
}
}
TDirectory *dd = f;
// Change dir, if required
if (dn && !(dd = f->GetDirectory(dn))) {
Error("Load","Cannot get to: %s", dn);
return (TTree *)0;
}
PDB(kLoop,2)
Info("Load","got directory: %s", dn);
// If a wild card we will use the first object of the type
// requested compatible with the reg expression we got
TString on(tn);
TString sreg(tn);
if (sreg.Length() <= 0 || sreg == "" || sreg.Contains("*")) {
if (sreg.Contains("*"))
sreg.ReplaceAll("*", ".*");
else
sreg = ".*";
TRegexp re(sreg);
if (dd->GetListOfKeys()) {
TIter nxk(dd->GetListOfKeys());
TKey *k = 0;
while ((k = (TKey *) nxk())) {
if (!strcmp(k->GetClassName(), "TTree")) {
TString kn(k->GetName());
if (kn.Index(re) != kNPOS) {
on = kn;
break;
}
}
}
}
}
// Point to the key
TKey *key = dd->GetKey(gSystem->BaseName(on));
if (key == 0) {
Error("Load", "Cannot find tree \"%s\" in %s", tn, fn);
return (TTree*)0;
}
PDB(kLoop,2) Info("Load", "Reading: %s", tn);
TTree *tree = dynamic_cast<TTree*> (key->ReadObj());
dd->cd();
if (tree == 0) {
Error("Load", "Cannot <dynamic_cast> obj to tree \"%s\"", tn);
return (TTree*)0;
}
// Add track in the cache
ft->fTrees->Add(tree);
ft->fUsed = kTRUE;
PDB(kLoop,2)