]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/FORWARD/analysis2/AliFMDEnergyFitterTask.cxx
Fixes for AliForwardCorrectionManager persistency
[u/mrichter/AliRoot.git] / PWG2 / FORWARD / analysis2 / AliFMDEnergyFitterTask.cxx
1 // 
2 // Histogram and fit the energy loss distributions for the FMD
3 // 
4 // Inputs: 
5 //   - AliESDEvent 
6 //
7 // Outputs: 
8 //   - None
9 // 
10 // Histograms:
11 //   
12 // Corrections used:
13 //   - None
14 // 
15 // 
16 //
17 #include "AliFMDEnergyFitterTask.h"
18 #include "AliLog.h"
19 #include "AliESDEvent.h"
20 #include "AliAODForwardMult.h"
21 #include "AliForwardCorrectionManager.h"
22 #include "AliAnalysisManager.h"
23 #include "AliAnalysisDataSlot.h"
24 #include "AliAnalysisDataContainer.h"
25 #include <TH1.h>
26 #include <TDirectory.h>
27 #include <TTree.h>
28 #include <TFile.h>
29
30 //====================================================================
31 AliFMDEnergyFitterTask::AliFMDEnergyFitterTask()
32   : AliAnalysisTaskSE(),
33     fFirstEvent(true),
34     fEventInspector(),
35     fEnergyFitter(),
36     fList(0)
37 {
38   // 
39   // Constructor
40   //
41 }
42
43 //____________________________________________________________________
44 AliFMDEnergyFitterTask::AliFMDEnergyFitterTask(const char* name)
45   : AliAnalysisTaskSE(name), 
46     fFirstEvent(true),
47     fEventInspector("event"),
48     fEnergyFitter("energy"),
49     fList(0)
50 {
51   // 
52   // Constructor 
53   // 
54   // Parameters:
55   //    name Name of task 
56   //
57   DefineOutput(1, TList::Class());
58 }
59
60 //____________________________________________________________________
61 AliFMDEnergyFitterTask::AliFMDEnergyFitterTask(const AliFMDEnergyFitterTask& o)
62   : AliAnalysisTaskSE(o),
63     fFirstEvent(o.fFirstEvent),
64     fEventInspector(o.fEventInspector),
65     fEnergyFitter(o.fEnergyFitter),
66     fList(o.fList) 
67 {
68   // 
69   // Copy constructor 
70   // 
71   // Parameters:
72   //    o Object to copy from 
73   //
74   DefineOutput(1, TList::Class());
75 }
76
77 //____________________________________________________________________
78 AliFMDEnergyFitterTask&
79 AliFMDEnergyFitterTask::operator=(const AliFMDEnergyFitterTask& o)
80 {
81   // 
82   // Assignment operator 
83   // 
84   // Parameters:
85   //    o Object to assign from 
86   // 
87   // Return:
88   //    Reference to this object 
89   //
90   AliAnalysisTaskSE::operator=(o);
91
92   fFirstEvent        = o.fFirstEvent;
93   fEventInspector    = o.fEventInspector;
94   fEnergyFitter      = o.fEnergyFitter;
95   fList              = o.fList;
96
97   return *this;
98 }
99
100 //____________________________________________________________________
101 void
102 AliFMDEnergyFitterTask::SetDebug(Int_t dbg)
103 {
104   // 
105   // Set the debug level 
106   // 
107   // Parameters:
108   //    dbg Debug level
109   //
110   fEventInspector.SetDebug(dbg);
111   fEnergyFitter.SetDebug(dbg);
112 }
113
114 //____________________________________________________________________
115 void
116 AliFMDEnergyFitterTask::Init()
117 {
118   // 
119   // Initialize the task 
120   // 
121   //
122   fFirstEvent = true;
123 }
124
125 //____________________________________________________________________
126 void
127 AliFMDEnergyFitterTask::InitializeSubs()
128 {
129   // 
130   // Initialise the sub objects and stuff.  Called on first event 
131   // 
132   //
133   AliForwardCorrectionManager& fcm = AliForwardCorrectionManager::Instance();
134   fcm.Init(fEventInspector.GetCollisionSystem(), 
135            fEventInspector.GetEnergy(),
136            fEventInspector.GetField(), 0);
137   TAxis eAxis(0,0,0);
138   TAxis vAxis(10,-10,10);
139   fEnergyFitter.Init(eAxis);
140   fEventInspector.Init(vAxis);
141 }
142
143 //____________________________________________________________________
144 void
145 AliFMDEnergyFitterTask::UserCreateOutputObjects()
146 {
147   // 
148   // Create output objects 
149   // 
150   //
151   fList = new TList;
152
153   fEventInspector.DefineOutput(fList);
154   fEnergyFitter.DefineOutput(fList);
155
156   PostData(1, fList);
157 }
158 //____________________________________________________________________
159 void
160 AliFMDEnergyFitterTask::UserExec(Option_t*)
161 {
162   // 
163   // Process each event 
164   // 
165   // Parameters:
166   //    option Not used
167   //  
168
169   // static Int_t cnt = 0;
170   // cnt++;
171   // Get the input data 
172   AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
173   // AliInfo(Form("Event # %6d (esd=%p)", cnt, esd));
174   if (!esd) { 
175     AliWarning("No ESD event found for input event");
176     return;
177   }
178
179   // On the first event, initialize the parameters 
180   if (fFirstEvent && esd->GetESDRun()) { 
181     fEventInspector.ReadRunDetails(esd);
182     
183     AliInfo(Form("Initializing with parameters from the ESD:\n"
184                  "         AliESDEvent::GetBeamEnergy()   ->%f\n"
185                  "         AliESDEvent::GetBeamType()     ->%s\n"
186                  "         AliESDEvent::GetCurrentL3()    ->%f\n"
187                  "         AliESDEvent::GetMagneticField()->%f\n"
188                  "         AliESDEvent::GetRunNumber()    ->%d\n",
189                  esd->GetBeamEnergy(), 
190                  esd->GetBeamType(),
191                  esd->GetCurrentL3(), 
192                  esd->GetMagneticField(),
193                  esd->GetRunNumber()));
194
195               
196
197     // AliFMDAnaParameters* pars = AliFMDAnaParameters::Instance();
198     // pars->SetParametersFromESD(esd);
199     // pars->PrintStatus();
200     fFirstEvent = false;
201
202     InitializeSubs();
203   }
204   Bool_t   lowFlux  = kFALSE;
205   UInt_t   triggers = 0;
206   UShort_t ivz      = 0;
207   Double_t vz       = 0;
208   UInt_t   found    = fEventInspector.Process(esd, triggers, lowFlux, ivz, vz);
209   if (found & AliFMDEventInspector::kNoEvent)    return;
210   if (found & AliFMDEventInspector::kNoTriggers) return;
211   if (found & AliFMDEventInspector::kNoSPD)     return;
212   if (found & AliFMDEventInspector::kNoFMD)     return;
213   if (found & AliFMDEventInspector::kNoVertex)  return;
214   if (found & AliFMDEventInspector::kBadVertex) return;
215
216   // Get FMD data 
217   AliESDFMD* esdFMD = esd->GetFMDData();
218   // Do the energy stuff 
219   if (!fEnergyFitter.Accumulate(*esdFMD, triggers & AliAODForwardMult::kEmpty)){
220     AliWarning("Energy fitter failed");
221     return;
222   }
223   PostData(1, fList);
224 }
225
226 //____________________________________________________________________
227 void
228 AliFMDEnergyFitterTask::Terminate(Option_t*)
229 {
230   // 
231   // End of job
232   // 
233   // Parameters:
234   //    option Not used 
235   //
236   AliInfo(Form("Running terminate of %s", GetName()));
237   TList* list = dynamic_cast<TList*>(GetOutputData(1));
238   if (!list) {
239     AliError(Form("No output list defined (%p)", GetOutputData(1)));
240     if (GetOutputData(1)) GetOutputData(1)->Print();
241     return;
242   }
243   
244   // Get our histograms from the container 
245   TH1I* hEventsTr    = 0;//static_cast<TH1I*>(list->FindObject("nEventsTr"));
246   TH1I* hEventsTrVtx = 0;//static_cast<TH1I*>(list->FindObject("nEventsTrVtx"));
247   TH1I* hTriggers    = 0;
248   if (!fEventInspector.FetchHistograms(list, hEventsTr, 
249                                        hEventsTrVtx, hTriggers)) { 
250     AliError(Form("Didn't get histograms from event selector "
251                   "(hEventsTr=%p,hEventsTrVtx=%p)", 
252                   hEventsTr, hEventsTrVtx));
253     list->ls();
254     return;
255   }
256   AliInfo("Fitting energy loss spectra");
257   fEnergyFitter.Fit(list);
258
259   // Investigate output slot 
260   AliAnalysisDataSlot* oslot = GetOutputSlot(1);
261   if (oslot) { 
262     AliAnalysisDataContainer* ocont = oslot->GetContainer();
263     if (ocont) { 
264       TFile* ofile = ocont->GetFile();
265       if (ofile) {
266         AliInfo(Form("Output file %s opened with option %s (%s)", 
267                      ofile->GetName(), ofile->GetOption(), 
268                      ofile->IsWritable() ? "read-write" : "read-only"));
269         ofile->Flush();
270       }
271       else 
272         AliWarning("No output file associated with data container");
273     }
274     else 
275       AliWarning("No container associated with slot 1");
276   }
277   else 
278     AliWarning("Slot number 1 not defined");
279   
280   // Temporary code to save output to a file - should be disabled once 
281   // the plugin stuff works 
282   list->ls();
283   TDirectory* savdir = gDirectory;
284   TFile* tmp = TFile::Open("elossfits.root", "RECREATE");
285   list->Write(list->GetName(), TObject::kSingleKey);
286   tmp->Write();
287   tmp->Close();
288   savdir->cd();
289 }
290
291 //____________________________________________________________________
292 void
293 AliFMDEnergyFitterTask::Print(Option_t*) const
294 {
295   // 
296   // Print information 
297   // 
298   // Parameters:
299   //    option Not used
300   //
301 }
302
303 //
304 // EOF
305 //