]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveBase/AliEveMacroExecutor.cxx
Coverity fix
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveMacroExecutor.cxx
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 #include "AliSysInfo.h"
14
15 #include <TEveUtil.h>
16 #include <TList.h>
17 #include <TROOT.h>
18
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
28 //______________________________________________________________________________
29 //
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.
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   {
66     TEveUtil::LoadMacro(mname);  
67   }
68   fMacros->Add(mac);
69 }
70
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
86 /******************************************************************************/
87
88 void AliEveMacroExecutor::RemoveMacros()
89 {
90   fMacros->Clear();
91 }
92
93 /******************************************************************************/
94
95 #include "Api.h"
96 #include "TInterpreter.h"
97
98 void AliEveMacroExecutor::ExecMacros()
99 {
100   // Execute registered macros.
101
102   TIter next(fMacros);
103   AliEveMacro* mac;
104   while ((mac = (AliEveMacro*) next()))
105   {
106     // printf ("macro '%s'; func '%s'; args '%s'\n", mac->GetMacro().Data(), mac->GetFunc().Data(), mac->GetArgs().Data());
107
108     mac->ResetExecState();
109     
110     if (mac->GetActive() == kFALSE || mac->GetFunc().IsNull())
111     {
112       continue;
113     }
114
115     if ((mac->RequiresRunLoader() && ! AliEveEventManager::HasRunLoader()) ||
116         (mac->RequiresESD()       && ! AliEveEventManager::HasESD())       ||
117         (mac->RequiresESDfriend() && ! AliEveEventManager::HasESDfriend()) ||
118         (mac->RequiresRawReader() && ! AliEveEventManager::HasRawReader()) ||
119         (mac->RequiresAOD()       && ! AliEveEventManager::HasAOD()))
120     {
121       mac->SetExecNoData();
122       continue;
123     }
124
125     TString cmd(mac->FormForExec());
126     try
127     {
128       Long_t                   result = 0;
129       TInterpreter::EErrorCode error  = TInterpreter::kNoError;
130
131       AliSysInfo::AddStamp(Form("%s_%s_before",mac->GetMacro().Data(), mac->GetFunc().Data()));
132       result = gInterpreter->ProcessLine(cmd, &error);
133       AliSysInfo::AddStamp(Form("%s_%s_after",mac->GetMacro().Data(), mac->GetFunc().Data()));
134
135       // Try to fix broken cint state? Code taken form pyroot.
136       if (G__get_return(0) > G__RETURN_NORMAL)
137       {
138         printf ("*** FIXING CINT STATE AFTER RETURN ***\n");
139         G__security_recover(0);
140       }
141
142       if (error != TInterpreter::kNoError)
143       {
144         mac->SetExecError();
145         Error("ExecMacros", "Executing %s::%s, CINT error ... hopefully recovered.",
146               mac->GetMacro().Data(), cmd.Data());
147       }
148       else
149       {
150         TEveElement *el  = (TEveElement*) result;
151         TObject     *obj = dynamic_cast<TObject*>(el);
152         if (el != 0 && obj == 0)
153         {
154           Warning("ExecMacros", "Executing %s::%s, returned TEveElement seems bad, setting it to 0.",
155                   mac->GetMacro().Data(), cmd.Data());
156           el = 0;
157         }
158         mac->SetExecOK(el);
159       }
160     }
161     catch(TEveException& exc)
162     {
163       mac->SetExecException(exc);
164
165       // Try to fix broken cint state? Code taken form pyroot.
166       if (G__get_return(0) > G__RETURN_NORMAL)
167       {
168         printf ("*** FIXING CINT STATE AFTER EXCEPTION ***\n");
169         G__security_recover(0);
170       }
171
172       Error("ExecMacros", "Executing %s::%s, caught exception: '%s'.",
173             mac->GetMacro().Data(), cmd.Data(), exc.Data());
174     }
175   }
176 }
177
178 /******************************************************************************/
179
180 #include <iostream>
181 #include <fstream>
182 using namespace std;
183
184 namespace
185 {
186 const char *gMacroSaveAsTypes[] = {"CINT Macro", "*.C",
187                                    0, 0};
188 }
189
190 void AliEveMacroExecutor::SaveAddedMacros()
191 {
192
193   TGFileInfo fi;
194   fi.fFileTypes   = gMacroSaveAsTypes;
195   fi.fIniDir      = StrDup(""); // current directory
196   fi.fFileTypeIdx = 0;
197   fi.fOverwrite   = kTRUE;
198   new TGFileDialog(gClient->GetDefaultRoot(), gEve->GetMainWindow(), kFDSave, &fi);
199   if (!fi.fFilename) return;
200
201   TPMERegexp filere(".*/([^/]+$)");
202   if (filere.Match(fi.fFilename) != 2)
203   {
204     Warning("AliEvePopupHandler", "file '%s' bad.", fi.fFilename);
205     return;
206   }
207   printf("Saving...\n");
208
209   TString file(filere[1]);
210   TString file1;
211   if (!file.EndsWith(".C"))
212   file1 = file + ".C";
213   gSystem->ChangeDirectory(fi.fIniDir);
214   ofstream myfile;
215   myfile.open (file1);
216
217   TIter next(fMacros);
218   AliEveMacro* mac;
219
220
221   myfile <<"//Macro generated automatically by AliEveMacroExecutor\n\n";
222
223   myfile <<"void "<<file<<"(){\n\n";
224   myfile <<"  AliEveMacroExecutor *exec = AliEveEventManager::GetMaster()->GetExecutor();\n";
225   myfile <<"  exec->RemoveMacros();\n";
226   myfile <<"  TEveBrowser *browser = gEve->GetBrowser();\n";
227   myfile <<"  browser->ShowCloseTab(kFALSE);\n";
228       
229   while ((mac = (AliEveMacro*) next()))
230   {
231     myfile <<"  exec->AddMacro(new AliEveMacro("<<mac->GetSources()<<", "<<char(34)<<mac->GetTags()<<char(34)<<", "
232      <<char(34)<<mac->GetMacro()<<char(34)<<", "<<char(34)<<mac->GetFunc()<<char(34)<<", "<<char(34)<<mac->GetArgs()
233      <<char(34)<<", "<<mac->GetActive()<<"));\n\n";
234   }
235
236   myfile <<"  TEveWindowSlot *slot = TEveWindow::CreateWindowInTab(browser->GetTabRight());\n";
237   myfile <<"  slot->StartEmbedding();\n";
238   myfile <<"  AliEveMacroExecutorWindow* exewin = new AliEveMacroExecutorWindow(exec);\n";
239   myfile <<"  slot->StopEmbedding("<<char(34)<<"DataSelection"<<char(34)<<");\n";
240   myfile <<"  exewin->PopulateMacros();\n\n";
241
242   myfile <<"\n}";
243   myfile.close();
244   printf("Saved...\n");
245
246 }