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