#include "TXMLEngine.h"
 
 
 
// const char nodes[][20]={"TMON0","TMON1", "dna", "VDD", "V2P","V2N","VSS","VTH1","VTH2","VCC12","VCC15","VCC25","V38P",0};
 
 
 
const double ul[20]   ={  20, 0.25 };
 
const double ll[20]   ={  -0.1,-0.1 };
 
 
 
Int_t mode=0;
 
Int_t mid=0;
 
Int_t chid=0;
 
const char measurement[2][0xFF]={"HV test;N,I(uA)","LV test &firmware upload;N;I(A)"};
 
const char smode[2][0xFF]={"n1470","pw18"};
 
const int nch[2]={1,4};
 
const char nodes[2][0xFF]={"V","I"};
 
const double hmax[2]   ={ 20, 0.25 };
 
double current=0;
 
double voltage=0;
 
void hinit( int id){
 
    char name[0xFF];
 
            sprintf(name,"title%d",id);
 
            TPaveText *pt =  new TPaveText(.05,.4,.95,.8);
 
                pt->SetName(name);
 
                gDirectory->GetList()->Add(pt);
 
                for (mode=0;mode<2;mode++){
 
                        for (chid=0;chid<nch[mode];chid++){
 
                                for (int inode=0;inode<2;inode++){
 
                                  sprintf(name,"%s_%s_%d_ch%d",smode[mode],nodes[inode],id,chid);
 
                                  printf("name=%s\n",name);
 
                                  TGraph *g0 = (TGraph *) (gROOT->FindObject(name)) ;
 
                          if (g0) printf("%s already exists!\n",name);
 
                
 
                                  TGraph *g=  new TGraph();
 
                                  g->SetName(name);
 
                                  g->SetLineColor(chid%9+1);
 
                                  g->SetLineWidth(3);
 
                                  gDirectory->GetList()->Add(g);
 
                                }
 
                        }
 
                }
 
 
 
    mid=0;
 
    mode=0;
 
    chid=0;     
 
}       
 
        
 
void hplot(int id, int what){
 
        int i=0;
 
        char name[0xFF];
 
        char title[0xFF];
 
        
 
   leg = new TLegend(0.60,0.9-nch[what]*0.1,0.99,0.9);
 
   //leg->SetHeader("Legend");
 
        for (i=0;i<nch[what];i++){
 
                sprintf(title,"%s",measurement[what]);
 
                sprintf(name,"%s_%s_%d_ch%d",smode[what],"I",id,i);
 
                printf("hplot name=%s\n",name);
 
                TGraph *g = (TGraph *) (gROOT->FindObject(name)) ;
 
                if (g ) {
 
                        
 
                    if (i==0) {
 
                                sprintf(name,"h2%s_%s_%d",smode[what],"I",id);
 
                                TH2F *frame = new TH2F(name,title,g->GetN(),0,g->GetN()+7,40,0,hmax[what]);
 
                frame->SetStats(0);                             
 
                frame->Draw(); 
 
                        }
 
                        double x, y;
 
                        g->GetPoint(g->GetN()-1,x,y);
 
                        if (y>ll[what] && y<ul[what]){
 
                            sprintf(title,"CH %d\t=%2.2f A",i, y);
 
                        }       else   {
 
                                sprintf(title,"CH %d\t=%2.2f A ERROR",i, y);
 
                        }
 
                        leg->AddEntry(g,title,"l");
 
                        if (g->GetN())  g->Draw("ls");
 
                } else {
 
                        printf("%s not found!\n",name);
 
                }       
 
        }
 
        leg->Draw();
 
}
 
 
 
void hend(){
 
   int i=0;
 
   char name[0xFF];   
 
   TCanvas *c[4];
 
   
 
   for ( i=0;i<4;i++) {
 
         sprintf(name,"cfebps%d",i);
 
         c[i] = new TCanvas(name,name, 500,800);
 
     c[i]->Divide(1,3);
 
         c[i]->cd(1);
 
         sprintf(name,"title%d",i);
 
     TPaveText *pt =  (TPaveText *) (gROOT->FindObject(name)); 
 
     if (pt) pt->Draw(); 
 
     c[i]->cd(2);        
 
         hplot(i,1);
 
         c[i]->cd(3);
 
         hplot(i,0);
 
         c[i]->Modified();
 
     c[i]->Update();
 
         printf("tekst je=%s\n", pt);
 
        }
 
}
 
 
 
 
 
 
 
void DisplayNode(TXMLEngine* xml, XMLNodePointer_t node, Int_t level, const char *fname) 
 
