]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGCF/Correlations/JCORRAN/AliJHistManager.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGCF / Correlations / JCORRAN / AliJHistManager.cxx
CommitLineData
9dc4f671 1#include "AliJHistManager.h"
277b9152 2#include <TMath.h>
9dc4f671 3using namespace std;
4//////////////////////////////////////////////////////
5// AliJBin
6//////////////////////////////////////////////////////
7
8AliJNamed::AliJNamed(TString name, TString title, TString opt, int mode) :
9 fName(name),
10 fTitle(title),
11 fOption(opt),
12 fMode(mode)
13{
14 // constructor
15}
16
17AliJNamed::~AliJNamed(){
18 // virtual destructor for base class
19}
20
21TString AliJNamed::GetOption(TString key){
22 TPMERegexp a("&"+key+"=?([^&]*)","i");
23 int nMatch = a.Match(fOption);
24 if( nMatch < 2 ) return UndefinedOption();
25 return a[1];
26}
27void AliJNamed::SetOption( TString key, TString value){
28 TPMERegexp a("&"+key+"=?[^&]*","i");
29 int nMatch = a.Match(fOption);
30 TString newOpt = "&"+key +( value.Length()?"="+value:"");
31 if( value == UndefinedOption() ) newOpt = "";
32 if( nMatch < 1 ) fOption += newOpt;
33 else fOption.ReplaceAll( a[0], newOpt );
34}
35void AliJNamed::RemoveOption( TString key ){
36 SetOption( key, UndefinedOption() );
37}
38TString AliJNamed::UndefinedOption(){
39 //static TString undefinedOption = "Undefined";
40 //return undefinedOption;
41 return "Undefined";
42}
43
44//////////////////////////////////////////////////////
45// AliJBin
46//////////////////////////////////////////////////////
47
48//_____________________________________________________
49AliJBin::AliJBin():
50 AliJNamed("AliJBin","%.2f-%2.f", "&Mode=Range", kRange),
51 fBinD(0),
52 fBinStr(0),
53 fIsFixedBin(false),
54 fIndexName("H"),
55 fHMG(NULL)
56{;}
57//_____________________________________________________
58AliJBin::AliJBin(TString config, AliJHistManager * hmg):
59 AliJNamed("AliJBin","%.2f-%2.f", "&Mode=Range", kRange),
60 fBinD(0),
61 fBinStr(0),
62 fIsFixedBin(false),
63 fIndexName("H"),
64 fHMG(NULL)
65{
66 cout<< config<<endl;
67 std::vector<TString> t = Tokenize(config, " \t,");
68 TString type = t[0];
69 SetName( t[1] );
70 fIndexName = t[2] ;
71 SetTitle( t[3] );
72 fTitle.ReplaceAll("\"","" );
73 SetFullOption( t[4] );
74 fMode = GetMode( GetOption("mode") );
75 AddToManager( hmg );
76 TString s;
77 for( int i=5;i<int(t.size());i++ ) s+=" "+t[i];
78 SetBin(s);
79}
80
81//_____________________________________________________
82AliJBin::AliJBin(const AliJBin& obj) :
83 AliJNamed(obj.fName,obj.fTitle,obj.fOption,obj.fMode),
84 fBinD(obj.fBinD),
85 fBinStr(obj.fBinStr),
86 fIsFixedBin(obj.fIsFixedBin),
87 fIndexName(obj.fIndexName),
88 fHMG(obj.fHMG)
89{
90 // copy constructor TODO: proper handling of pointer data members
91}
92
93//_____________________________________________________
94AliJBin& AliJBin::operator=(const AliJBin& obj)
95{
96 // assignment operator
97 if(this != &obj){
98 // TODO: proper implementation
99 }
100 return *this;
101}
102
103//_____________________________________________________
104void AliJBin::FixBin(){
105 if(fIsFixedBin ) return;
106 fIsFixedBin = true;
107 if(!fHMG) AddToManager( AliJHistManager::CurrentManager());
108
109}
110//_____________________________________________________
111void AliJBin::AddToManager( AliJHistManager* hmg ){
112 hmg->Add(this);
113}
114//_____________________________________________________
115AliJBin & AliJBin::Set( TString name, TString iname, TString title, int mode){
116 SetNameTitle( name, title );
117 fIndexName = iname;
118 fMode = mode;
119 SetOption("mode",GetModeString(mode));
120 return *this;
121}
122//_____________________________________________________
123TString AliJBin::GetModeString(int i){
124 static TString mode[] = { "Single","Range","String" };
125 if( i<0 || i>2 ) return "";
126 return mode[i];
127}
128int AliJBin::GetMode( TString mode ){
129 for( int i=0;i<kNMode;i++ ) if( mode == GetModeString(i) ) return i;
130 return -1;
131}
132//_____________________________________________________
133AliJBin & AliJBin::SetBin( const int n, const float * v ){
134 for( int i=0;i<n;i++ ) AddBin( v[i] );FixBin();
135 return *this;
136}
137//_____________________________________________________
138AliJBin & AliJBin::SetBin( const int n, const double * v ){
139 for( int i=0;i<n;i++ ) AddBin( v[i] );FixBin();
140 return *this;
141}
142AliJBin & AliJBin::SetBin( TVector *v ){
143 for( int i=0;i<v->GetNrows();i++ ) AddBin( (v->GetMatrixArray())[i] );FixBin();
144 return *this;
145}
146//_____________________________________________________
147AliJBin& AliJBin::SetBin(const TString v){
148 std::vector<TString> ar = Tokenize( v, "\t ,");
149 for( UInt_t i=0; i<ar.size();i++ ) AddBin( ar[i] );FixBin();
150 return *this;
151}
152
153//_____________________________________________________
154AliJBin& AliJBin::SetBin(const int n){
155 for( UInt_t i=0; i<UInt_t(n);i++ ) AddBin( i );FixBin();
156 return *this;
157}
158//_____________________________________________________
159void AliJBin::AddBin( const TString& v ){
277b9152 160 if( fIsFixedBin ) { JERROR( "You can't Add Bini "+GetName()); }
9dc4f671 161 fBinStr.push_back( (v=="_")?"":v );
162 fBinD.push_back( v.Atof() );
163}
164//_____________________________________________________
165void AliJBin::AddBin( float v ){
277b9152 166 if( fIsFixedBin ) { JERROR( "You can't Add Bin "+GetName()); }
9dc4f671 167 fBinD.push_back( v );
168 fBinStr.push_back(Form("%f",v));
169}
170//_____________________________________________________
171TString AliJBin::BuildTitle( int i ){
172 if( i < 0 || i > Size() ) return "";
173 if( fMode == kSingle )
174 return TString(Form(fTitle.Data(), fBinD[i] ));
175 if( fMode == kRange )
176 return TString(Form(fTitle.Data(), fBinD[i], fBinD[i+1]));
177 if( fMode == kString )
178 return TString( Form(fTitle.Data(), fBinStr[i].Data()) );
179 JERROR( TString("Bad Mode of AliJBin type ") + char(fMode) + " in " + fName+ "!!!" );
180 return "";
181}
182//_____________________________________________________
183TString AliJBin::GetString(){
184 SetOption( "mode",GetModeString(fMode) );
185 return "AliJBin\t"+fName+"\t"+fIndexName+"\t\""+fTitle+"\""+"\t"+fOption+"\t"+Join(fBinStr," ");
186
187}
188//_____________________________________________________
189void AliJBin::Print(){
190 std::cout<<"*"+GetString()<<std::endl;
191}
192
277b9152 193int AliJBin::GetBin(double x){
194 int i = TMath::BinarySearch( fBinD.size(), &fBinD[0], x );
195 if( fMode == kRange && i+1 >= int(fBinD.size()) ) return -1;
196 return i;
197}
198
9dc4f671 199//////////////////////////////////////////////////////
200// AliJArrayBase
201//////////////////////////////////////////////////////
202
203//_____________________________________________________
204AliJArrayBase::AliJArrayBase():
205 AliJNamed("AliJArayBase","","&Dir=default&Lazy",0),
206 //AliJNamed("AliJArayBase","","&Dir=default&LessLazy",0),
207 fDim(0),
208 fIndex(0),
209 fArraySize(0),
210 fNGenerated(0),
211 fIsBinFixed(false),
212 fIsBinLocked(false),
213 fAlg(NULL)
214{
215 // constrctor
216}
217//_____________________________________________________
218AliJArrayBase::~AliJArrayBase(){
219 //destructor
220 if(fAlg) delete fAlg;
221}
222
223//_____________________________________________________
224AliJArrayBase::AliJArrayBase(const AliJArrayBase& obj) :
225 AliJNamed(obj.fName,obj.fTitle,obj.fOption,obj.fMode),
226 fDim(obj.fDim),
227 fIndex(obj.fIndex),
228 fArraySize(obj.fArraySize),
229 fNGenerated(obj.fNGenerated),
230 fIsBinFixed(obj.fIsBinFixed),
231 fIsBinLocked(obj.fIsBinLocked),
232 fAlg(obj.fAlg)
233{
234 // copy constructor TODO: proper handling of pointer data members
235}
236
237//_____________________________________________________
238AliJArrayBase& AliJArrayBase::operator=(const AliJArrayBase& obj)
239{
240 // assignment operator
241 if(this != &obj){
242 // TODO: proper implementation
243 }
244 return *this;
245}
246
247//_____________________________________________________
248void* AliJArrayBase::GetItem(){
249 void * item = fAlg->GetItem();
250 if( !item ){
251 BuildItem() ;
252 item = fAlg->GetItem();
253 }
254 return item;
255}
256//_____________________________________________________
257void* AliJArrayBase::GetSingleItem(){
258 if(fMode == kSingle )return GetItem();
259 JERROR("This is not single array");
260 return NULL;
261}
262//_____________________________________________________
263void AliJArrayBase::FixBin(){
264 if( Dimension() == 0 ){
265 AddDim(1);SetOption("Single");
266 fMode = kSingle;
267 if( HasOption("dir","default")) RemoveOption("dir");
268 }
269 ClearIndex();
270 fAlg = new AliJArrayAlgorithmSimple(this);
271 fArraySize = fAlg->BuildArray();
272}
273//_____________________________________________________
274int AliJArrayBase::Index(int d){
275 if( OutOfDim(d) ) JERROR("Wrong Dim");
276 return fIndex[d];
277}
278void AliJArrayBase::SetIndex(int i, int d ){
279 if( OutOfSize( i, d ) ) JERROR( "Wrong Index" );
280 fIndex[d] = i;
281}
282
283void AliJArrayBase::InitIterator(){ fAlg->InitIterator(); }
284bool AliJArrayBase::Next(void *& item){ return fAlg->Next(item); }
285
286
287//////////////////////////////////////////////////////
288// AliJArrayAlgorithm
289//////////////////////////////////////////////////////
290
291//_____________________________________________________
292AliJArrayAlgorithm::AliJArrayAlgorithm(AliJArrayBase * cmd):
293 fCMD(cmd)
294{
295 // constructor
296}
297//_____________________________________________________
298AliJArrayAlgorithm::~AliJArrayAlgorithm(){
299 // destructor
300}
301
302//_____________________________________________________
303AliJArrayAlgorithm::AliJArrayAlgorithm(const AliJArrayAlgorithm& obj) :
304 fCMD(obj.fCMD)
305{
306 // copy constructor TODO: proper handling of pointer data members
307}
308
309//_____________________________________________________
310AliJArrayAlgorithm& AliJArrayAlgorithm::operator=(const AliJArrayAlgorithm& obj)
311{
312 // assignment operator
313 if(this != &obj){
314 *fCMD = *(obj.fCMD);
315 }
316 return *this;
317}
318
319//////////////////////////////////////////////////////
320// AliJArrayAlgorithmSimple
321//////////////////////////////////////////////////////
322
323//_____________________________________________________
324AliJArrayAlgorithmSimple::AliJArrayAlgorithmSimple(AliJArrayBase * cmd):
325 AliJArrayAlgorithm(cmd),
326 fDimFactor(0),
327 fArray(NULL),
328 fPos(0)
329{
330 // constructor
331}
332//_____________________________________________________
333AliJArrayAlgorithmSimple::~AliJArrayAlgorithmSimple(){
334 // Dimension, GetEntries, SizeOf
335 if( fArray ) delete [] (void**)fArray;
336}
337
338//_____________________________________________________
339AliJArrayAlgorithmSimple::AliJArrayAlgorithmSimple(const AliJArrayAlgorithmSimple& obj) :
340 AliJArrayAlgorithm(obj.fCMD),
341 fDimFactor(obj.fDimFactor),
342 fArray(obj.fArray),
343 fPos(obj.fPos)
344{
345 // copy constructor TODO: proper handling of pointer data members
346}
347
348//_____________________________________________________
349AliJArrayAlgorithmSimple& AliJArrayAlgorithmSimple::operator=(const AliJArrayAlgorithmSimple& obj)
350{
351 // assignment operator TODO: proper implementation
352 if(this != &obj){
353 *fCMD = *(obj.fCMD);
354 }
355 return *this;
356}
357//_____________________________________________________
358int AliJArrayAlgorithmSimple::BuildArray(){
359 fDimFactor.resize( Dimension(), 1 );
360 for( int i=Dimension()-2; i>=0; i-- ){
361 fDimFactor[i] = fDimFactor[i+1] * SizeOf(i+1);
362 } // TODO split to BuildArray and lazyArray in GetItem
363 int arraySize = fDimFactor[0] * SizeOf(0);
364 fArray = new void*[arraySize];
365 for( int i=0;i<arraySize;i++ ) fArray[i] = NULL;
366 return arraySize;
367}
368//_____________________________________________________
369int AliJArrayAlgorithmSimple::GlobalIndex(){
370 int iG = 0;
371 for( int i=0;i<Dimension();i++ ) // Index is checked by fCMD
372 iG+= Index(i)*fDimFactor[i];
373 // TODO check iG
374 return iG;
375
376}
377void AliJArrayAlgorithmSimple::ReverseIndex( int iG ){
378 int n = iG;
379 for( int i=0;i<Dimension();i++ ){
380 int n1 = int(n/fDimFactor[i]);
381 fCMD->SetIndex( n1 , i );
382 n-=n1*fDimFactor[i];
383 }
384}
385void * AliJArrayAlgorithmSimple::GetItem(){
386 return fArray[GlobalIndex()];
387
388}
389void AliJArrayAlgorithmSimple::SetItem(void * item){
390 fArray[GlobalIndex()] = item;
391}
392
393
394//////////////////////////////////////////////////////
395// AliJTH1
396//////////////////////////////////////////////////////
397//_____________________________________________________
398AliJTH1::AliJTH1():
399 fDirectory(NULL),
400 fSubDirectory(NULL),
401 fHMG(NULL),
402 fTemplate(NULL),
403 fBins(0)
404{
405 // default constructor
406 fName="AliJTH1";
407}
408
409//_____________________________________________________
410AliJTH1::AliJTH1(TString config, AliJHistManager * hmg):
411 fDirectory(NULL),
412 fSubDirectory(NULL),
413 fHMG(NULL),
414 fTemplate(NULL),
415 fBins(0)
416{
417 // constructor
418 std::vector<TString> t = Tokenize(config, " \t,");
419 TString type = t[0];
420 SetName( t[1] );
421 SetTitle( t[2] );
422 fTitle.ReplaceAll("\"","");
423 SetFullOption( t[3] );
424 fMode = HasOption("mode","Single")?kSingle:kNormal;
425 AddToManager( hmg );
426 TString s;
427 for( int i=4;i<int(t.size());i++ ) s+=" "+t[i];
428 AddDim( s );
429 FixBin();
430}
431//_____________________________________________________
432AliJTH1::~AliJTH1(){
433 // destructor
434 if( fNGenerated == 0 && fTemplate ) delete fTemplate;
435}
436
437//_____________________________________________________
438AliJTH1::AliJTH1(const AliJTH1& obj) :
439 AliJArrayBase(),
440 fDirectory(obj.fDirectory),
441 fSubDirectory(obj.fSubDirectory),
442 fHMG(obj.fHMG),
443 fTemplate(obj.fTemplate),
444 fBins(obj.fBins)
445{
446 // copy constructor TODO: proper handling of pointer data members
447}
448
449//_____________________________________________________
450AliJTH1& AliJTH1::operator=(const AliJTH1& obj)
451{
452 // assignment operator
453 if(this != &obj){
454 // TODO: proper implementation
455 }
456 return *this;
457}
458
459//_____________________________________________________
460int AliJTH1::AddDim( AliJBin * bin){
461 int ndim = this->AliJArrayBase::AddDim( bin->Size() );
462 fBins.resize( ndim, NULL );
463 fBins[ndim-1] = bin;
464 return ndim;
465}
466
467int AliJTH1::AddDim(TString v) {
468 if( v == "END" ) { FixBin(); }
469 else{
470 std::vector<TString> o= Tokenize(v, "\t ,");
471 for( UInt_t i=0;i<o.size();i++ ){
472 TString & s = o[i];
473 if(s.Length() == 0 ) continue;
474 if( s.IsFloat() ) { // TODO IsInt? IsDigit?
475 AddDim( s.Atoi() );
476 continue;
477 }
478 AliJBin * b = NULL;
479 if( fHMG ) b = fHMG->GetBin(s);
480 if( b ) this->AddDim(b);
481 else {JERROR("Wrong terminator of Array : \"" + s+"\" in " + fName ); }
482 }
483 }
484 return Dimension();
485}
486//_____________________________________________________
487Int_t AliJTH1::Write(){
488 TDirectory *owd = (TDirectory*) gDirectory;
489 InitIterator();
490 void * item;
491 if( fSubDirectory ) fSubDirectory->cd();
492 //else fDirectory->cd();
493 while( Next(item) ){
494 if( !item ) continue;
495 TH1 * obj = static_cast<TH1*>(item);
496 obj->Write( );
497 //obj->Write( 0, TObject::kOverwrite );
498 }
499 if( owd != gDirectory ) owd->cd();
500 return 0;
501}
502//_____________________________________________________
503TString AliJTH1::GetString( ){
504 TString s = Form( "%s\t%s\t\"%s\"\t%s\t",
505 ClassName(), fName.Data(), fTitle.Data(), fOption.Data() );
506 for( int i=0;i<Dimension();i++ ){
507 if( int(fBins.size()) > i && fBins[i] != NULL ){
508 s+= " "+fBins[i]->GetName();
509 }else{
510 s+= TString(" ")+Form("%d", SizeOf(i));
511 }
512 }
513 return s;
514}
515//_____________________________________________________
516void AliJTH1::FixBin(){
517 this->AliJArrayBase::FixBin();
518
519 if(!fHMG) {
520 AddToManager( AliJHistManager::CurrentManager() );
521 }
522 if(!fDirectory) fDirectory = fHMG->GetDirectory();
523}
524
525//_____________________________________________________
526void AliJTH1::AddToManager(AliJHistManager *hmg){
527 if(fHMG) return; // TODO handle error
528 fHMG = hmg;
529 hmg->Add(this);
530}
531//_____________________________________________________
532void AliJTH1::Print(){
533 std::cout<<"*"<<GetString()<<std::endl;
534 // TODO more details.
535}
536//_____________________________________________________
537void AliJTH1::SetTemplate(TH1 *h){
538 if( fTemplate ) return; /// TDOO give error
539 fTemplate = (TH1*)h->Clone();
540 fTemplate->Sumw2();
541 fTemplate->SetDirectory(0);
542 fName = h->GetName();
543 fTitle = h->GetTitle();
544}
545//_____________________________________________________
546TString AliJTH1::BuildName(){
547 TString name = fName;
548 if( !HasOption("Single") )
549 for( int i=0;i<Dimension();i++ ){
550 name+=((int(fBins.size()) > i && fBins[i] != NULL)?fBins[i]->GetIndexName():"H")
551 +Form("%02d",Index(i));
552 }
553 return name;
554}
555//_____________________________________________________
556TString AliJTH1::BuildTitle(){
557 TString title = fTitle;
558 for( int i=0;i<Dimension();i++ )
598ff595 559 title+=((int(fBins.size()) > i && fBins[i] != NULL)?" "+fBins[i]->BuildTitle(Index(i)):"")
9dc4f671 560 +Form("%02d",Index(i));
561 return title;
562}
563//_____________________________________________________
564void * AliJTH1::BuildItem(){
565 TDirectory * owd = (TDirectory*) gDirectory;
566 gROOT->cd();
567 TString name = BuildName();
568 TH1 * item = NULL;
569 if( !fSubDirectory ){
570 if( !HasOption("dir") ) {
571 fSubDirectory = fDirectory;
572 }
573 else {
574 fSubDirectory = fDirectory->GetDirectory(fName);
575 if( !fSubDirectory && !IsLoadMode() ){
576 fSubDirectory = fDirectory->mkdir(fName);
577 }
578 }
579 }
580 if( IsLoadMode() ){
581 //if( fSubDirectory ) JDEBUG(2, fSubDirectory->GetName() );
582 if( fSubDirectory )
583 item = dynamic_cast<TH1*>(fSubDirectory->Get( name ));
584 if( !item ){
585 void ** rawItem = fAlg->GetRawItem();
586 InitIterator();
587 void * tmp;
588 while( Next(tmp) ){
589 item = (TH1*)fSubDirectory->Get(BuildName());
590 if(item ) break;
591 }
592 if( item ) {
593 item = dynamic_cast<TH1*>((static_cast<TH1*>(item))->Clone(name));
594 item->Reset();
595 item->SetTitle( BuildTitle() );
596 item->SetDirectory(0);
597 *rawItem = (void*)item;
598 }
599 }
600 if( !item ){ JERROR("Any of "+fName+" doesn't exists. I need at least one");}
601 }
602 else{ // Gen Mode
603 TH1 * titem = NULL;
604 if(fNGenerated == 0 ) {
605 titem = fTemplate;
606 }
607 else titem =(TH1*) fTemplate->Clone();
608 titem->SetDirectory( fSubDirectory );
609 titem->Reset();
610 titem->SetName( BuildName() );
611 titem->SetTitle( BuildTitle() );
612 fNGenerated++;
613 item=titem;
614 }
615 if( item ) fAlg->SetItem(item);
616 owd->cd();
617 return (void*)item;
618}
619//_____________________________________________________
620bool AliJTH1::IsLoadMode(){
621 return fHMG->IsLoadMode();
622}
623
624
598ff595 625//////////////////////////////////////////////////////////////////////////
626// //
627// AliJTH1Derived //
628// //
629//////////////////////////////////////////////////////////////////////////
630template< typename T>
631AliJTH1Derived<T>::AliJTH1Derived():
632 AliJTH1(), fPlayer(this)
633{
634}
635template< typename T>
636AliJTH1Derived<T>::~AliJTH1Derived(){
637}
638
9dc4f671 639
640
641
642//////////////////////////////////////////////////////////////////////////
643// //
644// AliJHistManager //
645// //
646// Array Base Class //
647// //
648//////////////////////////////////////////////////////////////////////////
649AliJHistManager::AliJHistManager(TString name):
650 AliJNamed(name,"","",0),
651 fIsLoadMode(false),
652 fDirectory(gDirectory),
653 fConfigStr(),
654 fBin(0),
655 fHist(0),
656 fManager(0),
657 fBinNames(0),
658 fBinConfigs(0),
659 fHistNames(0),
660 fHistConfigs(0)
661{
662 // constructor
9dc4f671 663 fDirectory = gDirectory;
5dbedd98 664 this->cd();
9dc4f671 665}
666
598ff595 667AliJHistManager::AliJHistManager(TString name, TString dirname):
668 AliJNamed(name,"","",0),
669 fIsLoadMode(false),
670 fDirectory(gDirectory),
671 fConfigStr(),
672 fBin(0),
673 fHist(0),
674 fManager(0),
675 fBinNames(0),
676 fBinConfigs(0),
677 fHistNames(0),
678 fHistConfigs(0)
679{
680 // constructor
681 if( dirname.Length() > 0 ) {
682 fDirectory = (TDirectory*)gDirectory->Get(dirname);
683 if( !fDirectory ){
684 fDirectory = gDirectory->mkdir( dirname );
685 }
686 }
687 if( !fDirectory ){
688 fDirectory = gDirectory;
689 }
690 this->cd();
691}
692
9dc4f671 693//_____________________________________________________
694AliJHistManager::AliJHistManager(const AliJHistManager& obj) :
695 AliJNamed(obj.fName,obj.fTitle,obj.fOption,obj.fMode),
696 fIsLoadMode(obj.fIsLoadMode),
697 fDirectory(obj.fDirectory),
698 fConfigStr(obj.fConfigStr),
699 fBin(obj.fBin),
700 fHist(obj.fHist),
701 fManager(obj.fManager),
702 fBinNames(obj.fBinNames),
703 fBinConfigs(obj.fBinConfigs),
704 fHistNames(obj.fHistNames),
705 fHistConfigs(obj.fHistConfigs)
706{
707 // copy constructor TODO: proper handling of pointer data members
708}
709
710//_____________________________________________________
711AliJHistManager& AliJHistManager::operator=(const AliJHistManager& obj)
712{
713 // assignment operator
714 if(this != &obj){
715 // TODO: proper implementation
716 }
717 return *this;
718}
719
720AliJHistManager* AliJHistManager::GlobalManager(){
721 static AliJHistManager* singleton = new AliJHistManager("GlobalHistManager");
722 return singleton;
723}
724
725AliJHistManager* AliJHistManager::CurrentManager( AliJHistManager * hmg){
5dbedd98 726 static AliJHistManager* currentManager = NULL;//;AliJHistManager::GlobalManager();
9dc4f671 727 if( hmg ) currentManager = hmg;
728 return currentManager;
729}
730
731AliJBin* AliJHistManager::GetBuiltBin(TString s ){
732 for( int i=0;i<int(fBin.size());i++ )
733 if( fBin[i]->GetName() == s ) return fBin[i];
734 return NULL;
735}
736AliJBin* AliJHistManager::GetBin(TString s ){
737 AliJBin* h = GetBuiltBin(s);
738 if(h) return h;
739 for( int i=0;i<GetNBin();i++ )
740 if( fBinNames[i] == s ){
741 return new AliJBin( fBinConfigs[i],this );
742 }
743 return NULL;
744}
745AliJTH1 * AliJHistManager::GetBuiltTH1(TString s ){
746 for( int i=0;i<int(fHist.size());i++ )
747 if( fHist[i]->GetName() == s ) return fHist[i];
748 return NULL;
749}
750AliJTH1 * AliJHistManager::GetTH1(TString s ){
751 AliJTH1 * h = GetBuiltTH1(s);
752 if( h ) return h;
753 for( int i=0;i<GetNHist();i++ )
754 if( fHistNames[i] == s ){
755 if( fHistConfigs[i].BeginsWith("AliJTH1D")) return new AliJTH1D( fHistConfigs[i], this );
756 if( fHistConfigs[i].BeginsWith("AliJTH2D")) return new AliJTH2D( fHistConfigs[i], this );
757 if( fHistConfigs[i].BeginsWith("AliJTProfile")) return new AliJTProfile( fHistConfigs[i], this );
758 }
759 return NULL;
760}
761void AliJHistManager::Add(AliJBin *o ){
762 if( !o ) return;
763 if( GetBuiltBin( o->GetName() ) ) return; // TODO error handle
764 fBin.push_back( o );
765}
766void AliJHistManager::Add(AliJTH1 *o ){
767 if( !o ) return;
768 if( GetBuiltTH1( o->GetName() ) ) return; // TODO error handle
769 fHist.push_back( o );
770}
771void AliJHistManager::Print(){
772 if( IsLoadMode() ) {
773 cout<<fConfigStr<<endl;
774 return;
775 }
776 cout<<"============ AliJHistManager : "<<fName<<" ==================="<<endl;
777 cout<<endl;
778 cout<<"---- AliJBin ----"<<endl;
779 for( int i=0;i<GetNBin();i++ ){
780 fBin[i]->Print();
781 }
782 cout<<endl;
783 cout<<"---- AliJTH1 ----"<<endl;
784 for( int i=0;i<GetNHist();i++ ){
785 fHist[i]->Print();
786 }
787}
788void AliJHistManager::Write(){
789 for( int i=0;i<GetNHist();i++ )
790 fHist[i]->Write();
791}
792
793void AliJHistManager::WriteConfig(){
794 TDirectory *owd = gDirectory;
598ff595 795 cout<<"DEBUG_T1: "<<gDirectory<<endl;
796 gDirectory->Print();
5dbedd98 797 TDirectory * fHistConfigDir = fDirectory->mkdir("HistManager");
9dc4f671 798 fHistConfigDir->cd();
799 TObjString * config = new TObjString(GetString().Data());
800 config->Write("Config");
801 owd->cd();
802}
803
804int AliJHistManager::LoadConfig(){
805 SetLoadMode(true);
806 TObjString *strobj = (TObjString*)fDirectory->Get("HistManager/Config");
807 if( !strobj ) return 0; //TODO
808 TString config = strobj->String();
809 fConfigStr = config;
810 vector<TString> lines = Tokenize(config, "\n");
811 cout<< Form("Read Config.%d objects found\n", (int)lines.size() );
812 for( UInt_t i=0;i < lines.size();i++ ){
813 TString line = lines.at(i);
814 std::vector<TString> t = Tokenize(line, " \t,");
815 if(line.BeginsWith("AliJBin")) {
816 fBinNames.push_back( t[1] );
817 fBinConfigs.push_back( line );
818 }else if( line.BeginsWith("AliJ")){
819 fHistNames.push_back( t[1] );
820 fHistConfigs.push_back( line );
821 }
822 }
823 return 1;
824}
825
826
827//////////////////////////////////////////////////////
828// Utils
829//////////////////////////////////////////////////////
830vector<TString> Tokenize( TString s, TString d, int quote ){
831 //int nd = d.Length();
832 bool flagBeforeToken = 0;
833 bool inQuote = 0;
834 TString tok="";
835 vector<TString> toks;
836 s += d[0];
837 for( int i=0;i<s.Length();i++ ){
838 if( quote == 1 && s[i] == '\"' ){ inQuote = ! inQuote; }
839 if( d.First(s[i]) != kNPOS && !inQuote ){
840 if( flagBeforeToken == 0 && tok.Length()>0){
841 toks.push_back(tok);
842 tok.Clear();
843 flagBeforeToken = 1;
844 }
845 }else{
846 tok+=s[i];
847 flagBeforeToken = 0;
848 }
849 }
850 return toks;
851}
852
853
854TString Join( vector<TString>& ss , TString del){
855 if( ss.size() < 1 ) return "";
856 TString s = ss[0];
857 for( UInt_t i=1;i<ss.size();i++ ) s+=del+ss[i];
858 return s;
859}
860
861bool OutOf( int i, int x, int y ){ return ( i<x || i>y ); }
862#include <TFile.h>
863
864void testAliJArray(){
865 cout<<"START"<<endl;
866 AliJHistManager * fHMG;
867 AliJBin fCentBin;
868 AliJBin fVtxBin;
869 AliJBin fPTtBin;
870 AliJBin fPTaBin;
871 AliJBin fXEBin;
872 AliJBin fKLongBin;
873 AliJBin fRGapBin;
874 AliJBin fEtaGapBin;
875 AliJBin fPhiGapBin;
876 AliJBin fMassBin;
877 AliJBin fTypBin;
878 AliJBin fTypBin3;
879 AliJBin fPairPtBin;
880 AliJTH1D fhTriggPtBin;
881 AliJTH1D fhTriggMult;
882 AliJTH1D fhIphiTrigg;
883 AliJTH1D fhIetaTrigg;
884 AliJTH2D test1;
885 AliJTProfile test2;
886
887 TFile * f = new TFile("test.root","RECREATE");
888 fHMG = AliJHistManager::GlobalManager();
889 fCentBin .Set("Cent", "C", "C %2.0f-%2.0f%%" ).SetBin( "0 100");
890 fVtxBin .Set("Vtx", "V", "Vtx %.0f-%.0f" ).SetBin("-10 10");
891 fPTtBin .Set("PTt", "T", "p_{Tt} %.1f-%.1f").SetBin("3 5 8 10 15 20");
892 fPTaBin .Set("PTa", "A", "p_{Tt} %.1f-%.1f").SetBin("3 5 8 10 15 20");
893
894 fhTriggMult
895 << TH1D( "hTriggMult", "", 100, -0.5, 99.5)
896 << fCentBin << fPTtBin << "END";
897 fhIphiTrigg
898 << TH1D( "fhIphiTrigg", "", 3, -0.1, 0.1 )
899 << fCentBin << fPTtBin << "END";
900 fhIetaTrigg
901 << TH1D( "hIetaTrigg", "", 80, -5, 5 )
902 << fCentBin << fPTtBin << "END";// inclusive eta
903 fhTriggPtBin
904 << TH1D( "hTriggPtBin", "", 10,0,10 )
905 << fCentBin << fVtxBin << fPTtBin << "END";
906
907 fhTriggMult[0][0]->Fill(1);
908 fHMG->Print();
909
910 f->Write();
911 fHMG->Write();
912 fHMG->WriteConfig();
913
914}