]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/muondep/AliAnalysisMuMuSpectra.cxx
Merge branch 'master' into TPCdev
[u/mrichter/AliRoot.git] / PWG / muondep / AliAnalysisMuMuSpectra.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16
17 // AliAnalysisMuMuSpectra : a class to encapsulate results from MuMu analysis
18 //
19 // Spectra can be merged and converted into histograms
20 //
21 // author: L. Aphecetche (Subatech)
22 //
23
24 #include "AliAnalysisMuMuSpectra.h"
25
26 #include "AliLog.h"
27 #include "AliAnalysisMuMuBinning.h"
28 #include "AliAnalysisMuMuJpsiResult.h"
29 #include "Riostream.h"
30 #include "TH1.h"
31 #include "TH2.h"
32 #include "TList.h"
33 #include "TObjArray.h"
34
35 ClassImp(AliAnalysisMuMuSpectra)
36
37 //______________________________________________________________________________
38 AliAnalysisMuMuSpectra::AliAnalysisMuMuSpectra(const char* name, const char* title) :
39 TNamed(name,title),
40 fBinning(0x0),
41 fBins(0x0),
42 fWeight(1.0)
43 {
44   // default ctor
45 }
46
47 //______________________________________________________________________________
48 AliAnalysisMuMuSpectra::AliAnalysisMuMuSpectra(const AliAnalysisMuMuSpectra& rhs)
49 : TNamed(rhs.GetName(),rhs.GetTitle()),
50 fBinning(0x0),
51 fBins(0x0),
52 fWeight(rhs.Weight())
53 {
54   // copy ctor
55
56   if ( rhs.fBinning )
57   {
58     fBinning = new AliAnalysisMuMuBinning(*rhs.fBinning);
59   }
60
61   TIter next(rhs.fBins);
62   AliAnalysisMuMuResult* bin;
63   
64   while ( ( bin = static_cast<AliAnalysisMuMuResult*>(next()) ) )
65   {
66     if (!fBins)
67     {
68       fBins = new TObjArray;
69       fBins->SetOwner(kTRUE);
70     }
71     fBins->Add(bin);
72   }
73   
74   
75 }
76
77 //______________________________________________________________________________
78 AliAnalysisMuMuSpectra&
79 AliAnalysisMuMuSpectra::operator=(const AliAnalysisMuMuSpectra& rhs)
80 {
81   // assignment operator
82   
83   if (this==&rhs) return *this;
84   
85   delete fBinning;
86   fBinning = 0x0;
87   delete fBins;
88   fBins = 0x0;
89   
90   if ( rhs.fBinning )
91   {
92     fBinning = new AliAnalysisMuMuBinning(*rhs.fBinning);
93   }
94   
95   TIter next(rhs.fBins);
96   AliAnalysisMuMuResult* bin;
97   
98   while ( ( bin = static_cast<AliAnalysisMuMuResult*>(next()) ) )
99   {
100     if (!fBins)
101     {
102       fBins = new TObjArray;
103       fBins->SetOwner(kTRUE);
104     }
105     fBins->Add(bin);
106   }
107   
108   fWeight = rhs.Weight();
109   
110   return *this;
111 }
112
113 //______________________________________________________________________________
114 AliAnalysisMuMuSpectra::~AliAnalysisMuMuSpectra()
115 {
116   // dtor
117   delete fBinning;
118   delete fBins;
119 }
120
121 //______________________________________________________________________________
122 Bool_t AliAnalysisMuMuSpectra::AdoptResult(const AliAnalysisMuMuBinning::Range& bin,
123                                          AliAnalysisMuMuResult* result)
124 {
125   // adopt (i.e. we are becoming the owner) a result for a given bin
126   if ( !result )
127   {
128     AliError("Cannot adopt a null result");
129     return kFALSE
130     ;
131   }
132   if (!fBinning)
133   {
134     fBinning = new AliAnalysisMuMuBinning;
135     fBins = new TObjArray;
136     fBins->SetOwner(kTRUE);
137   }
138   fBinning->AddBin(bin); // Add a bin to the spectra
139   
140   Int_t sizeBeforeAdd = fBins->GetEntriesFast();
141   fBins->Add(result); // Add the result to the recently added bin
142   Int_t sizeAfterAdd = fBins->GetEntriesFast();
143   if ( sizeBeforeAdd >= sizeAfterAdd )
144   {
145    AliError(Form("Error adopting result %s to spectra %s",result->GetName(),GetName()));
146     return kFALSE;
147   }
148   else return kTRUE;
149 }
150
151 //______________________________________________________________________________
152 Bool_t AliAnalysisMuMuSpectra::Correct(const AliAnalysisMuMuSpectra& accEff, const char* particle, const char* subResultName)
153 {
154   /// Correct this spectra by acceff one
155   if (IsEmpty())
156   {
157     AliError("Cannot correct an empty spectra !");
158     return kFALSE;
159   }
160   
161   // check we have the same binning first
162   AliAnalysisMuMuBinning* accEffBins = accEff.Binning();
163   
164   if ( !fBinning->IsEqual(accEffBins) )
165   {
166     AliError("Cannot correct with a spectra which does not have the same binning");
167     return kFALSE;
168   }
169   
170   TObjArray* particles = fBinning->CreateWhatArray();
171   TObjArray* types = fBinning->CreateQuantityArray();
172   
173   // FIXME : cross-check what happens here with a binning containing
174   // centralities, etc...
175   
176   if (particles->GetEntries()!=1 || types->GetEntries()!=1 )
177   {
178     delete particles;
179     delete types;
180     return kFALSE;
181   }
182   
183   TObjArray* bins = accEff.BinContentArray();
184
185   
186   for ( Int_t i = 0; i < bins->GetEntries(); ++i )
187   {
188     AliAnalysisMuMuJpsiResult* thisResult = static_cast<AliAnalysisMuMuJpsiResult*>(fBins->At(i));
189     AliAnalysisMuMuJpsiResult* accResult = static_cast<AliAnalysisMuMuJpsiResult*>(bins->At(i));
190 //    AliInfoClass(Form("i=%d",i ));
191 //    StdoutToAliInfoClass(thisResult->Print("full");
192 //                         std::cout << "----" << std::endl;
193 //                         accResult->Print("full"));
194     
195     thisResult->Correct(*accResult,particle,subResultName);
196   }
197   
198   delete particles;
199   delete types;
200   return kTRUE;
201 }
202
203 //______________________________________________________________________________
204 AliAnalysisMuMuResult*
205 AliAnalysisMuMuSpectra::GetResultForBin(const AliAnalysisMuMuBinning::Range& bin) const
206 {
207   /// Get result for a given bin
208   /// Warning: this requires a loop on bins
209   
210   if ( IsEmpty() ) return 0x0;
211   
212   TObjArray* bins = fBinning->CreateBinObjArray();
213   
214   Int_t j(-1);
215   
216   StdoutToAliDebug(1,std::cout << "searching for "; bin.Print());
217   
218   for ( Int_t i = 0; i <= bins->GetLast() && j < 0 ; ++i )
219   {
220     AliAnalysisMuMuBinning::Range* b = static_cast<AliAnalysisMuMuBinning::Range*>(bins->At(i));
221
222     StdoutToAliDebug(1,b->Print(););
223     
224     if ( bin == *b )
225     {
226       j = i;
227     }
228   }
229   
230   delete bins;
231   
232   if (j>=0)
233   {
234     return static_cast<AliAnalysisMuMuResult*>(fBins->At(j));
235   }
236   else
237   {
238     StdoutToAliDebug(1,std::cout << "Could not find result for bin:" << std::endl; bin.Print(););
239   }
240   return 0x0;
241 }
242
243 //______________________________________________________________________________
244 AliAnalysisMuMuResult*
245 AliAnalysisMuMuSpectra::GetResultForBin(const char* binName) const
246 {
247   /// Get result for a given bin
248   /// Warning: this requires a loop on bins
249   
250   if ( IsEmpty() ) return 0x0;
251   
252   TObjArray* bins = fBinning->CreateBinObjArray();
253   
254   Int_t j(-1);
255   
256   AliDebug(1,Form("searching for bin %s",binName));
257   
258   for ( Int_t i = 0; i <= bins->GetLast() && j < 0 ; ++i )
259   {
260     AliAnalysisMuMuBinning::Range* b = static_cast<AliAnalysisMuMuBinning::Range*>(bins->At(i));
261     
262     if ( b->AsString() == binName )
263     {
264       j = i;
265     }
266   }
267   
268   delete bins;
269   
270   if (j>=0)
271   {
272     return static_cast<AliAnalysisMuMuResult*>(fBins->At(j));
273   }
274   else
275   {
276     AliDebug(1,Form("Could not find result for bin %s",binName));
277   }
278   return 0x0;
279 }
280
281 //______________________________________________________________________________
282 Bool_t AliAnalysisMuMuSpectra::HasValue(const char* what) const
283 {
284     // whether or not our result(s) has a given property
285   if ( IsEmpty() ) return kFALSE;
286   
287   AliAnalysisMuMuResult* r = static_cast<AliAnalysisMuMuResult*>(fBins->First());
288   
289   return r->HasValue(what);
290 }
291
292 //______________________________________________________________________________
293 Bool_t AliAnalysisMuMuSpectra::IsEmpty() const
294 {
295   // whether this spectra is empty or not
296   return ( fBins==0x0 || fBins->GetEntries()<=0 );
297 }
298
299 //______________________________________________________________________________
300 Long64_t AliAnalysisMuMuSpectra::Merge(TCollection* list)
301 {
302   /// Merge method
303   
304   // Merge a list of AliAnalysisMuMuSpectra objects with this
305   // Returns the number of merged objects (including this).
306   
307   if (!list) return 0;
308   
309   if (list->IsEmpty()) return 1;
310   
311   TIter next(list);
312   TObject* currObj;
313   Int_t count(0);
314   
315   TList binningList;
316   
317   // for each bin must do a list of results, and merge that list
318   
319   TObjArray* bins = fBinning->CreateBinObjArray();
320   TIter nextBin(bins);
321   AliAnalysisMuMuBinning::Range* bin;
322
323   Int_t i(0);
324   
325   while ( ( bin = static_cast<AliAnalysisMuMuBinning::Range*>(nextBin()) ) )
326   {
327     next.Reset();
328     
329     TList binList;
330     
331     while ( ( currObj = next() ) )
332     {
333       AliAnalysisMuMuSpectra* spectra = static_cast<AliAnalysisMuMuSpectra*>(currObj);
334
335       if (i==0)
336       {
337         binningList.Add(spectra->Binning());
338   
339         if ( !fBinning->IsEqual(spectra->Binning()) || spectra->BinContentArray()->GetLast() != BinContentArray()->GetLast() )
340         {
341           AliError("Cannot merge spectra with different binning");
342           continue;
343         }
344         ++count;
345       }
346       
347       binList.Add(spectra->GetResultForBin(*bin));
348     }
349     
350     ++i;
351     
352     AliAnalysisMuMuResult* r = static_cast<AliAnalysisMuMuResult*>(GetResultForBin(*bin));
353     r->Merge(&binList);
354     
355   }
356
357   delete bins;
358   
359   return count+1;
360 }
361
362 //_____________________________________________________________________________
363 TH1* AliAnalysisMuMuSpectra::Plot(const char* what, const char* subresult, Bool_t divideByBinWidth) const
364 {
365   // Convert the spectra into an histogram
366   
367   TString swhat(what);
368   TString swhatT(what);
369   swhat.ToUpper();
370   
371   Double_t* bins = fBinning->CreateBinArray();
372   Double_t* binsX = fBinning->CreateBinArrayX();
373   Double_t* binsY = fBinning->CreateBinArrayY();
374   
375   Int_t nbinsX = fBinning->GetNBinsX();
376   Int_t nbinsY = fBinning->GetNBinsY();
377   
378   TObjArray* binArray = fBinning->CreateBinObjArray();
379   
380   TH1* h(0x0);
381   
382   AliDebug(1,Form("nbins=%d nresults=%d",binArray->GetEntries(),fBins->GetEntries()));
383   
384   for ( Int_t j = 0; j < TMath::Min(binArray->GetEntries(),fBins->GetEntries()); ++j )
385   {
386     AliAnalysisMuMuJpsiResult* r = static_cast<AliAnalysisMuMuJpsiResult*>(fBins->At(j));
387     
388     if ( strlen(subresult) > 0 && r->SubResults() )
389     {
390       TString sub(subresult);
391       r = static_cast<AliAnalysisMuMuJpsiResult*>(r->SubResult(sub.Data()));
392       if (!r) continue;
393     }
394     
395     const AliAnalysisMuMuBinning::Range& b = r->Bin();
396     
397     if ( b.Is2D() )
398     {
399       if (!h)
400       {
401         h = new TH2F(r->GetName(),r->GetName(),nbinsX,binsX,nbinsY,binsY);
402         h->SetDirectory(0);
403       }
404       
405       Double_t z = r->GetValue(swhatT.Data());
406       Double_t zerr = r->GetErrorStat(swhatT.Data());
407       
408       if ( swhat.Contains("AccEff",TString::kIgnoreCase) )
409       {
410         if ( z < 0. || z > 5.)
411         {
412           z = -1.;
413           zerr = 0.;
414         }
415       }
416       
417       if ( divideByBinWidth && b.WidthX()>0 )
418       {
419         z /= (b.WidthX()*b.WidthY());
420         zerr /= (b.WidthX()*b.WidthY());
421         
422       }
423       
424       std::cout << b.AsString();
425       r->PrintValue(swhat.Data(),"",z,zerr);
426       
427       Int_t x = j/nbinsY + 1;
428       Int_t y = (j % nbinsY) + 1;
429       
430       std::cout << "x="  << x << ";" << "y=" << y << std::endl;
431       
432       h->SetBinContent(x,y,z);
433       h->SetBinError(x,y,zerr);
434       
435     }
436
437     else
438     {
439       if (!h)
440       {
441         h = new TH1F(r->GetName(),r->GetName(),binArray->GetEntries(),bins);
442         h->SetDirectory(0);
443       }
444       
445       Double_t y = r->GetValue(swhatT.Data());
446       Double_t yerr = r->GetErrorStat(swhatT.Data());
447       
448       if ( divideByBinWidth && b.WidthX()>0 )
449       {
450         y /= (b.WidthX());
451         yerr /= (b.WidthX());
452       }
453       
454       if (!TMath::Finite(y)) y = 0.0;
455       if (!TMath::Finite(yerr)) yerr = 0.0;
456       
457       std::cout << b.AsString();
458       r->PrintValue(swhat.Data(),"",y,yerr);
459       
460       h->SetBinContent(j+1,y);
461       h->SetBinError(j+1,yerr);
462     }
463   }
464   
465   delete binArray;
466   delete[] binsX;
467   delete[] binsY;
468   delete[] bins;
469   
470   return h;
471 }
472
473
474 //______________________________________________________________________________
475 void AliAnalysisMuMuSpectra::Print(Option_t* opt) const
476 {
477   // printout
478   
479   if (!IsEmpty())
480   {
481     TString sopt(opt);
482     Int_t nmax = sopt.Atoi();
483     if ( nmax <= 0 ) nmax = fBins->GetEntries();
484     for ( Int_t i = 0; i < nmax; ++i )
485     {
486       AliAnalysisMuMuResult* r = static_cast<AliAnalysisMuMuResult*>(fBins->At(i));
487       if (r) r->Print(opt);
488     }
489   }
490 }
491
492 //______________________________________________________________________________
493 void AliAnalysisMuMuSpectra::Scale(Double_t value)
494 {
495   // scale all bins by value
496
497   TIter next(fBins);
498   AliAnalysisMuMuResult* r;
499
500   while ( ( r = static_cast<AliAnalysisMuMuResult*>(next()) ) )
501   {
502     r->Scale(value);
503   }
504 }
505
506 //______________________________________________________________________________
507 void AliAnalysisMuMuSpectra::SetWeight(Double_t w)
508 {
509   // Set the weight of this spectra
510   fWeight = w;
511   TIter next(fBins);
512   AliAnalysisMuMuResult* r;
513   
514   while ( ( r = static_cast<AliAnalysisMuMuResult*>(next()) ) )
515   {
516     r->SetWeight(Weight());
517   }
518 }