source: Sophya/trunk/SophyaPI/PIext/servnobjm.cc@ 2605

Last change on this file since 2605 was 2605, checked in by cmv, 21 years ago

intro PARTIELLE de HistoErr cmv 3/9/04

File size: 40.3 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <ctype.h>
4
5#include <typeinfo>
6#include <iostream>
7#include <string>
8#include <list>
9#include <map>
10
11#include "strutil.h"
12
13#include "nobjmgr.h"
14#include "servnobjm.h"
15#include "nomgadapter.h"
16#include "pistdimgapp.h"
17
18#include "fct1dfit.h"
19#include "fct2dfit.h"
20
21#ifdef SANS_EVOLPLANCK
22#include "matrix.h"
23#include "cvector.h"
24#else
25#include "tmatrix.h"
26#include "tvector.h"
27#include "pitvmaad.h"
28#endif
29
30#include "ntuple.h"
31#include "cimage.h"
32
33#include "histos.h"
34#include "histos2.h"
35#include "ntuple.h"
36#include "hisprof.h"
37
38#include "piyfxdrw.h"
39#include "pisurfdr.h"
40
41#include "pintuple.h"
42#include "pintup3d.h"
43
44#include "pipodrw.h"
45
46
47
48/* --Methode-- */
49Services2NObjMgr::Services2NObjMgr(string& tmpdir)
50{
51SetTmpDir(tmpdir);
52mImgapp = NULL;
53mOmg = NULL;
54dynlink = NULL;
55}
56
57/* --Methode-- */
58Services2NObjMgr::~Services2NObjMgr()
59{
60CloseDLL();
61if (mOmg) delete mOmg;
62}
63
64/* --Methode-- */
65void Services2NObjMgr::RegisterClass(AnyDataObj* o, NObjMgrAdapter* oa)
66{
67ObjAdaptList::iterator it;
68for(it = objadaplist.begin(); it != objadaplist.end(); it++)
69#ifdef SANS_EVOLPLANCK
70 if (typeid(*o) == typeid(*((*it).obj))) THROW(dupIdErr);
71#else
72 if (typeid(*o) == typeid(*((*it).obj)))
73 throw(DuplicateIdExc("Services2NObjMgr::RegisterClass() - Duplicate class"));
74#endif
75dataobj_adapter oba;
76oba.obj = o;
77oba.obja = oa;
78objadaplist.push_back(oba);
79}
80
81/* --Methode-- */
82NObjMgrAdapter* Services2NObjMgr::GetAdapter(AnyDataObj* o)
83{
84ObjAdaptList::iterator it;
85for(it = objadaplist.begin(); it != objadaplist.end(); it++)
86 if (typeid(*o) == typeid(*((*it).obj))) return((*it).obja->Clone(o));
87return(new NObjMgrAdapter(o));
88}
89
90/* --Methode-- */
91void Services2NObjMgr::SetTmpDir(string const & tmpdir)
92{
93TmpDir = tmpdir;
94PDynLinkMgr::SetTmpDir(tmpdir);
95return;
96}
97
98/* --Methode-- */
99void Services2NObjMgr::PlotFunc(string const & expfunc, string & nom, double xmin, double xmax, int np, string dopt)
100{
101FILE *fip;
102string fname = TmpDir + "func1_pia_dl.c";
103string cmd;
104int rc;
105
106if (!mImgapp) return;
107
108cmd = "rm -f " + fname;
109rc = system(cmd.c_str());
110// printf("PlotFunc_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
111
112if ((fip = fopen(fname.c_str(), "w")) == NULL) {
113 string sn = fname;
114 cout << "Services2NObjMgr/PlotFunc_Error: fopen( " << sn << endl;
115 return;
116 }
117
118// constitution du fichier a compiler
119fputs("#include <math.h> \n", fip);
120fputs("double func1_pia_dl_func(double x) \n{\n", fip);
121fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
122fclose(fip);
123
124string func = "func1_pia_dl_func";
125DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname, func);
126if (!f) return;
127PlotFunc(f, nom, xmin, xmax, np, dopt);
128CloseDLL();
129return;
130}
131
132/* --Methode-- */
133void Services2NObjMgr::PlotFunc2D(string const & expfunc, string & nom, double xmin, double xmax,
134 double ymin, double ymax, int npx, int npy, string dopt)
135{
136FILE *fip;
137string fname = TmpDir + "func2_pia_dl.c";
138string cmd;
139int rc;
140
141if (!mImgapp) return;
142
143cmd = "rm " + fname;
144rc = system(cmd.c_str());
145// printf("PlotFunc2D_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
146
147if ((fip = fopen(fname.c_str(), "w")) == NULL) {
148 string sn = fname;
149 cout << "Services2NObjMgr/PlotFunc2D_Error: fopen( " << sn << endl;
150 return;
151 }
152
153// constitution du fichier a compiler
154fputs("#include <math.h> \n", fip);
155fputs("double func2_pia_dl_func(double x, double y) \n{\n", fip);
156fprintf(fip,"return(%s); \n}\n", expfunc.c_str());
157fclose(fip);
158
159string func = "func2_pia_dl_func";
160DlFunctionOfXY f = (DlFunctionOfXY) LinkFunctionFromFile(fname, func);
161if (!f) return;
162PlotFunc2D(f, nom, xmin, xmax, ymin, ymax, npx, npy, dopt);
163CloseDLL();
164return;
165}
166
167/* --Methode-- */
168void Services2NObjMgr::PlotFuncFrCFile(string const & fname, string const & func, string & nom,
169 double xmin, double xmax, int np, string dopt)
170{
171DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname, func);
172if (!f) return;
173PlotFunc(f, nom, xmin, xmax, np, dopt);
174CloseDLL();
175return;
176}
177
178/* --Methode-- */
179void Services2NObjMgr::PlotFunc2DFrCFile(string const & fname, string const & func, string & nom,
180 double xmin, double xmax, double ymin, double ymax, int npx, int npy, string dopt)
181{
182DlFunctionOfXY f = (DlFunctionOfXY) LinkFunctionFromFile(fname, func);
183if (!f) return;
184PlotFunc2D(f, nom, xmin, xmax, ymin, ymax, npx, npy, dopt);
185CloseDLL();
186return;
187}
188
189/* --Methode-- */
190void Services2NObjMgr::PlotFunc(DlFunctionOfX f, string & nom, double xmin, double xmax, int np, string dopt)
191{
192if (!mImgapp) return;
193
194int k;
195if (np < 1) np = 1;
196if (xmax <= xmin) xmax = xmin+1.;
197Vector* vpy = new Vector(np);
198
199double xx;
200double dx = (xmax-xmin)/np;
201
202try {
203 for(k=0; k<np; k++) { xx = xmin+dx*k; (*vpy)(k) = f(xx); }
204}
205#ifdef SANS_EVOLPLANCK
206CATCH(merr) {
207 fflush(stdout);
208 cout << endl;
209 cerr << endl;
210 string es = PeidaExc(merr);
211 cerr << "Services2NObjMgr::PlotFunc() Exception :" << merr << es;
212 delete vpy;
213 vpy = NULL;
214 } ENDTRY;
215#else
216catch ( PException exc ) {
217 fflush(stdout);
218 cout << endl;
219 cerr << endl;
220 cerr << "Services2NObjMgr::PlotFunc() Exception :" << exc.Msg() << endl;
221 delete vpy;
222 vpy = NULL;
223}
224#endif
225
226if (vpy) {
227 string titre;
228 if (nom.length() < 1) {
229 titre = "Function f(x)";
230 nom = "/autoc/f_x";
231 }
232 else titre = nom;
233
234 P1DArrayAdapter* vya = new POVectorAdapter(vpy, false);
235 vya->DefineXCoordinate(xmin, (xmax-xmin)/np);
236 PIYfXDrawer* dr = new PIYfXDrawer(vya, NULL, true) ;
237 dopt = "thinline " + dopt;
238 int rsid = mImgapp->DispScDrawer(dr, titre, dopt);
239 if (nom.length() > 0) {
240 MyObjMgr()->AddObj(vpy, nom);
241 MyObjMgr()->AddWRsId(nom, rsid);
242 }
243 }
244return;
245}
246
247/* --Methode-- */
248void Services2NObjMgr::PlotFunc2D(DlFunctionOfXY f, string & nom, double xmin, double xmax, double ymin, double ymax,
249 int npx, int npy, string dopt)
250{
251if (!mImgapp) return;
252
253if (npx < 3) npx = 3;
254if (npy < 3) npy = 3;
255if (npx > 250) npx = 250;
256if (npy > 250) npy = 250;
257if (xmax <= xmin) xmax = xmin+1.;
258if (ymax <= ymin) ymax = ymin+1.;
259
260Matrix* mtx = new Matrix(npy, npx);
261
262int i,j;
263double xx, yy;
264double dx = (xmax-xmin)/npx;
265double dy = (ymax-ymin)/npy;
266// printf(" -- DBG -- %d %d , %g %g , %g %g \n", npx, npy, xmin, xmax, ymin, ymax);
267try {
268 for(j=0; j<npy; j++) {
269 yy = ymin+dy*j;
270 for(i=0; i<npx; i++) {
271 xx = xmin+dx*i;
272 (*mtx)(j, i) = f(xx, yy);
273 }
274 }
275}
276#ifdef SANS_EVOLPLANCK
277CATCH(merr) {
278 fflush(stdout);
279 cout << endl;
280 cerr << endl;
281 string es = PeidaExc(merr);
282 cerr << "Services2NObjMgr::PlotFunc2D() Exception :" << merr << es;
283 delete mtx; mtx = NULL;
284 } ENDTRY;
285#else
286catch ( PException exc ) {
287 fflush(stdout);
288 cout << endl;
289 cerr << endl;
290 cerr << "Services2NObjMgr::PlotFunc2D() Exception :" << exc.Msg() << endl;
291 delete mtx; mtx = NULL;
292}
293#endif
294
295if (mtx) {
296 string titre;
297 if (nom.length() < 1) {
298 titre = "Function f(x,y)";
299 nom = "/autoc/f2d_xy";
300 }
301 else titre = nom;
302 P2DArrayAdapter* arr = new POMatrixAdapter(mtx, false);
303 arr->DefineXYCoordinates(xmin, ymin, dx, dy);
304 PISurfaceDrawer* sdr = new PISurfaceDrawer(arr, true, true, true);
305 int rsid = mImgapp->Disp3DDrawer(sdr, titre, dopt);
306 if (nom.length() > 0) {
307 MyObjMgr()->AddObj(mtx, nom);
308 MyObjMgr()->AddWRsId(nom, rsid);
309 }
310 }
311
312return;
313}
314
315/* --Methode-- */
316void Services2NObjMgr::ExpVal(string expval,string resultvarname)
317{
318 // Fill C-file to be executed
319 FILE *fip;
320 string func = "eval_pia_dl_func";
321 string fname = TmpDir + func; fname += ".c";
322 string cmd = "rm -f " + fname; system(cmd.c_str());
323 if((fip=fopen(fname.c_str(), "w"))==NULL) {
324 cout << "Services2NObjMgr/EvalExp_Error: fopen("<<fname<<")"<<endl;
325 return;
326 }
327 fprintf(fip,"#include <math.h>\n");
328 fprintf(fip,"double %s(double ___dummy_variable___) \n{\n",func.c_str());
329 // Add all variables already declared
330 DVList& varlist = MyObjMgr()->GetVarList();
331 DVList::ValList::const_iterator it;
332 for(it = varlist.Begin(); it != varlist.End(); it++) {
333#ifdef SANS_EVOLPLANCK
334 MuTyV mtv = (*it).second;
335 double value = (double)(mtv);
336#else
337 double value = (double)((*it).second.elval);
338#endif
339 string name_var = (*it).first;
340 fprintf(fip,"double %s = %.17f;\n",name_var.c_str(),value);
341 }
342 fprintf(fip,"return %s;\n}\n",expval.c_str());
343 fclose(fip);
344
345 // Dynamically link function
346 DlFunctionOfX f = (DlFunctionOfX) LinkFunctionFromFile(fname,func);
347 if(!f) {
348 cout<<"Services2NObjMgr/EvalExp_Error: linking DlFunctionOfX"<<endl;
349 cout<<"...expval = "<<expval<<endl;
350 return;
351 }
352
353 // Evaluate function and close dynamic link
354 double result;
355 try {
356 result = f(0.);
357 CloseDLL();
358 } catch ( ... ) {
359 cout<<"Services2NObjMgr/EvalExp_Error: Arithmetic exception"<<endl;
360 CloseDLL();
361 return;
362 }
363
364 // Eventually store the result into variable or just print it
365 if(resultvarname.size()>0) {
366 if(MyObjMgr()->HasVar(resultvarname)) MyObjMgr()->DeleteVar(resultvarname);
367 char str[512];
368 if(result==0.) sprintf(str,"%f",result);
369 else {
370 double lr = log10(fabs(result));
371 if(lr>-17. && lr<17.) sprintf(str,"%17f",result);
372 else sprintf(str,"%.17e",result);
373 }
374 MyObjMgr()->SetVar(resultvarname,(string)str);
375 } else cout<<result<<" = "<<expval<<endl;
376
377}
378
379/* --Methode-- */
380void Services2NObjMgr::DisplayPoints2D(string& nom, string& expx, string& expy,
381 string& experrx, string& experry,
382 string& expcut, string dopt, string loop)
383{
384NObjMgrAdapter* obja=NULL;
385obja = MyObjMgr()->GetObjAdapter(nom);
386if (obja == NULL) {
387 cout << "Services2NObjMgr::DisplayPoints2D() Error , No such object " << nom << endl;
388 return;
389 }
390if (!mImgapp) return;
391
392// Creation NTuple
393char* ntn[4] = {"expx","expy","expex","expey",};
394NTuple* nt = NULL;
395bool haserr = false;
396
397if ( (experrx.length() > 0 ) && (experry.length() > 0 ) ) { haserr = true; nt = new NTuple(4, ntn); }
398else { haserr = false; experrx = experry = "0."; nt = new NTuple(2, ntn); }
399
400ComputeExpressions(obja, expx, expy, experrx, experry, expcut, loop, nt, NULL, NULL);
401
402if (nt->NEntry() < 1) {
403 cout << "Services2NObjMgr::DisplayPoints2D() Warning Zero points satisfy cut !" << endl;
404 delete nt;
405 return;
406 }
407
408// nt->Show();
409// nt->Print(0,10);
410PINTuple* pin = new PINTuple(nt, true);
411pin->SelectXY(ntn[0], ntn[1]);
412if ( haserr ) pin->SelectErrBar(ntn[2], ntn[3]);
413
414dopt = "defline " + dopt;
415string titre = nom + ":" + expy + "%" + expx;
416mImgapp->DispScDrawer( (PIDrawer*)pin, titre, dopt);
417return;
418}
419
420/* --Methode-- */
421void Services2NObjMgr::DisplayPoints3D(string& nom, string& expx, string& expy,
422 string& expz, string& expcut, string dopt, string loop)
423{
424NObjMgrAdapter* obja=NULL;
425obja = MyObjMgr()->GetObjAdapter(nom);
426if (obja == NULL) {
427 cout << "Services2NObjMgr::DisplayPoints3D() Error , No such object " << nom << endl;
428 return;
429 }
430if (!mImgapp) return;
431
432char* ntn[3] = {"expx","expy","expz"};
433NTuple* nt = new NTuple(3,ntn); // Creation NTuple
434
435string expwt = "1.";
436ComputeExpressions(obja, expx, expy, expz, expwt, expcut, loop, nt, NULL, NULL);
437
438if (nt->NEntry() < 1) {
439 cout << "Services2NObjMgr::DisplayPoints3D() Warning Zero points satisfy cut !" << endl;
440 delete nt;
441 return;
442 }
443//DBG nt->Show();
444//DBG nt->Print(0,10);
445PINTuple3D* pin = new PINTuple3D(nt, true);
446pin->SelectXYZ(ntn[0], ntn[1], ntn[2]);
447dopt = "defline " + dopt;
448
449// Pour plot a partir de DispScDrawer
450// string nomdisp = "_NT3D_";
451// mImgapp->DispScDrawer( (PIDrawer*)pin, nomdisp, opt);
452// Pour plot a partir de Disp3DDrawer
453string titre = nom + ":" + expy + "%" + expx;
454mImgapp->Disp3DDrawer(pin, titre, dopt);
455
456return;
457}
458
459/* --Methode-- */
460void Services2NObjMgr::DisplayPoints2DW(string& nom, string& expx, string& expy,
461 string& expwt, string& expcut, string dopt, string loop)
462{
463NObjMgrAdapter* obja=NULL;
464obja = MyObjMgr()->GetObjAdapter(nom);
465if (obja == NULL) {
466 cout << "Services2NObjMgr::DisplayPoints2DW() Error , No such object " << nom << endl;
467 return;
468 }
469if (!mImgapp) return;
470
471char* ntn[3] = {"expx","expy","expw"};
472NTuple* nt = new NTuple(3,ntn); // Creation NTuple
473
474string exp = "1.";
475ComputeExpressions(obja, expx, expy, expwt, exp, expcut, loop, nt, NULL, NULL);
476
477if (nt->NEntry() < 1) {
478 cout << "Services2NObjMgr::DisplayPoints2DW() Warning Zero points satisfy cut !" << endl;
479 delete nt;
480 return;
481 }
482
483PINTuple* pin = new PINTuple(nt, true);
484pin->SelectXY(ntn[0], ntn[1]);
485pin->SelectWt(ntn[2]);
486
487string titre = nom + ":" + expwt + "_" + expy + "%" + expx ;
488mImgapp->DispScDrawer( (PIDrawer*)pin, titre, dopt);
489return;
490}
491
492/* --Methode-- */
493void Services2NObjMgr::ProjectH1(string& nom, string& expx, string& expwt,
494 string& expcut, string& nomh1, string dopt, string loop)
495{
496NObjMgrAdapter* obja=NULL;
497obja = MyObjMgr()->GetObjAdapter(nom);
498if (obja == NULL) {
499 cout << "Services2NObjMgr::ProjectH1() Error , No such object " << nom << endl;
500 return;
501 }
502if (!mImgapp) return;
503
504Histo* h1 = NULL;
505NTuple* nt = NULL;
506AnyDataObj* oh = NULL;
507if (nomh1.length() > 0) oh=MyObjMgr()->GetObj(nomh1);
508else nomh1 = "/tmp/projh1d";
509if ( (oh != NULL) && (typeid(*oh) == typeid(Histo)) ) h1 = (Histo*)oh; // Pas de remise a zero ! h1->Zero();
510else {
511 char* ntn[2]= {"hxval", "hwt"};
512 nt = new NTuple(2,ntn); // Creation NTuple
513 }
514string expz = "0.";
515ComputeExpressions(obja, expx, expwt, expz, expwt, expcut, loop, nt, h1, NULL);
516
517if ((!h1) && (!nt)) return;
518if (!h1) {
519 if (nt->NEntry() < 1) {
520 cout << "Services2NObjMgr::ProjectH1() Warning Zero points satisfy cut !" << endl;
521 delete nt;
522 return;
523 }
524 double xmin, xmax;
525 nt->GetMinMax(0, xmin, xmax);
526 h1 = new Histo(xmin, xmax, 100);
527 int k;
528 float* xn;
529 for(k=0; k<nt->NEntry(); k++) {
530 xn = nt->GetVec(k);
531 h1->Add(xn[0], xn[1]);
532 }
533 delete nt;
534 MyObjMgr()->AddObj(h1, nomh1);
535 }
536
537MyObjMgr()->DisplayObj(nomh1, dopt);
538return;
539}
540
541/* --Methode-- */
542void Services2NObjMgr::ProjectH2(string& nom, string& expx, string& expy, string& expwt,
543 string& expcut, string& nomh2, string dopt, string loop)
544{
545NObjMgrAdapter* obja=NULL;
546obja = MyObjMgr()->GetObjAdapter(nom);
547if (obja == NULL) {
548 cout << "Services2NObjMgr::ProjectH2() Error , No such object " << nom << endl;
549 return;
550 }
551if (!mImgapp) return;
552
553Histo2D* h2 = NULL;
554NTuple* nt = NULL;
555AnyDataObj* oh = NULL;
556if (nomh2.length() > 0) oh=MyObjMgr()->GetObj(nomh2);
557else nomh2 = "/tmp/projh2d";
558if ( (oh != NULL) && (typeid(*oh) == typeid(Histo2D)) ) h2 = (Histo2D*)oh; // Pas de remise a zero ! h2->Zero();
559else {
560 char* ntn[3]= {"hxval", "hyval", "hwt"};
561 nt = new NTuple(3,ntn); // Creation NTuple
562 }
563string expz = "0.";
564ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, loop, nt, NULL, h2);
565
566if ((!h2) && (!nt)) return;
567if (!h2) {
568 if (nt->NEntry() < 1) {
569 cout << "Services2NObjMgr::ProjectH2() Warning Zero points satisfy cut !" << endl;
570 delete nt;
571 return;
572 }
573 double xmin, xmax, ymin, ymax;
574 nt->GetMinMax(0, xmin, xmax);
575 nt->GetMinMax(1, ymin, ymax);
576 h2 = new Histo2D(xmin, xmax, 50, ymin, ymax, 50);
577 int k;
578 float* xn;
579 for(k=0; k<nt->NEntry(); k++) {
580 xn = nt->GetVec(k);
581 h2->Add(xn[0], xn[1], xn[2]);
582 }
583 delete nt;
584 MyObjMgr()->AddObj(h2, nomh2);
585 }
586
587MyObjMgr()->DisplayObj(nomh2, dopt);
588return;
589
590}
591
592/* --Methode-- cmv 13/10/98 */
593void Services2NObjMgr::ProjectHProf(string& nom, string& expx, string& expy, string& expwt,
594 string& expcut, string& nomprof, string dopt, string loop)
595// Pour remplir un ``GeneralFitData'' a partir de divers objets:
596//| nom = nom de l'objet a projeter dans un HProf.
597//| expx = expression X de definition du bin.
598//| expy = expression Y a additionner dans le bin.
599//| expwt = expression W du poids a additionner.
600//| expcut = expression du test de selection.
601//| nomprof = nom du HProf engendre (optionnel). Si l'objet n'existe pas
602//| les limites Xmin,Xmax sont calculees automatiquement.
603//| sinon ce sont celles de l'objet preexistant.
604//| opt = options generales pour le display.
605{
606NObjMgrAdapter* obja=NULL;
607obja = MyObjMgr()->GetObjAdapter(nom);
608if (obja == NULL) {
609 cout << "Services2NObjMgr::ProjectHProf() Error , No such object " << nom << endl;
610 return;
611 }
612if (!mImgapp) return;
613
614HProf* hprof = NULL;
615NTuple* nt = NULL;
616AnyDataObj* oh = NULL;
617if (nomprof.length() > 0) oh=MyObjMgr()->GetObj(nomprof);
618else nomprof = "/tmp/projprof";
619if( (oh!=NULL) && (typeid(*oh) == typeid(HProf)) ) hprof = (HProf*)oh;
620else {
621 char* ntn[3]= {"hxval", "hyval", "hwt"};
622 nt = new NTuple(3,ntn); // Creation NTuple
623}
624string expz = "0.";
625ComputeExpressions(obja, expx, expy, expwt, expwt, expcut, loop, nt, NULL, NULL, hprof);
626
627if((!hprof) && (!nt)) return;
628if(!hprof) {
629 if (nt->NEntry() < 1) {
630 cout << "Services2NObjMgr::ProjectHProf() Warning Zero points satisfy cut !" << endl;
631 delete nt;
632 return;
633 }
634 r_8 xmin, xmax;
635 nt->GetMinMax(0, xmin, xmax);
636 hprof = new HProf(xmin, xmax, 100);
637 int k;
638 float* xn;
639 for(k=0; k<nt->NEntry(); k++) {
640 xn = nt->GetVec(k);
641 hprof->Add(xn[0], xn[1], xn[2]);
642 }
643 delete nt;
644 MyObjMgr()->AddObj(hprof, nomprof);
645 }
646hprof->UpdateHisto();
647
648MyObjMgr()->DisplayObj(nomprof, dopt);
649return;
650}
651
652
653/* --Methode-- */
654void Services2NObjMgr::FillVect(string& nom, string& expx, string& expv,
655 string& expcut, string& nomvec, string dopt, string loop)
656{
657NObjMgrAdapter* obja=NULL;
658obja = MyObjMgr()->GetObjAdapter(nom);
659if (obja == NULL) {
660 cout << "Services2NObjMgr::FillVect() Error , No such object: " << nom << endl;
661 return;
662 }
663if (!mImgapp) return;
664
665Vector* v1 = NULL;
666AnyDataObj* ov = NULL;
667ov=MyObjMgr()->GetObj(nomvec);
668if (ov != NULL) v1 = dynamic_cast<Vector *>(ov);
669if (v1 == NULL) {
670 cout << "Services2NObjMgr::FillVect() Error , No such object or not a vector: " << nomvec << endl;
671 return;
672 }
673
674char* ntn[2]= {"vi", "vv"};
675NTuple* nt = new NTuple(2,ntn); // Creation NTuple
676
677string expz = "0.";
678ComputeExpressions(obja, expx, expv, expz, expz, expcut, loop, nt);
679
680if (!nt) return;
681if (nt->NEntry() < 1) {
682 cout << "Services2NObjMgr::FillVect() Warning Zero points satisfy cut !" << endl;
683 delete nt;
684 return;
685 }
686
687 int i,k;
688 double* xn;
689 for(k=0; k<nt->NEntry(); k++) {
690 xn = nt->GetLineD(k);
691 i = int(xn[0]+0.5);
692 if ( (i < 0) || i >= v1->NElts() ) continue;
693 (*v1)(i) = xn[1];
694 }
695 delete nt;
696
697
698MyObjMgr()->DisplayObj(nomvec, dopt);
699return;
700}
701
702/* --Methode-- */
703void Services2NObjMgr::FillMatx(string& nom, string& expx, string& expy, string& expv,
704 string& expcut, string& nommtx, string dopt, string loop)
705{
706NObjMgrAdapter* obja=NULL;
707obja = MyObjMgr()->GetObjAdapter(nom);
708if (obja == NULL) {
709 cout << "Services2NObjMgr::FillMatx() Error , No such objet " << nom << endl;
710 return;
711 }
712if (!mImgapp) return;
713
714Matrix* mtx = NULL;
715AnyDataObj* om = NULL;
716om=MyObjMgr()->GetObj(nommtx);
717if (om != NULL) mtx = dynamic_cast<Matrix *>(om);
718if (mtx == NULL) {
719 cout << "Services2NObjMgr::FillMatx() Error , No such object or not a matrix " << nommtx << endl;
720 return;
721 }
722
723char* ntn[3]= {"mi", "mj", "mv"};
724NTuple* nt = new NTuple(3,ntn); // Creation NTuple
725
726string expz = "0.";
727ComputeExpressions(obja, expx, expy, expv, expz, expcut, loop, nt);
728
729if (!nt) return;
730if (nt->NEntry() < 1) {
731 cout << "Services2NObjMgr::FillMatx() Warning Zero points satisfy cut !" << endl;
732 delete nt;
733 return;
734 }
735
736 int ic, jl, k;
737 double* xn;
738 for(k=0; k<nt->NEntry(); k++) {
739 xn = nt->GetLineD(k);
740 ic = int(xn[0]+0.5);
741 jl = int(xn[1]+0.5);
742 if ( (ic < 0) || ic >= mtx->NCol() ) continue;
743 if ( (jl < 0) || jl >= mtx->NRows() ) continue;
744 (*mtx)(jl, ic) = xn[2];
745 }
746 delete nt;
747
748
749MyObjMgr()->DisplayObj(nommtx, dopt);
750return;
751
752}
753
754/* --Methode-- */
755void Services2NObjMgr::ExpressionToVector(string& nom, string& expx, string& expcut,
756 string& nomvec, string dopt, string loop)
757{
758NObjMgrAdapter* obja=NULL;
759obja = MyObjMgr()->GetObjAdapter(nom);
760if (obja == NULL) {
761 cout << "Services2NObjMgr::ExpressionToVector() Error , No such object " << nom << endl;
762 return;
763 }
764if (!mImgapp) return;
765
766NTuple* nt = NULL;
767if (nomvec.length() < 1) nomvec = "/tmp/expvec";
768
769char* ntn[2]= {"vecval", "vecwt"};
770nt = new NTuple(1,ntn); // Creation NTuple
771
772string expwt = "1.";
773string expz = "0.";
774string dumexpcut = expcut; if(dumexpcut.size()<=0) dumexpcut = "1.";
775ComputeExpressions(obja,expx,expz,expz,expwt,dumexpcut,loop,nt,NULL,NULL);
776
777if (!nt) return;
778if (nt->NEntry() < 1) {
779 cout << "Services2NObjMgr::ExpressionToVector() Warning Zero points satisfy cut !" << endl;
780 delete nt;
781 return;
782 }
783
784Vector* vec = new Vector(nt->NEntry());
785int k;
786float* xn;
787for(k=0; k<nt->NEntry(); k++) {
788 xn = nt->GetVec(k);
789 (*vec)(k) = xn[0];
790 }
791delete nt;
792MyObjMgr()->AddObj(vec, nomvec);
793MyObjMgr()->DisplayObj(nomvec, dopt);
794return;
795}
796
797/* --Methode-- */
798void Services2NObjMgr::NtFromASCIIFile(string& nom,string& filename,double def_val)
799// Pour remplir un ntuple "nom" existant a partir du fichier
800// ASCII table "filename". Si il y a plus de variables dans le
801// ntuple que dans le fichier "filename",
802// les sur-numeraires sont mises a "def_val" par defaut.
803{
804AnyDataObj* mobj = MyObjMgr()->GetObj(nom);
805if(mobj == NULL)
806 {cout<<"NtFromASCIIFile() Error, object "<<nom<<" not existing"<<endl;
807 return;}
808if(typeid(*mobj) != typeid(NTuple))
809 {cout<<"NtFromASCIIFile() Error, object "<<nom<<" not an NTuple"<<endl;
810 return;}
811if (!mImgapp) return;
812
813NTuple* nt = (NTuple*) mobj;
814nt->FillFromASCIIFile(filename, def_val);
815return;
816}
817
818/* --Methode-- */
819void Services2NObjMgr::FillNT(string& nom, string& expx, string& expy, string& expz,
820 string& expt, string& expcut, string& nomnt, string loop)
821{
822NObjMgrAdapter* obja=NULL;
823obja = MyObjMgr()->GetObjAdapter(nom);
824if (obja == NULL) {
825 cout << "Services2NObjMgr::FillNT() Error , No such object " << nom << endl;
826 return;
827 }
828if (!mImgapp) return;
829
830bool fgnnt = false;
831NTuple* nt = NULL;
832AnyDataObj* oh = NULL;
833if (nomnt.length() > 0) oh=MyObjMgr()->GetObj(nomnt);
834else nomnt = "/tmp/fillnt";
835if ( (oh != NULL) && (typeid(*oh) == typeid(NTuple)) ) {
836 nt = (NTuple*)oh;
837 if (nt->NVar() > 10) {
838 cout << "Services2NObjMgr::FillNT() Warning , Max 10 var in NTuple -> new NTuple" << endl;
839 nt = NULL;
840 }
841 }
842if (nt == NULL) {
843 char* ntn[4]= {"x", "y","z","t"};
844 nt = new NTuple(4,ntn); // Creation NTuple
845 fgnnt = true;
846 }
847
848ComputeExpressions(obja, expx, expy, expz, expt, expcut, loop, nt, NULL, NULL);
849
850if (fgnnt) MyObjMgr()->AddObj(nt, nomnt);
851return;
852
853}
854
855/* --Methode-- */
856void Services2NObjMgr::FillNTFrCFile(string & nom, string const & fname,
857 string const & funcname, string & nomnt, string loop)
858{
859if (!mImgapp) return;
860
861NObjMgrAdapter* obja=NULL;
862obja = MyObjMgr()->GetObjAdapter(nom);
863if (obja == NULL) {
864 cout << "Services2NObjMgr::FillNTFrCFile( " << nom << "...) No such object" <<endl;
865 return;
866 }
867bool adel = true;
868NTupleInterface* objnt = obja->GetNTupleInterface(adel);
869if (objnt == NULL) {
870 cout << "Services2NObjMgr::FillNTFrCFile( " << nom << "...) Not an NTupleInterface !" <<endl;
871 return;
872 }
873
874NTLoopExprFunc f = (NTLoopExprFunc)LinkFunctionFromFile(fname, funcname);
875if (!f) {
876 cerr << "Services2NObjMgr::FillNTFrCFile Error Creation NTLoopExprFunc" << endl;
877 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
878 return;
879 }
880
881bool fgnnt = false;
882NTuple* nt = NULL;
883if (nomnt.length() > 0) {
884 AnyDataObj* oh = NULL;
885 oh=MyObjMgr()->GetObj(nomnt);
886 if ( (oh != NULL) && (typeid(*oh) == typeid(NTuple)) ) {
887 nt = (NTuple*)oh;
888 if (nt->NVar() > 10) {
889 cout << "Services2NObjMgr::FillNTFrCFile() Warning , Max 10 var in NTuple -> new NTuple" << endl;
890 nt = NULL;
891 }
892 }
893 if (nt == NULL) {
894 char* ntn[4]= {"x", "y","z","t"};
895 nt = new NTuple(4,ntn); // Creation NTuple
896 fgnnt = true;
897 }
898 }
899
900double xnt[10];
901float fxnt[10];
902
903int i,k;
904for(i=0; i<10; i++) fxnt[i] = xnt[i] = 0.;
905
906
907// $CHECK$ A virer des que possible - Pb blocage application quand trop d'impression
908// redirige - On redirige la sortie sur le terminal
909bool red = mImgapp->HasRedirectedStdOutErr();
910mImgapp->RedirectStdOutErr(false);
911
912int_8 k1,k2,dk;
913k1 = 0; k2 = objnt->NbLines(); dk = 1;
914DecodeLoopParameters(loop, k1, k2, dk);
915if (k1 < 0) k1 = 0;
916if (k2 < 0) k2 = objnt->NbLines();
917if (k2 > (int_8)objnt->NbLines()) k2 = objnt->NbLines();
918if (dk <= 0) dk = 1;
919
920try {
921 double* xn;
922 int_8 kstart = k1, kend = k2;
923 for(k=kstart; k<kend; k+=dk) {
924 xn = objnt->GetLineD(k);
925 if (f((int_8_exprf)k, xn, xnt, xnt+1, xnt+2, xnt+3, (int_8_exprf)kstart,(int_8_exprf) kend) != 0) {
926 if (nt) {
927 for(int i=0; i<4; i++) fxnt[i] = xnt[i];
928 nt->Fill(fxnt);
929 }
930 }
931 }
932 }
933#ifdef SANS_EVOLPLANCK
934CATCH(merr) {
935 fflush(stdout);
936 cout << endl;
937 cerr << endl;
938 string es = PeidaExc(merr);
939 cerr << "Services2NObjMgr::FillNTFrCFile() Exception :" << merr << es;
940 } ENDTRY;
941#else
942catch ( PException exc ) {
943 fflush(stdout);
944 cout << endl;
945 cerr << endl;
946 cerr << "Services2NObjMgr::FillNTFrCFile() Exception :" << exc.Msg() << endl;
947}
948#endif
949
950if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
951CloseDLL();
952
953// $CHECK$ A virer des que possible On redirige la sortie sur la fenetre PIConsole
954mImgapp->RedirectStdOutErr(red);
955
956if (fgnnt) MyObjMgr()->AddObj(nt, nomnt);
957return;
958}
959
960/* --Methode-- */
961void Services2NObjMgr::PrepareNTExpressionCFile(string & nom, string const & fname,
962 string const & funcname)
963{
964NObjMgrAdapter* obja=NULL;
965obja = MyObjMgr()->GetObjAdapter(nom);
966if (obja == NULL) {
967 cout << "Services2NObjMgr::PrepareNTExpressionCFile( " << nom << "...) No such object" <<endl;
968 return;
969 }
970bool adel = true;
971NTupleInterface* objnt = obja->GetNTupleInterface(adel);
972if (objnt == NULL) {
973 cout << "Services2NObjMgr::PrepareNTExpressionCFile( " << nom
974 << "...) No NTupleInterface !" <<endl;
975 return;
976 }
977string vardec = objnt->VarList_C("_xnti_");
978
979FILE *fip;
980if ((fip = fopen(fname.c_str(), "w")) == NULL) {
981 cout << "Services2NObjMgr::PrepareNTExpressionCFile()_Error: fopen " << fname << endl;
982 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
983 return;
984 }
985
986// constitution du fichier des decalarations des variables de l'interface NTuple
987fputs("#include <stdlib.h> \n", fip);
988fputs("#include <stdio.h> \n", fip);
989fputs("#include <math.h> \n\n", fip);
990
991fputs("/* ------ Compare bits on double --------- */ \n", fip);
992fputs("typedef long long int_8_exprf;\n", fip);
993fputs("int_8_exprf BitCmp64(double v,int_8_exprf flg)\n", fip);
994fputs("{return ((int_8_exprf)((v<0.) ? v-0.1 : v+0.1))&flg;}\n", fip);
995fputs("/* ------ Some random number generators --------- */ \n", fip);
996fputs("#if defined(__ppc__) && defined(__MACH__) \n",fip);
997fputs("#include <limits.h> \n", fip);
998fputs("#define drand48() ((double)(random())/LONG_MAX) \n",fip);
999fputs("#endif \n",fip);
1000fputs("#define frand01() ( (float) drand48() ) \n", fip);
1001fputs("#define drand01() drand48() \n", fip);
1002fputs("#define rand01() drand48() \n", fip);
1003fputs("#define frandpm1() ( 2. * frand01() - 1.) \n", fip);
1004fputs("#define drandpm1() ( 2. * drand01() - 1.) \n", fip);
1005fputs("#define randpm1() ( 2. * drand01() - 1.) \n", fip);
1006fputs("double NorRand(void) \n", fip);
1007fputs(" { \n double x,A,B; \n LAB10: \n A = drand01(); \n", fip);
1008fputs(" if ( A == 0. ) goto LAB10; \n B = drand01(); \n", fip);
1009fputs(" x = sqrt(-2.*log(A))*cos(2.*M_PI*B); \n", fip);
1010fputs(" return(x); \n } \n", fip);
1011fputs("#define GauRand() NorRand() \n", fip);
1012fputs("#define gaurand() NorRand() \n\n", fip);
1013
1014fputs("/* NTupleInterface Variable declaration - Generated by piapp */\n", fip);
1015fputs("/* -- Services2NObjMgr::PrepareNTExpressionCFile() -- */ \n", fip);
1016fputs("/* _nl line number or sequential index : _nstart <= _nl < _nend */ \n\n", fip);
1017fprintf(fip,"int %s(int_8_exprf _nl, double* _xnti_, double* _rx_, double* _ry_, double* _rz_, \n",
1018 funcname.c_str());
1019fprintf(fip," double* _rt_, int_8_exprf _nstart, int_8_exprf _nend) \n");
1020fprintf(fip, "{ \n %s \n", vardec.c_str());
1021fputs(" if (!1) { /* Cut Expression failed */ \n", fip);
1022fputs(" *_rx_ = *_ry_ = *_rz_ = *_rt_ = 0.; return(0);", fip);
1023fputs(" } \n /* Cut expression satisfied */ \n", fip);
1024fputs(" *_rx_ = 1.; \n *_ry_ = 1.; \n *_rz_ = 1.; \n *_rt_ = 1.; \n", fip);
1025fputs(" return(1); \n} \n", fip);
1026
1027fclose(fip);
1028
1029if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
1030return;
1031}
1032
1033/* --Methode-- cmv 13/10/98 */
1034void Services2NObjMgr::FillGFD(string& nom, string& expx, string& expy, string& expz,
1035 string& experr, string& expcut, string& nomgfd, string loop)
1036// Pour remplir un ``GeneralFitData'' a partir de divers objets:
1037//| nom = nom de l'objet a transcrire selon 1D: Z=f(X) ou 2D: Z=f(X,Y) .
1038//| Vector,Matrix,Histo,HProf,Histo2D,Image<T>,StarList,NTuple,GeneralFitData
1039//| expx = expression X du GeneralFitData (1er abscisse)
1040//| expy = expression Y du GeneralFitData (2sd abscisse si non "", Z=f(X,Y))
1041//| expz = expression Z du GeneralFitData (valeur de l'ordonnee)
1042//| experr = expression de l'erreur sur l'ordonnee Z
1043//| expcut = expression du test de selection
1044//| nomgfd = nom du GeneralFitData engendre (optionnel)
1045{
1046NObjMgrAdapter* obja=NULL;
1047obja = MyObjMgr()->GetObjAdapter(nom);
1048if (obja == NULL) {
1049 cout << "Services2NObjMgr::FillGFD() Error , No such object "<<nom<<endl;
1050 return;
1051 }
1052if(!mImgapp) return;
1053
1054// 2D ou 3D?
1055int nvar = 2;
1056if(expy.length()<=0) {nvar = 1; expy = "0.";}
1057
1058// Creation NTuple Buffer
1059char* ntn[4]= {"x","y","f","e"};
1060NTuple*nt = new NTuple(4,ntn);
1061
1062// Remplissage NTuple buffer
1063ComputeExpressions(obja, expx, expy, expz, experr, expcut, loop, nt, NULL, NULL);
1064if(nt->NEntry() < 1)
1065 {cout<<"Services2NObjMgr::FillGFD() Warning Zero points satisfy cut !"<<endl;
1066 delete nt; return;}
1067
1068//Remplissage de la structure GeneraFitData
1069if (nt->NEntry() <= 0) {
1070 cout<<"Services2NObjMgr::FillGFD() Warning - NData= " << nt->NEntry() << endl;
1071 delete nt;
1072 return;
1073 }
1074
1075GeneralFitData* gfd = new GeneralFitData(nvar,nt->NEntry(),0);
1076int k;
1077float* xn;
1078for(k=0; k<nt->NEntry(); k++) {
1079 xn = nt->GetVec(k);
1080 gfd->AddData(xn,xn[2],xn[3]);
1081}
1082
1083// Menage et table d'objets
1084delete nt;
1085MyObjMgr()->AddObj(gfd, nomgfd);
1086return;
1087}
1088
1089/* --Methode-- cmv 12/07/00 */
1090void Services2NObjMgr::FillGFDfrVec(string nomgfd,string namx,string namy,string namz,string name)
1091// Pour remplir un ``GeneralFitData'' a partir de vecteurs
1092//| gdfrvec nomgd X Y ! !
1093//| gdfrvec nomgd X Y ! EY
1094//| gdfrvec nomgd X Y Z !
1095//| gdfrvec nomgd X Y Z EZ
1096//| - nomgfd = nom du generaldata a remplir
1097//| - namx = nom du vecteur contenant les valeurs X
1098//| - namy = nom du vecteur contenant les valeurs Y
1099//| - namz = nom du vecteur contenant les valeurs Z (ou "!")
1100//| - name = nom du vecteur contenant les valeurs des erreurs EY ou EZ
1101{
1102// Decodage des noms des vecteurs pour le remplissage du generaldata
1103if(nomgfd=="!" || nomgfd.length()<1)
1104 {cout<<"FillGFDfrVec_Error: bad GenaralData name "<<nomgfd<<endl; return;}
1105if(namx=="!" || namx.length()<1)
1106 {cout<<"FillGFDfrVec_Error: bad X vector name "<<namx<<endl; return;}
1107if(namy=="!" || namy.length()<1)
1108 {cout<<"FillGFDfrVec_Error: bad Y vector name "<<namy<<endl; return;}
1109if(namz.length()<1) namz = "!";
1110if(name.length()<1) name = "!";
1111int nvar = 0;
1112if(namz=="!") nvar = 1; else nvar = 2;
1113
1114// Identify data
1115NamedObjMgr omg;
1116AnyDataObj* mobj = NULL;
1117Vector* v;
1118int nel = 0;
1119r_8 *x=NULL, *y=NULL, *z=NULL,* ez=NULL;
1120
1121if( (mobj=omg.GetObj(namx)) == NULL) {
1122 cout<<"FillGFDfrVec_Error: unknown X object "<<namx<<endl; return;
1123} else {
1124 v = (Vector*) mobj; x = v->Data(); nel=v->NElts();
1125}
1126
1127if( (mobj=omg.GetObj(namy)) == NULL) {
1128 cout<<"FillGFDfrVec_Error: unknown Y object "<<namy<<endl; return;
1129} else {
1130 v = (Vector*) mobj; y = z = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1131}
1132
1133if( nvar==2 && (mobj=omg.GetObj(namz)) == NULL) {
1134 cout<<"FillGFDfrVec_Error: unknown Z object "<<namz<<endl; return;
1135} else {
1136 v = (Vector*) mobj; z = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1137}
1138
1139if(name!="!") {
1140 if( (mobj=omg.GetObj(name)) == NULL) {
1141 cout<<"FillGFDfrVec_Error: unknown EZ object "<<name<<endl; return;
1142 } else {
1143 v = (Vector*) mobj; ez = v->Data(); if(v->NElts()<nel) nel=v->NElts();
1144 }
1145}
1146
1147if(nel<=0)
1148 {cout<<"FillGFDfrVec_Error: bad number of elements "<<nel<<endl; return;}
1149
1150// Create GeneralData and fill it with vectors
1151GeneralFitData* gfd = new GeneralFitData(nvar,nel+5,0);
1152if(nvar==1) gfd->SetData1(nel,x,z,ez); // On remplit Y=f(X)
1153else gfd->SetData2(nel,x,y,z,ez); // On remplit Z=f(X,y)
1154
1155// Menage et table d'objets
1156if( omg.GetObj(nomgfd) != NULL ) omg.DelObj(nomgfd);
1157MyObjMgr()->AddObj(gfd,nomgfd);
1158return;
1159}
1160
1161/* --Methode-- */
1162void Services2NObjMgr::ComputeExpressions(NObjMgrAdapter* obja, string& expx,
1163 string& expy, string& expz, string& expt, string& expcut, string& loop,
1164 NTuple* nt, Histo* h1, Histo2D* h2, HProf* hp)
1165{
1166if (obja == NULL) return;
1167bool adel = true;
1168NTupleInterface* objnt = obja->GetNTupleInterface(adel);
1169if (objnt == NULL) return;
1170string vardec = objnt->VarList_C("_zz6qi_");
1171
1172PlotExprFunc f = LinkExprFunc(vardec, expx, expy, expz, expt, expcut);
1173if (!f) {
1174 cerr << "Services2NObjMgr::::ComputeExpressions() Error Creation PlotExprFunc " << endl;
1175 if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
1176 return;
1177 }
1178
1179double xnt[10];
1180float fxnt[10];
1181
1182int_8 k;
1183for(k=0; k<10; k++) xnt[k] = 0.;
1184int_8 k1,k2,dk;
1185k1 = 0; k2 = objnt->NbLines(); dk = 1;
1186DecodeLoopParameters(loop, k1, k2, dk);
1187if (k1 < 0) k1 = 0;
1188if (k2 < 0) k2 = objnt->NbLines();
1189if (k2 > (int_8)objnt->NbLines()) k2 = objnt->NbLines();
1190if (dk <= 0) dk = 1;
1191
1192try {
1193 double* xn;
1194 for(k=k1; k<k2; k += dk) {
1195 xn = objnt->GetLineD(k);
1196 if (f((int_8_exprf)k,xn, xnt, xnt+1, xnt+2, xnt+3) != 0) {
1197 if (nt) {
1198 for(int i=0; i<4; i++) fxnt[i] = xnt[i];
1199 nt->Fill(fxnt);
1200 }
1201 if (h1) h1->Add(xnt[0], xnt[3]);
1202 if (h2) h2->Add(xnt[0], xnt[1], xnt[3]);
1203 if (hp) hp->Add(xnt[0], xnt[1], xnt[3]);
1204 }
1205 }
1206 }
1207#ifdef SANS_EVOLPLANCK
1208CATCH(merr) {
1209 fflush(stdout);
1210 cout << endl;
1211 cerr << endl;
1212 string es = PeidaExc(merr);
1213 cerr << "Services2NObjMgr::ComputeExpressions() Exception :" << merr << es;
1214 } ENDTRY;
1215#else
1216catch ( PException exc ) {
1217 fflush(stdout);
1218 cout << endl;
1219 cerr << endl;
1220 cerr << "Services2NObjMgr::ComputeExpressions() Exception :" << exc.Msg() << endl;
1221}
1222#endif
1223
1224if (adel) delete objnt; // Delete de l'objet NTupleInterface si necessaire
1225// Fermeture du fichier .so
1226CloseDLL();
1227return;
1228}
1229
1230
1231/* --Methode-- */
1232PlotExprFunc Services2NObjMgr::LinkExprFunc(string& vardec, string& expx, string& expy,
1233 string& expz, string& expt, string& cut)
1234{
1235FILE *fip;
1236string fname = TmpDir + "expf_pia_dl.c";
1237string cmd;
1238int rc;
1239
1240cmd = "rm -f " + fname;
1241rc = system(cmd.c_str());
1242//DBG printf("LinkExprFunc_Do> %s (Rc=%d)\n", cmd.c_str(), rc);
1243
1244if ((fip = fopen(fname.c_str(), "w")) == NULL) {
1245 string sn = fname;
1246 cout << "Services2NObjMgr/LinkExprFunc_Error: fopen( " << sn << endl;
1247 return(NULL);
1248 }
1249
1250// constitution du fichier a compiler
1251fputs("#include <stdlib.h> \n", fip);
1252fputs("#include <math.h> \n", fip);
1253
1254fputs("/* ------ Compare bits on double --------- */ \n", fip);
1255fputs("typedef long long int_8_exprf;\n", fip);
1256fputs("int_8_exprf BitCmp64(double v,int_8_exprf flg)\n", fip);
1257fputs("{return ((int_8_exprf)((v<0.) ? v-0.1 : v+0.1))&flg;}\n", fip);
1258fputs("/* ------ Some random number generators --------- */ \n", fip);
1259fputs("#if defined(__ppc__) && defined(__MACH__) \n",fip);
1260fputs("#include <limits.h> \n", fip);
1261fputs("#define drand48() ((double)(random())/LONG_MAX) \n",fip);
1262fputs("#endif \n",fip);
1263fputs("#define frand01() ( (float) drand48() ) \n", fip);
1264fputs("#define drand01() drand48() \n", fip);
1265fputs("#define rand01() drand48() \n", fip);
1266fputs("#define frandpm1() ( 2. * frand01() - 1.) \n", fip);
1267fputs("#define drandpm1() ( 2. * drand01() - 1.) \n", fip);
1268fputs("#define randpm1() ( 2. * drand01() - 1.) \n", fip);
1269fputs("double NorRand(void) \n", fip);
1270fputs(" { \n double x,A,B; \n LAB10: \n A = drand01(); \n", fip);
1271fputs(" if ( A == 0. ) goto LAB10; \n B = drand01(); \n", fip);
1272fputs(" x = sqrt(-2.*log(A))*cos(2.*M_PI*B); \n", fip);
1273fputs(" return(x); \n } \n", fip);
1274fputs("#define GauRand() NorRand() \n", fip);
1275fputs("#define gaurand() NorRand() \n\n", fip);
1276
1277fputs("int expf_pia_dl_func(int_8_exprf _nl, double* _zz6qi_, double* _rx_6q_, double* _ry_6q_, double* _rz_6q_, double* _rt_6q_) \n{\n", fip);
1278fprintf(fip,"%s \n", vardec.c_str());
1279fprintf(fip, "if (!(%s)) { *_rx_6q_ = *_ry_6q_ = *_rz_6q_ = *_rt_6q_ = 0.; return(0); } \n", cut.c_str());
1280fprintf(fip, "*_rx_6q_ = %s ; \n", expx.c_str());
1281fprintf(fip, "*_ry_6q_ = %s ; \n", expy.c_str());
1282fprintf(fip, "*_rz_6q_ = %s ; \n", expz.c_str());
1283fprintf(fip, "*_rt_6q_ = %s ; \n", expt.c_str());
1284fputs("return(1); \n} \n", fip);
1285fclose(fip);
1286string func = "expf_pia_dl_func";
1287return((PlotExprFunc)LinkFunctionFromFile(fname, func));
1288}
1289
1290
1291/* --Methode-- */
1292DlFunction Services2NObjMgr::LinkFunctionFromFile(string const & fname, string const & funcname)
1293{
1294// Le link dynamique
1295CloseDLL();
1296dynlink = PDynLinkMgr::BuildFromCFile(fname);
1297if (dynlink == NULL) {
1298 cerr << "Services2NObjMgr/LinkFunctionFromFile_Erreur: Erreur creation/Ouverture SO " << endl;
1299 return(NULL);
1300 }
1301
1302DlFunction retfunc = dynlink->GetFunction(funcname);
1303if (retfunc == NULL) {
1304 string sn = funcname;
1305 cerr << "Services2NObjMgr/LinkExprFunc_Erreur: Erreur linking " << sn << endl;
1306 CloseDLL();
1307 return(NULL);
1308 }
1309else return(retfunc);
1310}
1311
1312/* --Methode-- */
1313void Services2NObjMgr::CloseDLL()
1314{
1315if (dynlink) delete dynlink; dynlink = NULL;
1316}
1317
1318/* --Methode-- */
1319int Services2NObjMgr::ExecuteCommand(string line)
1320{
1321 if (mImgapp == NULL) return(99);
1322 return(mImgapp->CmdInterpreter()->Interpret(line));
1323}
1324
1325// Fonction static
1326/* --Methode-- */
1327void Services2NObjMgr::DecodeLoopParameters(string& loop, int_8& i1, int_8& i2, int_8& di)
1328{
1329// Decode des paramatres de boucle for(int i=i1; i<i2; i+=di) specifies
1330// sous forme i1[:i2[:di]]
1331// cout << "LoopParam() " << loop << " I1=" << i1 << " I2=" << i2 << " DI=" << di;
1332size_t l = loop.length();
1333if (l < 1) return;
1334size_t p = loop.find(':');
1335if (p >= l) { i1 = atol(loop.c_str()); return; }
1336i1 = atol(loop.substr(0, p).c_str());
1337string aa = loop.substr(p+1);
1338p = aa.find(':');
1339if (p < aa.length() ) {
1340 i2 = atol(aa.substr(0,p).c_str());
1341 di = atol(aa.substr(p+1).c_str());
1342 }
1343else i2 = atol(aa.c_str());
1344// cout << "-> I1= " << i1 << " I2= " << i2 << " DI= " << di << endl;
1345return;
1346}
1347
1348/* --Methode-- */
1349string Services2NObjMgr::FileName2Name(string const & fn)
1350{
1351
1352char fsep[2] = {FILESEP, '\0'};
1353char tsep[2] = {'.', '\0'};
1354size_t p = fn.find_last_of(fsep);
1355size_t l = fn.length();
1356if (p >= l) p = 0;
1357else p++;
1358size_t q = fn.find_first_of(tsep,p);
1359if (q < p) q = l;
1360return(fn.substr(p,q-p));
1361}
1362
1363
1364
1365
1366// SANS_EVOLPLANCK Attention !
1367#ifdef SANS_EVOLPLANCK
1368#include "pclassids.h"
1369
1370/* --Methode-- */
1371char* Services2NObjMgr::PClassIdToClassName(int cid)
1372{
1373switch (cid) {
1374 case ClassId_Poly1 :
1375 return("Poly1");
1376 case ClassId_Poly2 :
1377 return("Poly2");
1378 case ClassId_Matrix :
1379 return("Matrix");
1380 case ClassId_Vector :
1381 return("Vector");
1382
1383 case ClassId_DVList :
1384 return("DVList");
1385
1386 case ClassId_Histo1D :
1387 return("Histo1D");
1388 case ClassId_Histo2D :
1389 return("Histo2D");
1390 case ClassId_HProf :
1391 return("HProf");
1392 case ClassId_HistoErr :
1393 return("HistoErr");
1394 case ClassId_NTuple :
1395 return("NTuple");
1396 case ClassId_XNTuple :
1397 return("XNTuple");
1398 case ClassId_GeneralFitData :
1399 return("GeneralFitData");
1400
1401 case ClassId_Image :
1402 return("RzImage");
1403 case ClassId_Image + kuint_1 :
1404 return("ImageU1");
1405 case ClassId_Image + kint_1 :
1406 return("ImageI1");
1407 case ClassId_Image + kuint_2 :
1408 return("ImageU2");
1409 case ClassId_Image + kint_2 :
1410 return("ImageI2");
1411 case ClassId_Image + kuint_4 :
1412 return("ImageU4");
1413 case ClassId_Image + kint_4 :
1414 return("ImageI4");
1415 case ClassId_Image + kr_4 :
1416 return("ImageR4");
1417 case ClassId_Image + kr_8 :
1418 return("ImageR8");
1419 case ClassId_ZFidu :
1420 return("ZFidu");
1421
1422 case ClassId_StarList :
1423 return("StarList");
1424 case ClassId_Transfo :
1425 return("Transfo");
1426 case ClassId_PSF :
1427 return("PSF");
1428
1429
1430// - Ajout objet PPF
1431 default:
1432 return("AnyDataObj");
1433 }
1434}
1435
1436#else
1437char* Services2NObjMgr::PClassIdToClassName(int cid)
1438{
1439 return("AnyDataObj");
1440}
1441#endif
Note: See TracBrowser for help on using the repository browser.