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