]>
Commit | Line | Data |
---|---|---|
f6afd0e1 | 1 | // @(#)root/eve:$Id$ |
2 | // Author: Matevz Tadel 2007 | |
3 | ||
4 | /************************************************************************** | |
5 | * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. * | |
6 | * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for * | |
7 | * full copyright notice. * | |
8 | **************************************************************************/ | |
9 | ||
10 | #include "AliEveMacroExecutor.h" | |
11 | #include "AliEveMacro.h" | |
12 | #include "AliEveEventManager.h" | |
2be6d65c | 13 | #include "AliSysInfo.h" |
f6afd0e1 | 14 | |
15 | #include <TEveUtil.h> | |
16 | #include <TList.h> | |
17 | #include <TROOT.h> | |
18 | ||
a13d7c88 | 19 | #include <TEveManager.h> |
20 | #include <TGFileDialog.h> | |
21 | #include <TGMenu.h> | |
22 | ||
23 | #include <TSystem.h> | |
24 | #include <TPRegexp.h> | |
25 | #include <RVersion.h> | |
26 | ||
27 | ||
f6afd0e1 | 28 | //______________________________________________________________________________ |
f6afd0e1 | 29 | // |
68ca2fe7 | 30 | // Contains a list of AliEveMacros. |
31 | // The macros are added via AddMacro() and are owned by the executor. | |
32 | // The macros can be executed via ExecMacros(). | |
33 | // They are executed in order in which they are registered. | |
f6afd0e1 | 34 | |
35 | ClassImp(AliEveMacroExecutor) | |
36 | ||
37 | //______________________________________________________________________________ | |
38 | AliEveMacroExecutor::AliEveMacroExecutor() : | |
39 | TObject(), | |
40 | fMacros(new TList) | |
41 | { | |
42 | // Constructor. | |
43 | ||
44 | fMacros->SetOwner(kTRUE); | |
45 | } | |
46 | ||
47 | //______________________________________________________________________________ | |
48 | AliEveMacroExecutor::~AliEveMacroExecutor() | |
49 | { | |
50 | // Destructor. | |
51 | ||
52 | delete fMacros; | |
53 | } | |
54 | ||
55 | /******************************************************************************/ | |
56 | ||
57 | void AliEveMacroExecutor::AddMacro(AliEveMacro* mac) | |
58 | { | |
59 | // Add a new macro. Ownership transfered to the executor. | |
60 | ||
61 | static const TEveException kEH("AliEveMacroExecutor::AddMacro "); | |
62 | ||
63 | const TString mname = mac->GetMacro(); | |
64 | if ( ! mname.IsNull() && TEveUtil::CheckMacro(mname) == kFALSE) | |
65 | { | |
a13d7c88 | 66 | TEveUtil::LoadMacro(mname); |
f6afd0e1 | 67 | } |
68 | fMacros->Add(mac); | |
69 | } | |
70 | ||
7b2d546e | 71 | AliEveMacro* AliEveMacroExecutor::FindMacro(const TString& func) |
72 | { | |
73 | // Find macro with given function name (it is supposed to be unique). | |
74 | // Returns 0 if not found. | |
75 | ||
76 | TIter next(fMacros); | |
77 | AliEveMacro* mac; | |
78 | while ((mac = (AliEveMacro*) next())) | |
79 | { | |
80 | if (mac->GetFunc() == func) | |
81 | return mac; | |
82 | } | |
83 | return 0; | |
84 | } | |
85 | ||
f6afd0e1 | 86 | /******************************************************************************/ |
87 | ||
a13d7c88 | 88 | void AliEveMacroExecutor::RemoveMacros() |
89 | { | |
90 | fMacros->Clear(); | |
91 | } | |
92 | ||
93 | /******************************************************************************/ | |
94 | ||
33ebe062 | 95 | #if ROOT_VERSION_CODE < ROOT_VERSION(5,99,0) |
f6afd0e1 | 96 | #include "Api.h" |
33ebe062 | 97 | #endif |
f6afd0e1 | 98 | #include "TInterpreter.h" |
99 | ||
100 | void AliEveMacroExecutor::ExecMacros() | |
101 | { | |
102 | // Execute registered macros. | |
103 | ||
104 | TIter next(fMacros); | |
105 | AliEveMacro* mac; | |
106 | while ((mac = (AliEveMacro*) next())) | |
107 | { | |
108 | // printf ("macro '%s'; func '%s'; args '%s'\n", mac->GetMacro().Data(), mac->GetFunc().Data(), mac->GetArgs().Data()); | |
109 | ||
68ca2fe7 | 110 | mac->ResetExecState(); |
111 | ||
f6afd0e1 | 112 | if (mac->GetActive() == kFALSE || mac->GetFunc().IsNull()) |
113 | { | |
114 | continue; | |
115 | } | |
116 | ||
68ca2fe7 | 117 | if ((mac->RequiresRunLoader() && ! AliEveEventManager::HasRunLoader()) || |
118 | (mac->RequiresESD() && ! AliEveEventManager::HasESD()) || | |
119 | (mac->RequiresESDfriend() && ! AliEveEventManager::HasESDfriend()) || | |
08b0f222 | 120 | (mac->RequiresRawReader() && ! AliEveEventManager::HasRawReader()) || |
121 | (mac->RequiresAOD() && ! AliEveEventManager::HasAOD())) | |
f6afd0e1 | 122 | { |
68ca2fe7 | 123 | mac->SetExecNoData(); |
124 | continue; | |
f6afd0e1 | 125 | } |
126 | ||
127 | TString cmd(mac->FormForExec()); | |
128 | try | |
129 | { | |
68ca2fe7 | 130 | Long_t result = 0; |
131 | TInterpreter::EErrorCode error = TInterpreter::kNoError; | |
132 | ||
2be6d65c | 133 | AliSysInfo::AddStamp(Form("%s_%s_before",mac->GetMacro().Data(), mac->GetFunc().Data())); |
68ca2fe7 | 134 | result = gInterpreter->ProcessLine(cmd, &error); |
2be6d65c | 135 | AliSysInfo::AddStamp(Form("%s_%s_after",mac->GetMacro().Data(), mac->GetFunc().Data())); |
68ca2fe7 | 136 | |
33ebe062 | 137 | #if ROOT_VERSION_CODE < ROOT_VERSION(5,99,0) |
f6afd0e1 | 138 | // Try to fix broken cint state? Code taken form pyroot. |
68ca2fe7 | 139 | if (G__get_return(0) > G__RETURN_NORMAL) |
140 | { | |
141 | printf ("*** FIXING CINT STATE AFTER RETURN ***\n"); | |
142 | G__security_recover(0); | |
143 | } | |
33ebe062 | 144 | #endif |
1d059662 | 145 | if (error != TInterpreter::kNoError) |
68ca2fe7 | 146 | { |
147 | mac->SetExecError(); | |
148 | Error("ExecMacros", "Executing %s::%s, CINT error ... hopefully recovered.", | |
149 | mac->GetMacro().Data(), cmd.Data()); | |
150 | } | |
151 | else | |
f6afd0e1 | 152 | { |
68ca2fe7 | 153 | TEveElement *el = (TEveElement*) result; |
154 | TObject *obj = dynamic_cast<TObject*>(el); | |
155 | if (el != 0 && obj == 0) | |
156 | { | |
157 | Warning("ExecMacros", "Executing %s::%s, returned TEveElement seems bad, setting it to 0.", | |
158 | mac->GetMacro().Data(), cmd.Data()); | |
159 | el = 0; | |
160 | } | |
161 | mac->SetExecOK(el); | |
f6afd0e1 | 162 | } |
163 | } | |
164 | catch(TEveException& exc) | |
165 | { | |
68ca2fe7 | 166 | mac->SetExecException(exc); |
33ebe062 | 167 | #if ROOT_VERSION_CODE < ROOT_VERSION(5,99,0) |
68ca2fe7 | 168 | // Try to fix broken cint state? Code taken form pyroot. |
169 | if (G__get_return(0) > G__RETURN_NORMAL) | |
170 | { | |
171 | printf ("*** FIXING CINT STATE AFTER EXCEPTION ***\n"); | |
172 | G__security_recover(0); | |
173 | } | |
33ebe062 | 174 | #endif |
f6afd0e1 | 175 | Error("ExecMacros", "Executing %s::%s, caught exception: '%s'.", |
176 | mac->GetMacro().Data(), cmd.Data(), exc.Data()); | |
177 | } | |
f6afd0e1 | 178 | } |
179 | } | |
a13d7c88 | 180 | |
181 | /******************************************************************************/ | |
182 | ||
183 | #include <iostream> | |
184 | #include <fstream> | |
185 | using namespace std; | |
186 | ||
187 | namespace | |
188 | { | |
189 | const char *gMacroSaveAsTypes[] = {"CINT Macro", "*.C", | |
190 | 0, 0}; | |
191 | } | |
192 | ||
193 | void AliEveMacroExecutor::SaveAddedMacros() | |
194 | { | |
195 | ||
196 | TGFileInfo fi; | |
197 | fi.fFileTypes = gMacroSaveAsTypes; | |
198 | fi.fIniDir = StrDup(""); // current directory | |
199 | fi.fFileTypeIdx = 0; | |
200 | fi.fOverwrite = kTRUE; | |
201 | new TGFileDialog(gClient->GetDefaultRoot(), gEve->GetMainWindow(), kFDSave, &fi); | |
202 | if (!fi.fFilename) return; | |
203 | ||
204 | TPMERegexp filere(".*/([^/]+$)"); | |
205 | if (filere.Match(fi.fFilename) != 2) | |
206 | { | |
207 | Warning("AliEvePopupHandler", "file '%s' bad.", fi.fFilename); | |
208 | return; | |
209 | } | |
210 | printf("Saving...\n"); | |
211 | ||
212 | TString file(filere[1]); | |
213 | TString file1; | |
214 | if (!file.EndsWith(".C")) | |
215 | file1 = file + ".C"; | |
216 | gSystem->ChangeDirectory(fi.fIniDir); | |
217 | ofstream myfile; | |
218 | myfile.open (file1); | |
219 | ||
220 | TIter next(fMacros); | |
221 | AliEveMacro* mac; | |
222 | ||
223 | ||
224 | myfile <<"//Macro generated automatically by AliEveMacroExecutor\n\n"; | |
225 | ||
226 | myfile <<"void "<<file<<"(){\n\n"; | |
227 | myfile <<" AliEveMacroExecutor *exec = AliEveEventManager::GetMaster()->GetExecutor();\n"; | |
228 | myfile <<" exec->RemoveMacros();\n"; | |
229 | myfile <<" TEveBrowser *browser = gEve->GetBrowser();\n"; | |
230 | myfile <<" browser->ShowCloseTab(kFALSE);\n"; | |
231 | ||
232 | while ((mac = (AliEveMacro*) next())) | |
233 | { | |
234 | myfile <<" exec->AddMacro(new AliEveMacro("<<mac->GetSources()<<", "<<char(34)<<mac->GetTags()<<char(34)<<", " | |
235 | <<char(34)<<mac->GetMacro()<<char(34)<<", "<<char(34)<<mac->GetFunc()<<char(34)<<", "<<char(34)<<mac->GetArgs() | |
236 | <<char(34)<<", "<<mac->GetActive()<<"));\n\n"; | |
237 | } | |
238 | ||
239 | myfile <<" TEveWindowSlot *slot = TEveWindow::CreateWindowInTab(browser->GetTabRight());\n"; | |
240 | myfile <<" slot->StartEmbedding();\n"; | |
241 | myfile <<" AliEveMacroExecutorWindow* exewin = new AliEveMacroExecutorWindow(exec);\n"; | |
242 | myfile <<" slot->StopEmbedding("<<char(34)<<"DataSelection"<<char(34)<<");\n"; | |
243 | myfile <<" exewin->PopulateMacros();\n\n"; | |
244 | ||
245 | myfile <<"\n}"; | |
246 | myfile.close(); | |
247 | printf("Saved...\n"); | |
248 | ||
249 | } |