1 #include "AliAODExtension.h"
3 //-------------------------------------------------------------------------
4 // Support class for AOD extensions. This is created by the user analysis
5 // that requires a separate file for some AOD branches. The name of the
6 // AliAODExtension object is the file name where the AOD branches will be
8 //-------------------------------------------------------------------------
10 #include "AliAODBranchReplicator.h"
11 #include "AliAODEvent.h"
12 #include "AliCodeTimer.h"
14 #include "Riostream.h"
15 #include "TDirectory.h"
20 #include "TObjString.h"
27 ClassImp(AliAODExtension)
29 //______________________________________________________________________________
30 AliAODExtension::AliAODExtension() : TNamed(),
31 fAODEvent(0), fTreeE(0), fFileE(0), fNtotal(0), fNpassed(0),
32 fSelected(kFALSE), fRepFiMap(0x0), fRepFiList(0x0), fEnableReferences(kTRUE), fObjectList(0x0)
37 //______________________________________________________________________________
38 AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
48 fEnableReferences(kTRUE),
53 TObject::SetBit(kFilteredAOD);
54 printf("####### Added AOD filter %s\n", name);
55 } else printf("####### Added AOD extension %s\n", name);
56 KeepUnspecifiedBranches();
59 //______________________________________________________________________________
60 AliAODExtension::~AliAODExtension()
64 // is already handled in TerminateIO
70 if (fTreeE) delete fTreeE;
71 if (fRepFiMap) fRepFiMap->DeleteAll();
72 delete fRepFiMap; // the map is owner
73 delete fRepFiList; // the list is not
74 delete fObjectList; // not owner
77 //______________________________________________________________________________
78 void AliAODExtension::AddBranch(const char* cname, void* addobj)
80 // Add a new branch to the aod
84 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)%p, \"%%s\", s_tmp.Data());", type));
87 TDirectory *owd = gDirectory;
91 char** apointer = (char**) addobj;
92 TObject* obj = (TObject*) *apointer;
94 fAODEvent->AddObject(obj);
96 TString bname(obj->GetName());
98 if (!fTreeE->FindBranch(bname.Data()))
100 Bool_t acceptAdd(kTRUE);
102 if ( TestBit(kDropUnspecifiedBranches) )
104 // check that this branch is in our list of specified ones...
105 // otherwise do not add it !
106 TIter next(fRepFiMap);
111 while ( ( p = static_cast<TObjString*>(next()) ) && !acceptAdd )
113 if ( p->String() == bname ) acceptAdd=kTRUE;
119 // Do the same as if we book via
120 // TTree::Branch(TCollection*)
122 fObjectList->Add(obj);
124 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
125 const Int_t kBufsize = 32000; // default value in TTree::Branch()
127 fTreeE->Bronch(bname.Data(), cname,
128 fAODEvent->GetList()->GetObjectRef(obj),
129 kBufsize, kSplitlevel - 1);
135 //______________________________________________________________________________
136 Bool_t AliAODExtension::FinishEvent()
138 // Fill current event.
140 if (!IsFilteredAOD()) {
141 fAODEvent->MakeEntriesReferencable();
145 // Filtered AOD. Fill only if event is selected.
146 if (!fSelected) return kTRUE;
148 TIter next(fRepFiList);
150 AliAODBranchReplicator* repfi;
152 while ( ( repfi = static_cast<AliAODBranchReplicator*>(next()) ) )
154 repfi->ReplicateAndFilter(*fAODEvent);
158 fSelected = kFALSE; // so that next event will not be selected unless demanded
162 //______________________________________________________________________________
163 Bool_t AliAODExtension::Init(Option_t *option)
167 AliCodeTimerAuto(GetName(),0);
171 fAODEvent = new AliAODEvent();
174 TDirectory *owd = gDirectory;
178 if (opt.Contains("proof"))
181 // Merging via files. Need to access analysis manager via interpreter.
182 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
183 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\", \"%s\");", fName.Data()));
188 fFileE = new TFile(GetName(), "RECREATE");
190 fTreeE = new TTree("aodTree", "AliAOD tree");
193 fObjectList = new TList;
194 fObjectList->SetOwner(kFALSE); // be explicit we're not the owner...
195 TList* inputList = fAODEvent->GetList();
196 TIter next(inputList);
199 while ( ( o = next() ) )
201 // Loop on the objects that are within the main AOD, and see what to do with them :
202 // - transmit them to our AOD as they are
203 // - filter them (by means of an AliAODBranchReplicator)
204 // - drop them completely
206 Bool_t mustKeep(kFALSE);
208 TString test(o->ClassName());
210 // check if there is a replicator for the header
211 Bool_t headerHasReplicator = fRepFiMap && (fRepFiMap->FindObject(o->GetName())!=0x0);
212 if (test.BeginsWith("ALIAODHEADER") && !headerHasReplicator)
214 // do not allow to drop header branch
218 if ( fRepFiMap && !mustKeep )
220 // we have some replicators, so let's see what the relevant one decides about this object
221 TObject* specified = fRepFiMap->FindObject(o->GetName()); // FindObject finds key=o->GetName() in the map
224 AliAODBranchReplicator* repfi = dynamic_cast<AliAODBranchReplicator*>(fRepFiMap->GetValue(o->GetName())); // GetValue gets the replicator corresponding to key=o->GetName()
227 TList* replicatedList = repfi->GetList();
230 AliAODEvent::AssignIDtoCollection(replicatedList);
231 TIter nextRep(replicatedList);
233 while ( ( objRep = nextRep() ) )
235 if ( !fObjectList->FindObject(objRep) ) // insure we're not adding several times the same object
237 fObjectList->Add(objRep);
243 AliError(Form("replicatedList from %s is null !",repfi->GetName()));
249 if ( !TestBit(kDropUnspecifiedBranches) )
251 // object o will be transmitted to the output AOD, unchanged
258 // no replicator, so decide based on the policy about dropping unspecified branches
259 if ( mustKeep || !TestBit(kDropUnspecifiedBranches) )
261 // object o will be transmitted to the output AOD, unchanged
267 if (fEnableReferences)
272 fTreeE->Branch(fObjectList);
279 //______________________________________________________________________________
280 void AliAODExtension::Print(Option_t* opt) const
282 // Print info about this extension
284 cout << opt << Form("%s - %s - %s - aod %p",IsFilteredAOD() ? "FilteredAOD" : "Extension",
285 GetName(),GetTitle(),GetAOD()) << endl;
286 if ( !fEnableReferences )
288 cout << opt << opt << "References are disabled ! Hope you know what you are doing !" << endl;
290 if ( TestBit(kDropUnspecifiedBranches) )
292 cout << opt << opt << "All branches not explicitely specified will be dropped" << endl;
295 TIter next(fRepFiMap);
298 while ( ( s = static_cast<TObjString*>(next()) ) )
300 AliAODBranchReplicator* br = static_cast<AliAODBranchReplicator*>(fRepFiMap->GetValue(s->String().Data()));
302 cout << opt << opt << "Branch " << s->String();
305 cout << " will be filtered by class " << br->ClassName();
309 cout << " will be transmitted as is";
315 //______________________________________________________________________________
316 void AliAODExtension::SetEvent(AliAODEvent* event)
318 // Connects to an external event
319 if (!IsFilteredAOD()) {
320 Error("SetEvent", "Not allowed to set external event for non filtered AOD's");
326 //______________________________________________________________________________
327 void AliAODExtension::AddAODtoTreeUserInfo()
329 // Add aod event to tree user info
333 AliAODEvent* aodEvent(fAODEvent);
335 if ( IsFilteredAOD() )
337 // cannot attach fAODEvent (which is shared with our AliAODHandler mother)
338 // so we create a custom (empty) AliAODEvent
339 // Has also the advantage we can specify only the list of objects
340 // that are actually there in this filtered aod
342 aodEvent = new AliAODEvent;
343 TIter nextObj(fObjectList);
345 while ( ( o = nextObj() ) )
347 aodEvent->AddObject(o);
351 fTreeE->GetUserInfo()->Add(aodEvent);
354 //______________________________________________________________________________
355 Bool_t AliAODExtension::TerminateIO()
358 if (TObject::TestBit(kFilteredAOD))
359 printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
361 printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);
374 //______________________________________________________________________________
375 void AliAODExtension::FilterBranch(const char* branchName, AliAODBranchReplicator* repfi)
377 // Specify a filter/replicator for a given branch
379 // If repfi=0x0, this will disable the branch (in the output) completely.
381 // repfi is adopted by this class, i.e. user should not delete it.
383 // WARNING : branch name must be exact.
385 // See also the documentation for AliAODBranchReplicator class.
390 fRepFiMap = new TMap;
391 fRepFiMap->SetOwnerKeyValue(kTRUE,kTRUE);
392 fRepFiList = new TList;
393 fRepFiList->SetOwner(kFALSE);
396 fRepFiMap->Add(new TObjString(branchName),repfi);
398 if (repfi && !fRepFiList->FindObject(repfi))
400 // insure we get unique and non-null replicators in this list
401 fRepFiList->Add(repfi);