]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Reve/Reve.cxx
ad7cf2022ac55a5465813c4f515d67e84fff2835
[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 #include <TColor.h>
9
10 #include <TROOT.h>
11 #include <TInterpreter.h>
12
13 #include <list>
14
15 #include <iostream>
16
17 //______________________________________________________________________
18 // Reve
19 //
20
21 namespace Reve {
22
23 Exc_t operator+(const Exc_t &s1, const std::string &s2)
24 { return Exc_t((std::string&)s1 + s2); }
25
26 Exc_t operator+(const Exc_t &s1, const TString &s2)
27 { return Exc_t((std::string&)s1 + s2.Data()); }
28
29 Exc_t operator+(const Exc_t &s1,  const char *s2)
30 { return Exc_t((std::string&)s1 + s2); }
31
32
33 void WarnCaller(const TString& warning)
34 {
35   std::cout << "WRN: " << warning << std::endl;
36 }
37
38 void ColorFromIdx(Color_t ci, UChar_t* col)
39 {
40   TColor* c = gROOT->GetColor(ci);
41   if(c) { 
42     col[0] = (UChar_t)(255*c->GetRed());  
43     col[1] = (UChar_t)(255*c->GetGreen());
44     col[2] = (UChar_t)(255*c->GetBlue()); 
45     col[3] = 255;
46   }
47 }
48
49 Color_t* FindColorVar(TObject* obj, const Text_t* varname)
50 {
51   static const Exc_t eH("Reve::FindColorVar");
52
53   Int_t off = obj->IsA()->GetDataMemberOffset(varname);
54   if(off == 0)
55     throw(eH + "could not find member '" + varname + "' in class " + obj->IsA()->GetName() + ".");
56   return (Color_t*) (((char*)obj) + off);
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
69   if(gSystem->Getenv("REVESYS") == 0) {
70     if(gSystem->Getenv("ALICE_ROOT") != 0) {
71       Info(eH.Data(), "setting REVESYS from ALICE_ROOT.");
72       gSystem->Setenv("REVESYS", Form("%s/EVE", gSystem->Getenv("ALICE_ROOT")));
73     } else {
74       Error(eH.Data(), "REVESYS not defined, neither is ALICE_ROOT.");
75       gSystem->Exit(1);
76     }
77   }
78   if(gSystem->AccessPathName(gSystem->Getenv("REVESYS")) == kTRUE) {
79     Error(eH.Data(), "REVESYS '%s' does not exist.", gSystem->Getenv("REVESYS"));
80     gSystem->Exit(1);
81   }
82
83   TString macPath(gROOT->GetMacroPath());
84   macPath += Form(":%s/macros", gSystem->Getenv("REVESYS"));
85   gInterpreter->AddIncludePath(gSystem->Getenv("REVESYS"));
86   if(gSystem->Getenv("ALICE_ROOT") != 0) {
87     macPath += Form(":%s/alice-macros", gSystem->Getenv("REVESYS"));
88     gInterpreter->AddIncludePath(Form("%s/include", gSystem->Getenv("ALICE_ROOT")));
89     gInterpreter->AddIncludePath(gSystem->Getenv("ALICE_ROOT"));
90   }
91   gROOT->SetMacroPath(macPath);
92 }
93
94 /**************************************************************************/
95
96 namespace {
97   void ChompTail(TString& s, char c='.') {
98     Ssiz_t p = s.Last(c);
99     if(p != kNPOS)
100       s.Remove(p);
101   }
102 }
103
104 Bool_t CheckMacro(const Text_t* mac)
105 {
106   // Checks if macro 'mac' is loaded.
107
108   return gROOT->GetInterpreter()->IsLoaded(mac);
109
110   // Previous version expected function with same name and used ROOT's
111   // list of global functions.
112   /*
113   TString foo(mac); ChompTail(foo);
114   if(recreate) {
115     TCollection* logf = gROOT->GetListOfGlobalFunctions(kFALSE);
116     logf->SetOwner();
117     logf->Clear();
118   }
119   return (gROOT->GetGlobalFunction(foo.Data(), 0, kTRUE) != 0);
120   */
121 }
122
123 void AssertMacro(const Text_t* mac)
124 {
125   // Load and execute macro 'mac' if it has not been loaded yet.
126
127   if(CheckMacro(mac) == kFALSE) {
128     gROOT->Macro(mac);
129   }
130 }
131
132 void Macro(const Text_t* mac)
133 {
134   // Execute macro 'mac'. Do not reload the macro.
135
136   if(CheckMacro(mac) == kFALSE) {
137     gROOT->LoadMacro(mac);
138   }
139   TString foo(mac); ChompTail(foo); foo += "()";
140   gROOT->ProcessLine(foo.Data());
141 }
142
143 void LoadMacro(const Text_t* mac)
144 {
145   // Makes sure that macro 'mac' is loaded, but do not reload it.
146
147   if(CheckMacro(mac) == kFALSE) {
148     gROOT->LoadMacro(mac);
149   }
150 }
151
152 /**************************************************************************/
153 /**************************************************************************/
154
155 // Pad stack for RINT/GUI thread.
156 std::list<TVirtualPad*> s_Pad_Stack;
157
158 TVirtualPad* PushPad(TVirtualPad* new_gpad, Int_t subpad)
159 {
160   // printf("Reve::PushPad old=%p, new=%p\n", gPad, new_gpad);
161   s_Pad_Stack.push_back(gPad);
162   if(new_gpad != 0)
163     new_gpad->cd(subpad);
164   else
165     gPad = 0;
166   return gPad;
167 }
168
169 TVirtualPad* PopPad(Bool_t modify_update_p)
170 {
171   // printf("Reve::PopPad old=%p, new=%p\n", gPad, s_Pad_Stack.empty() ? 0 : s_Pad_Stack.back());
172   if(s_Pad_Stack.empty()) {
173     Warning("Reve::PopTPad", "stack empty.");
174   } else {
175     if(modify_update_p && gPad != 0) {
176       gPad->Modified();
177       gPad->Update();
178     }
179     gPad = s_Pad_Stack.back();
180     s_Pad_Stack.pop_back();
181   }
182   return gPad;
183 }
184
185 /**************************************************************************/
186 // 
187 /**************************************************************************/
188
189 GeoManagerHolder::GeoManagerHolder(TGeoManager* new_gmgr) :
190   fManager(gGeoManager)
191 {
192   gGeoManager = new_gmgr;
193 }
194
195 GeoManagerHolder::~GeoManagerHolder()
196 {
197   gGeoManager = fManager;
198 }
199
200
201 }