{
 
   // this function display all accessible information about xml node and its children
 
   
 
   printf("%*c node: %s\n",level,' ', xml->GetNodeName(node));
 
   
 
   // display namespace
 
   XMLNsPointer_t ns = xml->GetNS(node);
 
   if (ns!=0)
 
      printf("%*c namespace: %s refer: %s\n",level+2,' ', xml->GetNSName(ns), xml->GetNSReference(ns));
 
   
 
   // display attributes
 
   XMLAttrPointer_t attr = xml->GetFirstAttr(node);
 
   while (attr!=0) {
 
       printf("%*c attr: %s value: %s\n",level+2,' ', xml->GetAttrName(attr), xml->GetAttrValue(attr));
 
           if (strcmp(xml->GetNodeName(node),"feb")==0  && strcmp(xml->GetAttrName(attr),"id")==0 )  {
 
             mid=atoi( xml->GetAttrValue(attr) );
 
             printf("ID---->%d\n",mid);
 
       }
 
           if (strcmp(xml->GetNodeName(node),"pw18")==0  && strcmp(xml->GetAttrName(attr),"id")==0 )  {
 
             chid=atoi( xml->GetAttrValue(attr) );
 
       }
 
           if (strcmp(xml->GetNodeName(node),"n1470")==0  && strcmp(xml->GetAttrName(attr),"id")==0 )  {
 
             chid=atoi( xml->GetAttrValue(attr) );
 
       }
 
       attr = xml->GetNextAttr(attr);  
 
   }
 
      
 
   // display content (if exists)
 
   const char* content = xml->GetNodeContent(node);
 
   if (strcmp(xml->GetNodeName(node),"n1470")==0) {
 
                printf("mode %s\n",smode[mode]); 
 
                mode=0;
 
       printf("N1470 Channel ID---->%d\n",chid);                
 
   }      
 
   if (strcmp(xml->GetNodeName(node),"pw18")==0) {
 
            mode=1;
 
                printf("PW18 Channel ID---->%d\n",chid);
 
   }
 
   
 
   if (content!=0){
 
      printf("%*c cont: %s\n",level+2,' ', content);
 
          if (strcmp(xml->GetNodeName(node),"dna")==0) {
 
                  
 
                  char name[0xFF];
 
                  sprintf(name,"title%d",mid);
 
                  TPaveText *pt = (TPaveText *) (gROOT->FindObject(name)) ; 
 
                  if (pt) {
 
                        sprintf(name,"DNA=%s",content);  
 
                        pt->AddText(name);  
 
                  }
 
          }
 
      if (strcmp(xml->GetNodeName(node),"sn")==0 && mode==0) {
 
                  char name[0xFF];
 
                  sprintf(name,"title%d",mid);
 
                  TPaveText *pt = (TPaveText *) (gROOT->FindObject(name)) ; 
 
                  if (pt) {
 
                        sprintf(name,"SN =   %s",content);
 
            pt->AddText(fname);                 
 
                        pt->AddText(name);  
 
                  }
 
          }     
 
      if (strcmp(xml->GetNodeName(node),"time")==0) {
 
                  char name[0xFF];
 
                  for (int ii=0;ii<4;ii++){
 
                    sprintf(name,"title%d",ii);
 
                    TPaveText *pt = (TPaveText *) (gROOT->FindObject(name)) ; 
 
                    if (pt) {
 
                          sprintf(name,"Time=\t%s",content);  
 
                          pt->AddText("ARICH FEB QA test");
 
                          pt->AddText(name);  
 
                    }
 
              }
 
          }               
 
      if (strcmp(xml->GetNodeName(node),"V")==0) {
 
            voltage =atof( content );
 
                char name[0xFF];
 
                sprintf(name,"%s_%s_%d_ch%d",smode[mode],xml->GetNodeName(node),mid,chid);
 
                TGraph *g = (TGraph *) (gROOT->FindObject(name)) ;
 
                if (g) g->SetPoint(g->GetN(), g->GetN(), voltage );
 
                else printf("%s not found!\n",name);
 
      }
 
          if (strcmp(xml->GetNodeName(node),"I")==0) {
 
            current =atof( content );
 
                char name[0xFF];
 
                sprintf(name,"%s_%s_%d_ch%d",smode[mode],xml->GetNodeName(node),mid,chid);
 
                TGraph *g = (TGraph *) (gROOT->FindObject(name)) ;
 
                if (g) g->SetPoint(g->GetN(), g->GetN(), current );
 
                else printf("%s not found!\n",name);
 
      }
 
          
 
   }   
 
   // display all child nodes   
 
   XMLNodePointer_t child = xml->GetChild(node);
 
   while (child!=0) {
 
      DisplayNode(xml, child, level+2, fname); 
 
      child = xml->GetNext(child);
 
   }
 
}
 
 
 
 
 
void febpsxml2root(const char* filename = "febps_0001.xml")
 
{
 
   
 
   // First create engine
 
   TXMLEngine* xml = new TXMLEngine;
 
   
 
   // Now try to parse xml file
 
   // Only file with restricted xml syntax are supported
 
   XMLDocPointer_t xmldoc = xml->ParseFile(filename);
 
   if (xmldoc==0) {
 
      delete xml;
 
      return;  
 
   }
 
 
 
   // take access to main node   
 
   XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
 
   
 
   // display recursively all nodes and subnodes
 
   for (int i=0;i<4;i++) hinit(i);
 
  
 
   DisplayNode(xml, mainnode, 1, filename);
 
   hend();  
 
   // Release memory before exit
 
   xml->FreeDoc(xmldoc);
 
   delete xml; 
 
}