]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Reve/Reve.cxx
c39c4ea9aa8b1290a3e7eedbca629e508f0d0944
[u/mrichter/AliRoot.git] / EVE / Reve / Reve.cxx
1 // $Header$
2
3 #include "Reve.h"
4
5 #include <TError.h>
6 #include <TPad.h>
7 #include <TGeoManager.h>
8
9 #include <TStyle.h>
10 #include <TColor.h>
11
12 #include <TROOT.h>
13 #include <TInterpreter.h>
14
15 #include <list>
16 #include <string>
17 #include <iostream>
18
19 //______________________________________________________________________
20 // Reve
21 //
22
23 /**************************************************************************/
24 /**************************************************************************/
25 namespace Reve {
26 /**************************************************************************/
27
28
29 // TString .vs. string
30
31 bool operator==(const TString& t, const std::string& s)
32 { return (s == t.Data()); }
33
34 bool operator==(const std::string&  s, const TString& t)
35 { return (s == t.Data()); }
36
37 // Exc
38
39 Exc_t::Exc_t(const std::string& s) : TString(s.c_str()) {}
40
41 // Exc + ops
42
43 Exc_t operator+(const Exc_t &s1, const std::string &s2)
44 { return Exc_t((TString&)s1 + s2.c_str()); }
45
46 Exc_t operator+(const Exc_t &s1, const TString &s2)
47 { return Exc_t((TString&)s1 + s2); }
48
49 Exc_t operator+(const Exc_t &s1,  const char *s2)
50 { return Exc_t((TString&)s1 + s2); }
51
52 // ----------------------------------------------------------------
53
54 void WarnCaller(const TString& warning)
55 {
56   std::cout << "WRN: " << warning << std::endl;
57 }
58
59 /**************************************************************************/
60 /**************************************************************************/
61
62 void SetupEnvironment()
63 {
64   // Check if REVESYS exists, try fallback to $ALICE_ROOT/EVE.
65   // Setup Include and Macro paths.
66
67   static const Exc_t eH("Reve::SetupEnvironment");
68   static Bool_t setupDone = kFALSE;
69
70   if (setupDone) {
71     Info(eH.Data(), "has already been run.");
72     return;
73   }
74
75   if(gSystem->Getenv("REVESYS") == 0) {
76     if(gSystem->Getenv("ALICE_ROOT") != 0) {
77       Info(eH.Data(), "setting REVESYS from ALICE_ROOT.");
78       gSystem->Setenv("REVESYS", Form("%s/EVE", gSystem->Getenv("ALICE_ROOT")));
79     } else {
80       Error(eH.Data(), "REVESYS not defined, neither is ALICE_ROOT.");
81       gSystem->Exit(1);
82     }
83   }
84   if(gSystem->AccessPathName(gSystem->Getenv("REVESYS")) == kTRUE) {
85     Error(eH.Data(), "REVESYS '%s' does not exist.", gSystem->Getenv("REVESYS"));
86     gSystem->Exit(1);
87   }
88
89   TString macPath(gROOT->GetMacroPath());
90   macPath += Form(":%s/macros", gSystem->Getenv("REVESYS"));
91   gInterpreter->AddIncludePath(gSystem->Getenv("REVESYS"));
92   if(gSystem->Getenv("ALICE_ROOT") != 0) {
93     macPath += Form(":%s/alice-macros", gSystem->Getenv("REVESYS"));
94     gInterpreter->AddIncludePath(Form("%s/include", gSystem->Getenv("ALICE_ROOT")));
95     gInterpreter->AddIncludePath(gSystem->Getenv("ALICE_ROOT"));
96   }
97   gROOT->SetMacroPath(macPath);
98
99   setupDone = kTRUE;
100 }
101
102 /**************************************************************************/
103
104 namespace {
105   void ChompTail(TString& s, char c='.') {
106     Ssiz_t p = s.Last(c);
107     if(p != kNPOS)
108       s.Remove(p);
109   }
110 }
111
112 Bool_t CheckMacro(const Text_t* mac)
113 {
114   // Checks if macro 'mac' is loaded.
115
116   return gROOT->GetInterpreter()->IsLoaded(mac);
117
118   // Previous version expected function with same name and used ROOT's
119   // list of global functions.
120   /*
121   TString foo(mac); ChompTail(foo);
122   if(recreate) {
123     TCollection* logf = gROOT->GetListOfGlobalFunctions(kFALSE);
124     logf->SetOwner();
125     logf->Clear();
126   }
127   return (gROOT->GetGlobalFunction(foo.Data(), 0, kTRUE) != 0);
128   */
129 }
130
131 void AssertMacro(const Text_t* mac)
132 {
133   // Load and execute macro 'mac' if it has not been loaded yet.
134
135   if(CheckMacro(mac) == kFALSE) {
136     gROOT->Macro(mac);
137   }
138 }
139
140 void Macro(const Text_t* mac)
141 {
142   // Execute macro 'mac'. Do not reload the macro.
143
144   if(CheckMacro(mac) == kFALSE) {
145     gROOT->LoadMacro(mac);
146   }
147   TString foo(mac); ChompTail(foo); foo += "()";
148   gROOT->ProcessLine(foo.Data());
149 }
150
151 void LoadMacro(const Text_t* mac)
152 {
153   // Makes sure that macro 'mac' is loaded, but do not reload it.
154
155   if(CheckMacro(mac) == kFALSE) {
156     gROOT->LoadMacro(mac);
157   }
158 }
159
160 /**************************************************************************/
161 /**************************************************************************/
162
163 // Pad stack for RINT/GUI thread.
164 std::list<TVirtualPad*> s_Pad_Stack;
165
166 TVirtualPad* PushPad(TVirtualPad* new_gpad, Int_t subpad)
167 {
168   // printf("Reve::PushPad old=%p, new=%p\n", gPad, new_gpad);
169   s_Pad_Stack.push_back(gPad);
170   if(new_gpad != 0)
171     new_gpad->cd(subpad);
172   else
173     gPad = 0;
174   return gPad;
175 }
176
177 TVirtualPad* PopPad(Bool_t modify_update_p)
178 {
179   // printf("Reve::PopPad old=%p, new=%p\n", gPad, s_Pad_Stack.empty() ? 0 : s_Pad_Stack.back());
180   if(s_Pad_Stack.empty()) {
181     Warning("Reve::PopTPad", "stack empty.");
182   } else {
183     if(modify_update_p && gPad != 0) {
184       gPad->Modified();
185       gPad->Update();
186     }
187     gPad = s_Pad_Stack.back();
188     s_Pad_Stack.pop_back();
189   }
190   return gPad;
191 }
192
193 /**************************************************************************/
194 // 
195 /**************************************************************************/
196
197 GeoManagerHolder::GeoManagerHolder(TGeoManager* new_gmgr) :
198   fManager(gGeoManager)
199 {
200   gGeoManager = new_gmgr;
201 }
202
203 GeoManagerHolder::~GeoManagerHolder()
204 {
205   gGeoManager = fManager;
206 }
207
208 /**************************************************************************/
209 // Color, palette management
210 /**************************************************************************/
211
212 void ColorFromIdx(Color_t ci, UChar_t* col, Bool_t alpha)
213 {
214   if (ci < 0) {
215     col[0] = col[1] = col[2] = col[3] = 0;
216     return;
217   }
218   TColor* c = gROOT->GetColor(ci);
219   if(c) { 
220     col[0] = (UChar_t)(255*c->GetRed());  
221     col[1] = (UChar_t)(255*c->GetGreen());
222     col[2] = (UChar_t)(255*c->GetBlue()); 
223     if (alpha) col[3] = 255;
224   }
225 }
226
227 void ColorFromIdx(Float_t f1, Color_t c1, Float_t f2, Color_t c2,
228                   UChar_t* col, Bool_t alpha)
229 {
230   TColor* t1 = gROOT->GetColor(c1);
231   TColor* t2 = gROOT->GetColor(c2);
232   if(t1 && t2) { 
233     col[0] = (UChar_t)(255*(f1*t1->GetRed()   + f2*t2->GetRed()));
234     col[1] = (UChar_t)(255*(f1*t1->GetGreen() + f2*t2->GetGreen()));
235     col[2] = (UChar_t)(255*(f1*t1->GetBlue()  + f2*t2->GetBlue())); 
236     if (alpha) col[3] = 255;
237   }
238 }
239
240 Color_t* FindColorVar(TObject* obj, const Text_t* varname)
241 {
242   static const Exc_t eH("Reve::FindColorVar");
243
244   Int_t off = obj->IsA()->GetDataMemberOffset(varname);
245   if(off == 0)
246     throw(eH + "could not find member '" + varname + "' in class " + obj->IsA()->GetName() + ".");
247   return (Color_t*) (((char*)obj) + off);
248 }
249
250 /**************************************************************************/
251 // RGBAPalette
252 /**************************************************************************/
253
254 RGBAPalette::RGBAPalette() :
255   TObject(),
256   Reve::ReferenceCount(),
257   fInterpolate (kFALSE),
258   fWrap        (kFALSE),
259   fColorArray  (0)
260 {
261   SetMinMax(0, 100);
262 }
263
264 RGBAPalette::RGBAPalette(Int_t min, Int_t max) :
265   TObject(),
266   Reve::ReferenceCount(),
267   fInterpolate (kFALSE),
268   fWrap        (kFALSE),
269   fColorArray  (0)
270 {
271   SetMinMax(min, max);
272 }
273
274 RGBAPalette::RGBAPalette(Int_t min, Int_t max, Bool_t interp, Bool_t wrap) :
275   TObject(),
276   Reve::ReferenceCount(),
277   fInterpolate (interp),
278   fWrap        (wrap),
279   fColorArray  (0)
280 {
281   SetMinMax(min, max);
282 }
283
284 RGBAPalette::~RGBAPalette()
285 {
286   delete [] fColorArray;
287 }
288
289 /**************************************************************************/
290
291 void RGBAPalette::SetupColor(Int_t val, UChar_t* pixel) const
292 {
293   using namespace TMath;
294   Float_t div  = Max(1, fMaxVal - fMinVal);
295   Int_t   nCol = gStyle->GetNumberOfColors();
296
297   Float_t f;
298   if      (val <= fMinVal) f = 0;
299   else if (val >= fMaxVal) f = nCol - 1;
300   else                     f = (val - fMinVal)/div*(nCol - 1);
301
302   if (fInterpolate) {
303     Int_t  bin = (Int_t) f;
304     Float_t f1 = f - bin, f2 = 1.0f - f1;
305     ColorFromIdx(f1, gStyle->GetColorPalette(bin),
306                  f2, gStyle->GetColorPalette(Min(bin + 1, nCol - 1)),
307                  pixel);
308   } else {
309     ColorFromIdx(gStyle->GetColorPalette((Int_t) Nint(f)), pixel);    
310   }
311 }
312
313 void RGBAPalette::SetupColorArray() const
314 {
315   if(fColorArray)
316     return;
317
318   fColorArray = new UChar_t [4 * fNBins];
319   UChar_t* p = fColorArray;
320   for(Int_t v=fMinVal; v<=fMaxVal; ++v, p+=4)
321     SetupColor(v, p);
322 }
323
324 void RGBAPalette::ClearColorArray()
325 {
326   if(fColorArray) {
327     delete [] fColorArray;
328     fColorArray = 0;
329   }
330 }
331
332 /**************************************************************************/
333
334 void RGBAPalette::SetMinMax(Int_t min, Int_t max)
335 {
336   fMinVal = min;
337   fMaxVal = max;
338   fNBins  = max - min + 1;
339   ClearColorArray();
340 }
341
342 void RGBAPalette::SetInterpolate(Bool_t b)
343 {
344   fInterpolate = b;
345   ClearColorArray();
346 }
347
348 void RGBAPalette::SetWrap(Bool_t b)
349 {
350   fWrap = b;
351 }
352
353 /**************************************************************************/
354 } // end namespace Reve
355 /**************************************************************************/
356 /**************************************************************************/