Changeset 2669 in Sophya for trunk/SophyaPI/PIext


Ignore:
Timestamp:
Apr 15, 2005, 3:01:05 PM (20 years ago)
Author:
cmv
Message:
  • n/read pour creer et remplir un ntuple a partir d'un choix de colonnes d'un fichier ASCII
  • n/merge pour merger des ntuples dans un ntuple unique
  • savelist pour sauver une liste d'objects dans un fichier ppf

cmv 15/04/2005

Location:
trunk/SophyaPI/PIext
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaPI/PIext/basexecut.cc

    r2651 r2669  
    371371  if (tokens.size() < 2) { cout << "Usage: saveobjs patt filename " << endl; return(0); }
    372372  mObjMgr->SaveObjects(tokens[0], tokens[1]); 
     373}
     374else if (kw == "savelist")  {
     375  if (tokens.size() < 2) {
     376    cout << "Usage: savelist objname1 [objname2 ...] filename "<<endl;
     377    return(0);
     378  }
     379  mObjMgr->SaveListObjects(tokens); 
    373380}
    374381else if (kw == "saveall" ) {
     
    14691476usage += "  PPF file (pattern: x?y*) - Alias saveppf\n";
    14701477usage += "Usage: saveppf nameobjpattern filename";
    1471 usage += "\n  Related commands: saveobjs saveall openppf savefits"; 
     1478usage += "\n  Related commands: saveobjs savelist saveall openppf savefits"; 
    14721479mpiac->RegisterCommand(kw, usage, this, "FileIO");
    14731480kw = "saveobjs";
     
    14751482usage += "  PPF file (pattern: x?y*) - Alias saveppf\n";
    14761483usage += "Usage: saveobjs nameobjpattern filename";
    1477 usage += "\n  Related commands: saveppf saveall openppf savefits"; 
     1484usage += "\n  Related commands: saveppf savelist saveall openppf savefits"; 
    14781485mpiac->RegisterCommand(kw, usage, this, "FileIO");
    14791486kw = "saveall";
    14801487usage = "Saves all objects into a PPF file \n Usage: saveall filename";
     1488usage += "\n  Related commands: saveobj savelist openppf savefits"; 
     1489mpiac->RegisterCommand(kw, usage, this, "FileIO");
     1490kw = "savelist";
     1491usage = "Saves a list of objects into a PPF file";
     1492usage = "\n Usage: savelist objname1 [objname2 ...] filename";
    14811493usage += "\n  Related commands: saveobj openppf savefits"; 
    14821494mpiac->RegisterCommand(kw, usage, this, "FileIO");
  • trunk/SophyaPI/PIext/nobjmgr.cc

    r2651 r2669  
    12051205
    12061206/* --Methode-- */
     1207void NamedObjMgr::SaveListObjects(vector<string> &liste)
     1208// Les n-1 premiers elements (liste[0 -> n-2]) contiennent
     1209// les noms des objects a mettre  dans le fichier ppf
     1210// dont le nom est le dernier argument (liste[n-1])
     1211{
     1212 ZSync(*myMutex);
     1213
     1214 if(liste.size()<2) {
     1215   cerr<<"NamedObjMgr::SaveListObjects()/Error not enough argument"<<endl;
     1216   return;
     1217 }
     1218
     1219 // open ppf file
     1220 string ppfname = liste[liste.size()-1];
     1221 POutPersist* pout=NULL;
     1222 try {
     1223   pout = new POutPersist(ppfname);
     1224 } catch(IOExc iox) {
     1225   cerr<<"NamedObjMgr::SaveObjects()/Error Exception - Msg= "<<iox.Msg()<<endl;
     1226   return;
     1227 }
     1228 
     1229 // put objects in ppf file
     1230 for(int i=0;i<liste.size()-1;i++) {
     1231   bool keeppath = (liste[i][0] == '/') ? true : false;
     1232   SaveObj_P(liste[i], (*pout), keeppath);
     1233 }
     1234
     1235 // close ppf file
     1236 delete pout;
     1237
     1238 return;
     1239}
     1240
     1241/* --Methode-- */
    12071242void NamedObjMgr::SaveAll(string const& flnm)
    12081243{
  • trunk/SophyaPI/PIext/nobjmgr.h

    r2651 r2669  
    7575  virtual void          SaveObj(string & nom, POutPersist& s, bool keeppath=false);
    7676  virtual void          SaveObjects(string & patt, string const& nomppf);
     77  virtual void          SaveListObjects(vector<string> &liste);
    7778  virtual void          SaveAll(string const& nomppf);
    7879  virtual void          SaveFits(string & nom, string const & flnm);
  • trunk/SophyaPI/PIext/pawexecut.cc

    r2615 r2669  
    77#include "sopnamsp.h"
    88#include "strutil.h"
     9#include "strutilxx.h"
    910#include "histos.h"
    1011#include "histos2.h"
     
    7576usage += "\n           if given add exp1,exp2,exp3 to the variable list";
    7677usage += "\n -f:filename : write into \"filename\", Default is to stdout";
     78piac->RegisterCommand(kw,usage,this,hgrp);
     79
     80kw = "n/read";
     81usage = "Read columns in an ASCII file and fill a NTuple";
     82usage += "\n n/read nt fascii [options] var_1,c_1 var_2,c_2 ... var_n,c_n ";
     83usage += "\n var_i,c_i : ntuple variable name, associated column in ASCII file [0,n[";
     84usage += "\n where [options] are:";
     85usage += "\n   \"=s\": separator character is \'s\' (could be \"\t\")";
     86usage += "\n   \"-^abcd\": do not read lines beginning with string \"abcd\" ";
     87usage += "\n   \"+^abcd\": read only lines beginning with string \"abcd\" ";
     88usage += "\n   \"-abcd\": do not read lines which contain string \"abcd\" ";
     89usage += "\n   \"+abcd\": read only lines which contain string \"abcd\" ";
     90usage += "\n these options may be repeated (ex: \"-^abcd\" \"-^xyz\") ";
     91usage += "\n   - in case of \"do not read\" options are added with logical AND ";
     92usage += "\n   - in case of \"read only\" options are added with logical OR ";
     93piac->RegisterCommand(kw,usage,this,hgrp);
     94
     95kw = "n/merge";
     96usage = "Merge ntuples";
     97usage += "\n n/merge nt nt_1 nt_2 ... nt_n";
     98usage += "\n Merge ntuples nt_i into ntuple nt";
    7799piac->RegisterCommand(kw,usage,this,hgrp);
    78100
     
    235257} else if(kw == "n/scan") {
    236258  n_scan(tokens); return(0);
     259} else if(kw == "n/read") {
     260  n_read(tokens); return(0);
     261} else if(kw == "n/merge") {
     262  n_merge(tokens); return(0);
    237263} else if(kw == "h/integ") {
    238264  h_integ(tokens); return(0);
     
    561587
    562588/* methode */
     589#define __LENLINE_N_READ__ 8192
     590void PAWExecutor::n_read(vector<string>& tokens)
     591{
     592 int lp=1;
     593
     594 if(tokens.size()<3) {
     595   cerr<<"Usage: n/read nt fascii [options] var_1,c_1 ... var_n,c_n"<<endl;
     596   return;
     597 }
     598
     599 // decodage des arguments
     600 string nament = tokens[0];
     601 string nameascii = tokens[1];
     602 vector<string> donotreadbeg;
     603 vector<string> donotreadin;
     604 vector<string> onlyreadbeg;
     605 vector<string> onlyreadin;
     606 vector<string> varname;
     607 vector<int> colnum;
     608 char separator = ' ';
     609 int numcolmaxi=-1;
     610
     611 for(int i=2;i<tokens.size();i++) {
     612   int lc = tokens[i].size();
     613   if(lc<2) continue;
     614   const char *c = tokens[i].c_str();
     615   if(c[0]=='=') { // Separator
     616     separator = c[1];
     617     if(lc==3) if(c[1]=='\\' && c[2]=='t') separator = '\t';
     618     continue;
     619   }
     620   if(c[0]=='+') { // Selection des lignes a lire
     621     if(c[1]!='^') onlyreadin.push_back(&c[1]);
     622       else if(lc>2) onlyreadbeg.push_back(&c[2]);
     623     continue;
     624   }
     625   if(c[0]=='-') { // Selection des lignes commentaire
     626     if(c[1]!='^') donotreadin.push_back(&c[1]);
     627       else if(lc>2) donotreadbeg.push_back(&c[2]);
     628     continue;
     629   }
     630   // decodage des noms de variables et des colonnes associees
     631   int p = tokens[i].find(',');
     632   if(p<1 || p>=lc-1) continue;
     633   string vn = tokens[i].substr(0,p);
     634   string cn = tokens[i].substr(p+1,lc-p-1);
     635   int ic = atoi(cn.c_str());
     636   if(ic<0) continue;
     637   if( !isalpha(vn[0]) ) continue;
     638   if(ic>numcolmaxi) numcolmaxi = ic;
     639   varname.push_back(vn);
     640   colnum.push_back(ic);
     641 }
     642
     643 int nvar = varname.size();
     644 if(nvar<=0) {
     645   cerr<<"n_read: no variables to be read"<<endl;
     646   return;
     647 }
     648
     649 // Print what has to be done
     650 if(lp) {
     651   if(onlyreadin.size()>0) {
     652     cout<<"n_read Only read line containing ["<<onlyreadin.size()<<"]:";
     653     for(int i=0;i<onlyreadin.size();i++) cout<<" \'"<<onlyreadin[i]<<"\'";
     654     cout<<endl;
     655   }
     656   if(onlyreadbeg.size()>0) {
     657     cout<<"n_read Only read line begining with ["<<onlyreadbeg.size()<<"]:";
     658     for(int i=0;i<onlyreadbeg.size();i++) cout<<" \'"<<onlyreadbeg[i]<<"\'";
     659     cout<<endl;
     660   }
     661   if(donotreadin.size()>0) {
     662     cout<<"n_read Do not read line containing ["<<donotreadin.size()<<"]:";
     663     for(int i=0;i<donotreadin.size();i++) cout<<" \'"<<donotreadin[i]<<"\'";
     664     cout<<endl;
     665   }
     666   if(donotreadbeg.size()>0) {
     667     cout<<"n_read Do not read line begining with ["<<donotreadbeg.size()<<"]:";
     668     for(int i=0;i<donotreadbeg.size();i++) cout<<" \'"<<donotreadbeg[i]<<"\'";
     669     cout<<endl;
     670   }
     671   if(nvar>0) {
     672     cout<<"n_read Number of variables to be read: "<<nvar<<endl;
     673     for(int i=0;i<nvar;i++) cout<<" \'"<<varname[i].c_str()<<","<<colnum[i]<<"\'";
     674     cout<<endl;
     675   }
     676   if(separator!=' ') cout<<"n_read Separator is: \'"<<separator<<"\'"<<endl;
     677 }
     678
     679 // Open ASCII file
     680 FILE * fascii = fopen(nameascii.c_str(),"r");
     681 if(fascii==NULL) {
     682   cerr<<"n_read: cannot open file "<<nameascii<<endl;
     683   return;
     684 }
     685
     686 // Creation du NTuple
     687 char** ntvn = new char*[nvar];
     688 for(int i=0;i<nvar;i++) ntvn[i] = const_cast<char *>(varname[i].c_str());
     689 r_4 *xnt = new r_4[nvar];
     690 NTuple *nt = new NTuple(nvar,ntvn);
     691
     692 // Read file
     693 char *line = new char[__LENLINE_N_READ__];
     694 int nline=0, nlinecom=0;
     695 while(fgets(line,__LENLINE_N_READ__,fascii) != NULL ) {
     696   nline++;
     697   int lc = strlen(line); if(lc<1) continue;
     698   // Pour enlever le \n final
     699   if(line[lc-1]=='\n' || line[lc-1]=='\r')
     700     {line[lc-1]='\0'; lc = strlen(line); if(lc<1) continue;}
     701
     702   // On vire les blancs au debut et a la fin
     703   char *newline = line;
     704   for(int i=0;i<lc;i++) if(newline[i]!=' ') {newline = &line[i]; break;}
     705   lc = strlen(newline); if(lc<1) continue;
     706   for(int i=lc-1;i>=0;i--) if(newline[i]==' ') newline[i]='\0'; else break;
     707   lc = strlen(newline); if(lc<1) continue;
     708   string const sline(newline);
     709   //cout<<"\'"<<sline<<"\'  lc="<<lc<<endl;
     710
     711   // Faut t'il lire cette ligne ?
     712   bool read_line_1 = true;
     713   if(onlyreadin.size()>0 || onlyreadbeg.size()>0) read_line_1 = false;
     714   if(onlyreadin.size()>0) {
     715     for(int i=0;i<onlyreadin.size();i++) {
     716       uint_4 p = sline.find(onlyreadin[i].c_str());
     717       if(p<0 || p>=lc) continue;
     718       read_line_1 = true;
     719       break;
     720     }
     721   }
     722   if(onlyreadbeg.size()>0) {
     723     for(int i=0;i<onlyreadbeg.size();i++) {
     724       uint_4 p = sline.find(onlyreadbeg[i].c_str());
     725       if(p!=0) continue;
     726       read_line_1 = true;
     727       break;
     728     }
     729   }
     730
     731   // Faut t'il ne pas lire cette ligne ?
     732   bool read_line_2 = true;
     733   if(donotreadin.size()>0) {
     734     for(int i=0;i<donotreadin.size();i++) {
     735       uint_4 p = sline.find(donotreadin[i].c_str());
     736       if(p<0 || p>=lc) continue;
     737       read_line_2 = false;
     738       break;
     739     }
     740   }
     741   if(donotreadbeg.size()>0) {
     742     for(int i=0;i<donotreadbeg.size();i++) {
     743       uint_4 p = sline.find(donotreadbeg[i].c_str());
     744       if(p!=0) continue;
     745       read_line_2 = false;
     746       break;
     747     }
     748   }
     749   if(!read_line_2) nlinecom++;
     750
     751   if(!read_line_1 || !read_line_2) continue;
     752
     753   // Decodage de la ligne
     754   vector<string> vs;
     755   FillVStringFrString(sline,vs,separator);
     756   int lvs = vs.size();
     757   if(lvs<numcolmaxi) continue; // Pas assez de champs decodes, mauvaise ligne
     758
     759   // Remplissage du NTuple
     760   for(int i=0;i<nvar;i++) {
     761     xnt[i] = 0.;
     762     int ic = colnum[i];
     763     if(ic>=lvs) continue;
     764     xnt[i] = atof(vs[ic].c_str());
     765   }
     766   nt->Fill(xnt);
     767   //cout<<"...xnt"; for(int i=0;i<nvar;i++) cout<<" "<<xnt[i]; cout<<endl;
     768
     769 }
     770 cout<<"n_read: "<<nline<<" lines in file, "
     771     <<nlinecom<<" commentary, "
     772     <<nt->NEntry()<<" Ntuple entries"<<endl;
     773
     774 // On sauve le NTuple si besoin, on ferme et detruit ce qu'il faut
     775 NamedObjMgr omg;
     776 if(nt->NEntry()>0) omg.AddObj(nt,nament); else delete nt;
     777 delete [] ntvn;
     778 delete [] xnt;
     779 delete [] line;
     780 fclose(fascii);
     781
     782 return;
     783}
     784#undef __LENLINE_N_READ__
     785
     786/* methode */
     787void PAWExecutor::n_merge(vector<string>& tokens)
     788{
     789 if(tokens.size()<2) {
     790   cerr<<"Usage: n/read nt nt_1 nt_2 ... nt_n"<<endl;
     791   return;
     792 }
     793
     794 NamedObjMgr omg;
     795
     796 // decodage des arguments
     797 string nament = tokens[0];
     798
     799 // boucle sur les ntuples
     800 NTuple * nt = NULL;
     801 int nvar=0, nfill=0;
     802 r_4 *xnt=NULL;
     803
     804 for(int i=1;i<tokens.size();i++) {
     805
     806   AnyDataObj* mobj = omg.GetObj(tokens[i]);
     807   if(mobj==NULL) {
     808     cout<<"n_merge Error: unknow object"<<tokens[i]<<endl;
     809     continue;;
     810   }
     811   NTuple* nt1 = dynamic_cast<NTuple*>(mobj);
     812   if(nt1==NULL) {
     813     cout<<"n_merge Error: "<<tokens[i]<<" not a NTuple"<<endl;
     814     continue;
     815   }
     816   if(nt1->NEntry()==0) {
     817     cout<<"n_merge Error: "<<tokens[i]<<" is empty"<<endl;
     818     continue;
     819   }
     820   if(nt1->NVar()==0) {
     821     cout<<"n_merge Error: "<<tokens[i]<<" has no variable"<<endl;
     822     continue;
     823   }
     824
     825   // create receiving ntuple if first pass
     826   if(nt==NULL) {
     827     nvar = nt1->NVar();
     828     vector<string> sntvn;
     829     for(int i=0;i<nvar;i++) sntvn.push_back(nt1->ColumnName(i));
     830     char **ntvn = new char*[nvar];
     831     for(int i=0;i<nvar;i++) ntvn[i] = const_cast<char *>(sntvn[i].c_str());
     832     nt = new NTuple(nvar,ntvn);
     833     delete [] ntvn;
     834   }
     835
     836   // filling with current ntuple
     837   int nvar1 = nt1->NVar();
     838   int n = (nvar1>nvar)? nvar1: nvar;
     839   r_4 *xnt1 = new r_4[n];
     840   for(int i=0;i<n;i++) xnt1[i]=0.;
     841   for(uint_4 iev=0;iev<nt1->NEntry();iev++) {
     842     nt1->GetVec(iev,xnt1);
     843     nt->Fill(xnt1);
     844   }
     845   nfill++;
     846   delete [] xnt1;
     847
     848 }
     849
     850 if(xnt!=NULL) delete [] xnt;
     851 if(nt!=NULL) {
     852   cout<<"n_merge: ntuple filled with "<<nfill
     853       <<" ntuples, "<<nt->NEntry()<<" entries"<<endl;
     854   if(nt->NEntry()>0) omg.AddObj(nt,nament); else delete nt;
     855 }
     856 return;
     857}
     858
     859/* methode */
    563860void PAWExecutor::h_integ(vector<string>& tokens)
    564861// Pour remplacer le contenu d'un histo 1D par son integrale
  • trunk/SophyaPI/PIext/pawexecut.h

    r1912 r2669  
    2424  void  n_proj(vector<string>& tokens);
    2525  void  n_scan(vector<string>& tokens);
     26  void  n_read(vector<string>& tokens);
     27  void  n_merge(vector<string>& tokens);
    2628  void  h_integ(vector<string>& tokens);
    2729  void  v_integ(vector<string>& tokens);
Note: See TracChangeset for help on using the changeset viewer.