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 **************************************************************************/
16 //-----------------------------------------------------------------------------
17 /// \class AliCounterCollection
19 /// generic class to handle a collection of counters
21 /// \author Philippe Pillot
22 //-----------------------------------------------------------------------------
24 #include "AliCounterCollection.h"
29 #include <TObjString.h>
30 #include <TObjArray.h>
31 #include <THnSparse.h>
32 #include <THashList.h>
36 #include <TCollection.h>
38 ClassImp(AliCounterCollection)
40 //-----------------------------------------------------------------------
41 AliCounterCollection::AliCounterCollection(const char* name) :
43 fRubrics(new THashList(10)),
44 fRubricsSize(new TArrayI(10)),
46 fWeightedCounters(kFALSE)
52 //-----------------------------------------------------------------------
53 AliCounterCollection::~AliCounterCollection()
61 //-----------------------------------------------------------------------
62 void AliCounterCollection::Clear(Option_t*)
66 fRubricsSize->Reset();
67 delete fCounters; fCounters = 0x0;
68 fWeightedCounters = kFALSE;
71 //-----------------------------------------------------------------------
72 void AliCounterCollection::AddRubric(TString name, TString listOfKeyWords)
74 /// Add a new rubric with the complete list of related key words separated by "/".
75 /// If the key word "any" is not defined, the overall statistics is
76 /// assumed to be the sum of the statistics under each key word.
79 listOfKeyWords.ToUpper();
81 if (fRubrics->Contains(name.Data())) {
82 AliError(Form("rubric named %s already exist",name.Data()));
86 // add the list of autorized key words
87 TObjArray* rubric = listOfKeyWords.Tokenize("/");
88 CleanListOfStrings(rubric);
89 rubric->SetName(name.Data());
90 Int_t nRubrics = fRubrics->GetSize();
91 rubric->SetUniqueID(nRubrics);
92 fRubrics->AddLast(rubric);
94 // save the number of autorized key words (expand the array if needed)
95 if (nRubrics+1 > fRubricsSize->GetSize()) fRubricsSize->Set(2*fRubricsSize->GetSize());
96 (*fRubricsSize)[nRubrics] = rubric->GetEntriesFast();
99 //-----------------------------------------------------------------------
100 void AliCounterCollection::AddRubric(TString name, Int_t maxNKeyWords)
102 /// Add a new rubric containing at maximum maxNKeyWords key words.
103 /// Key words will be added as the counters get filled until the maximum is reached.
104 /// If the key word "any" is never defined, the overall statistics is
105 /// assumed to be the sum of the statistics under each key word.
109 if (fRubrics->Contains(name.Data())) {
110 AliError(Form("rubric named %s already exist",name.Data()));
114 // create the empty rubric
115 TObjString* rubric = new TObjString(name.Data());
116 Int_t nRubrics = fRubrics->GetSize();
117 rubric->SetUniqueID(nRubrics);
118 fRubrics->AddLast(rubric);
120 // save the maximum number of autorized key words
121 if (nRubrics+1 > fRubricsSize->GetSize()) fRubricsSize->Set(2*fRubricsSize->GetSize());
122 (*fRubricsSize)[nRubrics] = maxNKeyWords;
125 //-----------------------------------------------------------------------
126 void AliCounterCollection::Init(Bool_t weightedCounters)
128 /// Initialize the internal counters from the added rubrics.
130 // create the counters
132 fWeightedCounters = weightedCounters;
133 if (fWeightedCounters)
134 fCounters = new THnSparseT<TArrayF>("hCounters", "hCounters", fRubrics->GetSize(), fRubricsSize->GetArray(), 0x0, 0x0);
136 fCounters = new THnSparseT<TArrayI>("hCounters", "hCounters", fRubrics->GetSize(), fRubricsSize->GetArray(), 0x0, 0x0);
139 TObject* rubric = 0x0;
140 TIter nextRubric(fRubrics);
141 while ((rubric = nextRubric())) {
142 TAxis* axis = fCounters->GetAxis((Int_t)rubric->GetUniqueID());
145 axis->SetName(rubric->GetName());
147 // set labels if already known
148 TObjArray* keyWords = dynamic_cast<TObjArray*>(rubric);
150 TObjString* label = 0x0;
152 TIter nextLabel(keyWords);
153 while ((label = static_cast<TObjString*>(nextLabel()))) axis->SetBinLabel(bin++, label->String().Data());
160 //-----------------------------------------------------------------------
161 Int_t AliCounterCollection::GetNActiveBins(Int_t dim)
163 /// return the number of labels in that rubric.
164 THashList* labels = fCounters->GetAxis(dim)->GetLabels();
165 return (labels) ? labels->GetSize() : 0;
168 //-----------------------------------------------------------------------
169 Bool_t AliCounterCollection::ContainsAny(Int_t dim)
171 /// return kTRUE if that rubric contains the keyWord "ANY".
172 THashList* labels = fCounters->GetAxis(dim)->GetLabels();
173 return (labels && labels->Contains("ANY"));
176 //-----------------------------------------------------------------------
177 const Int_t* AliCounterCollection::FindBins(const TString& externalKey, Bool_t allocate, Int_t& nEmptySlots)
179 /// Return the corresponding bins ordered by rubric or 0x0 if externalKey is not valid.
180 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
181 /// If allocate = kTRUE, new key words are added to the corresponding rubric if possible.
182 /// If a rubric is not filled in, the coresponding slot contain -1 in the array.
183 /// It is the responsability of the user to delete the returned array.
185 // produce an empty array of keys
186 Int_t nRubrics = fRubrics->GetSize();
187 Int_t* bins = new Int_t[nRubrics];
188 for (Int_t i=0; i<nRubrics; i++) bins[i] = -1;
189 nEmptySlots = nRubrics;
190 Bool_t isValid = kTRUE;
192 // get the list of rubric:keyWord pairs
193 TObjArray* rubricKeyPairs = externalKey.Tokenize("/");
195 // loop over each rubric:keyWord pair
196 TObjString* pair = 0x0;
197 TIter next(rubricKeyPairs);
198 while ((pair = static_cast<TObjString*>(next()))) {
200 // get both rubric and associated key word
201 TObjArray* rubricKeyPair = pair->String().Tokenize(":");
203 // check the format of the pair
204 if (rubricKeyPair->GetEntriesFast() != 2) {
205 AliError("invalid key format");
207 delete rubricKeyPair;
211 // get the axis corresponding to that rubric
212 Int_t dim = FindDim(static_cast<TObjString*>(rubricKeyPair->UncheckedAt(0))->String());
215 delete rubricKeyPair;
219 // find the bin corresponding to that key word
220 Int_t bin = FindBin(dim, static_cast<TObjString*>(rubricKeyPair->UncheckedAt(1))->String(), allocate);
223 delete rubricKeyPair;
227 // check if the array of keys already contains something for that rubric
228 if (bins[dim] >= 0) {
229 AliWarning("key already given for that rubric --> ignored");
230 delete rubricKeyPair;
234 // store the corresponding bin for that slot
239 delete rubricKeyPair;
242 // delete the array in case of problem
246 nEmptySlots = nRubrics;
250 delete rubricKeyPairs;
255 //-----------------------------------------------------------------------
256 Int_t AliCounterCollection::FindDim(const TString& rubricName) const
258 /// Return the dimension corresponding to that rubric (or -1 in case of failure).
259 TObject* rubric = fRubrics->FindObject(rubricName.Data());
261 AliError(Form("invalid rubric: %s",rubricName.Data()));
264 return (Int_t) rubric->GetUniqueID();
267 //-----------------------------------------------------------------------
268 Int_t AliCounterCollection::FindBin(Int_t dim, const TString& keyWord, Bool_t allocate)
270 /// Return the bin number corresponding to that key word (or -1 in case of failure).
271 /// If allocate = kTRUE, try to add the key word if possible.
273 TAxis* axis = fCounters->GetAxis(dim);
275 // look for the bin corresponding to keyWord
276 THashList* labels = axis->GetLabels();
277 TObjString* label = (labels) ? static_cast<TObjString*>(labels->FindObject(keyWord.Data())) : 0x0;
278 Int_t bin = (label) ? (Int_t)label->GetUniqueID() : -1;
280 // in case the keyWord does not exist, try to add it if required
281 if (bin<0 && allocate) {
282 Int_t nLabels = (labels) ? labels->GetSize() : 0;
283 if (nLabels < axis->GetNbins()) {
285 axis->SetBinLabel(bin, keyWord.Data());
289 if (bin<0) AliError(Form("invalid key word: %s:%s",axis->GetName(),keyWord.Data()));
294 //-----------------------------------------------------------------------
295 Short_t** AliCounterCollection::DecodeSelection(const TString& selections, const TObjArray& displayedRubrics)
297 /// Tag the selected keywords in each rubric (-1=subtract; 0=discard; 1=add). Format:
298 /// "rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.." (order does not matter).
299 /// It is the responsability of the user to delete the returned array.
301 // produce an empty array of selected keys
302 Int_t nRubrics = fCounters->GetNdimensions();
303 Short_t** selects = new Short_t*[nRubrics];
304 for (Int_t i=0; i<nRubrics; i++) selects[i] = 0x0;
306 // get the list of rubric:LisOfKeyWord pairs
307 TObjArray* rubricKeyPairs = selections.Tokenize("/");
309 // loop over each rubric:keyWord pair
310 TObjString* pair = 0x0;
311 TIter next(rubricKeyPairs);
312 while ((pair = static_cast<TObjString*>(next()))) {
314 // get both rubric and associated list of key words
315 TObjArray* rubricKeyPair = pair->String().Tokenize(":");
317 // check the format of the pair
318 if (rubricKeyPair->GetEntriesFast() != 2) {
319 AliError("invalid key format");
320 delete rubricKeyPair;
321 delete rubricKeyPairs;
322 for (Int_t i=0; i<nRubrics; i++) if (selects[i]) delete[] selects[i];
327 // check wether to select or to discard the keyWords
328 Int_t include = kTRUE;
329 TString ListOfKeyWords(static_cast<TObjString*>(rubricKeyPair->UncheckedAt(1))->String());
330 if (ListOfKeyWords.BeginsWith("ANY-")) {
331 ListOfKeyWords.Remove(0,4);
335 // select the key words
336 const TString& rubric = static_cast<TObjString*>(rubricKeyPair->UncheckedAt(0))->String();
337 if (!Select(include, rubric, ListOfKeyWords, displayedRubrics.Contains(rubric.Data()), selects)) {
338 delete rubricKeyPair;
339 delete rubricKeyPairs;
340 for (Int_t i=0; i<nRubrics; i++) if (selects[i]) delete[] selects[i];
346 delete rubricKeyPair;
350 delete rubricKeyPairs;
352 // complete the selection of other rubrics
353 for (Int_t i=0; i<nRubrics; i++) {
355 // skip already processed rubrics
356 if (selects[i]) continue;
358 // create the list of bins
359 Int_t nBins = GetNActiveBins(i) + 1;
360 selects[i] = new Short_t[nBins];
362 // select all key words or only the key work "ANY"
363 if (ContainsAny(i) && !displayedRubrics.Contains(fCounters->GetAxis(i)->GetName())) {
364 memset(selects[i], 0, sizeof(Short_t) * nBins);
365 selects[i][FindBin(i, "ANY", kFALSE)] = 1;
366 } else for (Int_t j=0; j<nBins; j++) selects[i][j] = 1;
373 //-----------------------------------------------------------------------
374 Bool_t AliCounterCollection::Select(Bool_t include, const TString& rubric, const TString& keywords,
375 Bool_t displayed, Short_t* selectBins[])
377 /// Tag the selected keywords (separated by ',') in that rubric (-1=subtract; 0=discard; 1=add).
379 Int_t dim = FindDim(rubric);
380 if (dim < 0) return kFALSE;
382 if (selectBins[dim]) {
383 AliWarning(Form("selection already made for rubric %s --> ignored",rubric.Data()));
387 // get list of key words to select
388 TObjArray* keys = keywords.Tokenize(",");
389 if (keys->GetEntriesFast() == 0) {
390 AliError(Form("no key word specified for rubric %s",rubric.Data()));
395 // create the list of bins
396 Int_t nBins = GetNActiveBins(dim) + 1;
397 selectBins[dim] = new Short_t[nBins];
399 // select/unselect all bins
400 Bool_t containsAny = ContainsAny(dim);
401 if (include || (containsAny && !displayed)) {
402 memset(selectBins[dim], 0, sizeof(Short_t) * nBins);
403 if (!include) selectBins[dim][FindBin(dim, "ANY", kFALSE)] = 1;
404 } else for (Int_t j=0; j<nBins; j++) selectBins[dim][j] = 1;
406 // select/unselect specific key words
407 TObjString* key = 0x0;
409 while ((key = static_cast<TObjString*>(nextKey()))) {
411 // special case of key word "ANY"
412 if (key->String() == "ANY") {
416 Int_t binAny = FindBin(dim, "ANY", kFALSE);
417 if (include) selectBins[dim][binAny] = 1;
418 else selectBins[dim][binAny] = 0;
422 if (include) for (Int_t j=0; j<nBins; j++) selectBins[dim][j] = 1;
423 else memset(selectBins[dim], 0, sizeof(Short_t) * nBins);
427 } else { // other cases
429 // find the corresponding bin
430 Int_t bin = FindBin(dim, key->String().Data(), kFALSE);
436 // select/unselect it
437 if (include) selectBins[dim][bin] = 1;
438 else if (containsAny && !displayed) selectBins[dim][bin] = -1;
439 else selectBins[dim][bin] = 0;
451 //-----------------------------------------------------------------------
452 void AliCounterCollection::CleanListOfStrings(TObjArray* list)
454 /// Make sure all strings appear only once in this list
456 // remove multiple-occurrence
457 Int_t nEntries = list->GetEntriesFast();
458 for (Int_t i = 0; i < nEntries; i++) {
459 TObjString* entry1 = static_cast<TObjString*>(list->UncheckedAt(i));
460 if (!entry1) continue;
461 for (Int_t j = i+1; j < nEntries; j++) {
462 TObjString* entry2 = static_cast<TObjString*>(list->UncheckedAt(j));
463 if (entry2 && entry2->IsEqual(entry1)) {
464 AliWarning(Form("multiple-occurence of string \"%s\" --> removed",entry2->String().Data()));
470 // remove empty slots
474 //-----------------------------------------------------------------------
475 void AliCounterCollection::Count(TString externalKey, Int_t value)
477 /// Add "value" to the counter referenced by "externalKey".
478 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
479 if (value > 0) CountAsDouble(externalKey, (Double_t)value);
480 else if (value < 0) AliError("cannot count negative values");
483 //-----------------------------------------------------------------------
484 void AliCounterCollection::Count(TString externalKey, Double_t value)
486 /// Add "value" to the counter referenced by "externalKey".
487 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
488 if (fWeightedCounters) CountAsDouble(externalKey, value);
489 else AliError("non-weighted counters can only be filled with intergers");
492 //-----------------------------------------------------------------------
493 void AliCounterCollection::CountAsDouble(TString externalKey, Double_t value)
495 /// Add "value" to the counter referenced by "externalKey".
496 /// The externalKey format must be rubric:keyWord/rubric:keyWord/rubric:keyWord/...
499 AliError("counters are not initialized");
503 externalKey.ToUpper();
505 // convert external to internal key
506 Int_t nEmptySlots = 0;
507 const Int_t* bins = FindBins(externalKey, kTRUE, nEmptySlots);
510 // check for empty slots
511 if (nEmptySlots > 0) {
512 AliError("incomplete key");
517 // increment the corresponding counter
518 fCounters->AddBinContent(bins, value);
524 //-----------------------------------------------------------------------
525 void AliCounterCollection::Print(const Option_t* opt) const
527 /// Print every individual counters if opt=="" or call "Print(opt, "")".
529 if (strcmp(opt,"")) {
530 const_cast<AliCounterCollection*>(this)->Print(opt, "");
535 AliError("counters are not initialized");
539 if (fCounters->GetNbins() == 0) {
540 printf("\nall counters are empty\n\n");
544 Int_t nRubrics = fCounters->GetNdimensions();
545 Int_t* bins = new Int_t[nRubrics];
547 // loop over every filled counters
548 for (Long64_t i=0; i<fCounters->GetNbins(); ++i) {
550 // get the content of the bin
551 Double_t value = fCounters->GetBinContent(i, bins);
553 // build the corresponding counter name
555 for (Int_t j=0; j<nRubrics; j++) counter += Form("/%s",fCounters->GetAxis(j)->GetBinLabel(bins[j]));
559 if (fWeightedCounters) printf("\n%s %g", counter.Data(), value);
560 else printf("\n%s %d", counter.Data(), (Int_t)value);
568 //-----------------------------------------------------------------------
569 TString AliCounterCollection::GetKeyWords(TString rubric) const
571 /// return the list of key words for the given rubric.
573 TString keyWords = "";
576 AliError("counters are not initialized");
582 // get the dimension corresponding to that rubric
583 Int_t dim = FindDim(rubric);
584 if (dim < 0) return keyWords;
586 // build list of key words
587 TObjString* label = 0x0;
588 TIter nextLabel(fCounters->GetAxis(dim)->GetLabels());
589 while ((label = static_cast<TObjString*>(nextLabel()))) keyWords += Form("%s,",label->String().Data());
590 keyWords.Remove(TString::kTrailing, ',');
595 //-----------------------------------------------------------------------
596 Double_t AliCounterCollection::GetSum(TString selections)
598 /// Get the overall statistics for the given selection (result is integrated over not specified rubrics):
599 /// - format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
602 AliError("counters are not initialized");
606 selections.ToUpper();
608 // decode the selections
609 Short_t** select = DecodeSelection(selections, TObjArray());
610 if (!select) return 0.;
612 // loop over every filled counters and compute integral
614 Int_t nDims = fCounters->GetNdimensions();
615 Int_t* coord = new Int_t[nDims];
616 for (Long64_t i=0; i<fCounters->GetNbins(); ++i) {
618 // get the content of the counter
619 Double_t value = fCounters->GetBinContent(i, coord);
621 // discard not selected counters and compute the selection factor
622 Int_t selectionFactor = 1;
623 for (Int_t dim = 0; dim < nDims && selectionFactor != 0; dim++) selectionFactor *= select[dim][coord[dim]];
624 if (selectionFactor == 0) continue;
627 sum += selectionFactor * value;
631 for (Int_t iDim=0; iDim<nDims; iDim++) delete[] select[iDim];
638 //-----------------------------------------------------------------------
639 void AliCounterCollection::PrintKeyWords() const
641 /// Print the full list of key words.
644 AliError("counters are not initialized");
649 Int_t nRubrics = fCounters->GetNdimensions();
650 for (Int_t iDim=0; iDim<nRubrics; iDim++) {
651 TAxis* axis = fCounters->GetAxis(iDim);
653 // print rubric's name
654 printf("\n%s:", axis->GetName());
656 // loop over key words
657 Bool_t first = kTRUE;
658 TObjString* label = 0x0;
659 TIter nextLabel(axis->GetLabels());
660 while ((label = static_cast<TObjString*>(nextLabel()))) {
662 //print key word's name
664 printf("%s", label->String().Data());
666 } else printf(",%s", label->String().Data());
673 //-----------------------------------------------------------------------
674 void AliCounterCollection::PrintValue(TString selections)
676 /// Print value of selected counter.
677 /// format of "selections" is rubric:keyWord/rubric:keyWord/rubric:keyWord/...
680 AliError("counters are not initialized");
684 selections.ToUpper();
686 // convert external to internal key
687 Int_t nEmptySlots = 0;
688 const Int_t* selectedBins = FindBins(selections, kFALSE, nEmptySlots);
689 if (!selectedBins) return;
691 // check for empty slots
692 if (nEmptySlots > 0) {
693 AliError("incomplete key");
694 delete[] selectedBins;
699 if (fWeightedCounters) printf("\n%g\n\n", fCounters->GetBinContent(selectedBins));
700 else printf("\n%d\n\n", (Int_t) fCounters->GetBinContent(selectedBins));
703 delete[] selectedBins;
706 //-----------------------------------------------------------------------
707 void AliCounterCollection::Print(TString rubrics, TString selections, Bool_t removeEmpty)
709 /// Print desired rubrics for the given selection:
710 /// - format of "rubrics" is rubric1/rubric2/.. (order matters only for output).
711 /// - format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
712 /// If "data" contains 1 rubric, the output will be one counter for each element of that rubric.
713 /// If "data" contains 2 rubrics, the output will be an array of counters, rubric1 vs rubric2.
714 /// If "data" contains 3 rubrics, the output will be an array rubric1 vs rubric2 for each element in rubric3.
716 /// Results are integrated over rubrics not specified neither in "rubrics" nor in "selections".
719 AliError("counters are not initialized");
724 selections.ToUpper();
726 // get the rubrics to print
727 TObjArray* rubricsToPrint = rubrics.Tokenize("/");
728 if (rubricsToPrint->GetEntriesFast() == 0) {
729 delete rubricsToPrint;
733 // remove rubrics called twice
734 CleanListOfStrings(rubricsToPrint);
736 // project counters in the rubrics to print according to the selections
737 TObject* hist = Projection(*rubricsToPrint, selections);
739 delete rubricsToPrint;
744 Int_t nRubricsToPrint = rubricsToPrint->GetEntriesFast();
745 if (nRubricsToPrint == 1 && (static_cast<TH1D*>(hist))->GetEntries() > 0)
746 PrintList(static_cast<TH1D*>(hist), removeEmpty);
747 else if (nRubricsToPrint == 2 && (static_cast<TH2D*>(hist))->GetEntries() > 0)
748 PrintArray(static_cast<TH2D*>(hist), removeEmpty);
749 else if (nRubricsToPrint > 2 && (static_cast<THnSparse*>(hist))->GetEntries() > 0)
750 PrintListOfArrays(static_cast<THnSparse*>(hist), removeEmpty);
752 printf("\nselected counters are empty\n\n");
755 delete rubricsToPrint;
759 //-----------------------------------------------------------------------
760 void AliCounterCollection::PrintSum(TString selections)
762 /// Print the overall statistics for the given selection (result is integrated over not specified rubrics):
763 /// - format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
764 Double_t sum = GetSum(selections);
765 if (fWeightedCounters) printf("\n%g\n\n", sum);
766 else printf("\n%d\n\n", (Int_t)sum);
769 //-----------------------------------------------------------------------
770 void AliCounterCollection::PrintList(const TH1D* hist, Bool_t removeEmpty) const
772 /// Print the content of 1D histogram as a list.
774 // set the format to print labels
775 THashList* labels = hist->GetXaxis()->GetLabels();
777 if (fWeightedCounters) format = Form("\n%%%ds %%9g",GetMaxLabelSize(labels));
778 else format = Form("\n%%%ds %%9d",GetMaxLabelSize(labels));
780 // print value for each label
781 TObjString* label = 0x0;
782 TIter nextLabel(labels);
783 while ((label = static_cast<TObjString*>(nextLabel()))) {
784 Int_t bin = (Int_t) label->GetUniqueID();
785 Double_t value = hist->GetBinContent(bin);
786 if (removeEmpty && value == 0.) continue;
787 if (fWeightedCounters) printf(format.Data(), label->String().Data(), value);
788 else printf(format.Data(), label->String().Data(), (Int_t) value);
793 //-----------------------------------------------------------------------
794 void AliCounterCollection::PrintArray(const TH2D* hist, Bool_t removeEmpty) const
796 /// Print the content of 2D histogram as an array.
798 // set the format to print labels in X direction
799 THashList* labelsX = hist->GetXaxis()->GetLabels();
800 TString formatX(Form("\n%%%ds ",GetMaxLabelSize(labelsX)));
802 // set the format to print labels in Y direction and values
803 THashList* labelsY = hist->GetYaxis()->GetLabels();
804 Int_t maxLabelSizeY = TMath::Max(9, GetMaxLabelSize(labelsY));
805 TString formatYs(Form("%%%ds ",maxLabelSizeY));
806 TString formatYd(Form("%%%dd ",maxLabelSizeY));
807 TString formatYg(Form("%%%dg ",maxLabelSizeY));
809 // if required, set the list of labels for which all counters are not empty
810 Bool_t *useLabelX = 0x0, *useLabelY = 0x0;
811 TObjString *labelX = 0x0, *labelY = 0x0;
812 TIter nextLabelX(labelsX);
813 TIter nextLabelY(labelsY);
816 // create label flags and set them as unused
817 useLabelX = new Bool_t[labelsX->GetSize()+1];
818 memset(useLabelX, kFALSE, sizeof(Bool_t) * (labelsX->GetSize()+1));
819 useLabelY = new Bool_t[labelsY->GetSize()+1];
820 memset(useLabelY, kFALSE, sizeof(Bool_t) * (labelsY->GetSize()+1));
822 // loop over labels in X direction
823 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
824 Int_t binX = (Int_t) labelX->GetUniqueID();
826 // loop over labels in Y direction
828 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
829 Int_t binY = (Int_t) labelY->GetUniqueID();
831 // both labels already set as used
832 if (useLabelX[binX] && useLabelY[binY]) continue;
835 if (hist->GetBinContent(binX, binY) == 0.) continue;
838 useLabelX[binX] = kTRUE;
839 useLabelY[binY] = kTRUE;
847 // print labels in Y axis
848 printf(formatX.Data()," ");
850 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
851 if (removeEmpty && !useLabelY[labelY->GetUniqueID()]) continue;
852 printf(formatYs.Data(), labelY->String().Data());
855 // fill array for each label in X axis
857 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
858 Int_t binX = (Int_t) labelX->GetUniqueID();
860 if (removeEmpty && !useLabelX[binX]) continue;
863 printf(formatX.Data(), labelX->String().Data());
865 // print value for each label in Y axis
867 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
868 Int_t binY = (Int_t) labelY->GetUniqueID();
869 if (removeEmpty && !useLabelY[binY]) continue;
870 if (fWeightedCounters) printf(formatYg.Data(), hist->GetBinContent(binX, binY));
871 else printf(formatYd.Data(), (Int_t) hist->GetBinContent(binX, binY));
883 //-----------------------------------------------------------------------
884 void AliCounterCollection::PrintListOfArrays(const THnSparse* hist, Bool_t removeEmpty) const
886 /// Print the content of nD histogram as a list of arrays.
888 // set the format to print labels in X direction
889 THashList* labelsX = hist->GetAxis(0)->GetLabels();
890 TString formatX(Form("\n%%%ds ",GetMaxLabelSize(labelsX)));
892 // set the format to print labels in Y direction and values
893 THashList* labelsY = hist->GetAxis(1)->GetLabels();
894 Int_t maxLabelSizeY = TMath::Max(9, GetMaxLabelSize(labelsY));
895 TString formatYs(Form("%%%ds ",maxLabelSizeY));
896 TString formatYd(Form("%%%dd ",maxLabelSizeY));
897 TString formatYg(Form("%%%dg ",maxLabelSizeY));
899 // create a list containing each combination of labels refering the arrays to be printout
901 listOfCombis.SetOwner();
903 // add a first empty combination
904 Int_t nDim = hist->GetNdimensions();
905 listOfCombis.AddLast(new TObjArray(nDim-2));
907 // loop over the nDim-2 other rubrics
908 for (Int_t i=2; i<nDim; i++) {
910 // save the last label of that rubic
911 THashList* labels = hist->GetAxis(i)->GetLabels();
912 TObjString* lastLabel = (labels) ? static_cast<TObjString*>(labels->Last()) : 0x0;
913 if (!lastLabel) return;
915 // prepare iteration over the list of labels
916 TIter nextLabel(labels);
918 // loop over existing combinations
919 TObjLink* lnk = listOfCombis.FirstLink();
922 // get the current combination
923 TObjArray* currentCombi = static_cast<TObjArray*>(lnk->GetObject());
925 // loop over labels in the current rubric
927 TObjString* label = 0x0;
928 while ((label = static_cast<TObjString*>(nextLabel()))) {
930 // stop at the last one
931 if (label == lastLabel) break;
933 // copy the current combination, add the current label to it and add it to the list of combinations
934 TObjArray* combi = new TObjArray(*currentCombi);
935 combi->AddLast(label);
936 listOfCombis.AddBefore(lnk, combi);
939 // add the last label to the current combination
940 currentCombi->AddLast(lastLabel);
947 // create bin coordinates to access individual counters
948 Int_t* bins = new Int_t[nDim];
950 // create label flags
951 Bool_t *useLabelX = 0x0, *useLabelY = 0x0;
953 useLabelX = new Bool_t[labelsX->GetSize()+1];
954 useLabelY = new Bool_t[labelsY->GetSize()+1];
957 // loop over each combination of labels
958 TObjArray* combi = 0x0;
959 TIter nextCombi(&listOfCombis);
960 while ((combi = static_cast<TObjArray*>(nextCombi()))) {
962 // make the name of the combination and fill the corresponding bin coordinates
963 TString combiName = "/";
964 for (Int_t i=2; i<nDim; i++) {
965 TObjString* label = static_cast<TObjString*>(combi->UncheckedAt(i-2));
966 combiName += Form("%s/",label->String().Data());
967 bins[i] = (Int_t)label->GetUniqueID();
970 // reset the list of labels for which all counters are not empty
972 memset(useLabelX, kFALSE, sizeof(Bool_t) * (labelsX->GetSize()+1));
973 memset(useLabelY, kFALSE, sizeof(Bool_t) * (labelsY->GetSize()+1));
976 Bool_t empty = kTRUE;
977 TObjString* labelX = 0x0;
978 TObjString* labelY = 0x0;
979 TIter nextLabelX(labelsX);
980 TIter nextLabelY(labelsY);
981 // loop over labels in X direction
982 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
983 bins[0] = (Int_t) labelX->GetUniqueID();
985 // loop over labels in Y direction
987 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
988 bins[1] = (Int_t) labelY->GetUniqueID();
990 // both labels already set as used
991 if (removeEmpty && useLabelX[bins[0]] && useLabelY[bins[1]]) continue;
994 if (hist->GetBinContent(bins) == 0.) continue;
996 // set label as used and array as not empty
999 useLabelX[bins[0]] = kTRUE;
1000 useLabelY[bins[1]] = kTRUE;
1005 if (!removeEmpty && !empty) break;
1009 // skip empty arrays
1010 if (empty) continue;
1012 // print the name of the combination of labels refering the incoming array
1013 printf("\n%s:\n",combiName.Data());
1015 // print labels in Y axis
1016 printf(formatX.Data()," ");
1018 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
1019 if (removeEmpty && !useLabelY[labelY->GetUniqueID()]) continue;
1020 printf(formatYs.Data(), labelY->String().Data());
1023 // fill array for each label in X axis
1025 while ((labelX = static_cast<TObjString*>(nextLabelX()))) {
1026 bins[0] = (Int_t) labelX->GetUniqueID();
1028 if (removeEmpty && !useLabelX[bins[0]]) continue;
1031 printf(formatX.Data(), labelX->String().Data());
1033 // print value for each label in Y axis
1035 while ((labelY = static_cast<TObjString*>(nextLabelY()))) {
1036 bins[1] = (Int_t) labelY->GetUniqueID();
1037 if (removeEmpty && !useLabelY[bins[1]]) continue;
1038 if (fWeightedCounters) printf(formatYg.Data(), hist->GetBinContent(bins));
1039 else printf(formatYd.Data(), (Int_t) hist->GetBinContent(bins));
1053 //-----------------------------------------------------------------------
1054 Int_t AliCounterCollection::GetMaxLabelSize(THashList* labels) const
1056 /// Return the number of characters of the longest label.
1057 Int_t maxLabelSize = 0;
1058 TObjString* label = 0x0;
1059 TIter nextLabel(labels);
1060 while ((label = static_cast<TObjString*>(nextLabel())))
1061 maxLabelSize = TMath::Max(maxLabelSize, label->String().Length());
1062 return maxLabelSize;
1065 //-----------------------------------------------------------------------
1066 TH1D* AliCounterCollection::Get(TString rubric, TString selections)
1068 /// Get counters of the rubric "rubric" for the given "selection".
1069 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1070 /// Results are integrated over rubrics not specified neither in "rubric1" nor in "selections".
1071 /// It is the responsability of the user to delete the returned histogram.
1074 AliError("counters are not initialized");
1079 selections.ToUpper();
1081 // fill the rubrics to print
1082 TObjArray rubricsToPrint(1);
1083 rubricsToPrint.SetOwner();
1084 rubricsToPrint.AddLast(new TObjString(rubric.Data()));
1086 // project counters in the rubrics to print according to the selections
1087 TH1D* hist = static_cast<TH1D*>(Projection(rubricsToPrint, selections));
1089 // make it ready to display
1092 // remove statistic box
1093 hist->SetStats(kFALSE);
1096 TAxis* axis = hist->GetXaxis();
1097 THashList* labels = axis->GetLabels();
1098 Int_t nLabels = (labels) ? labels->GetSize() : 1;
1099 axis->SetRange(1,nLabels);
1100 axis->SetNdivisions(1,kFALSE);
1101 axis->SetTitle(rubric.Data());
1104 hist->GetYaxis()->SetTitle("Counts");
1110 //-----------------------------------------------------------------------
1111 TH2D* AliCounterCollection::Get(TString rubric1, TString rubric2, TString selections)
1113 /// Get counters of the "rubric1" vs "rubric2" for the given "selection".
1114 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1115 /// Results are integrated over rubrics not specified neither in "rubric1", "rubric2" nor in "selections".
1116 /// It is the responsability of the user to delete the returned histogram.
1119 AliError("counters are not initialized");
1125 selections.ToUpper();
1127 // fill the rubrics to print
1128 TObjArray rubricsToPrint(2);
1129 rubricsToPrint.SetOwner();
1130 rubricsToPrint.AddLast(new TObjString(rubric2.Data()));
1131 rubricsToPrint.AddLast(new TObjString(rubric1.Data()));
1133 // project counters in the rubrics to print according to the selections
1134 TH2D* hist = static_cast<TH2D*>(Projection(rubricsToPrint, selections));
1139 // remove statistic box
1140 hist->SetStats(kFALSE);
1143 TAxis* axisX = hist->GetXaxis();
1144 THashList* labelsX = axisX->GetLabels();
1145 Int_t nLabelsX = (labelsX) ? labelsX->GetSize() : 1;
1146 axisX->SetRange(1,nLabelsX);
1147 axisX->SetNdivisions(1,kFALSE);
1148 axisX->SetTitle(rubric2.Data());
1151 TAxis* axisY = hist->GetYaxis();
1152 THashList* labelsY = axisY->GetLabels();
1153 Int_t nLabelsY = (labelsY) ? labelsY->GetSize() : 1;
1154 axisY->SetRange(1,nLabelsY);
1155 axisY->SetNdivisions(1,kFALSE);
1156 axisY->SetTitle(rubric1.Data());
1162 //-----------------------------------------------------------------------
1163 TH1D* AliCounterCollection::Draw(TString rubric, TString selections)
1165 /// Draw counters of the rubric "rubric" for the given "selection".
1166 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1167 /// Results are integrated over rubrics not specified neither in "rubric1" nor in "selections".
1168 /// It is the responsability of the user to delete the returned histogram.
1169 TH1D* hist = Get(rubric, selections);
1170 if (hist) hist->Draw("htext");
1174 //-----------------------------------------------------------------------
1175 TH2D* AliCounterCollection::Draw(TString rubric1, TString rubric2, TString selections)
1177 /// Draw counters of the "rubric1" vs "rubric2" for the given "selection".
1178 /// Format of "selections" is rubric:[any-]keyWord,keyWord,../rubric:[any-]keyWord,.. (order does not matter).
1179 /// Results are integrated over rubrics not specified neither in "rubric1", "rubric2" nor in "selections".
1180 /// It is the responsability of the user to delete the returned histogram.
1181 TH2D* hist = Get(rubric1, rubric2, selections);
1182 if (hist) hist->Draw("text");
1186 //-----------------------------------------------------------------------
1187 TObject* AliCounterCollection::Projection(const TObjArray& data, const TString& selections)
1189 /// Return desired "data" for the given "selection" stored in a new histogram or 0x0 in case of failure.
1190 /// The type of the histogram (TH1D, TH2D or THnSparse) depend on the number of data.
1191 /// It is the responsability of the user to delete the returned histogram.
1193 // decode the selections
1194 Short_t** select = DecodeSelection(selections, data);
1195 if (!select) return 0x0;
1197 // define name and dimensions of projection histo
1198 TString name(fCounters->GetName());
1199 Int_t nDims = fCounters->GetNdimensions();
1200 Int_t nTargetDims = data.GetEntriesFast();
1201 TArrayI targetDims(nTargetDims);
1202 TArrayI nNewBins(nTargetDims);
1203 TArrayI* OldToNewCoord = new TArrayI[nTargetDims];
1204 for (Int_t i=0; i<nTargetDims; i++) {
1207 name += Form("_%s",static_cast<TObjString*>(data.UncheckedAt(i))->String().Data());
1210 targetDims[i] = FindDim(static_cast<TObjString*>(data.UncheckedAt(i))->String());
1212 // set number of selected bins in the target dims and make the correspondence between old and new coordinates
1214 if (targetDims[i] > -1) {
1215 Int_t nBins = GetNActiveBins(targetDims[i]) + 1;
1216 OldToNewCoord[i].Set(nBins);
1217 for (Int_t j=1; j<nBins; j++) if (select[targetDims[i]][j] > 0) OldToNewCoord[i][j] = ++nNewBins[i];
1220 // clean memory and return 0x0 in case of problem
1221 if (nNewBins[i] == 0) {
1222 for (Int_t iDim=0; iDim<nDims; iDim++) delete[] select[iDim];
1224 delete[] OldToNewCoord;
1230 // define title of projection histo
1231 TString title = "Selections: ";
1232 TString selectionString(selections);
1233 selectionString.Remove(TString::kBoth, '/');
1234 if (selectionString.Length() > 0) title += Form("%s/", selectionString.Data());
1236 TIter nextRubric(fRubrics);
1237 while ((rub = nextRubric())) {
1238 if (selectionString.Contains(Form("%s:",rub->GetName()))) continue;
1239 if (data.Contains(rub->GetName())) continue;
1240 title += Form("%s:ANY/", rub->GetName());
1242 title.ReplaceAll("/", " ");
1244 // Create new histograms
1246 if (nTargetDims == 1) hist = new TH1D(name.Data(), title.Data(), nNewBins[0], 0., 1.);
1247 else if (nTargetDims == 2) hist = new TH2D(name.Data(), title.Data(), nNewBins[0], 0., 1., nNewBins[1], 0., 1.);
1248 else if (fWeightedCounters) hist = new THnSparseT<TArrayF>(name.Data(), title.Data(), nTargetDims, nNewBins.GetArray(), 0x0, 0x0);
1249 else hist = new THnSparseT<TArrayI>(name.Data(), title.Data(), nTargetDims, nNewBins.GetArray(), 0x0, 0x0);
1251 // Set new axis labels
1253 if (nTargetDims < 3) {
1256 TIter nextLabelX(fCounters->GetAxis(targetDims[0])->GetLabels());
1257 while ((label = static_cast<TObjString*>(nextLabelX()))) {
1258 if (select[targetDims[0]][label->GetUniqueID()] > 0) {
1259 static_cast<TH1*>(hist)->GetXaxis()->SetBinLabel(OldToNewCoord[0][label->GetUniqueID()], label->String().Data());
1264 if (nTargetDims == 2) {
1265 TIter nextLabelY(fCounters->GetAxis(targetDims[1])->GetLabels());
1266 while ((label = static_cast<TObjString*>(nextLabelY()))) {
1267 if (select[targetDims[1]][label->GetUniqueID()] > 0) {
1268 static_cast<TH1*>(hist)->GetYaxis()->SetBinLabel(OldToNewCoord[1][label->GetUniqueID()], label->String().Data());
1276 for (Int_t i=0; i<nTargetDims; i++) {
1277 TIter nextLabel(fCounters->GetAxis(targetDims[i])->GetLabels());
1278 while ((label = static_cast<TObjString*>(nextLabel()))) {
1279 if (select[targetDims[i]][label->GetUniqueID()] > 0) {
1280 static_cast<THnSparse*>(hist)->GetAxis(i)->SetBinLabel(OldToNewCoord[i][label->GetUniqueID()], label->String().Data());
1287 // loop over every filled counters
1288 Int_t* coord = new Int_t[nDims];
1289 Int_t* newCoord = new Int_t[nTargetDims];
1291 for (Long64_t i=0; i<fCounters->GetNbins(); ++i) {
1293 // get the content of the counter
1294 Double_t value = fCounters->GetBinContent(i, coord);
1296 // discard not selected counters and compute the selection factor
1297 Int_t selectionFactor = 1;
1298 for (Int_t dim = 0; dim < nDims && selectionFactor != 0; dim++) selectionFactor *= select[dim][coord[dim]];
1299 if (selectionFactor == 0) continue;
1301 // find new coordinates in the projection histo
1302 for (Int_t d = 0; d < nTargetDims; ++d) newCoord[d] = OldToNewCoord[d][coord[targetDims[d]]];
1304 // fill projection histo
1305 if (nTargetDims < 3) {
1306 Int_t linBin = (nTargetDims == 1) ? newCoord[0] : static_cast<TH1*>(hist)->GetBin(newCoord[0], newCoord[1]);
1307 static_cast<TH1*>(hist)->AddBinContent(linBin, selectionFactor*value);
1308 } else static_cast<THnSparse*>(hist)->AddBinContent(newCoord, selectionFactor*value);
1313 // update the number of entries
1314 if (nTargetDims < 3) static_cast<TH1*>(hist)->SetEntries(nEntries);
1315 else static_cast<THnSparse*>(hist)->SetEntries(nEntries);
1318 for (Int_t iDim=0; iDim<nDims; iDim++) delete[] select[iDim];
1322 delete[] OldToNewCoord;
1327 //-----------------------------------------------------------------------
1328 Int_t* AliCounterCollection::CheckConsistency(const AliCounterCollection* c)
1330 /// Consistency check of the two counter collections. To be consistent, both counters
1331 /// must have the same rubrics with the same list of authorized key words if any.
1332 /// Return the correspondence between the local rubric ordering and the one of the other counter,
1333 /// or 0x0 in case of problem. It is the responsability of the user to delete the returned array.
1335 if (!fCounters || !c->fCounters) {
1336 AliError("counters are not initialized");
1340 // check if both counters are weighted or not
1341 if (c->fWeightedCounters != fWeightedCounters) AliWarning("merging non-weighted with weigthed counters");
1343 // check if the number of rubrics is the same
1344 Int_t nRubrics = fRubrics->GetSize();
1345 if (c->fRubrics->GetSize() != nRubrics) {
1346 AliError("both counters do not contain the same number of rubrics");
1350 Int_t* otherDims = new Int_t[nRubrics];
1352 // loop over local rubrics
1353 TObject* rubric1 = 0x0;
1354 TIter nextRubric(fRubrics);
1355 while ((rubric1 = nextRubric())) {
1357 // find that rubric in the other counter
1358 TObject* rubric2 = c->fRubrics->FindObject(rubric1->GetName());
1360 AliError(Form("the other counter does not contain the rubric %s", rubric1->GetName()));
1365 // check the list of authorized key words if any
1366 TObjArray* keyWords1 = dynamic_cast<TObjArray*>(rubric1);
1367 TObjArray* keyWords2 = dynamic_cast<TObjArray*>(rubric2);
1368 if (keyWords1 && keyWords2) {
1370 // check if the number of key words is the same
1371 if (keyWords1->GetEntriesFast() != keyWords2->GetEntriesFast()) {
1372 AliError("that rubric does not contain the same number of authorized key words in both counters");
1377 // loop over local key words
1378 TObjString* keyWord = 0x0;
1379 TIter nextKeyWord(keyWords1);
1380 while ((keyWord = static_cast<TObjString*>(nextKeyWord()))) {
1382 // find that key word in the corresponding rubric of the other counter
1383 if (!keyWords2->FindObject(keyWord->String().Data())) {
1384 AliError(Form("rubric %s does not contain the key word %s in the other counter", rubric1->GetName(), keyWord->String().Data()));
1391 } else if (keyWords1 || keyWords2) {
1393 // that rubric has not been initialized the same way in both counter
1395 AliError(Form("rubric %s of the other counter does not contain a list of authorized key words while this does", rubric1->GetName()));
1397 AliError(Form("rubric %s of this counter does not contain a list of authorized key words while the other does", rubric1->GetName()));
1404 // save the correspondence of rubric IDs in both counters
1405 otherDims[rubric1->GetUniqueID()] = rubric2->GetUniqueID();
1412 //-----------------------------------------------------------------------
1413 void AliCounterCollection::Add(const AliCounterCollection* counter)
1415 /// Add the given AliCounterCollections to this. They must have the
1416 /// same rubrics with the same list of authorized key words if any.
1418 // check the consistency between the other counter and this and get the correspondences between rubric IDs.
1419 Int_t* otherDims = CheckConsistency(counter);
1420 if (!otherDims) return;
1422 Int_t nRubrics = fCounters->GetNdimensions();
1423 Int_t* thisBins = new Int_t[nRubrics];
1424 Int_t* otherBins = new Int_t[nRubrics];
1426 // loop over every filled bins inside the other counter
1427 for (Long64_t i = 0; i < counter->fCounters->GetNbins(); i++) {
1429 // get the content of the bin
1430 Double_t value = counter->fCounters->GetBinContent(i, otherBins);
1432 // convert "other" bin coordinates to "this" bin coordinates
1434 for (Int_t dim = 0; dim < nRubrics; dim++) {
1435 TString label = counter->fCounters->GetAxis(otherDims[dim])->GetBinLabel(otherBins[otherDims[dim]]);
1436 thisBins[dim] = FindBin(dim, label, kTRUE);
1437 if (thisBins[dim] < 0) {
1438 AliError("this counter is full, unable to add that key word");
1445 // increment the corresponding local counter
1446 fCounters->AddBinContent(thisBins, value);
1455 //-----------------------------------------------------------------------
1456 Long64_t AliCounterCollection::Merge(TCollection* list)
1458 /// Merge this with a list of AliCounterCollections. All AliCounterCollections provided
1459 /// must have the same rubrics with the same list of authorized key words if any.
1461 if (!list || !fCounters) return 0;
1462 if (list->IsEmpty()) return (Long64_t)fCounters->GetEntries();
1465 const TObject* obj = 0x0;
1466 while ((obj = next())) {
1468 // check that "obj" is an object of the class AliCounterCollection
1469 const AliCounterCollection* counter = dynamic_cast<const AliCounterCollection*>(obj);
1471 AliError(Form("object named %s is not AliCounterCollection! Skipping it.", counter->GetName()));
1475 // merge counter to this one
1480 return (Long64_t)fCounters->GetEntries();
1483 //-----------------------------------------------------------------------
1484 void AliCounterCollection::Sort(Option_t* opt, Bool_t asInt)
1486 /// Sort rubrics defined without a list of authorized key words or all rubrics if opt=="all".
1487 /// If asInt=kTRUE, key words are ordered as interger instead of alphabetically.
1490 AliError("counters are not initialized");
1494 Bool_t all = (!strcasecmp(opt, "all"));
1496 Bool_t somethingToSort = kFALSE;
1497 Int_t nRubrics = fRubrics->GetSize();
1498 Bool_t* rubricsToSort = new Bool_t[nRubrics];
1499 memset(rubricsToSort, kFALSE, sizeof(Bool_t) * nRubrics);
1501 // choose rubrics to sort
1502 TObject* rubric = 0x0;
1503 TIter nextRubric(fRubrics);
1504 while ((rubric = nextRubric())) {
1506 if (all || dynamic_cast<TObjString*>(rubric)) {
1508 // check if something to sort
1509 THashList* labels = fCounters->GetAxis((Int_t)rubric->GetUniqueID())->GetLabels();
1510 if (!labels || labels->GetSize() < 2) continue;
1512 // select that rubric
1513 rubricsToSort[(Int_t)rubric->GetUniqueID()] = kTRUE;
1514 somethingToSort = kTRUE;
1520 // sort selected rubrics if any
1521 if (somethingToSort) Sort(rubricsToSort, asInt);
1524 delete[] rubricsToSort;
1527 //-----------------------------------------------------------------------
1528 void AliCounterCollection::SortRubric(TString rubric, Bool_t asInt)
1530 /// Sort only that rubric. If asInt=kTRUE, key words are ordered as interger instead of alphabetically.
1533 AliError("counters are not initialized");
1539 // find the rubric to sort
1540 Int_t dim = FindDim(rubric);
1541 if (dim < 0) return;
1543 // check if something to sort
1544 THashList* labels = fCounters->GetAxis(dim)->GetLabels();
1545 if (!labels || labels->GetSize() < 2) return;
1547 // select that rubric
1548 Int_t nRubrics = fRubrics->GetSize();
1549 Bool_t* rubricsToSort = new Bool_t[nRubrics];
1550 memset(rubricsToSort, kFALSE, sizeof(Bool_t) * nRubrics);
1551 rubricsToSort[dim] = kTRUE;
1554 Sort(rubricsToSort, asInt);
1557 delete[] rubricsToSort;
1560 //-----------------------------------------------------------------------
1561 void AliCounterCollection::Sort(const Bool_t* rubricsToSort, Bool_t asInt)
1563 /// Sort labels (alphabetically or as integer) in each rubric flagged in "rubricsToSort".
1565 // create a new counter
1566 THnSparse* oldCounters = fCounters;
1567 Int_t nRubrics = fRubrics->GetSize();
1568 if (fWeightedCounters)
1569 fCounters = new THnSparseT<TArrayF>("hCounters", "hCounters", nRubrics, fRubricsSize->GetArray(), 0x0, 0x0);
1571 fCounters = new THnSparseT<TArrayI>("hCounters", "hCounters", nRubrics, fRubricsSize->GetArray(), 0x0, 0x0);
1572 Int_t** newBins = new Int_t*[nRubrics];
1574 // define the new axes
1575 for (Int_t i=0; i<nRubrics; i++) {
1576 TAxis* oldAxis = oldCounters->GetAxis(i);
1577 TAxis* newAxis = fCounters->GetAxis(i);
1579 // set the name of the new axis
1580 newAxis->SetName(oldAxis->GetName());
1583 THashList* oldLabels = oldAxis->GetLabels();
1584 if (!oldLabels) continue;
1586 // sort them if required
1587 if (rubricsToSort[i]) {
1588 if (asInt) { oldLabels = SortAsInt(oldLabels); }
1589 else { oldLabels->Sort(); }
1592 // set labels in the new axis and save the correspondence between new and old bins
1593 newBins[i] = new Int_t[oldLabels->GetSize()+1];
1594 TObjString* label = 0x0;
1596 TIter nextLabel(oldLabels);
1597 while ((label = static_cast<TObjString*>(nextLabel()))) {
1598 newAxis->SetBinLabel(bin, label->String().Data());
1599 newBins[i][(Int_t)label->GetUniqueID()] = bin;
1604 if (rubricsToSort[i] && asInt) delete oldLabels;
1607 // fill the new counters
1608 Int_t* oldCoor = new Int_t[nRubrics];
1609 Int_t* newCoor = new Int_t[nRubrics];
1610 for (Long64_t i = 0; i < oldCounters->GetNbins(); i++) {
1611 Double_t value = oldCounters->GetBinContent(i, oldCoor);
1612 for (Int_t dim = 0; dim < nRubrics; dim++) newCoor[dim] = newBins[dim][oldCoor[dim]];
1613 fCounters->AddBinContent(newCoor, value);
1617 for (Int_t i=0; i<nRubrics; i++) delete[] newBins[i];
1624 //-----------------------------------------------------------------------
1625 THashList* AliCounterCollection::SortAsInt(const THashList* labels)
1627 /// Return a list (not owner) of labels sorted assuming they are integers.
1628 /// It is the responsability of user to delete the returned list.
1630 THashList* sortedLabels = new THashList(labels->GetSize());
1631 TIter nextSortedLabel(sortedLabels);
1634 TObjString* label = 0x0;
1635 TIter nextLabel(labels);
1636 while ((label = static_cast<TObjString*>(nextLabel()))) {
1638 // find where to add it
1639 TObjString* sortedLabel = 0x0;
1640 nextSortedLabel.Reset();
1641 while ((sortedLabel = static_cast<TObjString*>(nextSortedLabel())) &&
1642 (sortedLabel->String().Atoi() <= label->String().Atoi())) {}
1645 if (sortedLabel) sortedLabels->AddBefore(sortedLabel, label);
1646 else sortedLabels->AddLast(label);
1649 return sortedLabels;