]>
Commit | Line | Data |
---|---|---|
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; | |
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 | double GetMin(){ return fBinD[0]; } | |
102 | double GetMax(){ return fBinD[RawSize()-1]; } | |
103 | ||
104 | TString BuildTitle( int i ); | |
105 | int RawSize(){ return fBinD.size(); } | |
106 | int Size(){ return fMode==kRange?fBinD.size()-1:fBinD.size(); } | |
107 | double At(int i){ return fBinD[i]; } | |
108 | TString GetIndexName(){ return fIndexName; } | |
109 | ||
110 | void Print(); | |
111 | TString GetString(); | |
112 | ||
113 | operator int(){ return Size(); } | |
114 | ||
115 | static TString GetModeString(int i); | |
116 | static int GetMode( TString mode ); | |
117 | private: | |
118 | void AddBin( const TString & v ); | |
119 | void AddBin( float v ); | |
120 | virtual void FixBin(); | |
121 | ||
122 | std::vector<double> fBinD; | |
123 | std::vector<TString> fBinStr; | |
124 | bool fIsFixedBin; | |
125 | TString fIndexName; | |
126 | AliJHistManager * fHMG; | |
127 | }; | |
128 | ||
129 | ||
130 | ||
131 | ////////////////////////////////////////////////////////////////////////// | |
132 | // // | |
133 | // AliJArrayBase // | |
134 | // // | |
135 | // Array Base Class // | |
136 | // // | |
137 | ////////////////////////////////////////////////////////////////////////// | |
138 | ||
139 | //________________________________________________________________________ | |
140 | class AliJArrayBase : public AliJNamed{ | |
141 | public: | |
142 | enum { kNormal, kSingle }; | |
143 | virtual ~AliJArrayBase(); | |
144 | AliJArrayBase& operator=(const AliJArrayBase& obj); | |
145 | ||
146 | int AddDim( int i){ fDim.push_back(i);return fDim.size();} | |
147 | int Dimension(){ return fDim.size(); } | |
148 | int GetEntries(){ return fArraySize; } | |
149 | int SizeOf(int i) { return fDim.at(i); } | |
150 | ||
151 | ArrayInt& Index(){ return fIndex; } | |
152 | int Index( int d ); | |
153 | void SetIndex( int i, int d ); | |
154 | void ClearIndex(){ fIndex.clear();fIndex.resize( Dimension(), 0 ); } | |
155 | ||
156 | void * GetItem(); | |
157 | void * GetSingleItem(); | |
158 | ||
159 | ///void LockBin(bool is=true){}//TODO | |
160 | //bool IsBinLocked(){ return fIsBinLocked; } | |
161 | ||
162 | virtual void FixBin(); | |
163 | bool IsBinFixed(){ return fIsBinFixed; } | |
164 | ||
165 | bool OutOfDim( int d ){ return OutOf( d, 0, Dimension()-1 ); } | |
166 | bool OutOfSize( int i, int d ){ return OutOfDim(d) || OutOf( i, 0, SizeOf(d)-1); } | |
167 | ||
168 | ||
169 | // Virtual | |
170 | virtual void * BuildItem()=0; | |
171 | virtual TString BuildName()=0; | |
172 | virtual TString BuildTitle()=0; | |
173 | virtual void Print()=0; | |
174 | virtual TString GetString()=0; | |
175 | ||
176 | //int Resize( int size, int dim=-1 ); // NextStep | |
177 | void InitIterator(); | |
178 | bool Next(void *& item ); | |
179 | ||
180 | protected: | |
181 | AliJArrayBase(); // Prevent direct creation of AliJArrayBase | |
182 | AliJArrayBase(const AliJArrayBase& obj); | |
183 | ||
184 | ArrayInt fDim; // Comment test | |
185 | ArrayInt fIndex; /// Comment test | |
186 | int fArraySize; /// Comment test3 | |
187 | int fNGenerated; | |
188 | bool fIsBinFixed; | |
189 | bool fIsBinLocked; | |
190 | AliJArrayAlgorithm * fAlg; | |
191 | friend class AliJArrayAlgorithm; | |
192 | }; | |
193 | ||
194 | ||
195 | //________________________________________________________________________ | |
196 | class AliJArrayAlgorithm { | |
197 | public: | |
198 | AliJArrayAlgorithm(AliJArrayBase * cmd); //TODO Move to private | |
199 | AliJArrayAlgorithm(const AliJArrayAlgorithm& obj); | |
200 | AliJArrayAlgorithm& operator=(const AliJArrayAlgorithm& obj); | |
201 | virtual ~AliJArrayAlgorithm(); | |
202 | int Dimension(){ return fCMD->Dimension(); } | |
203 | int SizeOf(int i){ return fCMD->SizeOf(i); } | |
204 | int GetEntries(){ return fCMD->GetEntries(); } | |
205 | int Index(int i){ return fCMD->Index(i); } | |
206 | virtual int BuildArray()=0; | |
207 | virtual void * GetItem()=0; | |
208 | virtual void SetItem(void * item)=0; | |
209 | virtual void InitIterator()=0; | |
210 | virtual bool Next(void *& item) = 0; | |
211 | virtual void ** GetRawItem()=0; | |
212 | virtual void * GetPosition()=0; | |
213 | virtual bool IsCurrentPosition(void * pos)=0; | |
214 | virtual void SetPosition(void * pos )=0; | |
215 | virtual void DeletePosition( void * pos ) =0; | |
216 | protected: | |
217 | AliJArrayBase * fCMD; | |
218 | }; | |
219 | ||
220 | //________________________________________________________________________ | |
221 | class AliJArrayAlgorithmSimple : public AliJArrayAlgorithm { | |
222 | public: | |
223 | AliJArrayAlgorithmSimple( AliJArrayBase * cmd); | |
224 | AliJArrayAlgorithmSimple(const AliJArrayAlgorithmSimple& obj); | |
225 | AliJArrayAlgorithmSimple& operator=(const AliJArrayAlgorithmSimple& obj); | |
226 | virtual ~AliJArrayAlgorithmSimple(); | |
227 | virtual int BuildArray(); | |
228 | int GlobalIndex(); | |
229 | void ReverseIndex(int iG ); | |
230 | virtual void * GetItem(); | |
231 | virtual void SetItem(void * item); | |
232 | virtual void InitIterator(){ fPos = 0; } | |
233 | virtual void ** GetRawItem(){ return &fArray[GlobalIndex()]; } | |
234 | virtual bool Next(void *& item){ | |
235 | item = fPos<GetEntries()?(void*)fArray[fPos]:NULL; | |
236 | if( fPos<GetEntries() ) ReverseIndex(fPos); | |
237 | return fPos++<GetEntries(); | |
238 | } | |
239 | virtual void * GetPosition(){ return static_cast<void*> (new int( fPos )); } | |
240 | virtual bool IsCurrentPosition(void * pos){ return *static_cast<int*>(pos)==fPos; } | |
241 | virtual void SetPosition(void *pos){ fPos=*static_cast<int*>(pos);ReverseIndex(fPos); } | |
242 | virtual void DeletePosition(void *pos){ delete static_cast<int*>(pos); } | |
243 | private: | |
244 | ArrayInt fDimFactor; | |
245 | void **fArray; | |
246 | int fPos; | |
247 | }; | |
248 | ||
249 | ||
250 | ////////////////////////////////////////////////////////////////////////// | |
251 | // // | |
252 | // AliJTH1 // | |
253 | // // | |
254 | // Array Base Class // | |
255 | // // | |
256 | ////////////////////////////////////////////////////////////////////////// | |
257 | //________________________________________________________________________ | |
258 | class AliJTH1 : public AliJArrayBase{ | |
259 | public: | |
260 | AliJTH1(); | |
261 | AliJTH1(TString config, AliJHistManager * hmg); | |
262 | AliJTH1(const AliJTH1& obj); | |
263 | AliJTH1& operator=(const AliJTH1& obj); | |
264 | virtual ~AliJTH1(); | |
265 | ||
266 | int AddDim( int i){ fDim.push_back(i);return fDim.size();} | |
267 | int AddDim( AliJBin * bin ); | |
268 | int AddDim( TString v); | |
269 | void AddToManager( AliJHistManager * hmg ); | |
270 | ||
271 | // Virtual from AliJArrayBase | |
272 | virtual void * BuildItem() ; | |
273 | virtual TString GetString(); | |
274 | virtual void Print(); | |
275 | virtual void FixBin(); | |
276 | // Virtual from this | |
277 | virtual Int_t Write(); | |
278 | //virtual Int_t WriteAll(); | |
279 | virtual const char * ClassName(){ return "AliJTH1"; } | |
280 | ||
281 | // Not Virtual | |
282 | virtual TString BuildName(); | |
283 | virtual TString BuildTitle(); | |
284 | bool IsLoadMode(); | |
285 | void SetTemplate(TH1* h); | |
286 | ||
287 | ||
288 | ||
289 | protected: | |
290 | ||
291 | TDirectory *fDirectory; | |
292 | TDirectory *fSubDirectory; | |
293 | AliJHistManager *fHMG; | |
294 | TH1 *fTemplate; | |
295 | std::vector<AliJBin*> fBins; | |
296 | }; | |
297 | ////////////////////////////////////////////////////////////////////////// | |
298 | // // | |
299 | // AliJTH1Derived // | |
300 | // // | |
301 | // Array Base Class // | |
302 | // // | |
303 | ////////////////////////////////////////////////////////////////////////// | |
304 | template< typename T > | |
305 | class AliJTH1Derived : public AliJTH1 { | |
306 | protected: | |
307 | public: | |
308 | AliJTH1Derived(); | |
309 | AliJTH1Derived(TString config, AliJHistManager *hmg): | |
310 | AliJTH1(config, hmg),fPlayer(this){} | |
311 | virtual ~AliJTH1Derived(); | |
312 | ||
313 | //AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();return fPlayer[i]; } | |
314 | // AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();fPlayer[i];return fPlayer; } | |
315 | AliJTH1DerivedPlayer<T> & operator[](int i){ fPlayer.Init();fPlayer[i];return fPlayer; } | |
316 | T * operator->(){ return static_cast<T*>(GetSingleItem()); } | |
317 | operator T*(){ return static_cast<T*>(GetSingleItem()); } | |
318 | // Virtual from AliJArrayBase | |
319 | ||
320 | // Virtual from AliJTH1 | |
321 | virtual const char * ClassName(){ return Form("AliJ%s",T::Class()->GetName()); } | |
322 | ||
323 | AliJTH1Derived<T>& operator<<(int i){ AddDim(i);return *this; } | |
324 | AliJTH1Derived<T>& operator<<(AliJBin& v){ AddDim(&v);return *this; } | |
325 | AliJTH1Derived<T>& operator<<(TString v){ AddDim(v);return *this; } | |
326 | AliJTH1Derived<T>& operator<<(T v){ SetTemplate(&v);return *this; } | |
327 | protected: | |
328 | AliJTH1DerivedPlayer<T> fPlayer; | |
329 | ||
330 | }; | |
331 | ||
332 | ||
333 | ////////////////////////////////////////////////////////////////////////// | |
334 | // AliJTH1DerivedPlayer // | |
335 | ////////////////////////////////////////////////////////////////////////// | |
336 | template< typename T> | |
337 | class AliJTH1DerivedPlayer { | |
338 | public: | |
339 | AliJTH1DerivedPlayer( AliJTH1Derived<T> * cmd ):fLevel(0),fCMD(cmd){}; | |
340 | AliJTH1DerivedPlayer<T>& operator[](int i){ | |
341 | if( fLevel > fCMD->Dimension() ) { JERROR("Exceed Dimension"); } | |
342 | if( OutOf( i, 0, fCMD->SizeOf(fLevel)-1) ){ JERROR(Form("wrong Index %d of %dth in ",i, fLevel)+fCMD->GetName()); } | |
343 | fCMD->SetIndex(i, fLevel++); | |
344 | return *this; | |
345 | } | |
346 | void Init(){ fLevel=0;fCMD->ClearIndex(); } | |
347 | T* operator->(){ return static_cast<T*>(fCMD->GetItem()); } | |
348 | operator T*(){ return static_cast<T*>(fCMD->GetItem()); } | |
349 | operator TObject*(){ return static_cast<TObject*>(fCMD->GetItem()); } | |
350 | operator TH1*(){ return static_cast<TH1*>(fCMD->GetItem()); } | |
351 | private: | |
352 | int fLevel; | |
353 | AliJTH1Derived<T> * fCMD; | |
354 | }; | |
355 | ||
356 | typedef AliJTH1Derived<TH1D> AliJTH1D; | |
357 | typedef AliJTH1Derived<TH2D> AliJTH2D; | |
358 | typedef AliJTH1Derived<TProfile> AliJTProfile; | |
359 | ||
360 | ||
361 | ////////////////////////////////////////////////////////////////////////// | |
362 | // // | |
363 | // AliJHistManager // | |
364 | // // | |
365 | // Array Base Class // | |
366 | // // | |
367 | ////////////////////////////////////////////////////////////////////////// | |
368 | class AliJHistManager: public AliJNamed{ | |
369 | public: | |
370 | AliJHistManager(TString name ); | |
371 | AliJHistManager(const AliJHistManager& obj); | |
372 | AliJHistManager& operator=(const AliJHistManager& obj); | |
373 | void Add( AliJBin * o ); | |
374 | void Add( AliJTH1 * o ); | |
375 | ||
376 | int GetNBin(){ return fBin.size()>fBinNames.size()?fBin.size():fBinNames.size(); } // TODO | |
377 | int GetNHist(){ return fHist.size()>fHistNames.size()?fHist.size():fHistNames.size(); } // TODO | |
378 | void Print(); | |
379 | int LoadConfig(); | |
380 | TDirectory * GetDirectory(){ return fDirectory;} | |
381 | void SetDirectory(TDirectory* d){ fDirectory = d; } | |
382 | static AliJHistManager* GlobalManager(); | |
383 | static AliJHistManager* CurrentManager( AliJHistManager * hmg=NULL); | |
5dbedd98 | 384 | AliJHistManager * cd(){ return AliJHistManager::CurrentManager(this); } |
9dc4f671 | 385 | void SetLoadMode(bool b=true){ fIsLoadMode = b; } |
386 | bool IsLoadMode(){ return fIsLoadMode; } | |
387 | TString GetString(){ | |
388 | TString st; | |
389 | for( int i=0;i<GetNBin();i++ ) st+=fBin[i]->GetString()+"\n"; | |
390 | for( int i=0;i<GetNHist();i++ ){ | |
391 | st+=fHist[i]->GetString()+"\n"; | |
392 | } | |
393 | return st; | |
394 | } | |
395 | void Write(); | |
396 | void WriteConfig(); | |
397 | ||
398 | AliJBin * GetBin( TString name); | |
399 | AliJBin * GetBuiltBin( TString name); | |
400 | AliJTH1 * GetTH1( TString name ); | |
401 | AliJTH1 * GetBuiltTH1( TString name ); | |
402 | AliJTProfile& GetTProfile( TString name){ return dynamic_cast<AliJTProfile&>(*GetTH1(name)); } | |
403 | AliJTH1D& GetTH1D( TString name){ return dynamic_cast<AliJTH1D&>(*GetTH1(name)); } | |
404 | AliJTH2D& GetTH2D( TString name){ return dynamic_cast<AliJTH2D&>(*GetTH1(name)); } | |
405 | bool fIsLoadMode; | |
9dc4f671 | 406 | |
407 | TString GetHistName(int i){ return fHistNames[i]; } | |
408 | ||
409 | AliJTH1 * GetAliJTH1(int i){ return GetTH1(fHistNames[i]); } | |
410 | int GetNAliJTH1(){ return fHistNames.size(); } | |
411 | ||
412 | ||
5dbedd98 | 413 | TDirectory * fDirectory; // TODO move to private? |
414 | ||
9dc4f671 | 415 | private: |
416 | TString fConfigStr; | |
417 | std::vector<AliJBin*> fBin; | |
418 | std::vector<AliJTH1*> fHist; | |
419 | std::vector<AliJHistManager*> fManager; | |
420 | std::vector<TString> fBinNames; | |
421 | std::vector<TString> fBinConfigs; | |
422 | std::vector<TString> fHistNames; | |
423 | std::vector<TString> fHistConfigs; | |
424 | }; | |
425 | #endif |