PWGCF/Correlations converted to native cmake
[u/mrichter/AliRoot.git] / PWGCF / Correlations / JCORRAN / AliJHistManager.h
CommitLineData
9dc4f671 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;
25class AliJArrayBase;
26class AliJBin;
27class AliJArrayAlgorithm;
28class AliJArrayAlgorithmSimple;
29class AliJTH1;
30class AliJHistManager;
31template<typename t> class AliJTH1Derived;
32template<typename t> class AliJTH1DerivedPlayer;
33
34//////////////////////////////////////////////////////
35// Utils
36//////////////////////////////////////////////////////
37std::vector<TString> Tokenize( TString s, TString d, int quote=1 );
38TString Join( std::vector<TString>& ss , TString del=" ");
39bool OutOf( int, int, int );
40
41typedef std::vector<int> ArrayInt;
42typedef std::vector<double> ArrayDouble;
43//typedef AliJArray<void*> ArrayVoid*;
44
45//////////////////////////////////////////////////////////////////////////
46// //
47// AliJNamed //
48// //
49//
50// //
51//////////////////////////////////////////////////////////////////////////
52//________________________________________________________________________
53class 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//________________________________________________________________________
86class 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
277b9152 101 int GetBin( double x );
102
9dc4f671 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//________________________________________________________________________
142class 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//________________________________________________________________________
198class 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//________________________________________________________________________
223class 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//________________________________________________________________________
260class 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//////////////////////////////////////////////////////////////////////////
306template< typename T >
307class AliJTH1Derived : public AliJTH1 {
308 protected:
309 public:
0c61c973 310
311 AliJTH1Derived(): AliJTH1(), fPlayer(this) {}
312 virtual ~AliJTH1Derived(){}
313
314
315 /* AliJTH1Derived(); */
9dc4f671 316 AliJTH1Derived(TString config, AliJHistManager *hmg):
317 AliJTH1(config, hmg),fPlayer(this){}
0c61c973 318 /* virtual ~AliJTH1Derived(); */
9dc4f671 319
320 //AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();return fPlayer[i]; }
321 // AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();fPlayer[i];return fPlayer; }
322 AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();fPlayer[i];return fPlayer; }
323 T * operator->(){ return static_cast<T*>(GetSingleItem()); }
324 operator T*(){ return static_cast<T*>(GetSingleItem()); }
325 // Virtual from AliJArrayBase
326
327 // Virtual from AliJTH1
328 virtual const char * ClassName(){ return Form("AliJ%s",T::Class()->GetName()); }
329
330 AliJTH1Derived<T>& operator<<(int i){ AddDim(i);return *this; }
331 AliJTH1Derived<T>& operator<<(AliJBin& v){ AddDim(&v);return *this; }
332 AliJTH1Derived<T>& operator<<(TString v){ AddDim(v);return *this; }
333 AliJTH1Derived<T>& operator<<(T v){ SetTemplate(&v);return *this; }
334 protected:
335 AliJTH1DerivedPlayer<T> fPlayer;
336
337};
338
339
340//////////////////////////////////////////////////////////////////////////
341// AliJTH1DerivedPlayer //
342//////////////////////////////////////////////////////////////////////////
343template< typename T>
344class AliJTH1DerivedPlayer {
345 public:
346 AliJTH1DerivedPlayer( AliJTH1Derived<T> * cmd ):fLevel(0),fCMD(cmd){};
347 AliJTH1DerivedPlayer<T>& operator[](int i){
348 if( fLevel > fCMD->Dimension() ) { JERROR("Exceed Dimension"); }
349 if( OutOf( i, 0, fCMD->SizeOf(fLevel)-1) ){ JERROR(Form("wrong Index %d of %dth in ",i, fLevel)+fCMD->GetName()); }
350 fCMD->SetIndex(i, fLevel++);
351 return *this;
352 }
353 void Init(){ fLevel=0;fCMD->ClearIndex(); }
354 T* operator->(){ return static_cast<T*>(fCMD->GetItem()); }
355 operator T*(){ return static_cast<T*>(fCMD->GetItem()); }
356 operator TObject*(){ return static_cast<TObject*>(fCMD->GetItem()); }
357 operator TH1*(){ return static_cast<TH1*>(fCMD->GetItem()); }
358 private:
359 int fLevel;
360 AliJTH1Derived<T> * fCMD;
361};
362
363typedef AliJTH1Derived<TH1D> AliJTH1D;
364typedef AliJTH1Derived<TH2D> AliJTH2D;
365typedef AliJTH1Derived<TProfile> AliJTProfile;
366
367
368//////////////////////////////////////////////////////////////////////////
369// //
370// AliJHistManager //
371// //
372// Array Base Class //
373// //
374//////////////////////////////////////////////////////////////////////////
375class AliJHistManager: public AliJNamed{
376 public:
377 AliJHistManager(TString name );
378 AliJHistManager(const AliJHistManager& obj);
379 AliJHistManager& operator=(const AliJHistManager& obj);
380 void Add( AliJBin * o );
381 void Add( AliJTH1 * o );
382
383 int GetNBin(){ return fBin.size()>fBinNames.size()?fBin.size():fBinNames.size(); } // TODO
384 int GetNHist(){ return fHist.size()>fHistNames.size()?fHist.size():fHistNames.size(); } // TODO
385 void Print();
386 int LoadConfig();
387 TDirectory * GetDirectory(){ return fDirectory;}
388 void SetDirectory(TDirectory* d){ fDirectory = d; }
389 static AliJHistManager* GlobalManager();
390 static AliJHistManager* CurrentManager( AliJHistManager * hmg=NULL);
5dbedd98 391 AliJHistManager * cd(){ return AliJHistManager::CurrentManager(this); }
9dc4f671 392 void SetLoadMode(bool b=true){ fIsLoadMode = b; }
393 bool IsLoadMode(){ return fIsLoadMode; }
394 TString GetString(){
395 TString st;
396 for( int i=0;i<GetNBin();i++ ) st+=fBin[i]->GetString()+"\n";
397 for( int i=0;i<GetNHist();i++ ){
398 st+=fHist[i]->GetString()+"\n";
399 }
400 return st;
401 }
402 void Write();
403 void WriteConfig();
404
405 AliJBin * GetBin( TString name);
406 AliJBin * GetBuiltBin( TString name);
407 AliJTH1 * GetTH1( TString name );
408 AliJTH1 * GetBuiltTH1( TString name );
409 AliJTProfile& GetTProfile( TString name){ return dynamic_cast<AliJTProfile&>(*GetTH1(name)); }
410 AliJTH1D& GetTH1D( TString name){ return dynamic_cast<AliJTH1D&>(*GetTH1(name)); }
411 AliJTH2D& GetTH2D( TString name){ return dynamic_cast<AliJTH2D&>(*GetTH1(name)); }
412 bool fIsLoadMode;
9dc4f671 413
414 TString GetHistName(int i){ return fHistNames[i]; }
415
416 AliJTH1 * GetAliJTH1(int i){ return GetTH1(fHistNames[i]); }
417 int GetNAliJTH1(){ return fHistNames.size(); }
418
419
5dbedd98 420 TDirectory * fDirectory; // TODO move to private?
421
9dc4f671 422 private:
423 TString fConfigStr;
424 std::vector<AliJBin*> fBin;
425 std::vector<AliJTH1*> fHist;
426 std::vector<AliJHistManager*> fManager;
427 std::vector<TString> fBinNames;
428 std::vector<TString> fBinConfigs;
429 std::vector<TString> fHistNames;
430 std::vector<TString> fHistConfigs;
431};
432#endif