1 // **************************************
2 // Task used for the correction of determiantion of reconstructed jet spectra
3 // Compares input (gen) and output (rec) jets
4 // *******************************************
7 /**************************************************************************
8 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
10 * Author: The ALICE Off-line Project. *
11 * Contributors are mentioned in the code where appropriate. *
13 * Permission to use, copy, modify and distribute this software and its *
14 * documentation strictly for non-commercial purposes is hereby granted *
15 * without fee, provided that the above copyright notice appears in all *
16 * copies and that both the copyright notice and this permission notice *
17 * appear in the supporting documentation. The authors make no claims *
18 * about the suitability of this software for any purpose. It is *
19 * provided "as is" without express or implied warranty. *
20 **************************************************************************/
26 #include <THnSparse.h>
28 #include <TInterpreter.h>
30 #include <TLorentzVector.h>
31 #include <TRefArray.h>
32 #include "TDatabasePDG.h"
34 #include "AliAnalysisTaskJetBackgroundSubtract.h"
35 #include "AliAnalysisManager.h"
36 #include "AliAODHandler.h"
37 #include "AliAODTrack.h"
38 #include "AliAODJet.h"
39 #include "AliAODEvent.h"
40 #include "AliInputEventHandler.h"
41 #include "AliAODJetEventBackground.h"
44 ClassImp(AliAnalysisTaskJetBackgroundSubtract)
46 AliAnalysisTaskJetBackgroundSubtract::~AliAnalysisTaskJetBackgroundSubtract(){
48 delete fOutJetArrayList;
49 delete fInJetArrayList;
52 AliAnalysisTaskJetBackgroundSubtract::AliAnalysisTaskJetBackgroundSubtract():
57 fJBArray(new TObjArray()),
58 fBackgroundBranch(""),
60 fReplaceString1("B0"),
61 fReplaceString2("B%d"),
64 fOutJetArrayList(0x0),
70 AliAnalysisTaskJetBackgroundSubtract::AliAnalysisTaskJetBackgroundSubtract(const char* name):
72 AliAnalysisTaskSE(name),
76 fJBArray(new TObjArray()),
77 fBackgroundBranch(""),
79 fReplaceString1("B0"),
80 fReplaceString2("B%d"),
83 fOutJetArrayList(0x0),
86 DefineOutput(1, TList::Class());
91 Bool_t AliAnalysisTaskJetBackgroundSubtract::Notify()
94 fAODIn = dynamic_cast<AliAODEvent*>(InputEvent());
98 // Now we also have the Input Event Available! Fillvthe pointers in the list
100 fInJetArrayList->Clear();
101 fOutJetArrayList->Clear();
103 for(int iJB = 0;iJB<fJBArray->GetEntries();iJB++){
104 TObjString *ostr = (TObjString*)fJBArray->At(iJB);
106 TClonesArray* jarray = 0;
107 if(!jarray&&fAODOut){
108 jarray = (TClonesArray*)(fAODOut->FindListObject(ostr->GetString().Data()));
110 if(!jarray&&fAODExtension){
111 jarray = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(ostr->GetString().Data()));
114 jarray = (TClonesArray*)(fAODIn->FindListObject(ostr->GetString().Data()));
119 Printf("%s:%d Input jet branch %s not found",(char*)__FILE__,__LINE__,ostr->GetString().Data());
124 TString newName(ostr->GetString().Data());
125 newName.ReplaceAll(fReplaceString1.Data(),Form(fReplaceString2.Data(),fSubtraction));
126 TClonesArray* jarrayOut = 0;
127 if(!jarrayOut&&fAODOut){
128 jarrayOut = (TClonesArray*)(fAODOut->FindListObject(newName.Data()));
130 if(!jarrayOut&&fAODExtension){
131 jarrayOut = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(newName.Data()));
136 Printf("%s:%d Output jet branch %s not found",(char*)__FILE__,__LINE__,newName.Data());
141 if(jarrayOut&&jarray){
142 fOutJetArrayList->Add(jarrayOut);
143 fInJetArrayList->Add(jarray);
149 void AliAnalysisTaskJetBackgroundSubtract::UserCreateOutputObjects()
153 // Create the output container
157 if (fDebug > 1) printf("AnalysisTaskJetBackgroundSubtract::UserCreateOutputObjects() \n");
158 if(fNonStdFile.Length()!=0){
160 // case that we have an AOD extension we need to fetch the jets from the extended output
161 // we identifay the extension aod event by looking for the branchname
162 AliAODHandler *aodH = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler());
163 fAODExtension = aodH->GetExtension(fNonStdFile.Data());
166 if(fDebug>1)Printf("AODExtension found for %s",fNonStdFile.Data());
169 fAODOut = AODEvent();
171 // usually we do not have the input already here
173 if(!fInJetArrayList)fInJetArrayList =new TList();
174 if(!fOutJetArrayList)fOutJetArrayList =new TList();
176 for(int iJB = 0;iJB<fJBArray->GetEntries();iJB++){
177 TObjString *ostr = (TObjString*)fJBArray->At(iJB);
178 TString newName(ostr->GetString().Data());
179 if(!newName.Contains(fReplaceString1.Data())){
180 Printf("%s:%d cannot replace string %s in %s",(char*)__FILE__,__LINE__,fReplaceString1.Data(),
185 // add a new branch to the output for the background subtracted jets take the names from
186 // the input jets and replace the background flag names
187 TClonesArray *tca = new TClonesArray("AliAODJet", 0);
188 newName.ReplaceAll(fReplaceString1.Data(),Form(fReplaceString2.Data(),fSubtraction));
190 Printf("%s:%d created branch \n %s from \n %s",(char*)__FILE__,__LINE__,newName.Data(),
191 ostr->GetString().Data());
193 tca->SetName(newName.Data());
194 AddAODBranch("TClonesArray",&tca,fNonStdFile.Data());
198 if(!fHistList)fHistList = new TList();
199 fHistList->SetOwner();
201 for(int iJB = 0;iJB<fJBArray->GetEntries();iJB++){
202 TObjString *ostr = (TObjString*)fJBArray->At(iJB);
203 TString oldName(ostr->GetString().Data());
204 TString newName(ostr->GetString().Data());
205 if(!newName.Contains(fReplaceString1.Data())){
206 Printf("%s:%d cannot replace string %s in %s",(char*)__FILE__,__LINE__,fReplaceString1.Data(),
210 newName.ReplaceAll(fReplaceString1.Data(),Form(fReplaceString2.Data(),fSubtraction));
211 TH2F *hTmp = new TH2F(Form("h2PtInPtOut_%d",iJB),Form(";%s p_{T}; %s p_{T}",oldName.Data(),newName.Data()),200,0,200.,200,0.,200.);
212 fHistList->Add(hTmp);
215 Bool_t oldStatus = TH1::AddDirectoryStatus();
216 TH1::AddDirectory(kFALSE);
219 // Histogram booking, add som control histograms here
222 // =========== Switch on Sumw2 for all histos ===========
223 for (Int_t i=0; i<fHistList->GetEntries(); ++i) {
224 TH1 *h1 = dynamic_cast<TH1*>(fHistList->At(i));
229 THnSparse *hn = dynamic_cast<THnSparse*>(fHistList->At(i));
232 TH1::AddDirectory(oldStatus);
234 if(fBackgroundBranch.Length()==0)
235 AliError(Form("%s:%d No BackgroundBranch defined",(char*)__FILE__,__LINE__));
236 if(fJBArray->GetEntries()==0)
237 AliError(Form("%s:%d No Jet Branches defined defined",(char*)__FILE__,__LINE__));
240 void AliAnalysisTaskJetBackgroundSubtract::Init()
245 if (fDebug > 1) printf("AnalysisTaskJetBackgroundSubtract::Init() \n");
248 void AliAnalysisTaskJetBackgroundSubtract::UserExec(Option_t */*option*/)
251 if (fDebug > 1) printf("AnalysisTaskJetBackgroundSubtract::UserExec() \n");
253 if(fBackgroundBranch.Length()==0||fJBArray->GetEntries()==0){
254 if(fDebug)Printf("%s:%d No background subtraction done",(char*)__FILE__,__LINE__);
255 PostData(1,fHistList);
257 if(fJBArray->GetEntries()!=fInJetArrayList->GetEntries()){
258 if(fDebug)Printf("%s:%d different Array sizes %d %d %d",(char*)__FILE__,__LINE__,fJBArray->GetEntries(),fInJetArrayList->GetEntries(),fOutJetArrayList->GetEntries());
259 PostData(1,fHistList);
264 static AliAODJetEventBackground* evBkg = 0;
265 static TClonesArray* bkgClusters = 0;
266 static TString bkgClusterName(fBackgroundBranch.Data());
267 bkgClusterName.ReplaceAll(Form("%s_",AliAODJetEventBackground::StdBranchName()),"");
268 if(!evBkg&&!bkgClusters&&fAODOut){
269 evBkg = (AliAODJetEventBackground*)(fAODOut->FindListObject(fBackgroundBranch.Data()));
270 bkgClusters = (TClonesArray*)(fAODOut->FindListObject(bkgClusterName.Data()));
271 if(fDebug&&bkgClusters)Printf("%s:%d Background cluster branch %s found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
272 if(fDebug&&evBkg)Printf("%s:%d Backgroundbranch %s found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
274 if(!evBkg&&!bkgClusters&&fAODExtension){
275 evBkg = (AliAODJetEventBackground*)(fAODExtension->GetAOD()->FindListObject(fBackgroundBranch.Data()));
276 bkgClusters = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(bkgClusterName.Data()));
277 if(fDebug&&bkgClusters)Printf("%s:%d Background cluster branch %s found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
278 if(fDebug&&evBkg)Printf("%s:%d Backgroundbranch %s found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
281 if(!evBkg&&!bkgClusters&&fAODIn){
282 evBkg = (AliAODJetEventBackground*)(fAODIn->FindListObject(fBackgroundBranch.Data()));
283 bkgClusters = (TClonesArray*)(fAODIn->FindListObject(bkgClusterName.Data()));
284 if(fDebug&&bkgClusters)Printf("%s:%d Background cluster branch %s found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
285 if(fDebug&&evBkg)Printf("%s:%d Backgroundbranch %s found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
290 Printf("%s:%d Backgroundbranch %s not found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
293 PostData(1,fHistList);
299 Printf("%s:%d Background cluster branch %s not found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
302 PostData(1,fHistList);
307 // LOOP over all jet branches and subtract the background
310 if(fSubtraction==kArea)rho = evBkg->GetBackground(2);
313 for(int iJB = 0;iJB<fInJetArrayList->GetEntries();iJB++){
314 TClonesArray* jarray = (TClonesArray*)fInJetArrayList->At(iJB);
315 TClonesArray* jarrayOut = (TClonesArray*)fOutJetArrayList->At(iJB);
317 if(!jarray||!jarrayOut){
318 Printf("%s:%d Array not found %d: %p %p",(char*)__FILE__,__LINE__,iJB,jarray,jarrayOut);
321 TH2F* h2PtInOut = (TH2F*)fHistList->FindObject(Form("h2PtInPtOut_%d",iJB));
322 // loop over all jets
324 for(int i = 0;i < jarray->GetEntriesFast();i++){
325 AliAODJet *jet = (AliAODJet*)jarray->At(i);
326 AliAODJet tmpNewJet(*jet);
328 if(fSubtraction==kArea){
329 Double_t background = rho * jet->EffectiveAreaCharged();
330 Float_t ptSub = jet->Pt() - background;
332 Printf("%s:%d Jet %d %3.3f %3.3f",(char*)__FILE__,__LINE__,i,jet->Pt(),ptSub);
335 // optionally rescale it and keep??
336 bAdd = RescaleJetMomentum(&tmpNewJet,0.1);
337 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),0.1);
340 bAdd = RescaleJetMomentum(&tmpNewJet,ptSub);
341 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),ptSub);
343 // add background estimates to the new jet object
344 // allows to recover old p_T and rho...
345 tmpNewJet.SetBgEnergy(background,0);
347 else if(fSubtraction==kRhoRecalc1){
348 // Put a function call to calculate rho here
350 // * exclude clusters with small area
351 // * exclude areas around leading jets
355 AliAODJet *newJet = new ((*jarrayOut)[nOut++]) AliAODJet(tmpNewJet);
356 // what about track references, clear for now...
357 newJet->GetRefTracks()->Clear();
364 // subtract the background
372 PostData(1, fHistList);
375 void AliAnalysisTaskJetBackgroundSubtract::Terminate(Option_t */*option*/)
377 // Terminate analysis
379 if (fDebug > 1) printf("AnalysisJetBackgroundSubtract: Terminate() \n");
382 Bool_t AliAnalysisTaskJetBackgroundSubtract::RescaleJetMomentum(AliAODJet *jet,Float_t pT){
383 // keep the direction and the jet mass
384 if(pT<=0)return kFALSE;
385 Double_t pTold = jet->Pt();
386 Double_t scale = pT/pTold;
387 Double_t mass = jet->M();
388 Double_t pNew = jet->P() * scale;
389 jet->SetPxPyPzE(scale*jet->Px(),scale*jet->Py(),scale*jet->Pz(),TMath::Sqrt(mass*mass+pNew*pNew));
393 void AliAnalysisTaskJetBackgroundSubtract::ResetOutJets(){
394 if(!fOutJetArrayList)return;
395 for(int iJB = 0;iJB<fOutJetArrayList->GetEntries();iJB++){
396 TClonesArray* jarray = (TClonesArray*)fOutJetArrayList->At(iJB);
397 if(jarray)jarray->Delete();
402 void AliAnalysisTaskJetBackgroundSubtract::PrintAODContents(){
404 Printf("%s:%d >>>>>> Input",(char*)__FILE__,__LINE__);
408 Printf("%s:%d >>>>>> Extenstion",(char*)__FILE__,__LINE__);
409 fAODExtension->GetAOD()->Print();
412 Printf("%s:%d >>>>>> Output",(char*)__FILE__,__LINE__);