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"),
62 fSubtraction(kRhoRecalc),
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"),
81 fSubtraction(kRhoRecalc),
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);
107 TClonesArray* jarray = 0;
108 if(!jarray&&fAODOut){
109 jarray = (TClonesArray*)(fAODOut->FindListObject(ostr->GetString().Data()));
111 if(!jarray&&fAODExtension){
112 jarray = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(ostr->GetString().Data()));
115 jarray = (TClonesArray*)(fAODIn->FindListObject(ostr->GetString().Data()));
120 Printf("%s:%d Input jet branch %s not found",(char*)__FILE__,__LINE__,ostr->GetString().Data());
125 TString newName(ostr->GetString().Data());
126 newName.ReplaceAll(fReplaceString1.Data(),Form(fReplaceString2.Data(),fSubtraction));
127 TClonesArray* jarrayOut = 0;
128 if(!jarrayOut&&fAODOut){
129 jarrayOut = (TClonesArray*)(fAODOut->FindListObject(newName.Data()));
131 if(!jarrayOut&&fAODExtension){
132 jarrayOut = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(newName.Data()));
137 Printf("%s:%d Output jet branch %s not found",(char*)__FILE__,__LINE__,newName.Data());
142 if(jarrayOut&&jarray){
143 fOutJetArrayList->Add(jarrayOut);
144 fInJetArrayList->Add(jarray);
150 void AliAnalysisTaskJetBackgroundSubtract::UserCreateOutputObjects()
154 // Create the output container
158 if (fDebug > 1) printf("AnalysisTaskJetBackgroundSubtract::UserCreateOutputObjects() \n");
159 if(fNonStdFile.Length()!=0){
161 // case that we have an AOD extension we need to fetch the jets from the extended output
162 // we identifay the extension aod event by looking for the branchname
163 AliAODHandler *aodH = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler());
164 fAODExtension = aodH->GetExtension(fNonStdFile.Data());
167 if(fDebug>1)Printf("AODExtension found for %s",fNonStdFile.Data());
170 fAODOut = AODEvent();
172 // usually we do not have the input already here
174 if(!fInJetArrayList)fInJetArrayList =new TList();
175 if(!fOutJetArrayList)fOutJetArrayList =new TList();
177 for(int iJB = 0;iJB<fJBArray->GetEntries();iJB++){
178 TObjString *ostr = (TObjString*)fJBArray->At(iJB);
179 TString newName(ostr->GetString().Data());
180 if(!newName.Contains(fReplaceString1.Data())){
181 Printf("%s:%d cannot replace string %s in %s",(char*)__FILE__,__LINE__,fReplaceString1.Data(),
186 // add a new branch to the output for the background subtracted jets take the names from
187 // the input jets and replace the background flag names
188 TClonesArray *tca = new TClonesArray("AliAODJet", 0);
189 newName.ReplaceAll(fReplaceString1.Data(),Form(fReplaceString2.Data(),fSubtraction));
191 Printf("%s:%d created branch \n %s from \n %s",(char*)__FILE__,__LINE__,newName.Data(),
192 ostr->GetString().Data());
194 tca->SetName(newName.Data());
195 AddAODBranch("TClonesArray",&tca,fNonStdFile.Data());
199 if(!fHistList)fHistList = new TList();
200 fHistList->SetOwner();
202 for(int iJB = 0;iJB<fJBArray->GetEntries();iJB++){
203 TObjString *ostr = (TObjString*)fJBArray->At(iJB);
204 TString oldName(ostr->GetString().Data());
205 TString newName(ostr->GetString().Data());
206 if(!newName.Contains(fReplaceString1.Data())){
207 Printf("%s:%d cannot replace string %s in %s",(char*)__FILE__,__LINE__,fReplaceString1.Data(),
211 newName.ReplaceAll(fReplaceString1.Data(),Form(fReplaceString2.Data(),fSubtraction));
212 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.);
213 fHistList->Add(hTmp);
216 Bool_t oldStatus = TH1::AddDirectoryStatus();
217 TH1::AddDirectory(kFALSE);
220 // Histogram booking, add som control histograms here
223 // =========== Switch on Sumw2 for all histos ===========
224 for (Int_t i=0; i<fHistList->GetEntries(); ++i) {
225 TH1 *h1 = dynamic_cast<TH1*>(fHistList->At(i));
230 THnSparse *hn = dynamic_cast<THnSparse*>(fHistList->At(i));
233 TH1::AddDirectory(oldStatus);
235 if(fBackgroundBranch.Length()==0)
236 AliError(Form("%s:%d No BackgroundBranch defined",(char*)__FILE__,__LINE__));
237 if(fJBArray->GetEntries()==0)
238 AliError(Form("%s:%d No Jet Branches defined defined",(char*)__FILE__,__LINE__));
241 void AliAnalysisTaskJetBackgroundSubtract::Init()
246 if (fDebug > 1) printf("AnalysisTaskJetBackgroundSubtract::Init() \n");
249 void AliAnalysisTaskJetBackgroundSubtract::UserExec(Option_t */*option*/)
252 if (fDebug > 1) printf("AnalysisTaskJetBackgroundSubtract::UserExec() \n");
254 if(fBackgroundBranch.Length()==0||fJBArray->GetEntries()==0){
255 if(fDebug)Printf("%s:%d No background subtraction done",(char*)__FILE__,__LINE__);
256 PostData(1,fHistList);
258 if(fJBArray->GetEntries()!=fInJetArrayList->GetEntries()){
259 if(fDebug)Printf("%s:%d different Array sizes %d %d %d",(char*)__FILE__,__LINE__,fJBArray->GetEntries(),fInJetArrayList->GetEntries(),fOutJetArrayList->GetEntries());
260 PostData(1,fHistList);
265 AliAODJetEventBackground* evBkg = 0;
266 TClonesArray* bkgClusters = 0;
267 TClonesArray* bkgClustersRC = 0;
268 TString bkgClusterName(fBackgroundBranch.Data());
269 bkgClusterName.ReplaceAll(Form("%s_",AliAODJetEventBackground::StdBranchName()),"");
270 TString bkgClusterRCName(Form("%s%s",bkgClusterName.Data(),"RandomCone"));
272 if(!evBkg&&!bkgClusters&&!bkgClustersRC&&fAODOut){
273 evBkg = (AliAODJetEventBackground*)(fAODOut->FindListObject(fBackgroundBranch.Data()));
274 bkgClusters = (TClonesArray*)(fAODOut->FindListObject(bkgClusterName.Data()));
275 bkgClustersRC = (TClonesArray*)(fAODOut->FindListObject(bkgClusterRCName.Data()));
277 if(fDebug&&bkgClusters)Printf("%s:%d Background cluster branch %s found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
278 if(fDebug&&bkgClustersRC)Printf("%s:%d Background cluster RC branch %s found",(char*)__FILE__,__LINE__,bkgClusterRCName.Data());
279 if(fDebug&&evBkg)Printf("%s:%d Backgroundbranch %s found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
281 if(!evBkg&&!bkgClusters&&!bkgClustersRC&&fAODExtension){
282 evBkg = (AliAODJetEventBackground*)(fAODExtension->GetAOD()->FindListObject(fBackgroundBranch.Data()));
283 bkgClusters = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(bkgClusterName.Data()));
284 bkgClustersRC = (TClonesArray*)(fAODExtension->GetAOD()->FindListObject(bkgClusterRCName.Data()));
285 if(fDebug&&bkgClusters)Printf("%s:%d Background cluster branch %s found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
286 if(fDebug&&bkgClustersRC)Printf("%s:%d Background cluster RC branch %s found",(char*)__FILE__,__LINE__,bkgClusterRCName.Data());
288 if(fDebug&&evBkg)Printf("%s:%d Backgroundbranch %s found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
291 if(!evBkg&&!bkgClusters&&!bkgClustersRC&&fAODIn){
292 evBkg = (AliAODJetEventBackground*)(fAODIn->FindListObject(fBackgroundBranch.Data()));
293 bkgClusters = (TClonesArray*)(fAODIn->FindListObject(bkgClusterName.Data()));
294 bkgClustersRC = (TClonesArray*)(fAODIn->FindListObject(bkgClusterRCName.Data()));
296 if(fDebug&&bkgClusters)Printf("%s:%d Background cluster branch %s found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
297 if(fDebug&&bkgClustersRC)Printf("%s:%d Background cluster RC branch %s found",(char*)__FILE__,__LINE__,bkgClusterRCName.Data());
298 if(fDebug&&evBkg)Printf("%s:%d Backgroundbranch %s found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
301 if(!evBkg&&(fSubtraction==kArea||fSubtraction==kRhoRecalc)){
303 Printf("%s:%d Backgroundbranch %s not found",(char*)__FILE__,__LINE__,fBackgroundBranch.Data());
306 PostData(1,fHistList);
310 if(!bkgClusters&&(fSubtraction==kRhoRecalc)){
312 Printf("%s:%d Background cluster branch %s not found",(char*)__FILE__,__LINE__,bkgClusterName.Data());
315 PostData(1,fHistList);
319 if(!bkgClustersRC&&(fSubtraction==kRhoRC)){
321 Printf("%s:%d Background cluster RC branch %s not found",(char*)__FILE__,__LINE__,bkgClusterRCName.Data());
324 PostData(1,fHistList);
327 // LOOP over all jet branches and subtract the background
330 Double_t meanarea = 0;
331 if(fSubtraction==kArea)rho = evBkg->GetBackground(2);
332 if(fSubtraction==kRhoRecalc){
333 meanarea=evBkg->GetMeanarea(2);
334 rho =RecalcRho(bkgClusters,meanarea);
336 if(fSubtraction==kRhoRC)rho =RhoRC(bkgClustersRC);
338 for(int iJB = 0;iJB<fInJetArrayList->GetEntries();iJB++){
339 TClonesArray* jarray = (TClonesArray*)fInJetArrayList->At(iJB);
340 TClonesArray* jarrayOut = (TClonesArray*)fOutJetArrayList->At(iJB);
342 if(!jarray||!jarrayOut){
343 Printf("%s:%d Array not found %d: %p %p",(char*)__FILE__,__LINE__,iJB,jarray->GetName(),jarrayOut->GetName());
346 TH2F* h2PtInOut = (TH2F*)fHistList->FindObject(Form("h2PtInPtOut_%d",iJB));
347 // loop over all jets
351 for(int i = 0;i < jarray->GetEntriesFast();i++){
352 AliAODJet *jet = (AliAODJet*)jarray->At(i);
353 AliAODJet tmpNewJet(*jet);
357 if(fSubtraction==kArea){
358 Double_t background = rho * jet->EffectiveAreaCharged();
359 Float_t ptSub = jet->Pt() - background;
361 Printf("%s:%d Jet %d %3.3f %3.3f",(char*)__FILE__,__LINE__,i,jet->Pt(),ptSub);
364 // optionally rescale it and keep??
365 bAdd = RescaleJetMomentum(&tmpNewJet,0.1);
366 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),0.1);
369 bAdd = RescaleJetMomentum(&tmpNewJet,ptSub);
370 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),ptSub);
372 // add background estimates to the new jet object
373 // allows to recover old p_T and rho...
374 tmpNewJet.SetBgEnergy(background,0);
376 else if(fSubtraction==kRhoRecalc){
377 Double_t background = rho * jet->EffectiveAreaCharged();
378 Float_t ptSub = jet->Pt() - background;
380 Printf("%s:%d Jet %d %3.3f %3.3f %3.3f %3.3f",(char*)__FILE__,__LINE__,i,jet->Pt(),ptSub,background,rho);}
382 // optionally rescale it and keep??
383 bAdd = RescaleJetMomentum(&tmpNewJet,0.1);
384 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),0.1);
387 bAdd = RescaleJetMomentum(&tmpNewJet,ptSub);
388 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),ptSub);
390 // add background estimates to the new jet object
391 // allows to recover old p_T and rho...
392 tmpNewJet.SetBgEnergy(background,0);
395 else if(fSubtraction==kRhoRC){
396 Double_t background = rho * jet->EffectiveAreaCharged();
397 Float_t ptSub = jet->Pt() - background;
398 if(fDebug>2){ Printf("%s:%d Jet %d %3.3f %3.3f %3.3f %3.3f",(char*)__FILE__,__LINE__,i,jet->Pt(),ptSub,background,rho);}
400 // optionally rescale it and keep??
401 bAdd = RescaleJetMomentum(&tmpNewJet,0.1);
402 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),0.1);
405 bAdd = RescaleJetMomentum(&tmpNewJet,ptSub);
406 if(h2PtInOut)h2PtInOut->Fill(jet->Pt(),ptSub);
408 // add background estimates to the new jet object
409 // allows to recover old p_T and rho...
410 tmpNewJet.SetBgEnergy(background,0);
415 AliAODJet *newJet = new ((*jarrayOut)[nOut++]) AliAODJet(tmpNewJet);
416 // what about track references, clear for now...
417 newJet->GetRefTracks()->Clear();
429 // subtract the background
437 PostData(1, fHistList);
440 void AliAnalysisTaskJetBackgroundSubtract::Terminate(Option_t */*option*/)
442 // Terminate analysis
444 if (fDebug > 1) printf("AnalysisJetBackgroundSubtract: Terminate() \n");
447 Bool_t AliAnalysisTaskJetBackgroundSubtract::RescaleJetMomentum(AliAODJet *jet,Float_t pT){
448 // keep the direction and the jet mass
449 if(pT<=0)return kFALSE;
450 Double_t pTold = jet->Pt();
451 Double_t scale = pT/pTold;
452 Double_t mass = jet->M();
453 Double_t pNew = jet->P() * scale;
454 jet->SetPxPyPzE(scale*jet->Px(),scale*jet->Py(),scale*jet->Pz(),TMath::Sqrt(mass*mass+pNew*pNew));
458 Double_t AliAnalysisTaskJetBackgroundSubtract::RecalcRho(TClonesArray* bkgClusters,Double_t meanarea){
463 const Double_t Rlimit2=0.8*0.8; //2*jet radius.
464 TClonesArray* jarray=0;
466 for(int iJB = 0;iJB<fInJetArrayList->GetEntries();iJB++){
467 TObjString *ostr = (TObjString*)fInJetArrayList->At(iJB);
468 TString jetref=ostr->GetString().Data();
469 if(jetref.Contains("ANTIKT04")){
470 jarray = (TClonesArray*)fInJetArrayList->At(iJB);}}
471 if(jarray->GetEntries()>=2){
472 AliAODJet *first = (AliAODJet*)(jarray->At(0));
473 AliAODJet *second= (AliAODJet*)(jarray->At(1));
474 for(Int_t k=0;k<bkgClusters->GetEntriesFast();k++){
475 AliAODJet *clus = (AliAODJet*)(bkgClusters->At(k));
476 if(TMath::Abs(clus->Eta())>0.5) continue;
477 if((clus->EffectiveAreaCharged())<0.1*meanarea) continue;
478 Double_t distance1=(first->Eta()-clus->Eta())*(first->Eta()-clus->Eta())+
479 (first->Phi()-clus->Phi())*(first->Phi()-clus->Phi());
480 Double_t distance2= (second->Eta()-clus->Eta())*(second->Eta()-clus->Eta())+
481 (second->Phi()-clus->Phi())*(second->Phi()-clus->Phi());
482 if((distance1<Rlimit2)||(distance2<Rlimit2)) continue;
483 ptarea=ptarea+clus->Pt()/clus->EffectiveAreaCharged();
485 if(count!=0) rho=ptarea/count;
490 Double_t AliAnalysisTaskJetBackgroundSubtract::RhoRC(TClonesArray* bkgClustersRC){
495 const Double_t Rlimit2=0.8*0.8; //2*jet radius.
496 TClonesArray* jarray=0;
497 for(int iJB = 0;iJB<fInJetArrayList->GetEntries();iJB++){
498 TObjString *ostr = (TObjString*)fInJetArrayList->At(iJB);
499 TString jetref=ostr->GetString().Data();
500 if(jetref.Contains("ANTIKT04")){
501 jarray = (TClonesArray*)fInJetArrayList->At(iJB);}}
502 if(jarray->GetEntries()>=2){
503 AliAODJet *first = (AliAODJet*)(jarray->At(0));
504 AliAODJet *second=(AliAODJet*)(jarray->At(1));
505 for(Int_t k=0;k<bkgClustersRC->GetEntriesFast();k++){
506 AliAODJet *clus = (AliAODJet*)(bkgClustersRC->At(k));
507 if(TMath::Abs(clus->Eta())>0.5) continue;
508 Double_t distance1=(first->Eta()-clus->Eta())*(first->Eta()-clus->Eta())+
509 (first->Phi()-clus->Phi())*(first->Phi()-clus->Phi());
510 Double_t distance2= (second->Eta()-clus->Eta())*(second->Eta()-clus->Eta())+
511 (second->Phi()-clus->Phi())*(second->Phi()-clus->Phi());
512 if((distance1<Rlimit2)||(distance2<Rlimit2)) continue;
513 ptarea=ptarea+clus->Pt()/clus->EffectiveAreaCharged();
515 if(count!=0) rho=ptarea/count; }
527 void AliAnalysisTaskJetBackgroundSubtract::ResetOutJets(){
528 if(!fOutJetArrayList)return;
529 for(int iJB = 0;iJB<fOutJetArrayList->GetEntries();iJB++){
530 TClonesArray* jarray = (TClonesArray*)fOutJetArrayList->At(iJB);
531 if(jarray)jarray->Delete();
536 void AliAnalysisTaskJetBackgroundSubtract::PrintAODContents(){
538 Printf("%s:%d >>>>>> Input",(char*)__FILE__,__LINE__);
542 Printf("%s:%d >>>>>> Extenstion",(char*)__FILE__,__LINE__);
543 fAODExtension->GetAOD()->Print();
546 Printf("%s:%d >>>>>> Output",(char*)__FILE__,__LINE__);