AliRoot Core  ee782a0 (ee782a0)
AliAODExtension.cxx
Go to the documentation of this file.
1 #include "AliAODExtension.h"
2 
4 #include "AliAODEvent.h"
5 #include "AliCodeTimer.h"
6 #include "AliLog.h"
7 #include "Riostream.h"
8 #include "TDirectory.h"
9 #include "TFile.h"
10 #include "TList.h"
11 #include "TMap.h"
12 #include "TMap.h"
13 #include "TObjString.h"
14 #include "TROOT.h"
15 #include "TString.h"
16 #include "TTree.h"
17 
18 using std::endl;
19 using std::cout;
20 ClassImp(AliAODExtension)
21 
22 //______________________________________________________________________________
24 fAODEvent(0), fTreeE(0), fFileE(0), fNtotal(0), fNpassed(0),
25 fSelected(kFALSE), fTreeBuffSize(30000000), fMemCountAOD(0),
26 fRepFiMap(0x0), fRepFiList(0x0), fEnableReferences(kTRUE), fObjectList(0)
27 {
28  // default ctor
29 }
30 
31 //______________________________________________________________________________
32 AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
33 :TNamed(name,title),
34 fAODEvent(0),
35 fTreeE(0),
36 fFileE(0),
37 fNtotal(0),
38 fNpassed(0),
39 fSelected(kFALSE),
40 fTreeBuffSize(30000000),
41 fMemCountAOD(0),
42 fRepFiMap(0x0),
43 fRepFiList(0x0),
44 fEnableReferences(kTRUE),
45 fObjectList(0x0)
46 {
48 
49  if (isfilter) {
50  TObject::SetBit(kFilteredAOD);
51  printf("####### Added AOD filter %s\n", name);
52  } else printf("####### Added AOD extension %s\n", name);
54 }
55 
56 //______________________________________________________________________________
58 {
60 
61  if(fFileE){
62  // is already handled in TerminateIO
63  fFileE->Close();
64  delete fFileE;
65  fTreeE = 0;
66  fAODEvent = 0;
67  }
68  if (fTreeE) delete fTreeE;
69  if (fRepFiMap) fRepFiMap->DeleteAll();
70  delete fRepFiMap; // the map is owner
71  delete fRepFiList; // the list is not
72  delete fObjectList; // not owner
73 }
74 
75 //______________________________________________________________________________
76 void AliAODExtension::AddBranch(const char* cname, void* addobj)
77 {
79 
80  if (!fAODEvent) {
81  char type[20];
82  gROOT->ProcessLine(Form("{TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)%p, \"%%s\", s_tmp.Data());}", type));
83  Init(type);
84  }
85  TDirectory *owd = gDirectory;
86  if (fFileE) {
87  fFileE->cd();
88  }
89  char** apointer = (char**) addobj;
90  TObject* obj = (TObject*) *apointer;
91 
92  fAODEvent->AddObject(obj);
93 
94  TString bname(obj->GetName());
95 
96  if (!fTreeE->FindBranch(bname.Data()))
97  {
98  Bool_t acceptAdd(kTRUE);
99 
100  if ( TestBit(kDropUnspecifiedBranches) )
101  {
102  // check that this branch is in our list of specified ones...
103  // otherwise do not add it !
104  TIter next(fRepFiMap);
105  TObjString* p;
106 
107  acceptAdd=kFALSE;
108 
109  while ( ( p = static_cast<TObjString*>(next()) ) && !acceptAdd )
110  {
111  if ( p->String() == bname ) acceptAdd=kTRUE;
112  }
113  }
114 
115  if ( acceptAdd )
116  {
117  // Do the same as if we book via
118  // TTree::Branch(TCollection*)
119 
120  fObjectList->Add(obj);
121 
122  const Int_t kSplitlevel = 99; // default value in TTree::Branch()
123  const Int_t kBufsize = 32000; // default value in TTree::Branch()
124 
125  fTreeE->Bronch(bname.Data(), cname,
126  fAODEvent->GetList()->GetObjectRef(obj),
127  kBufsize, kSplitlevel - 1);
128  }
129  }
130  owd->cd();
131 }
132 
133 //______________________________________________________________________________
135 {
137 
138  fNtotal++;
139  if (!IsFilteredAOD()) {
141  FillTree();
142  return kTRUE;
143  }
144  // Filtered AOD. Fill only if event is selected.
145  if (!fSelected) return kTRUE;
146 
147  TIter next(fRepFiList);
148 
149  AliAODBranchReplicator* repfi;
150 
151  while ( ( repfi = static_cast<AliAODBranchReplicator*>(next()) ) )
152  {
153  repfi->ReplicateAndFilter(*fAODEvent);
154  }
155  fNpassed++;
156  FillTree();
157  fSelected = kFALSE; // so that next event will not be selected unless demanded
158  return kTRUE;
159 }
160 
161 //______________________________________________________________________________
163 {
165 
166  Long64_t nbf = fTreeE->Fill();
167 
168  // Check buffer size and set autoflush if fTreeBuffSize is reached
169  if (fTreeBuffSize>0 && fTreeE->GetAutoFlush()<0 &&
170  (fMemCountAOD += nbf)>fTreeBuffSize ) { // default limit is still not reached
171  nbf = fTreeE->GetZipBytes();
172  if (nbf>0) nbf = -nbf;
173  else nbf = fTreeE->GetEntries();
174  fTreeE->SetAutoFlush(nbf);
175  AliInfo(Form("Calling fTreeE->SetAutoFlush(%lld) | W:%lld T:%lld Z:%lld",
176  nbf,fMemCountAOD,fTreeE->GetTotBytes(),fTreeE->GetZipBytes()));
177 
178  }
179 }
180 
181 //______________________________________________________________________________
182 Bool_t AliAODExtension::Init(Option_t *option)
183 {
185 
186  AliCodeTimerAuto(GetName(),0);
187 
188  if(!fAODEvent)
189  {
190  fAODEvent = new AliAODEvent();
191  }
192 
193  TDirectory *owd = gDirectory;
194  TString opt(option);
195  opt.ToLower();
196 
197  if (opt.Contains("proof"))
198  {
199  // proof
200  // Merging via files. Need to access analysis manager via interpreter.
201  gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
202  gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\", \"%s\");", fName.Data()));
203  fFileE = gFile;
204  }
205  else
206  {
207  fFileE = new TFile(GetName(), "RECREATE");
208  }
209  fTreeE = new TTree("aodTree", "AliAOD tree");
210 
211  delete fObjectList;
212  fObjectList = new TList;
213  fObjectList->SetOwner(kFALSE); // be explicit we're not the owner...
214  TList* inputList = fAODEvent->GetList();
215  TIter next(inputList);
216  TObject* o;
217 
218  while ( ( o = next() ) )
219  {
220  // Loop on the objects that are within the main AOD, and see what to do with them :
221  // - transmit them to our AOD as they are
222  // - filter them (by means of an AliAODBranchReplicator)
223  // - drop them completely
224 
225  Bool_t mustKeep(kFALSE);
226 
227  TString test(o->ClassName());
228  test.ToUpper();
229  // check if there is a replicator for the header
230  Bool_t headerHasReplicator = fRepFiMap && (fRepFiMap->FindObject(o->GetName())!=0x0);
231  if (test.BeginsWith("ALIAODHEADER") && !headerHasReplicator)
232  {
233  // do not allow to drop header branch
234  mustKeep=kTRUE;
235  }
236 
237  if ( fRepFiMap && !mustKeep )
238  {
239  // we have some replicators, so let's see what the relevant one decides about this object
240  TObject* specified = fRepFiMap->FindObject(o->GetName()); // FindObject finds key=o->GetName() in the map
241  if (specified)
242  {
243  AliAODBranchReplicator* repfi = dynamic_cast<AliAODBranchReplicator*>(fRepFiMap->GetValue(o->GetName())); // GetValue gets the replicator corresponding to key=o->GetName()
244  if ( repfi )
245  {
246  TList* replicatedList = repfi->GetList();
247  if (replicatedList)
248  {
249  AliAODEvent::AssignIDtoCollection(replicatedList);
250  TIter nextRep(replicatedList);
251  TObject* objRep;
252  while ( ( objRep = nextRep() ) )
253  {
254  if ( !fObjectList->FindObject(objRep) ) // insure we're not adding several times the same object
255  {
256  fObjectList->Add(objRep);
257  }
258  }
259  }
260  else
261  {
262  AliError(Form("replicatedList from %s is null !",repfi->GetName()));
263  }
264  }
265  }
266  else
267  {
268  if ( !TestBit(kDropUnspecifiedBranches) )
269  {
270  // object o will be transmitted to the output AOD, unchanged
271  fObjectList->Add(o);
272  }
273  }
274  }
275  else
276  {
277  // no replicator, so decide based on the policy about dropping unspecified branches
278  if ( mustKeep || !TestBit(kDropUnspecifiedBranches) )
279  {
280  // object o will be transmitted to the output AOD, unchanged
281  fObjectList->Add(o);
282  }
283  }
284  }
285 
286  if (fEnableReferences)
287  {
288  fTreeE->BranchRef();
289  }
290 
291  fTreeE->Branch(fObjectList);
292 
293  owd->cd();
294 
295  return kTRUE;
296 }
297 
298 //______________________________________________________________________________
299 void AliAODExtension::Print(Option_t* opt) const
300 {
302 
303  cout << opt << Form("%s - %s - %s - aod %p",IsFilteredAOD() ? "FilteredAOD" : "Extension",
304  GetName(),GetTitle(),GetAOD()) << endl;
305  if ( !fEnableReferences )
306  {
307  cout << opt << opt << "References are disabled ! Hope you know what you are doing !" << endl;
308  }
309  if ( TestBit(kDropUnspecifiedBranches) )
310  {
311  cout << opt << opt << "All branches not explicitely specified will be dropped" << endl;
312  }
313 
314  TIter next(fRepFiMap);
315  TObjString* s;
316 
317  while ( ( s = static_cast<TObjString*>(next()) ) )
318  {
319  AliAODBranchReplicator* br = static_cast<AliAODBranchReplicator*>(fRepFiMap->GetValue(s->String().Data()));
320 
321  cout << opt << opt << "Branch " << s->String();
322  if (br)
323  {
324  cout << " will be filtered by class " << br->ClassName();
325  }
326  else
327  {
328  cout << " will be transmitted as is";
329  }
330  cout << endl;
331  }
332 }
333 
334 //______________________________________________________________________________
336 {
338 
339  if (!IsFilteredAOD()) {
340  Error("SetEvent", "Not allowed to set external event for non filtered AOD's");
341  return;
342  }
343  fAODEvent = event;
344 }
345 
346 //______________________________________________________________________________
348 {
350 
351  if (!fTreeE) return;
352 
353  AliAODEvent* aodEvent(fAODEvent);
354 
355  if ( IsFilteredAOD() )
356  {
357  // cannot attach fAODEvent (which is shared with our AliAODHandler mother)
358  // so we create a custom (empty) AliAODEvent
359  // Has also the advantage we can specify only the list of objects
360  // that are actually there in this filtered aod
361  //
362  aodEvent = new AliAODEvent;
363  TIter nextObj(fObjectList);
364  TObject* o;
365  while ( ( o = nextObj() ) )
366  {
367  aodEvent->AddObject(o);
368  }
369  }
370 
371  TList *l = aodEvent->GetList();
372  if (l) {
373  for(int i = 0;i < l->GetEntries(); ++i){
374  TObject *pObject = l->At(i);
375  if(pObject->InheritsFrom(TClonesArray::Class())){
376  ((TClonesArray*)pObject)->Delete();
377  } else if(!pObject->InheritsFrom(TCollection::Class())){
378  TClass *pClass = TClass::GetClass(pObject->ClassName());
379  if (pClass && pClass->GetListOfMethods()->FindObject("Clear")) {
380  AliDebug(1, Form("Clear for object %s class %s", pObject->GetName(), pObject->ClassName()));
381  pObject->Clear();
382  }
383  } else {
384  AliWarning(Form("No method to clear for object %s class %s", pObject->GetName(), pObject->ClassName()));
385  }
386  }
387  }
388  fTreeE->GetUserInfo()->Add(aodEvent);
389 }
390 
391 //______________________________________________________________________________
393 {
395 
396  if (TObject::TestBit(kFilteredAOD))
397  printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
398  else
399  printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);
400  if (fFileE)
401  {
402  fFileE->Write();
403  fFileE->Close();
404  delete fFileE;
405  fFileE = 0;
406  fTreeE = 0;
407  fAODEvent = 0;
408  }
409  return kTRUE;
410 }
411 
412 //______________________________________________________________________________
413 void AliAODExtension::FilterBranch(const char* branchName, AliAODBranchReplicator* repfi)
414 {
424 
425  if (!fRepFiMap)
426  {
427  fRepFiMap = new TMap;
428  fRepFiMap->SetOwnerKeyValue(kTRUE,kTRUE);
429  fRepFiList = new TList;
430  fRepFiList->SetOwner(kFALSE);
431  }
432 
433  fRepFiMap->Add(new TObjString(branchName),repfi);
434 
435  if (repfi && !fRepFiList->FindObject(repfi))
436  {
437  // insure we get unique and non-null replicators in this list
438  fRepFiList->Add(repfi);
439  }
440 }
441 
printf("Chi2/npoints = %f\n", TMath::Sqrt(chi2/npoints))
Bool_t fSelected
! Select current event for filtered AOD&#39;s. Made false at event start.
void AddBranch(const char *cname, void *addobj)
void MakeEntriesReferencable()
void SetEvent(AliAODEvent *event)
TFile * fFileE
! Output file
TList * fObjectList
! internal list of which objects to keep
virtual void ReplicateAndFilter(const AliAODEvent &source)=0
Replicate (and optionally filter) the given aod event.
Bool_t fEnableReferences
whether or not to enable the TRefTable branch
static void AssignIDtoCollection(const TCollection *col)
virtual TList * GetList() const =0
Return the list of object we manage.
TROOT * gROOT
Float_t p[]
Definition: kNNTest.C:133
TList * GetList() const
Definition: AliAODEvent.h:85
AliAODEvent * fAODEvent
! Pointer to the AOD event
#define AliWarning(message)
Definition: AliLog.h:541
Long64_t fMemCountAOD
Number of bytes filled in tree (accumulates until requested bytes reached)
TList * fRepFiList
list of unique filter/replicator
void AddObject(TObject *obj)
AliAODEvent * GetAOD() const
#define AliInfo(message)
Definition: AliLog.h:484
#define AliCodeTimerAuto(message, counter)
Definition: AliCodeTimer.h:137
void FilterBranch(const char *branchName, AliAODBranchReplicator *replicator=0x0)
TTree * fTreeE
! tree for AOD persistency
void Print(Option_t *opt="") const
Int_t fNpassed
! Number of events that passed the filter
AOD base class.
Definition: AliAODEvent.h:48
Bool_t IsFilteredAOD() const
TMap * fRepFiMap
which branch(es) to filter out / and or replicate
void test()
Definition: interpolTest.C:100
Bool_t Init(Option_t *option)
#define AliDebug(logLevel, message)
Definition: AliLog.h:300
Long64_t fTreeBuffSize
Requested buffer size for AOD tree.
void KeepUnspecifiedBranches()
#define AliError(message)
Definition: AliLog.h:591
Base class of an object used for the replication.
Support class for AOD extensions. This is created by the user analysis.
virtual ~AliAODExtension()
Int_t fNtotal
! Number of processed events