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