]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG0/trigger/AliTriggerTask.cxx
1ff9da7d3ac70328e903db6389ff2f827daba82f
[u/mrichter/AliRoot.git] / PWG0 / trigger / AliTriggerTask.cxx
1 /* $Id: AliTriggerTask.cxx 35782 2009-10-22 11:54:31Z jgrosseo $ */
2
3 #include "AliTriggerTask.h"
4
5 #include <TCanvas.h>
6 #include <TFile.h>
7 #include <TChain.h>
8 #include <TH1F.h>
9 #include <TH2F.h>
10 #include <TH3F.h>
11
12 #include <AliLog.h>
13 #include <AliESDEvent.h>
14 #include <AliHeader.h>
15 #include <AliAnalysisManager.h>
16 #include <AliESDInputHandler.h>
17 #include <AliESDHeader.h>
18
19 ClassImp(AliTriggerTask)
20
21 AliTriggerTask::AliTriggerTask(const char* opt) :
22   AliAnalysisTask("AliTriggerTask", ""),
23   fESD(0),
24   fOutput(0),
25   fOption(opt),
26   fStartTime(0),
27   fEndTime(0),
28   fNTriggers(0),
29   fTriggerList(0),
30   fStats(0)
31 {
32   //
33   // Constructor. Initialization of pointers
34   //
35
36   // Define input and output slots here
37   DefineInput(0, TChain::Class());
38   DefineOutput(0, TList::Class());
39   
40   fNTriggers = 10;
41   
42   static AliPWG0Helper::Trigger triggerList[] = { AliPWG0Helper::kAcceptAll, AliPWG0Helper::kMB1, AliPWG0Helper::kMB2, AliPWG0Helper::kMB3, AliPWG0Helper::kSPDGFO, AliPWG0Helper::kV0A, AliPWG0Helper::kV0C, AliPWG0Helper::kZDC, AliPWG0Helper::kZDCA, AliPWG0Helper::kZDCC };
43   fTriggerList = triggerList;
44   
45   fStats = new TH1*[fNTriggers];
46   
47   AliLog::SetClassDebugLevel("AliTriggerTask", AliLog::kWarning);
48 }
49
50 AliTriggerTask::~AliTriggerTask()
51 {
52   //
53   // Destructor
54   //
55
56   // histograms are in the output list and deleted when the output
57   // list is deleted by the TSelector dtor
58
59   if (fOutput) {
60     delete fOutput;
61     fOutput = 0;
62   }
63 }
64
65 //________________________________________________________________________
66 void AliTriggerTask::ConnectInputData(Option_t *)
67 {
68   // Connect ESD
69   // Called once
70
71   Printf("AliTriggerTask::ConnectInputData called");
72
73   AliESDInputHandler *esdH = dynamic_cast<AliESDInputHandler*> (AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
74
75   if (!esdH) {
76     Printf("ERROR: Could not get ESDInputHandler");
77   } else {
78     fESD = esdH->GetEvent();
79     
80     TString branches("AliESDHeader Vertex AliMultiplicity ALIESDVZERO ALIESDZDC FMD");
81
82     // Enable only the needed branches
83     esdH->SetActiveBranches(branches);
84   }
85 }
86
87 void AliTriggerTask::CreateOutputObjects()
88 {
89   // create result objects and add to output list
90
91   Printf("AliTriggerTask::CreateOutputObjects");
92
93   fOutput = new TList;
94   fOutput->SetOwner();
95   
96   if (fStartTime == fEndTime)
97     AliWarning("Start and endtime not set. Automatic binning will be used. This does not work in parallel systems");
98
99   Int_t nBins = 1000;
100   if (fEndTime - fStartTime > 0)
101     nBins = fEndTime - fStartTime;
102   for (Int_t i=0; i<fNTriggers; i++)
103   {
104     fStats[i] = new TH1F(Form("fStats_%d", i), Form("%s;time;counts", AliPWG0Helper::GetTriggerName(fTriggerList[i])), nBins, 0, fEndTime - fStartTime);
105     fOutput->Add(fStats[i]);
106   }
107 }
108
109 void AliTriggerTask::Exec(Option_t*)
110 {
111   // process the event
112
113   // post the data already here
114   PostData(0, fOutput);
115
116   if (!fESD)
117   {
118     AliError("ESD branch not available");
119     return;
120   }
121   
122   // check event type (should be PHYSICS = 7)
123   AliESDHeader* esdHeader = fESD->GetHeader();
124   if (!esdHeader)
125   {
126     Printf("ERROR: esdHeader could not be retrieved");
127     return;
128   }
129   
130   UInt_t eventType = esdHeader->GetEventType();
131   if (eventType != 7)
132   {
133     Printf("Skipping event because it is of type %d", eventType);
134     return;
135   }
136
137   //Printf("Trigger classes: %s:", fESD->GetFiredTriggerClasses().Data());
138   
139   UInt_t timeStamp = fESD->GetTimeStamp() - fStartTime;
140   //Printf("%d", timeStamp);
141   
142   for (Int_t i = 0; i < fNTriggers; i++)
143   {
144     Bool_t triggered = AliPWG0Helper::IsEventTriggered(fESD, (AliPWG0Helper::Trigger) (fTriggerList[i] | AliPWG0Helper::kOfflineFlag));
145     if (triggered)
146       fStats[i]->Fill(timeStamp);
147     //Printf("%s: %d", AliPWG0Helper::GetTriggerName(fTriggerList[i]), triggered);
148   }
149 }
150
151 void AliTriggerTask::Terminate(Option_t *)
152 {
153   // The Terminate() function is the last function to be called during
154   // a query. It always runs on the client, it can be used to present
155   // the results graphically or save the results to file.
156
157   fOutput = dynamic_cast<TList*> (GetOutputData(0));
158   if (!fOutput)
159     Printf("ERROR: fOutput not available");
160
161   if (fOutput)
162   {
163     for (Int_t i=0; i<fNTriggers; i++)
164       fStats[i] = dynamic_cast<TH1*> (fOutput->FindObject(Form("fStats_%d", i)));
165   }
166
167   TFile* fout = new TFile("trigger.root", "RECREATE");
168
169   for (Int_t i=0; i<fNTriggers; i++)
170     if (fStats[i])
171       fStats[i]->Write();
172
173   fout->Write();
174   fout->Close();
175   
176   Int_t nX = (Int_t) TMath::Sqrt(fNTriggers);
177   Int_t nY = nX;
178   
179   while (nX * nY < fNTriggers)
180   {
181     if (nX == nY)
182       nX++;
183     else
184       nY++;
185   }
186   
187   TCanvas* c = new TCanvas("c", "c", 800, 800);
188   c->Divide(nX, nY);
189   
190   Printf("+++++++++ TRIGGER STATS:");
191   
192   for (Int_t i=0; i<fNTriggers; i++)
193     if (fStats[i])
194     {
195       c->cd(i+1);
196       fStats[i]->Draw();
197       Printf("%s: %d triggers", AliPWG0Helper::GetTriggerName(fTriggerList[i]), (UInt_t) fStats[i]->Integral());
198     }
199
200   Printf("Writting result to trigger.root");
201 }