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"
25 ClassImp(AliAODExtension)
27 //______________________________________________________________________________
28 AliAODExtension::AliAODExtension() : TNamed(),
29 fAODEvent(0), fTreeE(0), fFileE(0), fNtotal(0), fNpassed(0),
30 fSelected(kFALSE), fRepFiMap(0x0), fRepFiList(0x0), fEnableReferences(kTRUE), fObjectList(0x0)
35 //______________________________________________________________________________
36 AliAODExtension::AliAODExtension(const char* name, const char* title, Bool_t isfilter)
46 fEnableReferences(kTRUE),
51 TObject::SetBit(kFilteredAOD);
52 printf("####### Added AOD filter %s\n", name);
53 } else printf("####### Added AOD extension %s\n", name);
54 KeepUnspecifiedBranches();
57 //______________________________________________________________________________
58 AliAODExtension::~AliAODExtension()
62 // is already handled in TerminateIO
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
75 //______________________________________________________________________________
76 void AliAODExtension::AddBranch(const char* cname, void* addobj)
78 // Add a new branch to the aod
82 gROOT->ProcessLine(Form("TString s_tmp; AliAnalysisManager::GetAnalysisManager()->GetAnalysisTypeString(s_tmp); sprintf((char*)%p, \"%%s\", s_tmp.Data());", type));
85 TDirectory *owd = gDirectory;
89 char** apointer = (char**) addobj;
90 TObject* obj = (TObject*) *apointer;
92 fAODEvent->AddObject(obj);
94 TString bname(obj->GetName());
96 if (!fTreeE->FindBranch(bname.Data()))
98 Bool_t acceptAdd(kTRUE);
100 if ( TestBit(kDropUnspecifiedBranches) )
102 // check that this branch is in our list of specified ones...
103 // otherwise do not add it !
104 TIter next(fRepFiMap);
109 while ( ( p = static_cast<TObjString*>(next()) ) && !acceptAdd )
111 if ( p->String() == bname ) acceptAdd=kTRUE;
117 // Do the same as if we book via
118 // TTree::Branch(TCollection*)
120 fObjectList->Add(obj);
122 const Int_t kSplitlevel = 99; // default value in TTree::Branch()
123 const Int_t kBufsize = 32000; // default value in TTree::Branch()
125 fTreeE->Bronch(bname.Data(), cname,
126 fAODEvent->GetList()->GetObjectRef(obj),
127 kBufsize, kSplitlevel - 1);
133 //______________________________________________________________________________
134 Bool_t AliAODExtension::FinishEvent()
136 // Fill current event.
138 if (!IsFilteredAOD()) {
139 fAODEvent->MakeEntriesReferencable();
143 // Filtered AOD. Fill only if event is selected.
144 if (!fSelected) return kTRUE;
146 TIter next(fRepFiList);
148 AliAODBranchReplicator* repfi;
150 while ( ( repfi = static_cast<AliAODBranchReplicator*>(next()) ) )
152 repfi->ReplicateAndFilter(*fAODEvent);
156 fSelected = kFALSE; // so that next event will not be selected unless demanded
160 //______________________________________________________________________________
161 Bool_t AliAODExtension::Init(Option_t *option)
165 AliCodeTimerAuto(GetName(),0);
169 fAODEvent = new AliAODEvent();
172 TDirectory *owd = gDirectory;
176 if (opt.Contains("proof"))
179 // Merging via files. Need to access analysis manager via interpreter.
180 gROOT->ProcessLine(Form("AliAnalysisDataContainer *c_common_out = AliAnalysisManager::GetAnalysisManager()->GetCommonOutputContainer();"));
181 gROOT->ProcessLine(Form("AliAnalysisManager::GetAnalysisManager()->OpenProofFile(c_common_out, \"RECREATE\", \"%s\");", fName.Data()));
186 fFileE = new TFile(GetName(), "RECREATE");
188 fTreeE = new TTree("aodTree", "AliAOD tree");
191 fObjectList = new TList;
192 fObjectList->SetOwner(kFALSE); // be explicit we're not the owner...
193 TList* inputList = fAODEvent->GetList();
194 TIter next(inputList);
197 while ( ( o = next() ) )
199 // Loop on the objects that are within the main AOD, and see what to do with them :
200 // - transmit them to our AOD as they are
201 // - filter them (by means of an AliAODBranchReplicator)
202 // - drop them completely
204 Bool_t mustKeep(kFALSE);
206 TString test(o->ClassName());
208 if (test.Contains("HEADER"))
210 // do not allow to drop header branch
214 if ( fRepFiMap && !mustKeep )
216 // we have some replicators, so let's see what the relevant one decides about this object
217 TObject* specified = fRepFiMap->FindObject(o->GetName()); // FindObject finds key=o->GetName() in the map
220 AliAODBranchReplicator* repfi = dynamic_cast<AliAODBranchReplicator*>(fRepFiMap->GetValue(o->GetName())); // GetValue gets the replicator corresponding to key=o->GetName()
223 TList* replicatedList = repfi->GetList();
226 AliAODEvent::AssignIDtoCollection(replicatedList);
227 TIter nextRep(replicatedList);
229 while ( ( objRep = nextRep() ) )
231 if ( !fObjectList->FindObject(objRep) ) // insure we're not adding several times the same object
233 fObjectList->Add(objRep);
239 AliError(Form("replicatedList from %s is null !",repfi->GetName()));
245 if ( !TestBit(kDropUnspecifiedBranches) )
247 // object o will be transmitted to the output AOD, unchanged
254 // no replicator, so decide based on the policy about dropping unspecified branches
255 if ( mustKeep || !TestBit(kDropUnspecifiedBranches) )
257 // object o will be transmitted to the output AOD, unchanged
263 if (fEnableReferences)
268 fTreeE->Branch(fObjectList);
275 //______________________________________________________________________________
276 void AliAODExtension::Print(Option_t* opt) const
278 // Print info about this extension
280 cout << opt << Form("%s - %s - %s - aod %p",IsFilteredAOD() ? "FilteredAOD" : "Extension",
281 GetName(),GetTitle(),GetAOD()) << endl;
282 if ( !fEnableReferences )
284 cout << opt << opt << "References are disabled ! Hope you know what you are doing !" << endl;
286 if ( TestBit(kDropUnspecifiedBranches) )
288 cout << opt << opt << "All branches not explicitely specified will be dropped" << endl;
291 TIter next(fRepFiMap);
294 while ( ( s = static_cast<TObjString*>(next()) ) )
296 AliAODBranchReplicator* br = static_cast<AliAODBranchReplicator*>(fRepFiMap->GetValue(s->String().Data()));
298 cout << opt << opt << "Branch " << s->String();
301 cout << " will be filtered by class " << br->ClassName();
305 cout << " will be transmitted as is";
311 //______________________________________________________________________________
312 void AliAODExtension::SetEvent(AliAODEvent* event)
314 // Connects to an external event
315 if (!IsFilteredAOD()) {
316 Error("SetEvent", "Not allowed to set external event for non filtered AOD's");
322 //______________________________________________________________________________
323 void AliAODExtension::AddAODtoTreeUserInfo()
325 // Add aod event to tree user info
329 AliAODEvent* aodEvent(fAODEvent);
331 if ( IsFilteredAOD() )
333 // cannot attach fAODEvent (which is shared with our AliAODHandler mother)
334 // so we create a custom (empty) AliAODEvent
335 // Has also the advantage we can specify only the list of objects
336 // that are actually there in this filtered aod
338 aodEvent = new AliAODEvent;
339 TIter nextObj(fObjectList);
341 while ( ( o = nextObj() ) )
343 aodEvent->AddObject(o);
347 fTreeE->GetUserInfo()->Add(aodEvent);
350 //______________________________________________________________________________
351 Bool_t AliAODExtension::TerminateIO()
354 if (TObject::TestBit(kFilteredAOD))
355 printf("AOD Filter %s: events processed: %d passed: %d\n", GetName(), fNtotal, fNpassed);
357 printf("AOD extension %s: events processed: %d\n", GetName(), fNtotal);
370 //______________________________________________________________________________
371 void AliAODExtension::FilterBranch(const char* branchName, AliAODBranchReplicator* repfi)
373 // Specify a filter/replicator for a given branch
375 // If repfi=0x0, this will disable the branch (in the output) completely.
377 // repfi is adopted by this class, i.e. user should not delete it.
379 // WARNING : branch name must be exact.
381 // See also the documentation for AliAODBranchReplicator class.
386 fRepFiMap = new TMap;
387 fRepFiMap->SetOwnerKeyValue(kTRUE,kTRUE);
388 fRepFiList = new TList;
389 fRepFiList->SetOwner(kFALSE);
392 fRepFiMap->Add(new TObjString(branchName),repfi);
394 if (repfi && !fRepFiList->FindObject(repfi))
396 // insure we get unique and non-null replicators in this list
397 fRepFiList->Add(repfi);