1 /**************************************************************************
2 * Copyright(c) 1998-2007, 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 **************************************************************************/
18 //-----------------------------------------------------------------------------
19 /// \class AliCounterCollection
21 /// generic class to handle a collection of counters
23 /// \author Philippe Pillot
24 //-----------------------------------------------------------------------------
26 #include "AliCounterCollection.h"
33 #include <TObjString.h>
34 #include <TObjArray.h>
35 #include <THnSparse.h>
36 #include <THashList.h>
40 #include <TCollection.h>
42 ClassImp(AliCounterCollection)
44 //-----------------------------------------------------------------------
45 AliCounterCollection::AliCounterCollection(const char* name) :
47 fRubrics(new THashList(10)),
48 fRubricsSize(new TArrayI(10)),
50 fWeightedCounters(kFALSE),
57 //-----------------------------------------------------------------------
58 AliCounterCollection::~AliCounterCollection()
66 //-----------------------------------------------------------------------
67 void AliCounterCollection::Clear(Option_t*)
71 fRubricsSize->Reset();
72 delete fCounters; fCounters = 0x0;
73 fWeightedCounters = kFALSE;
74 fLongCounters = kFALSE;
77 //-----------------------------------------------------------------------
78 void AliCounterCollection::AddRubric(TString name, TString listOfKeyWords)
80 /// Add a new rubric with the complete list of related key words separated by "/".
81 /// If the key word "any" is not defined, the overall statistics is
82 /// assumed to be the sum of the statistics under each key word.
85 listOfKeyWords.ToUpper();
87 if (fRubrics->Contains(name.Data())) {
88 AliError(Form("rubric named %s already exist",name.Data()));
92 // add the list of autorized key words
93 TObjArray* rubric = listOfKeyWords.Tokenize("/");
94 CleanListOfStrings(rubric);
95 rubric->SetName(name.Data());
96 Int_t nRubrics = fRubrics->GetSize();
97 rubric->SetUniqueID(nRubrics);
98 fRubrics->AddLast(rubric);
100 // save the number of autorized key words (expand the array if needed)
101 if (nRubrics+1 > fRubricsSize->GetSize()) fRubricsSize->Set(2*fRubricsSize->GetSize());
102 (*fRubricsSize)[nRubrics] = rubric->GetEntriesFast();
105 //-----------------------------------------------------------------------
106 void AliCounterCollection::AddRubric(TString name, Int_t maxNKeyWords)
108 /// Add a new rubric containing at maximum maxNKeyWords key words.
109 /// Key words will be added as the counters get filled until the maximum is reached.
110 /// If the key word "any" is never defined, the overall statistics is
111 /// assumed to be the sum of the statistics under each key word.
115 if (fRubrics->Contains(name.Data())) {
116 AliError(Form("rubric named %s already exist",name.Data()));
120 // create the empty rubric
121 TObjString* rubric = new TObjString(name.Data());
122 Int_t nRubrics = fRubrics->GetSize();
123 rubric->SetUniqueID(nRubrics);
124 fRubrics->AddLast(rubric);
126 // save the maximum number of autorized key words
127 if (nRubrics+1 > fRubricsSize->GetSize()) fRubricsSize->Set(2*fRubricsSize->GetSize());
128 (*fRubricsSize)[nRubrics] = maxNKeyWords;
131 //-----------------------------------------------------------------------
132 void AliCounterCollection::Init(Bool_t weightedCounters)
134 /// Initialize the internal counters from the added rubrics.
136 // create the counters
138 fWeightedCounters = weightedCounters;
139 if (fWeightedCounters)
140 fCounters = new THnSparseT<TArrayF>("hCounters", "hCounters", fRubrics->GetSize(), fRubricsSize->GetArray(), 0x0, 0x0);
142 fCounters = new THnSparseT<TArrayI>("hCounters", "hCounters", fRubrics->GetSize(), fRubricsSize->GetArray(), 0x0, 0x0);
145 TObject* rubric = 0x0;
146 TIter nextRubric(fRubrics);
147 while ((rubric = nextRubric())) {
148 TAxis* axis = fCounters->GetAxis((Int_t)rubric->GetUniqueID());
151 axis->SetName(rubric->GetName());
153 // set labels if already known
154 TObjArray* keyWords = dynamic_cast<TObjArray*>(rubric);
156 TObjString* label = 0x0;
158 TIter nextLabel(keyWords);
159 while ((label = static_cast<TObjString*>(nextLabel()))) axis->SetBinLabel(bin++, label->String().Data());
166 //-----------------------------------------------------------------------
167 Int_t AliCounterCollection::GetNActiveBins(Int_t dim)
169 /// return the number of labels in that rubric.
170 THashList* labels = fCounters->GetAxis(dim)->GetLabels();
171 return (labels) ? labels->GetSize() : 0;
174 //-----------------------------------------------------------------------
175 Bool_t AliCounterCollection::ContainsAny(Int_t dim)
177 /// return kTRUE if that rubric contains the keyWord "ANY".
178 THashList* labels = fCounters->GetAxis(dim)->GetLabels();
179 return (labels && labels->Contains("ANY"));
182 //-----------------------------------------------------------------------
183 const Int_t* AliCounterCollection::FindBins(const TString& externalKey, Bool_t allocate, Int_t& nEmptySlots)
185 /// Return the corresponding bins ordered by rubric or 0x0 if externalKey is not valid.
186 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
187 /// If allocate = kTRUE, new key words are added to the corresponding rubric if possible.
188 /// If a rubric is not filled in, the coresponding slot contain -1 in the array.
189 /// It is the responsability of the user to delete the returned array.
191 // produce an empty array of keys
192 Int_t nRubrics = fRubrics->GetSize();
193 Int_t* bins = new Int_t[nRubrics];
194 for (Int_t i=0; i<nRubrics; i++) bins[i] = -1;
195 nEmptySlots = nRubrics;
196 Bool_t isValid = kTRUE;
198 // get the list of rubric:keyWord pairs
199 TObjArray* rubricKeyPairs = externalKey.Tokenize("/");
201 // loop over each rubric:keyWord pair
202 TObjString* pair = 0x0;
203 TIter next(rubricKeyPairs);
204 while ((pair = static_cast<TObjString*>(next()))) {
206 // get both rubric and associated key word
207 TObjArray* rubricKeyPair = pair->String().Tokenize(":");
209 // check the format of the pair
210 if (rubricKeyPair->GetEntriesFast() != 2) {
211 AliError("invalid key format");
213 delete rubricKeyPair;
217 // get the axis corresponding to that rubric
218 Int_t dim = FindDim(static_cast<TObjString*>(rubricKeyPair->UncheckedAt(0))->String());
221 delete rubricKeyPair;
225 // find the bin corresponding to that key word
226 Int_t bin = FindBin(dim, static_cast<TObjString*>(rubricKeyPair->UncheckedAt(1))->String(), allocate);
229 delete rubricKeyPair;
233 // check if the array of keys already contains something for that rubric
234 if (bins[dim] >= 0) {
235 AliWarning("key already given for that rubric --> ignored");
236 delete rubricKeyPair;
240 // store the corresponding bin for that slot
245 delete rubricKeyPair;
248 // delete the array in case of problem
252 nEmptySlots = nRubrics;
256 delete rubricKeyPairs;
261 //-----------------------------------------------------------------------
262 Int_t AliCounterCollection::FindDim(const TString& rubricName) const
264 /// Return the dimension corresponding to that rubric (or -1 in case of failure).
265 TObject* rubric = fRubrics->FindObject(rubricName.Data());
267 AliError(Form("invalid rubric: %s",rubricName.Data()));
270 return (Int_t) rubric->GetUniqueID();
273 //-----------------------------------------------------------------------
274 Int_t AliCounterCollection::FindBin(Int_t dim, const TString& keyWord, Bool_t allocate)
276 /// Return the bin number corresponding to that key word (or -1 in case of failure).
277 /// If allocate = kTRUE, try to add the key word if possible.
279 TAxis* axis = fCounters->GetAxis(dim);
281 // look for the bin corresponding to keyWord
282 THashList* labels = axis->GetLabels();
283 TObjString* label = (labels) ? static_cast<TObjString*>(labels->FindObject(keyWord.Data())) : 0x0;
284 Int_t bin = (label) ? (Int_t)label->GetUniqueID() : -1;
286 // in case the keyWord does not exist, try to add it if required
287 if (bin<0 && allocate) {
288 Int_t nLabels = (labels) ? labels->GetSize() : 0;
289 if (nLabels < axis->GetNbins()) {
291 axis->SetBinLabel(bin, keyWord.Data());
295 if (bin<0) AliError(Form("invalid key word: %s:%s",axis->GetName(),keyWord.Data()));
300 //-----------------------------------------------------------------------
301 Short_t** AliCounterCollection::DecodeSelection(const TString& selections, const TObjArray& displayedRubrics)
303 /// Tag the selected keywords in each rubric (-1=subtract; 0=discard; 1=add). Format:
304 /// "rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.." (order does not matter).
305 /// It is the responsability of the user to delete the returned array.
307 // produce an empty array of selected keys
308 Int_t nRubrics = fCounters->GetNdimensions();
309 Short_t** selects = new Short_t*[nRubrics];
310 for (Int_t i=0; i<nRubrics; i++) selects[i] = 0x0;
312 // get the list of rubric:LisOfKeyWord pairs
313 TObjArray* rubricKeyPairs = selections.Tokenize("/");
315 // loop over each rubric:keyWord pair
316 TObjString* pair = 0x0;
317 TIter next(rubricKeyPairs);
318 while ((pair = static_cast<TObjString*>(next()))) {
320 // get both rubric and associated list of key words
321 TObjArray* rubricKeyPair = pair->String().Tokenize(":");
323 // check the format of the pair
324 if (rubricKeyPair->GetEntriesFast() != 2) {
325 AliError("invalid key format");
326 delete rubricKeyPair;
327 delete rubricKeyPairs;
328 for (Int_t i=0; i<nRubrics; i++) if (selects[i]) delete[] selects[i];
333 // check wether to select or to discard the keyWords
334 Int_t include = kTRUE;
335 TString ListOfKeyWords(static_cast<TObjString*>(rubricKeyPair->UncheckedAt(1))->String());
336 if (ListOfKeyWords.BeginsWith("ANY-")) {
337 ListOfKeyWords.Remove(0,4);
341 // select the key words
342 const TString& rubric = static_cast<TObjString*>(rubricKeyPair->UncheckedAt(0))->String();
343 if (!Select(include, rubric, ListOfKeyWords, displayedRubrics.Contains(rubric.Data()), selects)) {
344 delete rubricKeyPair;
345 delete rubricKeyPairs;
346 for (Int_t i=0; i<nRubrics; i++) if (selects[i]) delete[] selects[i];
352 delete rubricKeyPair;
356 delete rubricKeyPairs;
358 // complete the selection of other rubrics
359 for (Int_t i=0; i<nRubrics; i++) {
361 // skip already processed rubrics
362 if (selects[i]) continue;
364 // create the list of bins
365 Int_t nBins = GetNActiveBins(i) + 1;
366 selects[i] = new Short_t[nBins];
368 // select all key words or only the key work "ANY"
369 if (ContainsAny(i) && !displayedRubrics.Contains(fCounters->GetAxis(i)->GetName())) {
370 memset(selects[i], 0, sizeof(Short_t) * nBins);
371 selects[i][FindBin(i, "ANY", kFALSE)] = 1;
372 } else for (Int_t j=0; j<nBins; j++) selects[i][j] = 1;
379 //-----------------------------------------------------------------------
380 Bool_t AliCounterCollection::Select(Bool_t include, const TString& rubric, const TString& keywords,
381 Bool_t displayed, Short_t* selectBins[])
383 /// Tag the selected keywords (separated by ',') in that rubric (-1=subtract; 0=discard; 1=add).
385 Int_t dim = FindDim(rubric);
386 if (dim < 0) return kFALSE;
388 if (selectBins[dim]) {
389 AliWarning(Form("selection already made for rubric %s --> ignored",rubric.Data()));
393 // get list of key words to select
394 TObjArray* keys = keywords.Tokenize(",");
395 if (keys->GetEntriesFast() == 0) {
396 AliError(Form("no key word specified for rubric %s",rubric.Data()));
401 // create the list of bins
402 Int_t nBins = GetNActiveBins(dim) + 1;
403 selectBins[dim] = new Short_t[nBins];
405 // select/unselect all bins
406 Bool_t containsAny = ContainsAny(dim);
407 if (include || (containsAny && !displayed)) {
408 memset(selectBins[dim], 0, sizeof(Short_t) * nBins);
409 if (!include) selectBins[dim][FindBin(dim, "ANY", kFALSE)] = 1;
410 } else for (Int_t j=0; j<nBins; j++) selectBins[dim][j] = 1;
412 // select/unselect specific key words
413 TObjString* key = 0x0;
415 while ((key = static_cast<TObjString*>(nextKey()))) {
417 // special case of key word "ANY"
418 if (key->String() == "ANY") {
422 Int_t binAny = FindBin(dim, "ANY", kFALSE);
423 if (include) selectBins[dim][binAny] = 1;
424 else selectBins[dim][binAny] = 0;
428 if (include) for (Int_t j=0; j<nBins; j++) selectBins[dim][j] = 1;
429 else memset(selectBins[dim], 0, sizeof(Short_t) * nBins);
433 } else { // other cases
435 // find the corresponding bin
436 Int_t bin = FindBin(dim, key->String().Data(), kFALSE);
442 // select/unselect it
443 if (include) selectBins[dim][bin] = 1;
444 else if (containsAny && !displayed) selectBins[dim][bin] = -1;
445 else selectBins[dim][bin] = 0;
457 //-----------------------------------------------------------------------
458 void AliCounterCollection::CleanListOfStrings(TObjArray* list)
460 /// Make sure all strings appear only once in this list
462 // remove multiple-occurrence
463 Int_t nEntries = list->GetEntriesFast();
464 for (Int_t i = 0; i < nEntries; i++) {
465 TObjString* entry1 = static_cast<TObjString*>(list->UncheckedAt(i));
466 if (!entry1) continue;
467 for (Int_t j = i+1; j < nEntries; j++) {
468 TObjString* entry2 = static_cast<TObjString*>(list->UncheckedAt(j));
469 if (entry2 && entry2->IsEqual(entry1)) {
470 AliWarning(Form("multiple-occurence of string \"%s\" --> removed",entry2->String().Data()));
476 // remove empty slots
480 //-----------------------------------------------------------------------
481 void AliCounterCollection::Count(TString externalKey, Int_t value)
483 /// Add "value" to the counter referenced by "externalKey".
484 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
485 if (value > 0) CountAsDouble(externalKey, (Double_t)value);
486 else if (value < 0) AliError("cannot count negative values");
489 //-----------------------------------------------------------------------
490 void AliCounterCollection::Count(TString externalKey, Double_t value)
492 /// Add "value" to the counter referenced by "externalKey".
493 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
494 if (fWeightedCounters) CountAsDouble(externalKey, value);
495 else AliError("non-weighted counters can only be filled with intergers");
498 //-----------------------------------------------------------------------
499 void AliCounterCollection::CountAsDouble(TString externalKey, Double_t value)
501 /// Add "value" to the counter referenced by "externalKey".
502 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
505 AliError("counters are not initialized");
509 externalKey.ToUpper();
511 // convert external to internal key
512 Int_t nEmptySlots = 0;
513 const Int_t* bins = FindBins(externalKey, kTRUE, nEmptySlots);
516 // check for empty slots
517 if (nEmptySlots > 0) {
518 AliError("incomplete key");
523 if (fWeightedCounters || fLongCounters) {
525 // increment the corresponding counter
526 fCounters->AddBinContent(bins, value);
530 // switch to long counters if needed before incrementing
531 Long64_t linBin = fCounters->GetBin(bins, kTRUE);
532 Double_t currentValue = fCounters->GetBinContent(linBin);
533 if (currentValue+value > INT_MAX) {
534 ConvertToTHnSparseL(fCounters);
535 fLongCounters = kTRUE;
536 fCounters->AddBinContent(bins, value);
537 } else fCounters->AddBinContent(linBin, value);
545 //-----------------------------------------------------------------------
546 void AliCounterCollection::Print(const Option_t* opt) const
548 /// Print every individual counters if opt=="" or call "Print(opt, "")".
550 if (strcmp(opt,"")) {
551 const_cast<AliCounterCollection*>(this)->Print(opt, "");
556 AliError("counters are not initialized");
560 if (fCounters->GetNbins() == 0) {
561 printf("\nall counters are empty\n\n");
565 Int_t nRubrics = fCounters->GetNdimensions();
566 Int_t* bins = new Int_t[nRubrics];
568 // loop over every filled counters
569 for (Long64_t i=0; i<fCounters->GetNbins(); ++i) {
571 // get the content of the bin
572 Double_t value = fCounters->GetBinContent(i, bins);
574 // build the corresponding counter name
576 for (Int_t j=0; j<nRubrics; j++) counter += Form("/%s",fCounters->GetAxis(j)->GetBinLabel(bins[j]));
580 if (fWeightedCounters) printf("\n%s %g", counter.Data(), value);
581 else if (fLongCounters) printf("\n%s %ld", counter.Data(), (Long_t)value);
582 else printf("\n%s %d", counter.Data(), (Int_t)value);
590 //-----------------------------------------------------------------------
591 TString AliCounterCollection::GetKeyWords(TString rubric) const
593 /// return the list of key words for the given rubric.
595 TString keyWords = "";
598 AliError("counters are not initialized");
604 // get the dimension corresponding to that rubric
605 Int_t dim = FindDim(rubric);
606 if (dim < 0) return keyWords;
608 // build list of key words
609 TObjString* label = 0x0;
610 TIter nextLabel(fCounters->GetAxis(dim)->GetLabels());
611 while ((label = static_cast<TObjString*>(nextLabel()))) keyWords += Form("%s,",label->String().Data());
612 keyWords.Remove(TString::kTrailing, ',');
617 //-----------------------------------------------------------------------
618 Double_t AliCounterCollection::GetSum(TString selections, Bool_t* longCounters)
620 /// Get the overall statistics for the given selection (result is integrated over not specified rubrics):
621 /// - format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
622 /// - The flag "longCounters" tells whether the histogram contains value(s) larger than INT_MAX.
624 // set the flag "longCounters" according to the content of current counters
625 if (longCounters) *longCounters = fLongCounters;
628 AliError("counters are not initialized");
632 selections.ToUpper();
634 // decode the selections
635 Short_t** select = DecodeSelection(selections, TObjArray());
636 if (!select) return 0.;
638 // loop over every filled counters and compute integral
640 Int_t nDims = fCounters->GetNdimensions();
641 Int_t* coord = new Int_t[nDims];
642 for (Long64_t i=0; i<fCounters->GetNbins(); ++i) {
644 // get the content of the counter
645 Double_t value = fCounters->GetBinContent(i, coord);
647 // discard not selected counters and compute the selection factor
648 Int_t selectionFactor = 1;
649 for (Int_t dim = 0; dim < nDims && selectionFactor != 0; dim++) selectionFactor *= select[dim][coord[dim]];
650 if (selectionFactor == 0) continue;
653 sum += selectionFactor * value;
656 // check if the sum exceed INT_MAX (only in case of integer counters)
657 if (longCounters && !fWeightedCounters && sum > INT_MAX) *longCounters = kTRUE;
660 for (Int_t iDim=0; iDim<nDims; iDim++) delete[] select[iDim];
667 //-----------------------------------------------------------------------
668 void AliCounterCollection::PrintKeyWords() const
670 /// Print the full list of key words.
673 AliError("counters are not initialized");
678 Int_t nRubrics = fCounters->GetNdimensions();
679 for (Int_t iDim=0; iDim<nRubrics; iDim++) {
680 TAxis* axis = fCounters->GetAxis(iDim);
682 // print rubric's name
683 printf("\n%s:", axis->GetName());
685 // loop over key words
686 Bool_t first = kTRUE;
687 TObjString* label = 0x0;
688 TIter nextLabel(axis->GetLabels());
689 while ((label = static_cast<TObjString*>(nextLabel()))) {
691 //print key word's name
693 printf("%s", label->String().Data());
695 } else printf(",%s", label->String().Data());
702 //-----------------------------------------------------------------------
703 void AliCounterCollection::PrintValue(TString selections)
705 /// Print value of selected counter.
706 /// format of "selections" is rubric:keyWord/rubric:keyWord/rubric:keyWord/...
709 AliError("counters are not initialized");
713 selections.ToUpper();
715 // convert external to internal key
716 Int_t nEmptySlots = 0;
717 const Int_t* selectedBins = FindBins(selections, kFALSE, nEmptySlots);
718 if (!selectedBins) return;
720 // check for empty slots
721 if (nEmptySlots > 0) {
722 AliError("incomplete key");
723 delete[] selectedBins;
728 if (fWeightedCounters) printf("\n%g\n\n", fCounters->GetBinContent(selectedBins));
729 else if (fLongCounters) printf("\n%ld\n\n", (Long_t) fCounters->GetBinContent(selectedBins));
730 else printf("\n%d\n\n", (Int_t) fCounters->GetBinContent(selectedBins));
733 delete[] selectedBins;
736 //-----------------------------------------------------------------------
737 void AliCounterCollection::Print(TString rubrics, TString selections, Bool_t removeEmpty)
739 /// Print desired rubrics for the given selection:
740 /// - format of "rubrics" is rubric1/rubric2/.. (order matters only for output).
741 /// - format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
742 /// If "data" contains 1 rubric, the output will be one counter for each element of that rubric.
743 /// If "data" contains 2 rubrics, the output will be an array of counters, rubric1 vs rubric2.
744 /// If "data" contains 3 rubrics, the output will be an array rubric1 vs rubric2 for each element in rubric3.
746 /// Results are integrated over rubrics not specified neither in "rubrics" nor in "selections".
749 AliError("counters are not initialized");
754 selections.ToUpper();
756 // get the rubrics to print
757 TObjArray* rubricsToPrint = rubrics.Tokenize("/");
758 if (rubricsToPrint->GetEntriesFast() == 0) {
759 delete rubricsToPrint;
763 // remove rubrics called twice
764 CleanListOfStrings(rubricsToPrint);
766 // project counters in the rubrics to print according to the selections
767 Bool_t longCounters = kFALSE;
768 TObject* hist = Projection(*rubricsToPrint, selections, longCounters);
770 delete rubricsToPrint;
775 Int_t nRubricsToPrint = rubricsToPrint->GetEntriesFast();
776 if (nRubricsToPrint == 1 && (static_cast<TH1D*>(hist))->GetEntries() > 0)
777 PrintList(static_cast<TH1D*>(hist), removeEmpty, longCounters);
778 else if (nRubricsToPrint == 2 && (static_cast<TH2D*>(hist))->GetEntries() > 0)
779 PrintArray(static_cast<TH2D*>(hist), removeEmpty, longCounters);
780 else if (nRubricsToPrint > 2 && (static_cast<THnSparse*>(hist))->GetEntries() > 0)
781 PrintListOfArrays(static_cast<THnSparse*>(hist), removeEmpty, longCounters);
783 printf("\nselected counters are empty\n\n");
786 delete rubricsToPrint;
790 //-----------------------------------------------------------------------
791 void AliCounterCollection::PrintSum(TString selections)
793 /// Print the overall statistics for the given selection (result is integrated over not specified rubrics):
794 /// - format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
795 Bool_t longCounters = kFALSE;
796 Double_t sum = GetSum(selections, &longCounters);
797 if (fWeightedCounters) printf("\n%g\n\n", sum);
798 else if (longCounters) printf("\n%ld\n\n", (Long_t)sum);
799 else printf("\n%d\n\n", (Int_t)sum);
802 //-----------------------------------------------------------------------
803 void AliCounterCollection::PrintList(const TH1D* hist, Bool_t removeEmpty, Bool_t longCounters) const
805 /// Print the content of 1D histogram as a list.
807 // set the format to print labels
808 THashList* labels = hist->GetXaxis()->GetLabels();
810 if (fWeightedCounters) format = Form("\n%%%ds %%10g",GetMaxLabelSize(labels));
811 else if (longCounters) format = Form("\n%%%ds %%16ld",GetMaxLabelSize(labels));
812 else format = Form("\n%%%ds %%10d",GetMaxLabelSize(labels));
814 // print value for each label
815 TObjString* label = 0x0;
816 TIter nextLabel(labels);
817 while ((label = static_cast<TObjString*>(nextLabel()))) {
818 Int_t bin = (Int_t) label->GetUniqueID();
819 Double_t value = hist->GetBinContent(bin);
820 if (removeEmpty && value == 0.) continue;
821 if (fWeightedCounters) printf(format.Data(), label->String().Data(), value);
822 else if (longCounters) printf(format.Data(), label->String().Data(), (Long_t) value);
823 else printf(format.Data(), label->String().Data(), (Int_t) value);
828 //-----------------------------------------------------------------------
829 void AliCounterCollection::PrintArray(const TH2D* hist, Bool_t removeEmpty, Bool_t longCounters) const
831 /// Print the content of 2D histogram as an array.
833 // set the format to print labels in X direction
834 THashList* labelsX = hist->GetXaxis()->GetLabels();
835 TString formatX(Form("\n%%%ds ",GetMaxLabelSize(labelsX)));
837 // set the format to print labels in Y direction
838 THashList* labelsY = hist->GetYaxis()->GetLabels();
839 Int_t maxLabelSizeY = GetMaxLabelSize(labelsY);
840 TString formatYv = "";
841 if (fWeightedCounters) {
842 maxLabelSizeY = TMath::Max(10, maxLabelSizeY);
843 formatYv = Form("%%%dg ",maxLabelSizeY);
844 } else if (longCounters) {
845 maxLabelSizeY = TMath::Max(16, maxLabelSizeY);
846 formatYv = Form("%%%dld ",maxLabelSizeY);
848 maxLabelSizeY = TMath::Max(10, maxLabelSizeY);
849 formatYv = Form("%%%dd ",maxLabelSizeY);
851 TString formatYs = Form("%%%ds ",maxLabelSizeY);
853 // if required, set the list of labels for which all counters are not empty
854 Bool_t *useLabelX = 0x0, *useLabelY = 0x0;
855 TObjString *labelX = 0x0, *labelY = 0x0;
856 TIter nextLabelX(labelsX);
857 TIter nextLabelY(labelsY);
860 // create label flags and set them as unused
861 useLabelX = new Bool_t[labelsX->GetSize()+1];
862 memset(useLabelX, kFALSE, sizeof(Bool_t) * (labelsX->GetSize()+1));
863 useLabelY = new Bool_t[labelsY->GetSize()+1];
864 memset(useLabelY, kFALSE, sizeof(Bool_t) * (labelsY->GetSize()+1));
866 // loop over labels in X direction
867 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
868 Int_t binX = (Int_t) labelX->GetUniqueID();
870 // loop over labels in Y direction
872 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
873 Int_t binY = (Int_t) labelY->GetUniqueID();
875 // both labels already set as used
876 if (useLabelX[binX] && useLabelY[binY]) continue;
879 if (hist->GetBinContent(binX, binY) == 0.) continue;
882 useLabelX[binX] = kTRUE;
883 useLabelY[binY] = kTRUE;
891 // print labels in Y axis
892 printf(formatX.Data()," ");
894 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
895 if (removeEmpty && !useLabelY[labelY->GetUniqueID()]) continue;
896 printf(formatYs.Data(), labelY->String().Data());
899 // fill array for each label in X axis
901 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
902 Int_t binX = (Int_t) labelX->GetUniqueID();
904 if (removeEmpty && !useLabelX[binX]) continue;
907 printf(formatX.Data(), labelX->String().Data());
909 // print value for each label in Y axis
911 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
912 Int_t binY = (Int_t) labelY->GetUniqueID();
913 if (removeEmpty && !useLabelY[binY]) continue;
914 if (fWeightedCounters) printf(formatYv.Data(), hist->GetBinContent(binX, binY));
915 else if (longCounters) printf(formatYv.Data(), (Long_t) hist->GetBinContent(binX, binY));
916 else printf(formatYv.Data(), (Int_t) hist->GetBinContent(binX, binY));
928 //-----------------------------------------------------------------------
929 void AliCounterCollection::PrintListOfArrays(const THnSparse* hist, Bool_t removeEmpty, Bool_t longCounters) const
931 /// Print the content of nD histogram as a list of arrays.
933 // set the format to print labels in X direction
934 THashList* labelsX = hist->GetAxis(0)->GetLabels();
935 TString formatX(Form("\n%%%ds ",GetMaxLabelSize(labelsX)));
937 // set the format to print labels in Y direction
938 THashList* labelsY = hist->GetAxis(1)->GetLabels();
939 Int_t maxLabelSizeY = GetMaxLabelSize(labelsY);
940 TString formatYv = "";
941 if (fWeightedCounters) {
942 maxLabelSizeY = TMath::Max(10, maxLabelSizeY);
943 formatYv = Form("%%%dg ",maxLabelSizeY);
944 } else if (longCounters) {
945 maxLabelSizeY = TMath::Max(16, maxLabelSizeY);
946 formatYv = Form("%%%dld ",maxLabelSizeY);
948 maxLabelSizeY = TMath::Max(10, maxLabelSizeY);
949 formatYv = Form("%%%dd ",maxLabelSizeY);
951 TString formatYs(Form("%%%ds ",maxLabelSizeY));
953 // create a list containing each combination of labels refering the arrays to be printout
955 listOfCombis.SetOwner();
957 // add a first empty combination
958 Int_t nDim = hist->GetNdimensions();
959 listOfCombis.AddLast(new TObjArray(nDim-2));
961 // loop over the nDim-2 other rubrics
962 for (Int_t i=2; i<nDim; i++) {
964 // save the last label of that rubic
965 THashList* labels = hist->GetAxis(i)->GetLabels();
966 TObjString* lastLabel = (labels) ? static_cast<TObjString*>(labels->Last()) : 0x0;
967 if (!lastLabel) return;
969 // prepare iteration over the list of labels
970 TIter nextLabel(labels);
972 // loop over existing combinations
973 TObjLink* lnk = listOfCombis.FirstLink();
976 // get the current combination
977 TObjArray* currentCombi = static_cast<TObjArray*>(lnk->GetObject());
979 // loop over labels in the current rubric
981 TObjString* label = 0x0;
982 while ((label = static_cast<TObjString*>(nextLabel()))) {
984 // stop at the last one
985 if (label == lastLabel) break;
987 // copy the current combination, add the current label to it and add it to the list of combinations
988 TObjArray* combi = new TObjArray(*currentCombi);
989 combi->AddLast(label);
990 listOfCombis.AddBefore(lnk, combi);
993 // add the last label to the current combination
994 currentCombi->AddLast(lastLabel);
1001 // create bin coordinates to access individual counters
1002 Int_t* bins = new Int_t[nDim];
1004 // create label flags
1005 Bool_t *useLabelX = 0x0, *useLabelY = 0x0;
1007 useLabelX = new Bool_t[labelsX->GetSize()+1];
1008 useLabelY = new Bool_t[labelsY->GetSize()+1];
1011 // loop over each combination of labels
1012 TObjArray* combi = 0x0;
1013 TIter nextCombi(&listOfCombis);
1014 while ((combi = static_cast<TObjArray*>(nextCombi()))) {
1016 // make the name of the combination and fill the corresponding bin coordinates
1017 TString combiName = "/";
1018 for (Int_t i=2; i<nDim; i++) {
1019 TObjString* label = static_cast<TObjString*>(combi->UncheckedAt(i-2));
1020 combiName += Form("%s/",label->String().Data());
1021 bins[i] = (Int_t)label->GetUniqueID();
1024 // reset the list of labels for which all counters are not empty
1026 memset(useLabelX, kFALSE, sizeof(Bool_t) * (labelsX->GetSize()+1));
1027 memset(useLabelY, kFALSE, sizeof(Bool_t) * (labelsY->GetSize()+1));
1030 Bool_t empty = kTRUE;
1031 TObjString* labelX = 0x0;
1032 TObjString* labelY = 0x0;
1033 TIter nextLabelX(labelsX);
1034 TIter nextLabelY(labelsY);
1035 // loop over labels in X direction
1036 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
1037 bins[0] = (Int_t) labelX->GetUniqueID();
1039 // loop over labels in Y direction
1041 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
1042 bins[1] = (Int_t) labelY->GetUniqueID();
1044 // both labels already set as used
1045 if (removeEmpty && useLabelX[bins[0]] && useLabelY[bins[1]]) continue;
1048 if (hist->GetBinContent(bins) == 0.) continue;
1050 // set label as used and array as not empty
1053 useLabelX[bins[0]] = kTRUE;
1054 useLabelY[bins[1]] = kTRUE;
1059 if (!removeEmpty && !empty) break;
1063 // skip empty arrays
1064 if (empty) continue;
1066 // print the name of the combination of labels refering the incoming array
1067 printf("\n%s:\n",combiName.Data());
1069 // print labels in Y axis
1070 printf(formatX.Data()," ");
1072 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
1073 if (removeEmpty && !useLabelY[labelY->GetUniqueID()]) continue;
1074 printf(formatYs.Data(), labelY->String().Data());
1077 // fill array for each label in X axis
1079 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
1080 bins[0] = (Int_t) labelX->GetUniqueID();
1082 if (removeEmpty && !useLabelX[bins[0]]) continue;
1085 printf(formatX.Data(), labelX->String().Data());
1087 // print value for each label in Y axis
1089 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
1090 bins[1] = (Int_t) labelY->GetUniqueID();
1091 if (removeEmpty && !useLabelY[bins[1]]) continue;
1092 if (fWeightedCounters) printf(formatYv.Data(), hist->GetBinContent(bins));
1093 else if (longCounters) printf(formatYv.Data(), (Long_t) hist->GetBinContent(bins));
1094 else printf(formatYv.Data(), (Int_t) hist->GetBinContent(bins));
1108 //-----------------------------------------------------------------------
1109 Int_t AliCounterCollection::GetMaxLabelSize(THashList* labels) const
1111 /// Return the number of characters of the longest label.
1112 Int_t maxLabelSize = 0;
1113 TObjString* label = 0x0;
1114 TIter nextLabel(labels);
1115 while ((label = static_cast<TObjString*>(nextLabel())))
1116 maxLabelSize = TMath::Max(maxLabelSize, label->String().Length());
1117 return maxLabelSize;
1120 //-----------------------------------------------------------------------
1121 TH1D* AliCounterCollection::Get(TString rubric, TString selections)
1123 /// Get counters of the rubric "rubric" for the given "selection".
1124 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1125 /// Results are integrated over rubrics not specified neither in "rubric1" nor in "selections".
1126 /// It is the responsability of the user to delete the returned histogram.
1129 AliError("counters are not initialized");
1134 selections.ToUpper();
1136 // fill the rubrics to print
1137 TObjArray rubricsToPrint(1);
1138 rubricsToPrint.SetOwner();
1139 rubricsToPrint.AddLast(new TObjString(rubric.Data()));
1141 // project counters in the rubrics to print according to the selections
1142 Bool_t longCounters = kFALSE;
1143 TH1D* hist = static_cast<TH1D*>(Projection(rubricsToPrint, selections, longCounters));
1145 // make it ready to display
1148 // remove statistic box
1149 hist->SetStats(kFALSE);
1152 TAxis* axis = hist->GetXaxis();
1153 THashList* labels = axis->GetLabels();
1154 Int_t nLabels = (labels) ? labels->GetSize() : 1;
1155 axis->SetRange(1,nLabels);
1156 axis->SetNdivisions(1,kFALSE);
1157 axis->SetTitle(rubric.Data());
1160 hist->GetYaxis()->SetTitle("Counts");
1166 //-----------------------------------------------------------------------
1167 TH2D* AliCounterCollection::Get(TString rubric1, TString rubric2, TString selections)
1169 /// Get counters of the "rubric1" vs "rubric2" for the given "selection".
1170 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1171 /// Results are integrated over rubrics not specified neither in "rubric1", "rubric2" nor in "selections".
1172 /// It is the responsability of the user to delete the returned histogram.
1175 AliError("counters are not initialized");
1181 selections.ToUpper();
1183 // fill the rubrics to print
1184 TObjArray rubricsToPrint(2);
1185 rubricsToPrint.SetOwner();
1186 rubricsToPrint.AddLast(new TObjString(rubric2.Data()));
1187 rubricsToPrint.AddLast(new TObjString(rubric1.Data()));
1189 // project counters in the rubrics to print according to the selections
1190 Bool_t longCounters = kFALSE;
1191 TH2D* hist = static_cast<TH2D*>(Projection(rubricsToPrint, selections, longCounters));
1196 // remove statistic box
1197 hist->SetStats(kFALSE);
1200 TAxis* axisX = hist->GetXaxis();
1201 THashList* labelsX = axisX->GetLabels();
1202 Int_t nLabelsX = (labelsX) ? labelsX->GetSize() : 1;
1203 axisX->SetRange(1,nLabelsX);
1204 axisX->SetNdivisions(1,kFALSE);
1205 axisX->SetTitle(rubric2.Data());
1208 TAxis* axisY = hist->GetYaxis();
1209 THashList* labelsY = axisY->GetLabels();
1210 Int_t nLabelsY = (labelsY) ? labelsY->GetSize() : 1;
1211 axisY->SetRange(1,nLabelsY);
1212 axisY->SetNdivisions(1,kFALSE);
1213 axisY->SetTitle(rubric1.Data());
1219 //-----------------------------------------------------------------------
1220 TH1D* AliCounterCollection::Draw(TString rubric, TString selections)
1222 /// Draw counters of the rubric "rubric" for the given "selection".
1223 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1224 /// Results are integrated over rubrics not specified neither in "rubric1" nor in "selections".
1225 /// It is the responsability of the user to delete the returned histogram.
1226 TH1D* hist = Get(rubric, selections);
1227 if (hist) hist->Draw("htext");
1231 //-----------------------------------------------------------------------
1232 TH2D* AliCounterCollection::Draw(TString rubric1, TString rubric2, TString selections)
1234 /// Draw counters of the "rubric1" vs "rubric2" for the given "selection".
1235 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1236 /// Results are integrated over rubrics not specified neither in "rubric1", "rubric2" nor in "selections".
1237 /// It is the responsability of the user to delete the returned histogram.
1238 TH2D* hist = Get(rubric1, rubric2, selections);
1239 if (hist) hist->Draw("text");
1243 //-----------------------------------------------------------------------
1244 TObject* AliCounterCollection::Projection(const TObjArray& data, const TString& selections, Bool_t& longCounters)
1246 /// Return desired "data" for the given "selection" stored in a new histogram or 0x0 in case of failure.
1247 /// The type of the histogram (TH1D, TH2D or THnSparse) depend on the number of data.
1248 /// The flag "longCounters" tells whether the histogram contains value(s) larger than INT_MAX.
1249 /// It is the responsability of the user to delete the returned histogram.
1251 // set the flag "longCounters" according to the content of current counters
1252 longCounters = fLongCounters;
1254 // decode the selections
1255 Short_t** select = DecodeSelection(selections, data);
1256 if (!select) return 0x0;
1258 // define name and dimensions of projection histo
1259 TString name(fCounters->GetName());
1260 Int_t nDims = fCounters->GetNdimensions();
1261 Int_t nTargetDims = data.GetEntriesFast();
1262 TArrayI targetDims(nTargetDims);
1263 TArrayI nNewBins(nTargetDims);
1264 TArrayI* OldToNewCoord = new TArrayI[nTargetDims];
1265 for (Int_t i=0; i<nTargetDims; i++) {
1268 name += Form("_%s",static_cast<TObjString*>(data.UncheckedAt(i))->String().Data());
1271 targetDims[i] = FindDim(static_cast<TObjString*>(data.UncheckedAt(i))->String());
1273 // set number of selected bins in the target dims and make the correspondence between old and new coordinates
1275 if (targetDims[i] > -1) {
1276 Int_t nBins = GetNActiveBins(targetDims[i]) + 1;
1277 OldToNewCoord[i].Set(nBins);
1278 for (Int_t j=1; j<nBins; j++) if (select[targetDims[i]][j] > 0) OldToNewCoord[i][j] = ++nNewBins[i];
1281 // clean memory and return 0x0 in case of problem
1282 if (nNewBins[i] == 0) {
1283 for (Int_t iDim=0; iDim<nDims; iDim++) delete[] select[iDim];
1285 delete[] OldToNewCoord;
1291 // define title of projection histo
1292 TString title = "Selections: ";
1293 TString selectionString(selections);
1294 selectionString.Remove(TString::kBoth, '/');
1295 if (selectionString.Length() > 0) title += Form("%s/", selectionString.Data());
1297 TIter nextRubric(fRubrics);
1298 while ((rub = nextRubric())) {
1299 if (selectionString.Contains(Form("%s:",rub->GetName()))) continue;
1300 if (data.Contains(rub->GetName())) continue;
1301 title += Form("%s:ANY/", rub->GetName());
1303 title.ReplaceAll("/", " ");
1305 // Create new histograms
1307 if (nTargetDims == 1) hist = new TH1D(name.Data(), title.Data(), nNewBins[0], 0., 1.);
1308 else if (nTargetDims == 2) hist = new TH2D(name.Data(), title.Data(), nNewBins[0], 0., 1., nNewBins[1], 0., 1.);
1309 else if (fWeightedCounters) hist = new THnSparseT<TArrayF>(name.Data(), title.Data(), nTargetDims, nNewBins.GetArray(), 0x0, 0x0);
1310 else if (fLongCounters) hist = new THnSparseT<TArrayL>(name.Data(), title.Data(), nTargetDims, nNewBins.GetArray(), 0x0, 0x0);
1311 else hist = new THnSparseT<TArrayI>(name.Data(), title.Data(), nTargetDims, nNewBins.GetArray(), 0x0, 0x0);
1313 // Set new axis labels
1315 if (nTargetDims < 3) {
1318 TIter nextLabelX(fCounters->GetAxis(targetDims[0])->GetLabels());
1319 while ((label = static_cast<TObjString*>(nextLabelX()))) {
1320 if (select[targetDims[0]][label->GetUniqueID()] > 0) {
1321 static_cast<TH1*>(hist)->GetXaxis()->SetBinLabel(OldToNewCoord[0][label->GetUniqueID()], label->String().Data());
1326 if (nTargetDims == 2) {
1327 TIter nextLabelY(fCounters->GetAxis(targetDims[1])->GetLabels());
1328 while ((label = static_cast<TObjString*>(nextLabelY()))) {
1329 if (select[targetDims[1]][label->GetUniqueID()] > 0) {
1330 static_cast<TH1*>(hist)->GetYaxis()->SetBinLabel(OldToNewCoord[1][label->GetUniqueID()], label->String().Data());
1338 for (Int_t i=0; i<nTargetDims; i++) {
1339 TIter nextLabel(fCounters->GetAxis(targetDims[i])->GetLabels());
1340 while ((label = static_cast<TObjString*>(nextLabel()))) {
1341 if (select[targetDims[i]][label->GetUniqueID()] > 0) {
1342 static_cast<THnSparse*>(hist)->GetAxis(i)->SetBinLabel(OldToNewCoord[i][label->GetUniqueID()], label->String().Data());
1349 // loop over every filled counters
1350 Int_t* coord = new Int_t[nDims];
1351 Int_t* newCoord = new Int_t[nTargetDims];
1353 for (Long64_t i=0; i<fCounters->GetNbins(); ++i) {
1355 // get the content of the counter
1356 Double_t value = fCounters->GetBinContent(i, coord);
1358 // discard not selected counters and compute the selection factor
1359 Int_t selectionFactor = 1;
1360 for (Int_t dim = 0; dim < nDims && selectionFactor != 0; dim++) selectionFactor *= select[dim][coord[dim]];
1361 if (selectionFactor == 0) continue;
1363 // find new coordinates in the projection histo
1364 for (Int_t d = 0; d < nTargetDims; ++d) newCoord[d] = OldToNewCoord[d][coord[targetDims[d]]];
1366 // fill projection histo
1367 if (nTargetDims < 3) {
1369 Int_t linBin = (nTargetDims == 1) ? newCoord[0] : static_cast<TH1*>(hist)->GetBin(newCoord[0], newCoord[1]);
1370 static_cast<TH1*>(hist)->AddBinContent(linBin, selectionFactor*value);
1372 // check if the new value exceed INT_MAX (only in case of integer counters)
1373 if (!fWeightedCounters && !longCounters && static_cast<TH1*>(hist)->GetBinContent(linBin) > INT_MAX)
1374 longCounters = kTRUE;
1376 } else if (fWeightedCounters || longCounters) {
1378 static_cast<THnSparse*>(hist)->AddBinContent(newCoord, selectionFactor*value);
1382 // switch to long counters if needed before filling
1383 Long64_t linBin = static_cast<THnSparse*>(hist)->GetBin(newCoord, kTRUE);
1384 Double_t currentValue = static_cast<THnSparse*>(hist)->GetBinContent(linBin);
1385 if (currentValue+selectionFactor*value > INT_MAX) {
1386 THnSparse* h = static_cast<THnSparse*>(hist);
1387 ConvertToTHnSparseL(h);
1389 longCounters = kTRUE;
1390 static_cast<THnSparse*>(hist)->AddBinContent(newCoord, selectionFactor*value);
1391 } else static_cast<THnSparse*>(hist)->AddBinContent(linBin, selectionFactor*value);
1398 // update the number of entries
1399 if (nTargetDims < 3) static_cast<TH1*>(hist)->SetEntries(nEntries);
1400 else static_cast<THnSparse*>(hist)->SetEntries(nEntries);
1403 for (Int_t iDim=0; iDim<nDims; iDim++) delete[] select[iDim];
1407 delete[] OldToNewCoord;
1412 //-----------------------------------------------------------------------
1413 Int_t* AliCounterCollection::CheckConsistency(const AliCounterCollection* c)
1415 /// Consistency check of the two counter collections. To be consistent, both counters
1416 /// must have the same rubrics with the same list of authorized key words if any.
1417 /// Return the correspondence between the local rubric ordering and the one of the other counter,
1418 /// or 0x0 in case of problem. It is the responsability of the user to delete the returned array.
1420 if (!fCounters || !c->fCounters) {
1421 AliError("counters are not initialized");
1425 // check if both counters are weighted or not
1426 if (c->fWeightedCounters != fWeightedCounters) AliWarning("merging non-weighted with weigthed counters");
1428 // check if the number of rubrics is the same
1429 Int_t nRubrics = fRubrics->GetSize();
1430 if (c->fRubrics->GetSize() != nRubrics) {
1431 AliError("both counters do not contain the same number of rubrics");
1435 Int_t* otherDims = new Int_t[nRubrics];
1437 // loop over local rubrics
1438 TObject* rubric1 = 0x0;
1439 TIter nextRubric(fRubrics);
1440 while ((rubric1 = nextRubric())) {
1442 // find that rubric in the other counter
1443 TObject* rubric2 = c->fRubrics->FindObject(rubric1->GetName());
1445 AliError(Form("the other counter does not contain the rubric %s", rubric1->GetName()));
1450 // check the list of authorized key words if any
1451 TObjArray* keyWords1 = dynamic_cast<TObjArray*>(rubric1);
1452 TObjArray* keyWords2 = dynamic_cast<TObjArray*>(rubric2);
1453 if (keyWords1 && keyWords2) {
1455 // check if the number of key words is the same
1456 if (keyWords1->GetEntriesFast() != keyWords2->GetEntriesFast()) {
1457 AliError("that rubric does not contain the same number of authorized key words in both counters");
1462 // loop over local key words
1463 TObjString* keyWord = 0x0;
1464 TIter nextKeyWord(keyWords1);
1465 while ((keyWord = static_cast<TObjString*>(nextKeyWord()))) {
1467 // find that key word in the corresponding rubric of the other counter
1468 if (!keyWords2->FindObject(keyWord->String().Data())) {
1469 AliError(Form("rubric %s does not contain the key word %s in the other counter", rubric1->GetName(), keyWord->String().Data()));
1476 } else if (keyWords1 || keyWords2) {
1478 // that rubric has not been initialized the same way in both counter
1480 AliError(Form("rubric %s of the other counter does not contain a list of authorized key words while this does", rubric1->GetName()));
1482 AliError(Form("rubric %s of this counter does not contain a list of authorized key words while the other does", rubric1->GetName()));
1489 // save the correspondence of rubric IDs in both counters
1490 otherDims[rubric1->GetUniqueID()] = rubric2->GetUniqueID();
1497 //-----------------------------------------------------------------------
1498 void AliCounterCollection::Add(const AliCounterCollection* counter)
1500 /// Add the given AliCounterCollections to this. They must have the
1501 /// same rubrics with the same list of authorized key words if any.
1503 // check the consistency between the other counter and this and get the correspondences between rubric IDs.
1504 Int_t* otherDims = CheckConsistency(counter);
1505 if (!otherDims) return;
1507 // switch to long counters if the given counter collection is of that type
1508 if (counter->fLongCounters && !fLongCounters) {
1509 ConvertToTHnSparseL(fCounters);
1510 fLongCounters = kTRUE;
1513 Int_t nRubrics = fCounters->GetNdimensions();
1514 Int_t* thisBins = new Int_t[nRubrics];
1515 Int_t* otherBins = new Int_t[nRubrics];
1517 // loop over every filled bins inside the other counter
1518 for (Long64_t i = 0; i < counter->fCounters->GetNbins(); i++) {
1520 // get the content of the bin
1521 Double_t value = counter->fCounters->GetBinContent(i, otherBins);
1523 // convert "other" bin coordinates to "this" bin coordinates
1525 for (Int_t dim = 0; dim < nRubrics; dim++) {
1526 TString label = counter->fCounters->GetAxis(otherDims[dim])->GetBinLabel(otherBins[otherDims[dim]]);
1527 thisBins[dim] = FindBin(dim, label, kTRUE);
1528 if (thisBins[dim] < 0) {
1529 AliError("this counter is full, unable to add that key word");
1536 if (fWeightedCounters || fLongCounters) {
1538 // increment the corresponding local counter
1539 fCounters->AddBinContent(thisBins, value);
1543 // switch to long counters if needed before incrementing
1544 Long64_t linBin = fCounters->GetBin(thisBins, kTRUE);
1545 Double_t currentValue = fCounters->GetBinContent(linBin);
1546 if (currentValue+value > INT_MAX) {
1547 ConvertToTHnSparseL(fCounters);
1548 fLongCounters = kTRUE;
1549 fCounters->AddBinContent(thisBins, value);
1550 } else fCounters->AddBinContent(linBin, value);
1562 //-----------------------------------------------------------------------
1563 Long64_t AliCounterCollection::Merge(TCollection* list)
1565 /// Merge this with a list of AliCounterCollections. All AliCounterCollections provided
1566 /// must have the same rubrics with the same list of authorized key words if any.
1568 if (!list || !fCounters) return 0;
1569 if (list->IsEmpty()) return (Long64_t)fCounters->GetEntries();
1572 const TObject* obj = 0x0;
1573 while ((obj = next())) {
1575 // check that "obj" is an object of the class AliCounterCollection
1576 const AliCounterCollection* counter = dynamic_cast<const AliCounterCollection*>(obj);
1578 AliFatal(Form("object named \"%s\" is a %s instead of an AliCounterCollection!", obj->GetName(), obj->ClassName()));
1582 // merge counter to this one
1587 return (Long64_t)fCounters->GetEntries();
1590 //-----------------------------------------------------------------------
1591 void AliCounterCollection::Sort(Option_t* opt, Bool_t asInt)
1593 /// Sort rubrics defined without a list of authorized key words or all rubrics if opt=="all".
1594 /// If asInt=kTRUE, key words are ordered as interger instead of alphabetically.
1597 AliError("counters are not initialized");
1601 Bool_t all = (!strcasecmp(opt, "all"));
1603 Bool_t somethingToSort = kFALSE;
1604 Int_t nRubrics = fRubrics->GetSize();
1605 Bool_t* rubricsToSort = new Bool_t[nRubrics];
1606 memset(rubricsToSort, kFALSE, sizeof(Bool_t) * nRubrics);
1608 // choose rubrics to sort
1609 TObject* rubric = 0x0;
1610 TIter nextRubric(fRubrics);
1611 while ((rubric = nextRubric())) {
1613 if (all || dynamic_cast<TObjString*>(rubric)) {
1615 // check if something to sort
1616 THashList* labels = fCounters->GetAxis((Int_t)rubric->GetUniqueID())->GetLabels();
1617 if (!labels || labels->GetSize() < 2) continue;
1619 // select that rubric
1620 rubricsToSort[(Int_t)rubric->GetUniqueID()] = kTRUE;
1621 somethingToSort = kTRUE;
1627 // sort selected rubrics if any
1628 if (somethingToSort) Sort(rubricsToSort, asInt);
1631 delete[] rubricsToSort;
1634 //-----------------------------------------------------------------------
1635 void AliCounterCollection::SortRubric(TString rubric, Bool_t asInt)
1637 /// Sort only that rubric. If asInt=kTRUE, key words are ordered as interger instead of alphabetically.
1640 AliError("counters are not initialized");
1646 // find the rubric to sort
1647 Int_t dim = FindDim(rubric);
1648 if (dim < 0) return;
1650 // check if something to sort
1651 THashList* labels = fCounters->GetAxis(dim)->GetLabels();
1652 if (!labels || labels->GetSize() < 2) return;
1654 // select that rubric
1655 Int_t nRubrics = fRubrics->GetSize();
1656 Bool_t* rubricsToSort = new Bool_t[nRubrics];
1657 memset(rubricsToSort, kFALSE, sizeof(Bool_t) * nRubrics);
1658 rubricsToSort[dim] = kTRUE;
1661 Sort(rubricsToSort, asInt);
1664 delete[] rubricsToSort;
1667 //-----------------------------------------------------------------------
1668 void AliCounterCollection::Sort(const Bool_t* rubricsToSort, Bool_t asInt)
1670 /// Sort labels (alphabetically or as integer) in each rubric flagged in "rubricsToSort".
1672 // create a new counter
1673 THnSparse* oldCounters = fCounters;
1674 Int_t nRubrics = fRubrics->GetSize();
1675 if (fWeightedCounters)
1676 fCounters = new THnSparseT<TArrayF>("hCounters", "hCounters", nRubrics, fRubricsSize->GetArray(), 0x0, 0x0);
1677 else if (fLongCounters)
1678 fCounters = new THnSparseT<TArrayL>("hCounters", "hCounters", nRubrics, fRubricsSize->GetArray(), 0x0, 0x0);
1680 fCounters = new THnSparseT<TArrayI>("hCounters", "hCounters", nRubrics, fRubricsSize->GetArray(), 0x0, 0x0);
1681 Int_t** newBins = new Int_t*[nRubrics];
1682 Bool_t newBinsFilled = kTRUE;
1684 // define the new axes
1685 for (Int_t i=0; i<nRubrics; i++) {
1686 TAxis* oldAxis = oldCounters->GetAxis(i);
1687 TAxis* newAxis = fCounters->GetAxis(i);
1689 // set the name of the new axis
1690 newAxis->SetName(oldAxis->GetName());
1693 THashList* oldLabels = oldAxis->GetLabels();
1696 newBinsFilled = kFALSE;
1700 // sort them if required
1701 if (rubricsToSort[i]) {
1702 if (asInt) { oldLabels = SortAsInt(oldLabels); }
1703 else { oldLabels->Sort(); }
1706 // set labels in the new axis and save the correspondence between new and old bins
1707 newBins[i] = new Int_t[oldLabels->GetSize()+1];
1708 TObjString* label = 0x0;
1710 TIter nextLabel(oldLabels);
1711 while ((label = static_cast<TObjString*>(nextLabel()))) {
1712 newAxis->SetBinLabel(bin, label->String().Data());
1713 newBins[i][(Int_t)label->GetUniqueID()] = bin;
1718 if (rubricsToSort[i] && asInt) delete oldLabels;
1721 // fill the new fCounters only if all axes have label(s) defined (otherwise it is empty)
1722 if (newBinsFilled) {
1724 // fill the new counters
1725 Int_t* oldCoor = new Int_t[nRubrics];
1726 Int_t* newCoor = new Int_t[nRubrics];
1727 for (Long64_t i = 0; i < oldCounters->GetNbins(); i++) {
1728 Double_t value = oldCounters->GetBinContent(i, oldCoor);
1729 for (Int_t dim = 0; dim < nRubrics; dim++) newCoor[dim] = newBins[dim][oldCoor[dim]];
1730 fCounters->AddBinContent(newCoor, value);
1739 for (Int_t i=0; i<nRubrics; i++) delete[] newBins[i];
1744 //-----------------------------------------------------------------------
1745 THashList* AliCounterCollection::SortAsInt(const THashList* labels)
1747 /// Return a list (not owner) of labels sorted assuming they are integers.
1748 /// It is the responsability of user to delete the returned list.
1750 THashList* sortedLabels = new THashList(labels->GetSize());
1751 TIter nextSortedLabel(sortedLabels);
1754 TObjString* label = 0x0;
1755 TIter nextLabel(labels);
1756 while ((label = static_cast<TObjString*>(nextLabel()))) {
1758 // find where to add it
1759 TObjString* sortedLabel = 0x0;
1760 nextSortedLabel.Reset();
1761 while ((sortedLabel = static_cast<TObjString*>(nextSortedLabel())) &&
1762 (sortedLabel->String().Atoi() <= label->String().Atoi())) {}
1765 if (sortedLabel) sortedLabels->AddBefore(sortedLabel, label);
1766 else sortedLabels->AddLast(label);
1769 return sortedLabels;
1772 //-----------------------------------------------------------------------
1773 void AliCounterCollection::ConvertToTHnSparseL(THnSparse* &h)
1775 /// Convert the given THnSparse to a THnSparseL (able to handle numbers >= 2^31)
1777 // create the new THnSparse
1778 Int_t nDims = h->GetNdimensions();
1779 Int_t* nBins = new Int_t[nDims];
1780 for (Int_t i=0; i<nDims; i++) nBins[i] = h->GetAxis(i)->GetNbins();
1781 THnSparse* hNew = new THnSparseT<TArrayL>("new", "new", nDims, nBins, 0x0, 0x0);
1784 // transfer the axes
1785 for (Int_t i=0; i<nDims; i++) {
1786 TAxis* oldAxis = h->GetAxis(i);
1787 TAxis* newAxis = hNew->GetAxis(i);
1789 // transfer the name
1790 newAxis->SetName(oldAxis->GetName());
1793 TObjString* label = 0x0;
1794 TIter nextLabel(oldAxis->GetLabels());
1795 while ((label = static_cast<TObjString*>(nextLabel())))
1796 newAxis->SetBinLabel(label->GetUniqueID(), label->String().Data());
1799 // fill the new THnSparse
1800 Int_t* coor = new Int_t[nDims];
1801 for (Long64_t i = 0; i < h->GetNbins(); i++) {
1802 Double_t value = h->GetBinContent(i, coor);
1803 hNew->AddBinContent(coor, value);
1807 // transfer the number of entries
1808 hNew->SetEntries(h->GetEntries());
1810 // remove old THnSparse and transfer its name and title to the new one
1811 TString name(h->GetName());
1812 TString title(h->GetTitle());
1815 h->SetNameTitle(name.Data(), title.Data());