Locked History Actions

attachment:TArtCSVParameter.cc of ANAROOT/掲示板

Attachment 'TArtCSVParameter.cc'

Download

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

#include <TString.h>

#include "TArtCore.hh"
#include "TArtCSVParameter.hh"

using namespace std;

//___________________________________________________________________________
TArtCSVParameter::TArtCSVParameter(){
  fDoc = xmlNewDoc((xmlChar*)"1.0");   //1.0 is XML version
  fRootNode = xmlNewNode(NULL,(xmlChar*)"dataroot");
  xmlDocSetRootElement(fDoc,fRootNode);

  ftRootNode = new TXMLNode(fRootNode); // for ROOT XML
}
//___________________________________________________________________________
TArtCSVParameter::~TArtCSVParameter(){
  xmlFreeDoc(fDoc);  // free the document
  xmlCleanupParser();// free the global variables that may have been allocated  by the parser
  xmlMemoryDump();   // this is to debug memory for regression tests
}
//___________________________________________________________________________
void TArtCSVParameter::LoadParameterList(const char *filename){
  ifstream fs(filename);
  if ( fs.fail() ){
    TArtCore::Error(__FILE__,"Cannot open %s", filename);
    return;
  }
  string line;
  Int_t iline=0;
  while(getline(fs,line,'\n')){

    /* Skip space line */
    TString tsline=line;
    tsline=tsline.Strip(TString::kBoth);
    if (tsline.IsNull()) {iline++;continue;}

    // skip comment line
    if (tsline.First("//")==0) {iline++;continue;}

    istringstream ss(line);string s;
    int ncol=0;
    vector<TString> v_str(2);
    while (getline(ss,s,',')){
      TString tsline = s;
      tsline=tsline.Strip(TString::kBoth);
      if (ncol>=2) {
        TArtCore::Error(__FILE__,"Too much Column at %d in %s", 
                        iline+1, filename);
        break;
      }
      v_str[ncol] = tsline;
      ++ncol;
    }
    LoadParameter(v_str[0].Data(),v_str[1].Data());
    iline++;
  }
  fs.close();
}
//___________________________________________________________________________
void TArtCSVParameter::LoadParameter(const char *NodeName, const char *csvfile){
  ifstream fs(csvfile);
  if ( fs.fail() ){
    TArtCore::Error(__FILE__,"Cannot open %s", csvfile);
    return;
  }

  vector<TString> str_line0;

  string line;
  int iline=0;
  while(getline(fs,line,'\n')){

    /* Skip space line */
    TString tsline=line;
    tsline=tsline.Strip(TString::kBoth);
    if (tsline.IsNull()) {iline++;continue;}

    // skip comment line
    if (tsline.First("//")==0) {iline++;continue;}

    //--------------------------------------
    /* Scan one line */
    int  i=0;
    bool IDExist=false;
    xmlNodePtr Node;
    istringstream ss(line);string s;
    while (getline(ss,s,',')){

      TString ts=s;
      ts=ts.Strip(TString::kBoth);
      //--------------------------------------
      if (iline==0) { // first line : Node Names
        if (ts.IsNull())
          TArtCore::Warning(__FILE__,"Empty column at the first line in  %s", csvfile);
        str_line0.push_back(ts); // store Node names

      //--------------------------------------
      }else{          // 2nd. line- : Node Contents
        if (ts.IsNull()){++i; continue;}// skip space column

        if (i>=(int)str_line0.size()){ 
          TArtCore::Warning(__FILE__,
                            "Too much column at line %d in  %s",iline+1,csvfile);
          continue;}

        //--------------------------------------
        if ("ID"==str_line0[i]) { // ID column
          Node = FindSameIDNode(NodeName, "ID",ts.Data());
          if (0==Node) {          // ID is not found, create new Node

            Node = xmlNewNode(NULL,(xmlChar*)NodeName);
            xmlNewChild(Node,NULL,(xmlChar*)str_line0[i].Data(),(xmlChar*)ts.Data());
            xmlAddChild(fRootNode,Node);
          }else IDExist=true;     // ID has already existed

        //--------------------------------------
        }else{                    // column is not for ID
          if (IDExist){
            xmlNodePtr NodeMatch = FindNode(Node,str_line0[i].Data());
            if (NodeMatch==0) 
              xmlNewChild(Node,NULL,(xmlChar*)str_line0[i].Data(),(xmlChar*)ts.Data());
            else              
              xmlNodeSetContent(NodeMatch,(xmlChar*)ts.Data()); // update the content
          }else  // Chlid Node is added when ID does not exist
            xmlNewChild(Node,NULL,(xmlChar*)str_line0[i].Data(),(xmlChar*)ts.Data());
        }
      }
      ++i; //increment of column number
    }
    ++iline;//increment of line number
  }
  fs.close();
}
//___________________________________________________________________________
xmlNodePtr TArtCSVParameter::FindNode(xmlNodePtr NodePare, const char *NodeName){
  xmlNodePtr node_child = NodePare->children;
  while (node_child!=NULL){
    if (strcmp(NodeName,(char*)node_child->name)==0) return node_child;
    node_child = node_child->next;
  }
  return 0;
}
//___________________________________________________________________________
xmlNodePtr TArtCSVParameter::FindSameIDNode(const char *NodeName, const char *NodeContent){
  xmlNodePtr NodePare = fRootNode->children;
  while(NodePare!=NULL){
    xmlNodePtr node_child = NodePare->children;
    while (node_child!=NULL){
      if (strcmp(NodeName,(char*)node_child->name)!=0) {
        node_child=node_child->next;
        continue;
      }
      if (strcmp(NodeContent,(char*)xmlNodeGetContent(node_child))==0)
             return NodePare;
      node_child = node_child->next;
    }
    NodePare = NodePare->next;
  }
  return 0;
}
//___________________________________________________________________________
// for test
xmlNodePtr TArtCSVParameter::FindSameIDNode(const char *PareNodeName,
                                            const char *NodeName, const char *NodeContent){
  xmlNodePtr NodePare = fRootNode->children;//SAMURAIPla, etc...
  while(NodePare!=NULL){
    xmlNodePtr node_child = NodePare->children;// ID, Layer, etc...
    if (strcmp((char*)NodePare->name,PareNodeName)!=0) {
      NodePare = NodePare->next;
      continue;
    }
    while (node_child!=NULL){
      if (strcmp(NodeName,(char*)node_child->name)!=0) {
        node_child=node_child->next;
        continue;
      }
      if (strcmp(NodeContent,(char*)xmlNodeGetContent(node_child))==0)
             return NodePare;
      node_child = node_child->next;
    }
    NodePare = NodePare->next;
  }
  return 0;
}
//___________________________________________________________________________
void TArtCSVParameter::Write(const char *xmlfile){
  //output for check
  xmlSaveFormatFileEnc(xmlfile, fDoc, "UTF-8", 1); // output to file
}
//___________________________________________________________________________

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2012-10-17 06:59:08, 278.1 KB) [[attachment:ANAROOT講習会資料.pdf]]
  • [get | view] (2013-06-25 06:26:53, 0.5 KB) [[attachment:CounterExample.cc]]
  • [get | view] (2012-10-17 06:53:24, 1.6 KB) [[attachment:MakePlaHist.C]]
  • [get | view] (2012-08-20 06:37:41, 6.2 KB) [[attachment:TArtCSVParameter.cc]]
  • [get | view] (2012-08-20 06:37:53, 1.0 KB) [[attachment:TArtCSVParameter.hh]]
  • [get | view] (2013-06-25 06:26:20, 5.3 KB) [[attachment:TArtCounter.cc]]
  • [get | view] (2013-06-25 06:26:40, 1.2 KB) [[attachment:TArtCounter.hh]]
  • [get | view] (2012-11-29 12:05:55, 4.8 KB) [[attachment:TArtParameters.cc]]
  • [get | view] (2012-11-29 12:05:35, 1.0 KB) [[attachment:TArtParameters.hh]]
  • [get | view] (2012-12-04 15:34:16, 6.3 KB) [[attachment:TArtUserParameters.cc]]
  • [get | view] (2012-12-04 15:34:00, 1.6 KB) [[attachment:TArtUserParameters.hh]]
  • [get | view] (2012-12-04 15:34:38, 1.6 KB) [[attachment:UserParameters.xml]]
  • [get | view] (2012-04-19 08:38:22, 3.0 KB) [[attachment:ana-mode.el]]
  • [get | view] (2012-04-20 03:38:36, 2.7 KB) [[attachment:anaroot_filter.ver1.00.jar]]
  • [get | view] (2012-06-04 04:07:52, 2.6 KB) [[attachment:anaroot_filter.ver1.01.jar]]
  • [get | view] (2012-06-01 17:27:17, 4.7 KB) [[attachment:calc2xml.v2.xsl]]
  • [get | view] (2012-04-20 03:33:51, 6.3 KB) [[attachment:calc2xml.xsl]]
  • [get | view] (2012-10-17 07:32:11, 1.0 KB) [[attachment:install-example.txt]]
  • [get | view] (2012-11-29 12:06:11, 1.0 KB) [[attachment:parameters.xml]]
  • [get | view] (2012-04-20 03:33:17, 3.1 KB) [[attachment:xml2calc.xsl]]

You are not allowed to attach a file to this page.