]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGCF/Correlations/JCORRAN/AliJHistManager.cxx
3e30f89cd944b47cbb890747afaad714d7dabcde
[u/mrichter/AliRoot.git] / PWGCF / Correlations / JCORRAN / AliJHistManager.cxx
1 #include "AliJHistManager.h"
2 #include <TMath.h>
3 using namespace std;
4 //////////////////////////////////////////////////////
5 //  AliJBin
6 //////////////////////////////////////////////////////
7
8 AliJNamed::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
17 AliJNamed::~AliJNamed(){
18     // virtual destructor for base class
19 }
20
21 TString 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 }
27 void 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 }
35 void AliJNamed::RemoveOption( TString key ){
36     SetOption( key, UndefinedOption() );
37 }
38 TString AliJNamed::UndefinedOption(){
39     //static TString undefinedOption = "Undefined";
40     //return undefinedOption;
41     return "Undefined";
42 }
43
44 //////////////////////////////////////////////////////
45 //  AliJBin
46 //////////////////////////////////////////////////////
47
48 //_____________________________________________________
49 AliJBin::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 //_____________________________________________________
58 AliJBin::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 //_____________________________________________________
82 AliJBin::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 //_____________________________________________________
94 AliJBin& AliJBin::operator=(const AliJBin& obj)
95 {
96   // assignment operator
97   if(this != &obj){
98     // TODO: proper implementation
99   }
100   return *this;
101 }
102
103 //_____________________________________________________
104 void AliJBin::FixBin(){ 
105     if(fIsFixedBin ) return;
106     fIsFixedBin = true;
107     if(!fHMG) AddToManager( AliJHistManager::CurrentManager());
108     
109 }
110 //_____________________________________________________
111 void AliJBin::AddToManager( AliJHistManager* hmg ){ 
112     hmg->Add(this); 
113 }
114 //_____________________________________________________
115 AliJBin & 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 //_____________________________________________________
123 TString AliJBin::GetModeString(int i){
124     static TString mode[] = { "Single","Range","String" };
125     if( i<0 || i>2 ) return "";
126     return mode[i];
127 }
128 int AliJBin::GetMode( TString mode ){
129     for( int i=0;i<kNMode;i++ ) if( mode == GetModeString(i) ) return i;
130     return -1;
131 }
132 //_____________________________________________________
133 AliJBin & 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 //_____________________________________________________
138 AliJBin & AliJBin::SetBin( const int n, const double * v ){
139     for( int i=0;i<n;i++ ) AddBin( v[i] );FixBin();
140     return *this;
141 }
142 AliJBin & AliJBin::SetBin( TVector *v ){
143     for( int i=0;i<v->GetNrows();i++ ) AddBin( (v->GetMatrixArray())[i] );FixBin();
144     return *this;
145 }
146 //_____________________________________________________
147 AliJBin& 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 //_____________________________________________________
154 AliJBin& AliJBin::SetBin(const int  n){
155     for( UInt_t i=0; i<UInt_t(n);i++ ) AddBin( i );FixBin();
156     return *this;
157 }
158 //_____________________________________________________
159 void AliJBin::AddBin( const TString& v ){
160     if( fIsFixedBin ) { JERROR( "You can't Add Bini "+GetName()); }
161     fBinStr.push_back( (v=="_")?"":v );
162     fBinD.push_back( v.Atof() );
163 }
164 //_____________________________________________________
165 void AliJBin::AddBin( float v ){
166     if( fIsFixedBin ) { JERROR( "You can't Add Bin "+GetName()); }
167     fBinD.push_back( v ); 
168     fBinStr.push_back(Form("%f",v));
169 }
170 //_____________________________________________________
171 TString 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 //_____________________________________________________
183 TString AliJBin::GetString(){
184     SetOption( "mode",GetModeString(fMode) );
185     return "AliJBin\t"+fName+"\t"+fIndexName+"\t\""+fTitle+"\""+"\t"+fOption+"\t"+Join(fBinStr," ");
186
187 }
188 //_____________________________________________________
189 void AliJBin::Print(){
190     std::cout<<"*"+GetString()<<std::endl;
191 }
192
193 int 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
199 //////////////////////////////////////////////////////
200 // AliJArrayBase 
201 //////////////////////////////////////////////////////
202
203 //_____________________________________________________
204 AliJArrayBase::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 //_____________________________________________________
218 AliJArrayBase::~AliJArrayBase(){
219     //destructor
220     if(fAlg) delete fAlg;
221 }
222
223 //_____________________________________________________
224 AliJArrayBase::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 //_____________________________________________________
238 AliJArrayBase& AliJArrayBase::operator=(const AliJArrayBase& obj)
239 {
240   // assignment operator
241   if(this != &obj){
242     // TODO: proper implementation
243   }
244   return *this;
245 }
246
247 //_____________________________________________________
248 void* AliJArrayBase::GetItem(){
249     void * item = fAlg->GetItem();
250     if( !item ){ 
251         BuildItem() ; 
252         item = fAlg->GetItem();
253     }
254     return item;
255 }
256 //_____________________________________________________
257 void* AliJArrayBase::GetSingleItem(){
258     if(fMode == kSingle )return GetItem();
259     JERROR("This is not single array");
260     return NULL;
261 }
262 //_____________________________________________________
263 void 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 //_____________________________________________________
274 int AliJArrayBase::Index(int d){
275     if( OutOfDim(d) ) JERROR("Wrong Dim");
276     return fIndex[d];
277 }
278 void AliJArrayBase::SetIndex(int i, int d ){
279     if( OutOfSize( i, d ) ) JERROR( "Wrong Index" );
280     fIndex[d] = i;
281 }
282
283 void AliJArrayBase::InitIterator(){ fAlg->InitIterator(); }
284 bool AliJArrayBase::Next(void *& item){ return fAlg->Next(item); }
285
286
287 //////////////////////////////////////////////////////
288 //  AliJArrayAlgorithm
289 //////////////////////////////////////////////////////
290
291 //_____________________________________________________
292 AliJArrayAlgorithm::AliJArrayAlgorithm(AliJArrayBase * cmd):
293     fCMD(cmd)
294 {
295   // constructor
296 }
297 //_____________________________________________________
298 AliJArrayAlgorithm::~AliJArrayAlgorithm(){
299   // destructor
300 }
301
302 //_____________________________________________________
303 AliJArrayAlgorithm::AliJArrayAlgorithm(const AliJArrayAlgorithm& obj) :
304     fCMD(obj.fCMD)
305 {
306   // copy constructor TODO: proper handling of pointer data members
307 }
308
309 //_____________________________________________________
310 AliJArrayAlgorithm& 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 //_____________________________________________________
324 AliJArrayAlgorithmSimple::AliJArrayAlgorithmSimple(AliJArrayBase * cmd):
325     AliJArrayAlgorithm(cmd),
326     fDimFactor(0),
327     fArray(NULL),
328     fPos(0)
329 {
330   // constructor
331 }
332 //_____________________________________________________
333 AliJArrayAlgorithmSimple::~AliJArrayAlgorithmSimple(){
334     // Dimension, GetEntries, SizeOf
335     if( fArray ) delete [] (void**)fArray;
336 }
337
338 //_____________________________________________________
339 AliJArrayAlgorithmSimple::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 //_____________________________________________________
349 AliJArrayAlgorithmSimple& 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 //_____________________________________________________
358 int 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 //_____________________________________________________
369 int  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 }
377 void 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 }
385 void * AliJArrayAlgorithmSimple::GetItem(){
386     return fArray[GlobalIndex()];
387
388 }
389 void AliJArrayAlgorithmSimple::SetItem(void * item){
390     fArray[GlobalIndex()] = item;
391 }
392
393
394 //////////////////////////////////////////////////////
395 //  AliJTH1
396 //////////////////////////////////////////////////////
397 //_____________________________________________________
398 AliJTH1::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 //_____________________________________________________
410 AliJTH1::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 //_____________________________________________________
432 AliJTH1::~AliJTH1(){
433     // destructor
434     if( fNGenerated == 0 && fTemplate ) delete fTemplate;
435 }
436
437 //_____________________________________________________
438 AliJTH1::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 //_____________________________________________________
450 AliJTH1& AliJTH1::operator=(const AliJTH1& obj)
451 {
452   // assignment operator
453   if(this != &obj){
454     // TODO: proper implementation
455   }
456   return *this;
457 }
458
459 //_____________________________________________________
460 int 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
467 int 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 //_____________________________________________________
487 Int_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 //_____________________________________________________
503 TString 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 //_____________________________________________________
516 void AliJTH1::FixBin(){
517     this->AliJArrayBase::FixBin();
518
519     if(!fHMG) {
520         AddToManager( AliJHistManager::CurrentManager() );
521     }
522     if(!fDirectory) fDirectory = fHMG->GetDirectory();
523 }
524
525 //_____________________________________________________
526 void AliJTH1::AddToManager(AliJHistManager *hmg){
527     if(fHMG) return; // TODO handle error
528     fHMG = hmg;
529     hmg->Add(this);
530 }
531 //_____________________________________________________
532 void AliJTH1::Print(){
533     std::cout<<"*"<<GetString()<<std::endl;
534     // TODO more details.
535 }
536 //_____________________________________________________
537 void 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 //_____________________________________________________
546 TString 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 //_____________________________________________________
556 TString AliJTH1::BuildTitle(){
557     TString title = fTitle;
558     for( int i=0;i<Dimension();i++ )
559         title+=((int(fBins.size()) > i && fBins[i] != NULL)?" "+fBins[i]->BuildTitle(i):"")
560             +Form("%02d",Index(i));
561     return title;
562 }
563 //_____________________________________________________
564 void * 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 //_____________________________________________________
620 bool AliJTH1::IsLoadMode(){
621     return fHMG->IsLoadMode();
622 }
623
624
625 //////////////////////////////////////////////////////////////////////////
626 //                                                                      //
627 // AliJTH1Derived                                                       //
628 //                                                                      //
629 //////////////////////////////////////////////////////////////////////////
630 template< typename T>
631 AliJTH1Derived<T>::AliJTH1Derived():
632     AliJTH1(), fPlayer(this)
633 {
634 }
635 template< typename T>
636 AliJTH1Derived<T>::~AliJTH1Derived(){
637 }
638
639
640
641
642 //////////////////////////////////////////////////////////////////////////
643 //                                                                      //
644 // AliJHistManager                                                       //
645 //                                                                      //
646 // Array Base Class                                                     //
647 //                                                                      //
648 //////////////////////////////////////////////////////////////////////////
649 AliJHistManager::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
663     fDirectory = gDirectory;
664     this->cd();
665 }
666
667 //_____________________________________________________
668 AliJHistManager::AliJHistManager(const AliJHistManager& obj) :
669     AliJNamed(obj.fName,obj.fTitle,obj.fOption,obj.fMode),
670     fIsLoadMode(obj.fIsLoadMode),
671     fDirectory(obj.fDirectory),
672     fConfigStr(obj.fConfigStr),
673     fBin(obj.fBin),
674     fHist(obj.fHist),
675     fManager(obj.fManager),
676     fBinNames(obj.fBinNames),
677     fBinConfigs(obj.fBinConfigs),
678     fHistNames(obj.fHistNames),
679     fHistConfigs(obj.fHistConfigs)
680 {
681   // copy constructor TODO: proper handling of pointer data members
682 }
683
684 //_____________________________________________________
685 AliJHistManager& AliJHistManager::operator=(const AliJHistManager& obj)
686 {
687   // assignment operator
688   if(this != &obj){
689     // TODO: proper implementation
690   }
691   return *this;
692 }
693
694 AliJHistManager* AliJHistManager::GlobalManager(){
695     static AliJHistManager* singleton = new AliJHistManager("GlobalHistManager");
696     return singleton;
697 }
698
699 AliJHistManager* AliJHistManager::CurrentManager( AliJHistManager * hmg){
700     static AliJHistManager* currentManager = NULL;//;AliJHistManager::GlobalManager();
701     if( hmg ) currentManager = hmg; 
702     return currentManager;
703 }
704
705 AliJBin* AliJHistManager::GetBuiltBin(TString s ){
706     for( int i=0;i<int(fBin.size());i++ )
707         if( fBin[i]->GetName() == s ) return fBin[i];
708     return NULL;
709 }
710 AliJBin* AliJHistManager::GetBin(TString s ){
711     AliJBin* h = GetBuiltBin(s);
712     if(h) return h;
713     for( int i=0;i<GetNBin();i++ )
714         if( fBinNames[i] == s ){
715             return new AliJBin( fBinConfigs[i],this );
716         }
717     return NULL;
718 }
719 AliJTH1 * AliJHistManager::GetBuiltTH1(TString s ){
720     for( int i=0;i<int(fHist.size());i++ )
721         if( fHist[i]->GetName() == s ) return fHist[i];
722     return NULL;
723 }
724 AliJTH1 * AliJHistManager::GetTH1(TString s ){
725     AliJTH1 * h = GetBuiltTH1(s);
726     if( h ) return h;
727     for( int i=0;i<GetNHist();i++ )
728         if( fHistNames[i] == s ){
729             if( fHistConfigs[i].BeginsWith("AliJTH1D")) return new AliJTH1D( fHistConfigs[i], this );
730             if( fHistConfigs[i].BeginsWith("AliJTH2D")) return new AliJTH2D( fHistConfigs[i], this );
731             if( fHistConfigs[i].BeginsWith("AliJTProfile")) return new AliJTProfile( fHistConfigs[i], this );
732         }
733     return NULL;
734 }
735 void AliJHistManager::Add(AliJBin *o ){
736     if( !o ) return;
737     if( GetBuiltBin( o->GetName() ) ) return; // TODO error handle
738     fBin.push_back( o ); 
739 }
740 void AliJHistManager::Add(AliJTH1 *o ){
741     if( !o ) return;
742     if( GetBuiltTH1( o->GetName() ) ) return; // TODO error handle
743     fHist.push_back( o ); 
744 }
745 void AliJHistManager::Print(){
746     if( IsLoadMode() ) {
747         cout<<fConfigStr<<endl;
748         return;
749     }
750     cout<<"============ AliJHistManager : "<<fName<<" ==================="<<endl;
751     cout<<endl;
752     cout<<"---- AliJBin ----"<<endl;
753     for( int i=0;i<GetNBin();i++ ){
754         fBin[i]->Print();
755     }
756     cout<<endl;
757     cout<<"---- AliJTH1 ----"<<endl;
758     for( int i=0;i<GetNHist();i++ ){
759         fHist[i]->Print();
760     }
761 }
762 void AliJHistManager::Write(){
763     for( int i=0;i<GetNHist();i++ )
764         fHist[i]->Write();
765 }
766
767 void AliJHistManager::WriteConfig(){
768     TDirectory *owd = gDirectory;
769     TDirectory * fHistConfigDir = fDirectory->mkdir("HistManager");
770     fHistConfigDir->cd();
771     TObjString * config = new TObjString(GetString().Data());
772     config->Write("Config");
773     owd->cd();
774 }
775
776 int AliJHistManager::LoadConfig(){
777     SetLoadMode(true);
778     TObjString *strobj = (TObjString*)fDirectory->Get("HistManager/Config");
779     if( !strobj ) return 0; //TODO
780     TString config = strobj->String();
781     fConfigStr = config;
782     vector<TString> lines = Tokenize(config, "\n");
783     cout<< Form("Read Config.%d objects found\n", (int)lines.size() );
784     for( UInt_t i=0;i < lines.size();i++ ){
785         TString line = lines.at(i);
786         std::vector<TString> t = Tokenize(line, " \t,");
787         if(line.BeginsWith("AliJBin")) {
788             fBinNames.push_back( t[1] );
789             fBinConfigs.push_back( line );
790         }else if( line.BeginsWith("AliJ")){
791             fHistNames.push_back( t[1] );
792             fHistConfigs.push_back( line );
793         }
794     }
795     return 1;
796 }
797
798
799 //////////////////////////////////////////////////////
800 //  Utils
801 //////////////////////////////////////////////////////
802 vector<TString> Tokenize( TString s, TString d, int quote ){
803     //int nd = d.Length();
804     bool flagBeforeToken = 0;
805     bool inQuote = 0;
806     TString tok="";
807     vector<TString> toks;
808     s += d[0];
809     for( int i=0;i<s.Length();i++ ){
810         if( quote == 1 && s[i] == '\"' ){ inQuote = ! inQuote; }
811         if( d.First(s[i]) != kNPOS && !inQuote ){
812             if( flagBeforeToken == 0 && tok.Length()>0){
813                 toks.push_back(tok);
814                 tok.Clear();
815                 flagBeforeToken = 1;
816             }
817         }else{
818             tok+=s[i];
819             flagBeforeToken = 0;
820         }
821     }
822     return toks;
823 }
824
825
826 TString Join( vector<TString>& ss , TString del){
827     if( ss.size() < 1 ) return "";
828     TString s = ss[0];
829     for( UInt_t i=1;i<ss.size();i++ ) s+=del+ss[i];
830     return s;
831 }
832
833 bool OutOf( int i, int x, int y ){ return ( i<x || i>y ); }
834 #include <TFile.h>
835
836 void testAliJArray(){
837     cout<<"START"<<endl;
838     AliJHistManager * fHMG;
839     AliJBin fCentBin;
840     AliJBin fVtxBin;
841     AliJBin fPTtBin;
842     AliJBin fPTaBin;
843     AliJBin fXEBin;
844     AliJBin fKLongBin;
845     AliJBin fRGapBin;
846     AliJBin fEtaGapBin;
847     AliJBin fPhiGapBin;
848     AliJBin fMassBin;
849     AliJBin fTypBin;
850     AliJBin fTypBin3;
851     AliJBin fPairPtBin;
852     AliJTH1D fhTriggPtBin;
853     AliJTH1D fhTriggMult;
854     AliJTH1D fhIphiTrigg;
855     AliJTH1D fhIetaTrigg;
856     AliJTH2D test1;
857     AliJTProfile test2;
858
859     TFile * f = new TFile("test.root","RECREATE");
860     fHMG = AliJHistManager::GlobalManager();
861     fCentBin   .Set("Cent",   "C", "C %2.0f-%2.0f%%" ).SetBin( "0 100");
862     fVtxBin    .Set("Vtx",    "V", "Vtx %.0f-%.0f" ).SetBin("-10 10");
863     fPTtBin    .Set("PTt",    "T", "p_{Tt} %.1f-%.1f").SetBin("3 5 8 10 15 20");
864     fPTaBin    .Set("PTa",    "A", "p_{Tt} %.1f-%.1f").SetBin("3 5 8 10 15 20");
865
866     fhTriggMult
867         << TH1D( "hTriggMult", "",  100, -0.5, 99.5) 
868         <<  fCentBin << fPTtBin  << "END";
869     fhIphiTrigg
870         << TH1D( "fhIphiTrigg", "",  3, -0.1, 0.1 ) 
871         <<  fCentBin << fPTtBin  << "END";
872     fhIetaTrigg
873         << TH1D( "hIetaTrigg", "",  80, -5, 5 ) 
874         <<  fCentBin << fPTtBin  << "END";// inclusive eta
875     fhTriggPtBin
876         << TH1D( "hTriggPtBin", "", 10,0,10 )
877         <<  fCentBin << fVtxBin << fPTtBin  << "END";
878
879     fhTriggMult[0][0]->Fill(1);
880     fHMG->Print();
881
882     f->Write();
883     fHMG->Write();
884     fHMG->WriteConfig();
885
886 }