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

Last change on this file since 2922 was 2922, checked in by ansari, 20 years ago

Ajout commande expmeansig pour TD/TP SurvEnv - Reza 27/3/2006

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