source: PSPA/madxPSPA/src/mad_rplot.cpp @ 430

Last change on this file since 430 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 9.0 KB
Line 
1/* Piotr Skowronski, CERN */
2
3#include <cstdio>
4#include <cstdlib>
5#include <cstring>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include "mad_extrn_f.h"
12#include "mad_err.h"
13#include "mad_rplot.h"
14
15#ifdef __cplusplus
16}
17#endif
18
19#ifndef _WIN32
20#include <dlfcn.h>
21#endif
22
23#ifdef ROOT_PLOT
24
25#include <MadxPlotter.h>
26#include <MadxViewer.h>
27
28#endif
29
30#ifdef _PLUGIN
31
32/* pointer to rplotter function, C intrtface to MadxPlotter::Fill, see MadxPlotter for details */
33typedef void (*rplot_plottrack_fctn)(int,int,int,double, double,double,double,double,double,double);
34
35static void*                rplot_handle    = 0;
36static rplot_plottrack_fctn rplot_plottrack = 0; /*pointer to function*/
37
38static void
39loadrplotlib(void)
40{
41  void *handle;
42  char buff[200];
43  char* homedir = 0x0;
44 
45  /*simple check if the library was linked dynamically*/
46  handle = dlopen( 0,  RTLD_GLOBAL | RTLD_LAZY);
47  if (handle == 0x0)
48  {
49    warning("rplot.c: loadrplotlib()",
50            "Most probobly yur binary is statically linked, what disables plugin support.\n");
51    return;
52  }
53/*   printf("0 loading Done\n");*/
54  if (!handle) {
55    fprintf (stderr, "%s\n", dlerror());
56    return;
57  }
58
59
60  handle = dlopen( "libCint.so",   RTLD_GLOBAL | RTLD_LAZY);
61  if (!handle) {
62    fprintf (stderr, "%s\n", dlerror());
63    return;
64  }
65
66  handle = dlopen( "libCore.so",   RTLD_GLOBAL | RTLD_LAZY);
67  if (!handle) {
68    fprintf (stderr, "%s\n", dlerror());
69    return;
70  }
71 
72  handle = dlopen( "libRIO.so",   RTLD_GLOBAL | RTLD_LAZY);
73  if (!handle) {
74    fprintf (stderr, "%s\n", dlerror());
75    return;
76  }
77
78  handle = dlopen( "libMathCore.so",   RTLD_GLOBAL | RTLD_LAZY);
79  if (!handle) {
80    fprintf (stderr, "%s\n", dlerror());
81    return;
82  }
83 
84  handle = dlopen( "libNet.so",   RTLD_GLOBAL | RTLD_LAZY);
85  if (!handle) {
86    fprintf (stderr, "%s\n", dlerror());
87    return;
88  }
89 
90  handle = dlopen( "libTree.so",   RTLD_GLOBAL | RTLD_LAZY);
91  if (!handle) {
92    fprintf (stderr, "%s\n", dlerror());
93    return;
94  }
95
96  handle = dlopen( "libMatrix.so",   RTLD_GLOBAL | RTLD_LAZY);
97  if (!handle) {
98    fprintf (stderr, "%s\n", dlerror());
99    return;
100  }
101
102  handle = dlopen( "libHist.so",   RTLD_GLOBAL | RTLD_LAZY);
103  if (!handle) {
104    fprintf (stderr, "%s\n", dlerror());
105    return;
106  }
107
108  handle = dlopen( "libGraf.so",   RTLD_GLOBAL | RTLD_LAZY);
109  if (!handle) {
110    fprintf (stderr, "%s\n", dlerror());
111    return;
112  }
113
114  handle = dlopen( "libGpad.so",   RTLD_GLOBAL | RTLD_LAZY);
115  if (!handle) {
116    fprintf (stderr, "%s\n", dlerror());
117    return;
118  }
119
120
121  handle = dlopen( "libGui.so",   RTLD_GLOBAL | RTLD_LAZY);
122  if (!handle) {
123    fprintf (stderr, "%s\n", dlerror());
124    return;
125  }
126
127  handle = dlopen( "libMatrix.so",   RTLD_GLOBAL | RTLD_LAZY);
128  if (!handle) {
129    fprintf (stderr, "%s\n", dlerror());
130    return;
131  }
132
133  handle = dlopen( "libHist.so",   RTLD_GLOBAL | RTLD_LAZY);
134  if (!handle) {
135    fprintf (stderr, "%s\n", dlerror());
136    return;
137  }
138
139  handle = dlopen( "libGraf.so",   RTLD_GLOBAL | RTLD_LAZY);
140  if (!handle) {
141    fprintf (stderr, "%s\n", dlerror());
142    return;
143  }
144
145  handle = dlopen( "libGpad.so",   RTLD_GLOBAL | RTLD_LAZY);
146  if (!handle) {
147    fprintf (stderr, "%s\n", dlerror());
148    return;
149  }
150
151  handle = dlopen( "libGed.so",   RTLD_GLOBAL | RTLD_NOW);
152  if (!handle) {
153    fprintf (stderr, "%s\n", dlerror());
154    return;
155  }
156
157
158  printf("Loading $(HOME)/.madx/plugins/librplot.so\n");
159  homedir = getenv("HOME");
160  if (strlen(homedir) > 150)
161   {
162     printf("Home directory name too long: %s",homedir);
163   }
164 
165  sprintf(buff,"%s/.madx/plugins/librplot.so",homedir);
166
167  rplot_handle = dlopen (buff, RTLD_GLOBAL | RTLD_LAZY );
168  if (!rplot_handle) {
169
170    printf("Loading librplot.so\n");
171    rplot_handle = dlopen ("librplot.so", RTLD_GLOBAL | RTLD_LAZY );
172    if (!rplot_handle) {
173      fprintf (stderr, "%s\n", dlerror());
174      return;
175    }
176
177    return;
178  }
179
180#if 0 // not used
181  warning("rplot.c: loadrplotlib()",
182          "Plugin support is not enabled in this version. Unable to use rplot.");
183#endif
184}
185
186static void
187unloadrplotlib(void)
188{
189  dlclose(rplot_handle);
190  rplot_handle = 0;
191  rplot_plottrack = 0;
192}
193
194#endif // _PLUGIN
195
196void
197plottrack(int* particleno, int* obspoint, int* turn,
198          double* x, double* xp, double* y, double* yp, double* dpOverP, double* p,
199          double* length)
200{
201  (void)particleno, (void)obspoint, (void)turn;
202  (void)x, (void)xp, (void)y, (void)yp;
203  (void)dpOverP, (void)p, (void)length; 
204
205#ifdef ROOT_PLOT
206
207  if (MadxPlotter::GetDebug() >9)
208  {
209    printf("rlot.c: plottrack: %d    %d %d %10.7f %10.7f %10.7f %10.7f %10.7f %10.7f  %10.7f \n",
210           *particleno, *obspoint,*turn, *x,     *xp,    *y,     *yp,    *dpOverP, *p, *length);
211  }
212
213  MadxPlotter::Instance()->Fill(*particleno, *obspoint, *turn, *x,  *xp,  *y,  *yp,  *dpOverP, *p ,*length);
214#elif defined _PLUGIN
215
216  if (rplot_plottrack == 0) return;
217
218  rplot_plottrack(*particleno, *obspoint, *turn, *x,  *xp,  *y,  *yp,  *dpOverP, *p ,*length);
219
220#endif
221}
222
223// not used in madx
224void
225plottwiss(int* obspoint,
226          double* betax, double* alfax,
227          double* betay, double* alfay,
228          double* betaz, double* alfaz,
229          double* length)
230{
231  (void)obspoint;
232  (void)betax, (void)alfax, (void)betay, (void)alfay, (void)betaz, (void)alfaz;
233  (void)length; 
234
235#ifdef ROOT_PLOT
236  if (MadxPlotter::GetDebug() >9)
237  {
238    printf("rlot.c: plottwiss:   %d   %12.8f %12.8f %12.8f %12.8f %12.8f %12.8f %12.8f\n",
239           *obspoint,  *betax, *alfax, *betay, *alfay, *betaz, *alfaz, *length);
240  }
241
242  MadxPlotter::Instance()->Fill(*obspoint, *betax, *alfax, *betay, *alfay, *betaz, *alfaz, *length);
243#endif
244}
245
246void
247rplotfinish(void)
248{
249/*terminates plotter*/
250#ifdef ROOT_PLOT
251  MadxPlotter::Instance()->Finish();/*writes and deletes the current plot*/
252#elif defined _PLUGIN
253
254  typedef void (*rvfun)();
255  rvfun rfinish;
256  char *error;
257
258  if (rplot_handle == 0x0) return;
259
260  rfinish = (rvfun)dlsym(rplot_handle, "madxplotter_rplotfinish");
261  if ((error = dlerror()) != NULL)
262  {
263    fprintf (stderr, "%s\n", error);
264    return;
265  }
266
267  rfinish();
268
269#endif
270
271}
272
273void
274newrplot(void)
275{
276/*adds new plotter*/
277#ifdef ROOT_PLOT
278  MadxPlotter::Instance()->NewPlot();
279#elif defined _PLUGIN
280
281  typedef void (*rvfun)();
282  rvfun newrplot;
283  char *error;
284
285
286  if (rplot_handle == 0)
287  {
288    loadrplotlib();
289    if (rplot_handle == 0)
290    {
291      /*It means that library was not loaded*/
292      return;
293    }
294  }
295
296  newrplot = (rvfun)dlsym(rplot_handle, "madxplotter_newrplot");
297  if ((error = dlerror()) != NULL)
298  {
299    fprintf (stderr, "%s\n", error);
300    return;
301  }
302
303  rplot_plottrack = (rplot_plottrack_fctn)dlsym(rplot_handle, "madxplotter_plottrack");
304  if ((error = dlerror()) != NULL)
305  {
306    fprintf (stderr, "%s\n", error);
307    return;
308  }
309
310  newrplot();
311#else
312  warning("rplot.c: newrplot()",
313          "Plugin support is not enabled in this version. Unable to use rplot.");
314#endif
315}
316
317#if 0 // not used
318static void
319plotter(void)
320{
321/*adds new plotter*/
322#ifdef ROOT_PLOT
323  MadxPlotter::Instance()->Plotter();
324#endif
325}
326#endif
327
328#if 0 // not used
329static void
330print(void)
331{
332/*adds new plotter*/
333#ifdef ROOT_PLOT
334  MadxPlotter::Instance()->Plotter();
335#endif
336}
337#endif
338
339void
340rviewer(void)
341{
342/*adds new plotter*/
343
344#ifdef ROOT_PLOT
345
346  printf("Compiled with librplot.so support\n");
347
348  MadxViewer::Instance();
349
350#elif defined _PLUGIN
351
352  typedef void (*rvfun)();
353  rvfun fctn = 0;
354  char *error;
355
356  loadrplotlib();
357
358  if (rplot_handle == 0) return;
359
360  printf("Looking for runrviewer() \n");
361  fctn = (rvfun)dlsym(rplot_handle, "runrviewer");
362  if ((error = dlerror()) != 0)  {
363    fprintf (stderr, "%s\n", error);
364    return;
365  }
366
367  fctn();
368
369  unloadrplotlib();
370#else
371  warning("rplot.c: rviewer()",
372          "Plugin support is not enabled in this version. Unable to use rviewer.");
373#endif
374}
375
376void
377madxv_setknobname(int* n, const char* name)
378{
379  (void)n, (void)name;
380 
381#ifdef ROOT_PLOT
382  MadxViewer::Instance()->SetKnobName(*n,name);
383#elif defined _PLUGIN
384
385  typedef void (*sknfctn)(int*,const char*);
386  sknfctn fctn;
387  char *error;
388
389  if(rplot_handle == 0) return;
390
391  fctn = (sknfctn)dlsym(rplot_handle, "madxviewer_setknobname");
392  if ((error = dlerror()) != 0)
393  {
394    fprintf (stderr, "%s\n", error);
395    return;
396  }
397
398  fctn(n,name);
399
400#endif
401}
402
403void
404madxv_setfctnname(int* n, const char* name)
405{
406  (void)n, (void)name;
407 
408#ifdef ROOT_PLOT
409  MadxViewer::Instance()->SetFunctionName(*n,name);
410#elif defined _PLUGIN
411
412  typedef void (*sknfctn)(int*,const char*);
413  sknfctn fctn;
414  char *error;
415
416  if(rplot_handle == 0) return;
417
418  fctn = (sknfctn)dlsym(rplot_handle, "madxviewer_setfctnname");
419  if ((error = dlerror()) != 0)
420  {
421    fprintf (stderr, "%s\n", error);
422    return;
423  }
424
425  fctn(n,name);
426
427#endif
428}
429
430void
431madxv_setfunctionat(int* el, int* n,  const char* name)
432{
433  (void)el, (void)n, (void)name;
434 
435#ifdef ROOT_PLOT
436  MadxViewer::Instance()->SetFunctionAt(*el,*n,name);
437#elif defined _PLUGIN
438
439  typedef void (*sknfctn)(int*,int*,const char*);
440  sknfctn fctn;
441  char *error;
442
443  if(rplot_handle == 0) return;
444
445  fctn = (sknfctn)dlsym(rplot_handle, "madxviewer_setfunctionat");
446  if ((error = dlerror()) != 0)
447  {
448    fprintf (stderr, "%s\n", error);
449    return;
450  }
451
452  fctn(el,n,name);
453
454#endif
455}
456
Note: See TracBrowser for help on using the repository browser.