PWGCF/Correlations converted to native cmake
[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
628 //////////////////////////////////////////////////////////////////////////
629 //                                                                      //
630 // AliJHistManager                                                       //
631 //                                                                      //
632 // Array Base Class                                                     //
633 //                                                                      //
634 //////////////////////////////////////////////////////////////////////////
635 AliJHistManager::AliJHistManager(TString name):
636     AliJNamed(name,"","",0),
637     fIsLoadMode(false),
638     fDirectory(gDirectory),
639     fConfigStr(),
640     fBin(0),
641     fHist(0),
642     fManager(0),
643     fBinNames(0),
644     fBinConfigs(0),
645     fHistNames(0),
646     fHistConfigs(0)
647 {
648     // constructor
649     fDirectory = gDirectory;
650     this->cd();
651 }
652
653 //_____________________________________________________
654 AliJHistManager::AliJHistManager(const AliJHistManager& obj) :
655     AliJNamed(obj.fName,obj.fTitle,obj.fOption,obj.fMode),
656     fIsLoadMode(obj.fIsLoadMode),
657     fDirectory(obj.fDirectory),
658     fConfigStr(obj.fConfigStr),
659     fBin(obj.fBin),
660     fHist(obj.fHist),
661     fManager(obj.fManager),
662     fBinNames(obj.fBinNames),
663     fBinConfigs(obj.fBinConfigs),
664     fHistNames(obj.fHistNames),
665     fHistConfigs(obj.fHistConfigs)
666 {
667   // copy constructor TODO: proper handling of pointer data members
668 }
669
670 //_____________________________________________________
671 AliJHistManager& AliJHistManager::operator=(const AliJHistManager& obj)
672 {
673   // assignment operator
674   if(this != &obj){
675     // TODO: proper implementation
676   }
677   return *this;
678 }
679
680 AliJHistManager* AliJHistManager::GlobalManager(){
681     static AliJHistManager* singleton = new AliJHistManager("GlobalHistManager");
682     return singleton;
683 }
684
685 AliJHistManager* AliJHistManager::CurrentManager( AliJHistManager * hmg){
686     static AliJHistManager* currentManager = NULL;//;AliJHistManager::GlobalManager();
687     if( hmg ) currentManager = hmg; 
688     return currentManager;
689 }
690
691 AliJBin* AliJHistManager::GetBuiltBin(TString s ){
692     for( int i=0;i<int(fBin.size());i++ )
693         if( fBin[i]->GetName() == s ) return fBin[i];
694     return NULL;
695 }
696 AliJBin* AliJHistManager::GetBin(TString s ){
697     AliJBin* h = GetBuiltBin(s);
698     if(h) return h;
699     for( int i=0;i<GetNBin();i++ )
700         if( fBinNames[i] == s ){
701             return new AliJBin( fBinConfigs[i],this );
702         }
703     return NULL;
704 }
705 AliJTH1 * AliJHistManager::GetBuiltTH1(TString s ){
706     for( int i=0;i<int(fHist.size());i++ )
707         if( fHist[i]->GetName() == s ) return fHist[i];
708     return NULL;
709 }
710 AliJTH1 * AliJHistManager::GetTH1(TString s ){
711     AliJTH1 * h = GetBuiltTH1(s);
712     if( h ) return h;
713     for( int i=0;i<GetNHist();i++ )
714         if( fHistNames[i] == s ){
715             if( fHistConfigs[i].BeginsWith("AliJTH1D")) return new AliJTH1D( fHistConfigs[i], this );
716             if( fHistConfigs[i].BeginsWith("AliJTH2D")) return new AliJTH2D( fHistConfigs[i], this );
717             if( fHistConfigs[i].BeginsWith("AliJTProfile")) return new AliJTProfile( fHistConfigs[i], this );
718         }
719     return NULL;
720 }
721 void AliJHistManager::Add(AliJBin *o ){
722     if( !o ) return;
723     if( GetBuiltBin( o->GetName() ) ) return; // TODO error handle
724     fBin.push_back( o ); 
725 }
726 void AliJHistManager::Add(AliJTH1 *o ){
727     if( !o ) return;
728     if( GetBuiltTH1( o->GetName() ) ) return; // TODO error handle
729     fHist.push_back( o ); 
730 }
731 void AliJHistManager::Print(){
732     if( IsLoadMode() ) {
733         cout<<fConfigStr<<endl;
734         return;
735     }
736     cout<<"============ AliJHistManager : "<<fName<<" ==================="<<endl;
737     cout<<endl;
738     cout<<"---- AliJBin ----"<<endl;
739     for( int i=0;i<GetNBin();i++ ){
740         fBin[i]->Print();
741     }
742     cout<<endl;
743     cout<<"---- AliJTH1 ----"<<endl;
744     for( int i=0;i<GetNHist();i++ ){
745         fHist[i]->Print();
746     }
747 }
748 void AliJHistManager::Write(){
749     for( int i=0;i<GetNHist();i++ )
750         fHist[i]->Write();
751 }
752
753 void AliJHistManager::WriteConfig(){
754     TDirectory *owd = gDirectory;
755     TDirectory * fHistConfigDir = fDirectory->mkdir("HistManager");
756     fHistConfigDir->cd();
757     TObjString * config = new TObjString(GetString().Data());
758     config->Write("Config");
759     owd->cd();
760 }
761
762 int AliJHistManager::LoadConfig(){
763     SetLoadMode(true);
764     TObjString *strobj = (TObjString*)fDirectory->Get("HistManager/Config");
765     if( !strobj ) return 0; //TODO
766     TString config = strobj->String();
767     fConfigStr = config;
768     vector<TString> lines = Tokenize(config, "\n");
769     cout<< Form("Read Config.%d objects found\n", (int)lines.size() );
770     for( UInt_t i=0;i < lines.size();i++ ){
771         TString line = lines.at(i);
772         std::vector<TString> t = Tokenize(line, " \t,");
773         if(line.BeginsWith("AliJBin")) {
774             fBinNames.push_back( t[1] );
775             fBinConfigs.push_back( line );
776         }else if( line.BeginsWith("AliJ")){
777             fHistNames.push_back( t[1] );
778             fHistConfigs.push_back( line );
779         }
780     }
781     return 1;
782 }
783
784
785 //////////////////////////////////////////////////////
786 //  Utils
787 //////////////////////////////////////////////////////
788 vector<TString> Tokenize( TString s, TString d, int quote ){
789     //int nd = d.Length();
790     bool flagBeforeToken = 0;
791     bool inQuote = 0;
792     TString tok="";
793     vector<TString> toks;
794     s += d[0];
795     for( int i=0;i<s.Length();i++ ){
796         if( quote == 1 && s[i] == '\"' ){ inQuote = ! inQuote; }
797         if( d.First(s[i]) != kNPOS && !inQuote ){
798             if( flagBeforeToken == 0 && tok.Length()>0){
799                 toks.push_back(tok);
800                 tok.Clear();
801                 flagBeforeToken = 1;
802             }
803         }else{
804             tok+=s[i];
805             flagBeforeToken = 0;
806         }
807     }
808     return toks;
809 }
810
811
812 TString Join( vector<TString>& ss , TString del){
813     if( ss.size() < 1 ) return "";
814     TString s = ss[0];
815     for( UInt_t i=1;i<ss.size();i++ ) s+=del+ss[i];
816     return s;
817 }
818
819 bool OutOf( int i, int x, int y ){ return ( i<x || i>y ); }
820 #include <TFile.h>
821
822 void testAliJArray(){
823     cout<<"START"<<endl;
824     AliJHistManager * fHMG;
825     AliJBin fCentBin;
826     AliJBin fVtxBin;
827     AliJBin fPTtBin;
828     AliJBin fPTaBin;
829     AliJBin fXEBin;
830     AliJBin fKLongBin;
831     AliJBin fRGapBin;
832     AliJBin fEtaGapBin;
833     AliJBin fPhiGapBin;
834     AliJBin fMassBin;
835     AliJBin fTypBin;
836     AliJBin fTypBin3;
837     AliJBin fPairPtBin;
838     AliJTH1D fhTriggPtBin;
839     AliJTH1D fhTriggMult;
840     AliJTH1D fhIphiTrigg;
841     AliJTH1D fhIetaTrigg;
842     AliJTH2D test1;
843     AliJTProfile test2;
844
845     TFile * f = new TFile("test.root","RECREATE");
846     fHMG = AliJHistManager::GlobalManager();
847     fCentBin   .Set("Cent",   "C", "C %2.0f-%2.0f%%" ).SetBin( "0 100");
848     fVtxBin    .Set("Vtx",    "V", "Vtx %.0f-%.0f" ).SetBin("-10 10");
849     fPTtBin    .Set("PTt",    "T", "p_{Tt} %.1f-%.1f").SetBin("3 5 8 10 15 20");
850     fPTaBin    .Set("PTa",    "A", "p_{Tt} %.1f-%.1f").SetBin("3 5 8 10 15 20");
851
852     fhTriggMult
853         << TH1D( "hTriggMult", "",  100, -0.5, 99.5) 
854         <<  fCentBin << fPTtBin  << "END";
855     fhIphiTrigg
856         << TH1D( "fhIphiTrigg", "",  3, -0.1, 0.1 ) 
857         <<  fCentBin << fPTtBin  << "END";
858     fhIetaTrigg
859         << TH1D( "hIetaTrigg", "",  80, -5, 5 ) 
860         <<  fCentBin << fPTtBin  << "END";// inclusive eta
861     fhTriggPtBin
862         << TH1D( "hTriggPtBin", "", 10,0,10 )
863         <<  fCentBin << fVtxBin << fPTtBin  << "END";
864
865     fhTriggMult[0][0]->Fill(1);
866     fHMG->Print();
867
868     f->Write();
869     fHMG->Write();
870     fHMG->WriteConfig();
871
872 }