1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 //-----------------------------------------------------------------------
17 // Example of task (running locally, on AliEn and CAF),
18 // which provides standard way of calculating acceptance and efficiency
19 // between different steps of the procedure.
20 // The ouptut of the task is a AliCFContainer from which the efficiencies
22 //-----------------------------------------------------------------------
23 // Author : Marta Verweij - UU
24 //-----------------------------------------------------------------------
27 #ifndef ALIPWG4HIGHPTSPECTRA_CXX
28 #define ALIPWG4HIGHPTSPECTRA_CXX
30 #include "AliPWG4HighPtSpectra.h"
44 #include "AliAnalysisManager.h"
45 #include "AliESDInputHandler.h"
46 #include "AliESDtrack.h"
47 #include "AliESDtrackCuts.h"
48 #include "AliExternalTrackParam.h"
53 #include "TParticle.h"
54 #include "AliMCEvent.h"
55 #include "AliMCEventHandler.h"
56 #include "AliCFContainer.h"
57 #include "AliGenPythiaEventHeader.h"
58 #include "AliGenCocktailEventHeader.h"
59 //#include "$ALICE_ROOT/PWG4/JetTasks/AliAnalysisHelperJetTasks.h"
62 using namespace std; //required for resolving the 'cout' symbol
64 ClassImp(AliPWG4HighPtSpectra)
66 //__________________________________________________________________________
67 AliPWG4HighPtSpectra::AliPWG4HighPtSpectra() : AliAnalysisTask("AliPWG4HighPtSpectra", ""),
75 fTrackCutsTPConly(0x0),
90 //___________________________________________________________________________
91 AliPWG4HighPtSpectra::AliPWG4HighPtSpectra(const Char_t* name) :
92 AliAnalysisTask(name,""),
100 fTrackCutsTPConly(0x0),
112 // Constructor. Initialization of Inputs and Outputs
114 AliDebug(2,Form("AliPWG4HighPtSpectra Calling Constructor"));
115 // Input slot #0 works with a TChain ESD
116 DefineInput(0, TChain::Class());
117 // Output slot #0 writes into a TList
118 DefineOutput(0,TList::Class());
119 // Output slot #1, #2 writes into a AliCFContainer
120 DefineOutput(1,AliCFContainer::Class());
121 DefineOutput(2,AliCFContainer::Class());
122 // Output slot #3 writes into a AliESDtrackCuts
123 DefineOutput(3, AliESDtrackCuts::Class());
124 DefineOutput(4, AliESDtrackCuts::Class());
127 //________________________________________________________________________
128 void AliPWG4HighPtSpectra::LocalInit()
131 // Only called once at beginning
133 PostData(3,fTrackCuts);
134 PostData(4,fTrackCutsTPConly);
137 //________________________________________________________________________
138 void AliPWG4HighPtSpectra::ConnectInputData(Option_t *)
142 AliDebug(2,Form(">> AliPWG4HighPtSpectra::ConnectInputData \n"));
144 TTree* tree = dynamic_cast<TTree*> (GetInputData(0));
146 AliDebug(2,Form( "ERROR: Could not read chain from input slot 0 \n"));
150 AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
153 AliDebug(2,Form("ERROR: Could not get ESDInputHandler"));
156 fESD = esdH->GetEvent();
158 AliMCEventHandler *eventHandler = dynamic_cast<AliMCEventHandler*> (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
160 AliDebug(2,Form( "ERROR: Could not retrieve MC event handler \n"));
163 fMC = eventHandler->MCEvent();
167 //________________________________________________________________________
168 Bool_t AliPWG4HighPtSpectra::SelectEvent() {
170 // Decide if event should be selected for analysis
173 // Checks following requirements:
175 // - trigger info from AliPhysicsSelection
176 // - number of reconstructed tracks > 1
177 // - primary vertex reconstructed
178 // - z-vertex < 10 cm
180 Bool_t selectEvent = kTRUE;
182 //fESD object available?
184 AliDebug(2,Form("ERROR: fInputEvent not available\n"));
185 fNEventReject->Fill("noESD",1);
186 selectEvent = kFALSE;
191 UInt_t isSelected = ((AliInputEventHandler*)(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler()))->IsEventSelected();
192 if(!(isSelected&AliVEvent::kMB)) { //Select collison candidates
193 AliDebug(2,Form(" Trigger Selection: event REJECTED ... "));
194 fNEventReject->Fill("Trigger",1);
195 selectEvent = kFALSE;
199 //Check if number of reconstructed tracks is larger than 1
200 if(!fESD->GetNumberOfTracks() || fESD->GetNumberOfTracks()<2) {
201 fNEventReject->Fill("NTracks<2",1);
202 selectEvent = kFALSE;
206 //Check if vertex is reconstructed
207 const AliESDVertex *vtx = fESD->GetPrimaryVertex();
209 TString vtxName(vtx->GetName());
210 if(vtx->GetNContributors() < 2 || (vtxName.Contains("TPCVertex")) ) {
212 vtx = fESD->GetPrimaryVertexSPD();
213 if(vtx->GetNContributors()<2) {
215 fNEventReject->Fill("noVTX",1);
216 selectEvent = kFALSE;
221 //Check if z-vertex < 10 cm
223 vtx->GetXYZ(primVtx);
224 if(TMath::Sqrt(primVtx[0]*primVtx[0] + primVtx[1]*primVtx[1])>1. || TMath::Abs(primVtx[2]>10.)){
225 fNEventReject->Fill("ZVTX>10",1);
226 selectEvent = kFALSE;
230 AliDebug(2,Form("Vertex title %s, status %d, nCont %d\n",vtx->GetTitle(), vtx->GetStatus(), vtx->GetNContributors()));
236 //_________________________________________________
237 void AliPWG4HighPtSpectra::Exec(Option_t *)
240 // Main loop function
242 AliDebug(2,Form(">> AliPWG4HighPtSpectra::Exec \n"));
244 // All events without selection
245 fNEventAll->Fill(0.);
248 fNEventReject->Fill("NTracks<2",1);
250 PostData(0,fHistList);
251 PostData(1,fCFManagerPos->GetParticleContainer());
252 PostData(2,fCFManagerNeg->GetParticleContainer());
259 AliDebug(2,Form("MC particles: %d", fMC->GetNumberOfTracks()));
260 fStack = fMC->Stack(); //Particles Stack
261 AliDebug(2,Form("MC particles stack: %d", fStack->GetNtrack()));
264 // ---- Get MC Header information (for MC productions in pThard bins) ----
265 Double_t ptHard = 0.;
266 Double_t nTrials = 1; // trials for MC trigger weight for real data
269 AliGenPythiaEventHeader* pythiaGenHeader = GetPythiaEventHeader(fMC);
271 nTrials = pythiaGenHeader->Trials();
272 ptHard = pythiaGenHeader->GetPtHard();
274 fh1PtHard->Fill(ptHard);
275 fh1PtHardTrials->Fill(ptHard,nTrials);
277 fh1Trials->Fill("#sum{ntrials}",fAvgTrials);
281 Int_t nTracks = fESD->GetNumberOfTracks();
282 AliDebug(2,Form("nTracks %d", nTracks));
285 fNEventReject->Fill("noTrackCuts",1);
287 PostData(0,fHistList);
288 PostData(1,fCFManagerPos->GetParticleContainer());
289 PostData(2,fCFManagerNeg->GetParticleContainer());
293 // Selected events for analysis
294 fNEventSel->Fill(0.);
297 Double_t containerInputRec[3] = {0.,0.,0.};
298 Double_t containerInputTPConly[3] = {0.,0.,0.};
299 Double_t containerInputMC[3] = {0.,0.,0.};
300 Double_t containerInputRecMC[3] = {0.,0.,0.};
301 Double_t containerInputTPConlyMC[3] = {0.,0.,0.};
303 //Now go to rec level
304 for (Int_t iTrack = 0; iTrack<nTracks; iTrack++)
306 if(!fESD->GetTrack(iTrack) ) continue;
307 AliESDtrack* track = fESD->GetTrack(iTrack);
308 if(!(AliExternalTrackParam *)track->GetTPCInnerParam()) continue;
309 AliExternalTrackParam *trackTPC = (AliExternalTrackParam *)track->GetTPCInnerParam();
310 if(!track || !trackTPC) continue;
313 containerInputRec[0] = track->Pt();
314 containerInputRec[1] = track->Phi();
315 containerInputRec[2] = track->Eta();
317 //Store TPC Inner Params for TPConly tracks
318 containerInputTPConly[0] = trackTPC->Pt();
319 containerInputTPConly[1] = trackTPC->Phi();
320 containerInputTPConly[2] = trackTPC->Eta();
322 AliESDtrack* trackTPCESD = fTrackCutsTPConly->GetTPCOnlyTrack(fESD, iTrack);
324 if (fTrackCutsTPConly->AcceptTrack(trackTPCESD)) {
325 if(trackTPC->GetSign()>0.) fCFManagerPos->GetParticleContainer()->Fill(containerInputTPConly,kStepReconstructedTPCOnly);
326 if(trackTPC->GetSign()<0.) fCFManagerNeg->GetParticleContainer()->Fill(containerInputTPConly,kStepReconstructedTPCOnly);
328 //Only fill the MC containers if MC information is available
330 Int_t label = TMath::Abs(track->GetLabel());
331 TParticle *particle = fStack->Particle(label) ;
332 if(!particle) continue;
334 containerInputTPConlyMC[0] = particle->Pt();
335 containerInputTPConlyMC[1] = particle->Phi();
336 containerInputTPConlyMC[2] = particle->Eta();
338 //Container with primaries
339 if(fStack->IsPhysicalPrimary(label)) {
340 if(particle->GetPDG()->Charge()>0.) {
341 fCFManagerPos->GetParticleContainer()->Fill(containerInputTPConlyMC,kStepReconstructedTPCOnlyMC);
343 if(particle->GetPDG()->Charge()<0.) {
344 fCFManagerNeg->GetParticleContainer()->Fill(containerInputTPConlyMC,kStepReconstructedTPCOnlyMC);
352 if (fTrackCuts->AcceptTrack(track)) {
353 if(track->GetSign()>0.) fCFManagerPos->GetParticleContainer()->Fill(containerInputRec,kStepReconstructed);
354 if(track->GetSign()<0.) fCFManagerNeg->GetParticleContainer()->Fill(containerInputRec,kStepReconstructed);
357 //Only fill the MC containers if MC information is available
359 Int_t label = TMath::Abs(track->GetLabel());
360 TParticle *particle = fStack->Particle(label) ;
361 if(!particle) continue;
363 containerInputRecMC[0] = particle->Pt();
364 containerInputRecMC[1] = particle->Phi();
365 containerInputRecMC[2] = particle->Eta();
367 //Container with primaries
368 if(fStack->IsPhysicalPrimary(label)) {
369 if(particle->GetPDG()->Charge()>0.) {
370 fCFManagerPos->GetParticleContainer()->Fill(containerInputRecMC,kStepReconstructedMC);
372 if(particle->GetPDG()->Charge()<0.) {
373 fCFManagerNeg->GetParticleContainer()->Fill(containerInputRecMC,kStepReconstructedMC);
377 //Container with secondaries
378 if (!fStack->IsPhysicalPrimary(label) ) {
379 if(particle->GetPDG()->Charge()>0.) {
380 fCFManagerPos->GetParticleContainer()->Fill(containerInputRec,kStepSecondaries);
382 if(particle->GetPDG()->Charge()<0.) {
383 fCFManagerNeg->GetParticleContainer()->Fill(containerInputRec,kStepSecondaries);
394 //Fill MC containters if particles are findable
396 for(int iPart = 1; iPart<(fMC->GetNumberOfPrimaries()); iPart++) {
397 AliMCParticle *mcPart = (AliMCParticle*)fMC->GetTrack(iPart);
398 if(!mcPart) continue;
400 containerInputMC[0] = mcPart->Pt();
401 containerInputMC[1] = mcPart->Phi();
402 containerInputMC[2] = mcPart->Eta();
404 if(fStack->IsPhysicalPrimary(iPart)) {
405 if(mcPart->Charge()>0. && fCFManagerPos->CheckParticleCuts(kStepMCAcceptance,mcPart)) fCFManagerPos->GetParticleContainer()->Fill(containerInputMC,kStepMCAcceptance);
406 if(mcPart->Charge()<0. && fCFManagerNeg->CheckParticleCuts(kStepMCAcceptance,mcPart)) fCFManagerNeg->GetParticleContainer()->Fill(containerInputMC,kStepMCAcceptance);
411 PostData(0,fHistList);
412 PostData(1,fCFManagerPos->GetParticleContainer());
413 PostData(2,fCFManagerNeg->GetParticleContainer());
416 //________________________________________________________________________
417 Bool_t AliPWG4HighPtSpectra::PythiaInfoFromFile(const char* currFile,Float_t &fXsec,Float_t &fTrials){
419 // get the cross section and the trails either from pyxsec.root or from pysec_hists.root
420 // This is to called in Notify and should provide the path to the AOD/ESD file
421 // Copied from AliAnalysisTaskJetSpectrum2
424 TString file(currFile);
428 if(file.Contains("root_archive.zip#")){
429 Ssiz_t pos1 = file.Index("root_archive",12,TString::kExact);
430 Ssiz_t pos = file.Index("#",1,pos1,TString::kExact);
431 file.Replace(pos+1,20,"");
434 // not an archive take the basename....
435 file.ReplaceAll(gSystem->BaseName(file.Data()),"");
437 // Printf("%s",file.Data());
439 TFile *fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec.root")); // problem that we cannot really test the existance of a file in a archive so we have to lvie with open error message from root
441 // next trial fetch the histgram file
442 fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec_hists.root"));
444 // not a severe condition but inciate that we have no information
448 // find the tlist we want to be independtent of the name so use the Tkey
449 TKey* key = (TKey*)fxsec->GetListOfKeys()->At(0);
454 TList *list = dynamic_cast<TList*>(key->ReadObj());
459 fXsec = ((TProfile*)list->FindObject("h1Xsec"))->GetBinContent(1);
460 fTrials = ((TH1F*)list->FindObject("h1Trials"))->GetBinContent(1);
463 } // no tree pyxsec.root
465 TTree *xtree = (TTree*)fxsec->Get("Xsection");
471 Double_t xsection = 0;
472 xtree->SetBranchAddress("xsection",&xsection);
473 xtree->SetBranchAddress("ntrials",&ntrials);
481 //________________________________________________________________________
482 Bool_t AliPWG4HighPtSpectra::Notify()
485 // Implemented Notify() to read the cross sections
486 // and number of trials from pyxsec.root
487 // Copied from AliAnalysisTaskJetSpectrum2
490 TTree *tree = AliAnalysisManager::GetAnalysisManager()->GetTree();
491 Float_t xsection = 0;
496 TFile *curfile = tree->GetCurrentFile();
498 Error("Notify","No current file");
501 if(!fh1Xsec||!fh1Trials){
502 // Printf("%s%d No Histogram fh1Xsec",(char*)__FILE__,__LINE__);
505 PythiaInfoFromFile(curfile->GetName(),xsection,ftrials);
506 fh1Xsec->Fill("<#sigma>",xsection);
507 // construct a poor man average trials
508 Float_t nEntries = (Float_t)tree->GetTree()->GetEntries();
509 if(ftrials>=nEntries && nEntries>0.)fAvgTrials = ftrials/nEntries;
514 //________________________________________________________________________
515 AliGenPythiaEventHeader* AliPWG4HighPtSpectra::GetPythiaEventHeader(AliMCEvent *mcEvent){
517 if(!mcEvent)return 0;
518 AliGenEventHeader* genHeader = mcEvent->GenEventHeader();
519 AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(genHeader);
520 if(!pythiaGenHeader){
522 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(genHeader);
524 if (!genCocktailHeader) {
525 // AliWarningGeneral(Form(" %s:%d",(char*)__FILE__,__LINE__),"Unknown header type (not Pythia or Cocktail)");
526 // AliWarning(Form("%s %d: Unknown header type (not Pythia or Cocktail)",(char*)__FILE__,__LINE__));
529 TList* headerList = genCocktailHeader->GetHeaders();
530 for (Int_t i=0; i<headerList->GetEntries(); i++) {
531 pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
535 if(!pythiaGenHeader){
536 AliWarningGeneral(Form(" %s:%d",(char*)__FILE__,__LINE__),"Pythia event header not found");
540 return pythiaGenHeader;
545 //___________________________________________________________________________
546 void AliPWG4HighPtSpectra::Terminate(Option_t*)
548 // The Terminate() function is the last function to be called during
549 // a query. It always runs on the client, it can be used to present
550 // the results graphically or save the results to file.
554 //___________________________________________________________________________
555 void AliPWG4HighPtSpectra::CreateOutputObjects() {
556 //HERE ONE CAN CREATE OUTPUT OBJECTS, IN PARTICULAR IF THE OBJECT PARAMETERS DON'T NEED
557 //TO BE SET BEFORE THE EXECUTION OF THE TASK
559 AliDebug(2,Form("CreateOutputObjects CreateOutputObjects of task %s", GetName()));
561 Bool_t oldStatus = TH1::AddDirectoryStatus();
562 TH1::AddDirectory(kFALSE);
566 fHistList = new TList();
567 fHistList->SetOwner(kTRUE);
568 fNEventAll = new TH1F("fNEventAll","NEventAll",1,-0.5,0.5);
569 fHistList->Add(fNEventAll);
570 fNEventSel = new TH1F("fNEventSel","NEvent Selected for analysis",1,-0.5,0.5);
571 fHistList->Add(fNEventSel);
573 fNEventReject = new TH1F("fNEventReject","Reason events are rejectected for analysis",20,0,20);
574 fHistList->Add(fNEventReject);
576 fh1Xsec = new TProfile("fh1Xsec","xsec from pyxsec.root",1,0,1);
577 fh1Xsec->GetXaxis()->SetBinLabel(1,"<#sigma>");
578 fHistList->Add(fh1Xsec);
580 fh1Trials = new TH1F("fh1Trials","trials root file",1,0,1);
581 fh1Trials->GetXaxis()->SetBinLabel(1,"#sum{ntrials}");
582 fHistList->Add(fh1Trials);
584 fh1PtHard = new TH1F("fh1PtHard","PYTHIA Pt hard;p_{T,hard}",350,-.5,349.5);
585 fHistList->Add(fh1PtHard);
586 fh1PtHardTrials = new TH1F("fh1PtHardTrials","PYTHIA Pt hard weight with trials;p_{T,hard}",350,-.5,349.5);
587 fHistList->Add(fh1PtHardTrials);
589 TH1::AddDirectory(oldStatus);