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