Changeset 2817 in Sophya for trunk


Ignore:
Timestamp:
Jul 13, 2005, 4:48:41 PM (20 years ago)
Author:
cmv
Message:

n/assoc/col merge de colonnes de ntuples avec idexation 1->2 cmv 13/07/05

Location:
trunk/SophyaPI/PIext
Files:
2 edited

Legend:

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

    r2816 r2817  
    123123
    124124kw = "n/merge/col";
    125 usage = "Merge ntuples (by adding columns)";
     125usage = "Merge ntuples (by adding columns for each line)";
    126126usage += "\n n/merge/col nt nt_1[,ext] nt_2[,ext] ... nt_n[,ext]";
    127127usage += "\n Merge ntuples nt_i columns into ntuple nt";
     
    129129usage += "\n   if \"ext\"=\"!\" no extension is added";
    130130usage += "\n   if no \"ext\" is given, automatic extension \"_0\" \"_1\" \"_2\" ...";
     131piac->RegisterCommand(kw,usage,this,hgrp);
     132
     133kw = "n/assoc/col";
     134usage = "Merge ntuples columns with an association index";
     135usage += "\n n/assoc/col ntass[,u/i] nt_1[,ext] nt_2[,ext] nt_assoc[,icass]";
     136usage += "\n Merge ntuples nt_1 and nt_2 columns into ntuple ntass";
     137usage += "\n   with line association number given by ntuple nt_assoc";
     138usage += "\n ---";
     139usage += "\n icass: association number given by column \"icass\" of nt_assoc (def=0)";
     140usage += "\n ---";
     141usage += "\n ext : character string to add at the end of the ntuple variable names";
     142usage += "\n   if \"ext\"=\"!\" no extension is added";
     143usage += "\n   if no \"ext\" is given, automatic extension \"_0\" \"_1\" \"_2\" ...";
     144usage += "\n ---";
     145usage += "\n u   : do the union of all the lines of nt_1 and nt_2";
     146usage += "\n       (all ssociated and un-associated lines of nt_1 and nt_2)";
     147usage += "\n i   : do the intersection of all the lines of nt_1 and nt_2";
     148usage += "\n       (only associated lines of nt_1 and nt_2)";
     149usage += "\n def : write all lines of nt_1 with possible associated line of nt_2";
     150usage += "\n       (number of entries of ntass = number of entries of nt_1)";
    131151piac->RegisterCommand(kw,usage,this,hgrp);
    132152
     
    302322} else if(kw == "n/merge/col") {
    303323  n_merge_col(tokens); return(0);
     324} else if(kw == "n/assoc/col") {
     325  n_assoc_col(tokens); return(0);
    304326} else if(kw == "n/copy") {
    305327  n_copy(tokens); return(0);
     
    919941     nvar = nt1->NVar();
    920942     vector<string> sntvn;
    921      for(int i=0;i<nvar;i++) sntvn.push_back(nt1->ColumnName(i));
     943     for(int iv=0;iv<nvar;iv++) sntvn.push_back(nt1->ColumnName(iv));
    922944     char **ntvn = new char*[nvar];
    923      for(int i=0;i<nvar;i++) ntvn[i] = const_cast<char *>(sntvn[i].c_str());
     945     for(int iv=0;iv<nvar;iv++) ntvn[iv] = const_cast<char *>(sntvn[iv].c_str());
    924946     nt = new NTuple(nvar,ntvn);
    925947     delete [] ntvn;
     
    930952   int n = (nvar1>nvar)? nvar1: nvar;
    931953   r_8 *xnt1 = new r_8[n];
    932    for(int i=0;i<n;i++) xnt1[i]=0.;
     954   for(int iev=0;iev<n;iev++) xnt1[iev]=0.;
    933955   for(uint_4 iev=0;iev<(uint_4)nt1->NEntry();iev++) {
    934956     nt1->GetVecD(iev,xnt1);
     
    10081030   posfirst.push_back(ipos);
    10091031   if(nt1->NEntry()>(int_4)nentmax) nentmax = nt1->NEntry();
    1010    for(int i=0;i<nt1->NVar();i++) {
    1011      string str = nt1->ColumnName(i) + ext;
     1032   for(int iv=0;iv<nt1->NVar();iv++) {
     1033     string str = nt1->ColumnName(iv) + ext;
    10121034     varname.push_back(str);
    10131035   }
     
    10261048 int nvar = varname.size();
    10271049 char **ntvn = new char*[nvar];
    1028  for(int i=0;i<nvar;i++) ntvn[i] = const_cast<char *>(varname[i].c_str());
     1050 for(int iv=0;iv<nvar;iv++) ntvn[iv] = const_cast<char *>(varname[iv].c_str());
    10291051 NTuple * nt = new NTuple(nvar,ntvn);
    10301052 //for(int i=0;i<(int_4)ntname.size();i++) cout<<ntname[i]<<" , p="<<posfirst[i]<<endl;
     
    10481070     <<" ntuples, "<<nt->NEntry()<<" entries"<<endl;
    10491071 if(nt->NEntry()>0) omg.AddObj(nt,nament); else delete nt;
     1072
     1073 return;
     1074}
     1075
     1076/* methode */
     1077void PAWExecutor::n_assoc_col(vector<string>& tokens)
     1078{
     1079 if(tokens.size()<4) {
     1080   cerr<<"Usage: n/assoc/col ntass[,u] nt_1[,ext] nt_2[,ext] nt_assoc[,icass]"<<endl;
     1081   return;
     1082 }
     1083
     1084 NamedObjMgr omg;
     1085
     1086 // decodage du nom du ntuple final associe
     1087 // do_merge = 0  :  on remplit toutes les lignes de nt_1 associees ou non
     1088 //                  lignes associees + non-associees de nt_1
     1089 //                  -> ntass.NEntry == nt_1.NEntry
     1090 //          = 1  :  on remplit uniquement les lignes associees
     1091 //                  lignes associees uniquement
     1092 //                  -> ntass.NEntry <= min(nt_1.NEntry,nt_2.NEntry)
     1093 //          = 2  :  on remplit l'union des lignes
     1094 //                  lignes associees + non-associees de nt_1 + non-associees de nt_2
     1095 //                  -> ntass.NEntry >= max(nt_1.NEntry,nt_2.NEntry)
     1096 uint_2 do_merge=0;
     1097 string ntmerge = tokens[0];
     1098 int lc = tokens[0].size();
     1099 int p = tokens[0].find(',');
     1100 if(p>=0 && p<lc) {
     1101   ntmerge = ntmerge.substr(0,p);
     1102   p = tokens[0].find(",u");
     1103   if(p>=0 && p<lc) do_merge = 2;
     1104   p = tokens[0].find(",i");
     1105   if(p>=0 && p<lc) do_merge = 1;
     1106 }
     1107 cout<<"n_assoc_col: final ntuple "<<ntmerge<<", do_merge="<<do_merge<<endl;
     1108
     1109 // boucle pour decoder et valider les 3 ntuples
     1110 NTuple *nt[3];
     1111 string ntname;
     1112 vector<string> varname;
     1113 int icass = 0;
     1114
     1115 for(int i=0;i<3;i++) {
     1116
     1117   // Decode NTuple name and extension
     1118   ntname = tokens[i+1];
     1119   int_4 lc = ntname.size();
     1120   string ext = "";
     1121   int p = ntname.find(',');
     1122   if(p>=0 && p<lc) {
     1123     if(p<lc-1) ext = ntname.substr(p+1,lc-p-1);
     1124     if(p>0) ntname = ntname.substr(0,p); else ntname = "";
     1125   }
     1126   // choose the extension: given,automatic,none
     1127   if(i==2) {  // numero de colonne avec l'index d'association
     1128     if(ext[0] == '!') ext = "";
     1129     if(ext.size()>0) icass = atoi(ext.c_str());
     1130   } else {
     1131     if(ext.size()<1) {
     1132       char str[8]; sprintf(str,"_%d",i);
     1133       ext = str;
     1134     } else if(ext[0] == '!') {
     1135       ext = "";
     1136     }
     1137   }
     1138
     1139   AnyDataObj* mobj = omg.GetObj(ntname);
     1140   if(mobj==NULL) {
     1141     cout<<"n_assoc_col Error: unknow object "<<ntname<<" ("<<tokens[i]<<")"<<endl;
     1142     return;
     1143   }
     1144   nt[i] = dynamic_cast<NTuple*>(mobj);
     1145   if(nt[i]==NULL) {
     1146     cout<<"n_assoc_col Error: "<<ntname<<" not a NTuple"<<endl;
     1147     return;
     1148   }
     1149   if(nt[i]->NEntry()==0) {
     1150     cout<<"n_assoc_col Error: "<<ntname<<" is empty"<<endl;
     1151     return;
     1152   }
     1153   if(nt[i]->NVar()==0) {
     1154     cout<<"n_assoc_col Error: "<<ntname<<" has no variable"<<endl;
     1155     return;
     1156   }
     1157
     1158   if(i<2)
     1159     for(int iv=0;iv<nt[i]->NVar();iv++) {
     1160       string str = nt[i]->ColumnName(iv) + ext;
     1161       varname.push_back(str);
     1162     }
     1163
     1164 }
     1165
     1166 // Checking size of associated ntuple
     1167 if(nt[0]->NEntry() != nt[2]->NEntry()) {
     1168   cout<<"n_assoc_col Error: nentries mismatch "
     1169       <<" first ntuple"<<"="<<nt[0]->NEntry()
     1170       <<", assoc ntuple"<<"="<<nt[2]->NEntry()<<endl;
     1171   return;
     1172 }
     1173 if(icass<0 || icass>=nt[2]->NVar()) {
     1174   cout<<"n_assoc_col Error: bad association column number "
     1175       <<icass<<" / "<<nt[2]->NVar()<<endl;
     1176   return;
     1177 }
     1178
     1179 // create receiving ntuple
     1180 int nvar = varname.size();
     1181 char **ntvn = new char*[nvar];
     1182 for(int iv=0;iv<nvar;iv++) ntvn[iv] = const_cast<char *>(varname[iv].c_str());
     1183 NTuple * ntass = new NTuple(nvar,ntvn);
     1184 delete [] ntvn;
     1185
     1186 // Filling with current ntuple
     1187 // the first column gives the associated line in nt[1]
     1188 int_4 nfill[2]={0,0};
     1189 r_8 *xass = new r_8[nt[2]->NVar()];
     1190 r_8 *xnt = new r_8[nvar];
     1191 uint_2 *use2 = NULL;
     1192 if(do_merge==2) {
     1193   use2 = new uint_2[nt[1]->NEntry()];
     1194   for(int i=0;i<nt[1]->NEntry();i++) use2[i] = 0;
     1195 }
     1196 for(int iev=0;iev<nt[0]->NEntry();iev++) {
     1197   nt[2]->GetVecD(iev,xass);
     1198   int iass = (xass[icass]<0.) ? -1: int(xass[icass]+0.5);
     1199   bool okass = (iass>=0 && iass<nt[1]->NEntry());
     1200   if(do_merge==1 && !okass) continue; // pas d'assoc et intersection demandee
     1201   for(int iv=0;iv<nvar;iv++) xnt[iv]=0.;
     1202   nt[0]->GetVecD(iev,xnt);
     1203   if(okass) {
     1204     nt[1]->GetVecD(iass,&xnt[nt[0]->NVar()]);
     1205     if(do_merge==2) use2[iass] = 1;
     1206   } else nfill[0]++;
     1207   ntass->Fill(xnt);
     1208 }
     1209 // Filling with remaining unassociated in nt[1] if requested
     1210 if(do_merge==2) {
     1211   for(int iev=0;iev<nt[1]->NEntry();iev++) {
     1212     if(use2[iev]!=0) continue; // deja rempli
     1213     for(int iv=0;iv<nvar;iv++) xnt[iv]=0.;
     1214     nt[1]->GetVecD(iev,&xnt[nt[0]->NVar()]);
     1215     ntass->Fill(xnt);
     1216     nfill[1]++;
     1217   }
     1218 }
     1219
     1220 // Ending and saving merged ntuple
     1221 delete [] xnt; delete [] xass;
     1222 if(use2) delete [] use2;
     1223 cout<<"n_assoc_col: ntuple "<<ntmerge
     1224     <<" filled with "<<ntass->NEntry()<<" entries"
     1225     <<", unassoc in nt_1="<<nfill[0]
     1226     <<", unassoc in nt_2="<<nfill[1]<<endl;
     1227 omg.AddObj(ntass,ntmerge);
    10501228
    10511229 return;
  • trunk/SophyaPI/PIext/pawexecut.h

    r2816 r2817  
    2929  void  n_merge(vector<string>& tokens);
    3030  void  n_merge_col(vector<string>& tokens);
     31  void  n_assoc_col(vector<string>& tokens);
    3132  void  n_copy(vector<string>& tokens);
    3233  void  h_integ(vector<string>& tokens);
Note: See TracChangeset for help on using the changeset viewer.