40448a98b51531e80aebcffdf2ca07995a0b3ad9
[u/mrichter/AliRoot.git] / PWGCF / Correlations / JCORRAN / AliJHistManager.h
1 // @(#)utils:$Id$
2 // Author: Beonsu Chang   26/12/94
3 #ifndef UTILS_ALIJARRAY_H
4 #define UTILS_ALIJARRAY_H
5
6 #include <vector>
7 #include <map>
8 #include <TString.h>
9 #include <TDirectory.h>
10 #include <TH1D.h>
11 #include <TH2D.h>
12 #include <TProfile.h>
13 #include <TPRegexp.h>
14 #include <TVector.h>
15 #include <TSystem.h>
16 #include <TROOT.h>
17 #include <TObjString.h>
18 #include <iostream>
19 #include <TClass.h>
20
21 #define JERROR(x)  {std::cout<<"!!! JERROR : "<<x<<" "<<__LINE__<<" "<<__FILE__<<" "<<std::endl , gSystem->Exit(100); }
22 #define JDEBUG(x,y)  if(x<100){std::cout<<"JDEBUG : "<<#x<<" : "<<(y)<<" "<<__LINE__<<" "<<__FILE__<<" "<<std::endl;}
23
24 //class AliJMap;
25 class AliJArrayBase;
26 class AliJBin;
27 class AliJArrayAlgorithm;
28 class AliJArrayAlgorithmSimple;
29 class AliJTH1;
30 class AliJHistManager;
31 template<typename t> class AliJTH1Derived;
32 template<typename t> class AliJTH1DerivedPlayer;
33
34 //////////////////////////////////////////////////////
35 //  Utils
36 //////////////////////////////////////////////////////
37 std::vector<TString> Tokenize( TString s, TString d, int quote=1 );
38 TString Join( std::vector<TString>& ss , TString del=" ");
39 bool OutOf( int, int, int );
40
41 typedef std::vector<int> ArrayInt;
42 typedef std::vector<double> ArrayDouble;
43 //typedef AliJArray<void*> ArrayVoid*;
44
45 //////////////////////////////////////////////////////////////////////////
46 //                                                                      //
47 // AliJNamed                                                              //
48 //                                                                      //
49 // 
50 //                                                                      //
51 //////////////////////////////////////////////////////////////////////////
52 //________________________________________________________________________
53 class AliJNamed{
54     public:
55         AliJNamed( TString name, TString title, TString opt, int mode );
56         virtual ~AliJNamed();
57         TString GetName(){ return fName; }
58         TString GetTitle(){ return fTitle; }
59         TString GetOption(){ return fOption; }
60         TString GetOption(TString key);
61         bool    HasOption(TString key){ return GetOption(key)!=UndefinedOption(); }
62         bool    HasOption(TString key, TString val){ return GetOption(key)==val; } // TODO sensitive?
63         void SetName( const char * s ) { fName=s; }
64         void SetTitle( const char * s ) { fTitle=s; }
65         void SetNameTitle( TString n, TString t ){ SetName(n);SetTitle(t); }
66         void SetFullOption( const char * s ) { fOption=s; }
67         void SetOption( TString key, TString value="" );
68         void RemoveOption( TString key ); // TODO
69         //void SetOptionWithString( TString s );// TODO
70         static TString UndefinedOption();
71     protected:
72         TString  fName;
73         TString  fTitle;
74         TString  fOption;
75         int      fMode;
76 };
77
78 //////////////////////////////////////////////////////////////////////////
79 //                                                                      //
80 // AliJBin                                                              //
81 //                                                                      //
82 // 
83 //                                                                      //
84 //////////////////////////////////////////////////////////////////////////
85 //________________________________________________________________________
86 class AliJBin : public AliJNamed {
87     public:
88         enum { kSingle, kRange, kString , kNMode};
89         AliJBin();
90         AliJBin(TString config, AliJHistManager * hmg);
91         AliJBin(const AliJBin& obj);
92         AliJBin& operator=(const AliJBin& obj);
93         AliJBin & Set( TString name, TString iname, TString Title, int mode=kRange);
94         void      AddToManager( AliJHistManager* hmg );
95         AliJBin & SetBin( const int n, const float * v );
96         AliJBin & SetBin( const int n, const double * v );
97         AliJBin & SetBin( TVector * v );
98         AliJBin & SetBin( const TString  v );
99         AliJBin & SetBin( const int n );
100
101         int GetBin( double x );
102
103         double GetMin(){ return fBinD[0]; }
104         double GetMax(){ return fBinD[RawSize()-1]; }
105
106         TString BuildTitle( int i );
107         int RawSize(){ return fBinD.size(); }
108         int Size(){ return fMode==kRange?fBinD.size()-1:fBinD.size(); }
109         double At(int i){ return fBinD[i]; }
110         TString GetIndexName(){ return fIndexName; }
111
112         void Print();
113         TString GetString();
114
115         operator int(){ return Size(); }
116
117         static TString GetModeString(int i);
118         static int GetMode( TString mode );
119     private:
120         void AddBin( const TString & v );
121         void AddBin( float v );
122         virtual void FixBin();
123
124         std::vector<double>  fBinD;
125         std::vector<TString> fBinStr;
126         bool    fIsFixedBin;
127         TString fIndexName;
128         AliJHistManager * fHMG;
129 };
130
131
132
133 //////////////////////////////////////////////////////////////////////////
134 //                                                                      //
135 // AliJArrayBase                                                        //
136 //                                                                      //
137 // Array Base Class                                                     //
138 //                                                                      //
139 //////////////////////////////////////////////////////////////////////////
140
141 //________________________________________________________________________
142 class AliJArrayBase : public AliJNamed{
143     public:
144         enum { kNormal, kSingle };
145         virtual ~AliJArrayBase();
146         AliJArrayBase& operator=(const AliJArrayBase& obj);
147
148         int AddDim( int i){ fDim.push_back(i);return fDim.size();}
149         int Dimension(){ return fDim.size(); }
150         int GetEntries(){ return fArraySize; }
151         int SizeOf(int i) { return fDim.at(i); }
152
153         ArrayInt& Index(){ return fIndex; }
154         int  Index( int d );
155         void SetIndex( int i, int d );
156         void ClearIndex(){ fIndex.clear();fIndex.resize( Dimension(), 0 ); }
157
158         void * GetItem();
159         void * GetSingleItem();
160
161         ///void LockBin(bool is=true){}//TODO
162         //bool IsBinLocked(){ return fIsBinLocked; }
163
164         virtual void FixBin();
165         bool IsBinFixed(){ return fIsBinFixed; }
166
167         bool OutOfDim( int d ){ return OutOf( d, 0, Dimension()-1 ); }
168         bool OutOfSize( int i, int d ){ return OutOfDim(d) || OutOf( i, 0, SizeOf(d)-1); }
169
170
171         // Virtual 
172         virtual void *  BuildItem()=0;
173         virtual TString BuildName()=0;
174         virtual TString BuildTitle()=0;
175         virtual void  Print()=0;
176         virtual TString GetString()=0;
177
178         //int Resize( int size, int dim=-1 ); // NextStep 
179         void    InitIterator();
180         bool    Next(void *& item ); 
181
182     protected:
183         AliJArrayBase(); // Prevent direct creation of AliJArrayBase
184         AliJArrayBase(const AliJArrayBase& obj);
185
186         ArrayInt        fDim;           // Comment test
187         ArrayInt        fIndex;         /// Comment test
188         int         fArraySize;         /// Comment test3
189         int         fNGenerated;
190         bool        fIsBinFixed;
191         bool        fIsBinLocked;
192         AliJArrayAlgorithm * fAlg;
193         friend class AliJArrayAlgorithm;
194 };
195
196
197 //________________________________________________________________________
198 class AliJArrayAlgorithm {
199     public:
200         AliJArrayAlgorithm(AliJArrayBase * cmd); //TODO Move to private
201         AliJArrayAlgorithm(const AliJArrayAlgorithm& obj);
202         AliJArrayAlgorithm& operator=(const AliJArrayAlgorithm& obj);
203         virtual ~AliJArrayAlgorithm();
204         int Dimension(){ return fCMD->Dimension(); }
205         int SizeOf(int i){ return fCMD->SizeOf(i); }
206         int GetEntries(){ return fCMD->GetEntries(); }
207         int Index(int i){ return fCMD->Index(i); }
208         virtual int BuildArray()=0;
209         virtual void * GetItem()=0;
210         virtual void SetItem(void * item)=0;
211         virtual void InitIterator()=0;
212         virtual bool Next(void *& item) = 0;
213         virtual void ** GetRawItem()=0;
214         virtual void * GetPosition()=0;
215         virtual bool IsCurrentPosition(void * pos)=0;
216         virtual void SetPosition(void * pos )=0;
217         virtual void DeletePosition( void * pos ) =0;
218     protected:
219         AliJArrayBase * fCMD;
220 };
221
222 //________________________________________________________________________
223 class AliJArrayAlgorithmSimple : public AliJArrayAlgorithm {
224     public:
225         AliJArrayAlgorithmSimple( AliJArrayBase * cmd);
226         AliJArrayAlgorithmSimple(const AliJArrayAlgorithmSimple& obj);
227         AliJArrayAlgorithmSimple& operator=(const AliJArrayAlgorithmSimple& obj);
228         virtual ~AliJArrayAlgorithmSimple();
229         virtual int BuildArray();
230         int  GlobalIndex();
231         void ReverseIndex(int iG );
232         virtual void * GetItem();
233         virtual void SetItem(void * item);
234         virtual void InitIterator(){ fPos = 0; }
235         virtual void ** GetRawItem(){ return &fArray[GlobalIndex()]; }
236         virtual bool Next(void *& item){
237             item = fPos<GetEntries()?(void*)fArray[fPos]:NULL;
238             if( fPos<GetEntries() ) ReverseIndex(fPos);
239             return fPos++<GetEntries();
240         }
241         virtual void * GetPosition(){ return  static_cast<void*> (new int( fPos )); }
242         virtual bool IsCurrentPosition(void * pos){ return *static_cast<int*>(pos)==fPos; }
243         virtual void  SetPosition(void *pos){ fPos=*static_cast<int*>(pos);ReverseIndex(fPos); } 
244         virtual void  DeletePosition(void *pos){ delete static_cast<int*>(pos); }
245     private:
246         ArrayInt    fDimFactor;
247         void    **fArray;
248         int     fPos;
249 };
250
251
252 //////////////////////////////////////////////////////////////////////////
253 //                                                                      //
254 // AliJTH1                                                              //
255 //                                                                      //
256 // Array Base Class                                                     //
257 //                                                                      //
258 //////////////////////////////////////////////////////////////////////////
259 //________________________________________________________________________
260 class AliJTH1 : public AliJArrayBase{
261     public:
262         AliJTH1();
263         AliJTH1(TString config, AliJHistManager * hmg);
264         AliJTH1(const AliJTH1& obj);
265         AliJTH1& operator=(const AliJTH1& obj);
266         virtual ~AliJTH1();
267
268         int AddDim( int i){ fDim.push_back(i);return fDim.size();}
269         int AddDim( AliJBin * bin );
270         int AddDim( TString v);
271         void AddToManager( AliJHistManager * hmg );
272
273         // Virtual from AliJArrayBase
274         virtual void * BuildItem() ;
275         virtual TString GetString();
276         virtual void  Print();
277         virtual void  FixBin();
278         // Virtual from this
279         virtual Int_t Write();
280         //virtual Int_t WriteAll();
281         virtual const char * ClassName(){ return "AliJTH1"; }
282
283         // Not Virtual
284         virtual TString BuildName();
285         virtual TString BuildTitle();
286         bool    IsLoadMode(); 
287         void    SetTemplate(TH1* h);
288
289
290
291     protected:
292
293         TDirectory      *fDirectory;
294         TDirectory      *fSubDirectory;
295         AliJHistManager *fHMG;
296         TH1             *fTemplate;
297         std::vector<AliJBin*>  fBins;
298 };
299 //////////////////////////////////////////////////////////////////////////
300 //                                                                      //
301 // AliJTH1Derived                                                       //
302 //                                                                      //
303 // Array Base Class                                                     //
304 //                                                                      //
305 //////////////////////////////////////////////////////////////////////////
306 template< typename T >
307 class AliJTH1Derived : public AliJTH1 {
308     protected:
309     public:
310         AliJTH1Derived();
311         AliJTH1Derived(TString config, AliJHistManager *hmg):
312             AliJTH1(config, hmg),fPlayer(this){}
313         virtual ~AliJTH1Derived();
314
315         //AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();return fPlayer[i]; }
316    //     AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();fPlayer[i];return fPlayer; }
317         AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();fPlayer[i];return fPlayer; }
318         T * operator->(){ return static_cast<T*>(GetSingleItem()); }
319         operator T*(){ return static_cast<T*>(GetSingleItem()); }
320         // Virtual from AliJArrayBase
321
322         // Virtual from AliJTH1
323         virtual const char * ClassName(){ return Form("AliJ%s",T::Class()->GetName()); }
324
325         AliJTH1Derived<T>& operator<<(int i){ AddDim(i);return *this; }
326         AliJTH1Derived<T>& operator<<(AliJBin& v){ AddDim(&v);return *this; }
327         AliJTH1Derived<T>& operator<<(TString v){ AddDim(v);return *this; }
328         AliJTH1Derived<T>& operator<<(T v){ SetTemplate(&v);return *this; }
329     protected:
330         AliJTH1DerivedPlayer<T> fPlayer;
331
332 };
333
334
335 //////////////////////////////////////////////////////////////////////////
336 // AliJTH1DerivedPlayer                                                 //
337 //////////////////////////////////////////////////////////////////////////
338 template< typename T>
339 class AliJTH1DerivedPlayer {
340     public:
341         AliJTH1DerivedPlayer( AliJTH1Derived<T> * cmd ):fLevel(0),fCMD(cmd){};
342         AliJTH1DerivedPlayer<T>& operator[](int i){
343             if( fLevel > fCMD->Dimension() ) { JERROR("Exceed Dimension"); }
344             if( OutOf( i, 0,  fCMD->SizeOf(fLevel)-1) ){ JERROR(Form("wrong Index %d of %dth in ",i, fLevel)+fCMD->GetName()); }
345             fCMD->SetIndex(i, fLevel++);
346             return *this;
347         }
348         void Init(){ fLevel=0;fCMD->ClearIndex(); }
349         T* operator->(){ return static_cast<T*>(fCMD->GetItem()); } 
350         operator T*(){ return static_cast<T*>(fCMD->GetItem()); } 
351         operator TObject*(){ return static_cast<TObject*>(fCMD->GetItem()); } 
352         operator TH1*(){ return static_cast<TH1*>(fCMD->GetItem()); } 
353     private:
354         int fLevel;
355         AliJTH1Derived<T> * fCMD;
356 };
357
358 typedef AliJTH1Derived<TH1D> AliJTH1D;
359 typedef AliJTH1Derived<TH2D> AliJTH2D;
360 typedef AliJTH1Derived<TProfile> AliJTProfile;
361
362
363 //////////////////////////////////////////////////////////////////////////
364 //                                                                      //
365 // AliJHistManager                                                       //
366 //                                                                      //
367 // Array Base Class                                                     //
368 //                                                                      //
369 //////////////////////////////////////////////////////////////////////////
370 class AliJHistManager: public AliJNamed{
371     public:
372         AliJHistManager(TString name );
373         AliJHistManager(const AliJHistManager& obj);
374         AliJHistManager& operator=(const AliJHistManager& obj);
375         void Add( AliJBin * o );
376         void Add( AliJTH1 * o );
377
378         int GetNBin(){ return fBin.size()>fBinNames.size()?fBin.size():fBinNames.size(); } // TODO
379         int GetNHist(){ return fHist.size()>fHistNames.size()?fHist.size():fHistNames.size(); } // TODO
380         void Print();
381         int  LoadConfig();
382         TDirectory * GetDirectory(){ return fDirectory;} 
383         void SetDirectory(TDirectory* d){ fDirectory = d; }
384         static AliJHistManager* GlobalManager();
385         static AliJHistManager* CurrentManager( AliJHistManager * hmg=NULL);
386         AliJHistManager * cd(){ return AliJHistManager::CurrentManager(this); }
387         void SetLoadMode(bool b=true){ fIsLoadMode = b; }
388         bool IsLoadMode(){ return fIsLoadMode; }
389         TString GetString(){
390             TString st;
391             for( int i=0;i<GetNBin();i++ ) st+=fBin[i]->GetString()+"\n";
392             for( int i=0;i<GetNHist();i++ ){
393                 st+=fHist[i]->GetString()+"\n";
394             }
395             return st;
396         }
397         void Write();
398         void WriteConfig();
399
400         AliJBin * GetBin( TString name); 
401         AliJBin * GetBuiltBin( TString name); 
402         AliJTH1 * GetTH1( TString name );
403         AliJTH1 * GetBuiltTH1( TString name );
404         AliJTProfile& GetTProfile( TString name){ return dynamic_cast<AliJTProfile&>(*GetTH1(name)); }
405         AliJTH1D& GetTH1D( TString name){ return dynamic_cast<AliJTH1D&>(*GetTH1(name)); }
406         AliJTH2D& GetTH2D( TString name){ return dynamic_cast<AliJTH2D&>(*GetTH1(name)); }
407         bool fIsLoadMode;
408
409         TString GetHistName(int i){ return fHistNames[i]; }
410
411         AliJTH1 * GetAliJTH1(int i){ return GetTH1(fHistNames[i]); }
412         int GetNAliJTH1(){ return fHistNames.size(); }
413
414
415         TDirectory * fDirectory; // TODO move to private?
416
417     private:
418         TString                     fConfigStr;
419         std::vector<AliJBin*>       fBin;
420         std::vector<AliJTH1*>  fHist;
421         std::vector<AliJHistManager*> fManager;
422         std::vector<TString>        fBinNames;
423         std::vector<TString>        fBinConfigs;
424         std::vector<TString>        fHistNames;
425         std::vector<TString>        fHistConfigs;
426 };
427 #endif