Locked History Actions

attachment:TArtCounter.cc of ANAROOT/掲示板

Attachment 'TArtCounter.cc'

Download

#include <iostream>
#include <string>
#include <sys/stat.h>

#include <TROOT.h>

#include "TArtCore.hh"

#include "TArtCounter.hh"

TArtCounter* TArtCounter::fCounter = 0;
//_____________________________________________________________________
TArtCounter* TArtCounter::Instance() 
{
  if (fCounter==0) fCounter = new TArtCounter();
  return fCounter;
}
//_____________________________________________________________________
TArtCounter* TArtCounter::SetCounterFromRIDF(const char* ridfname) 
{
  if (fCounter==0) fCounter = new TArtCounter();
  fCounter->Reset();
  if (strlen(ridfname)!=0) fCounter->SetMaxNum(ridfname);
  return fCounter;
}
//_____________________________________________________________________
TArtCounter* TArtCounter::SetCounter(const unsigned int num) 
{
  if (fCounter==0) fCounter = new TArtCounter();
  fCounter->Reset();
  fCounter->SetMaxNum(num);
  return fCounter;
}
//_____________________________________________________________________
void TArtCounter::Draw()
{

  if (fCurrentNum==0) fStartTime = time(NULL);
  if (fOldTime==0)    fOldTime   = time(NULL);
  time_t CurrentTime = time(NULL);
  double DiffTime = difftime(CurrentTime,fOldTime);

  if (DiffTime>=1) {// update every 1sec

    unsigned int DiffNum = fCurrentNum - fOldNum;

    Double_t PassedTime    = difftime(CurrentTime, fStartTime);
    Double_t RemainingTime;
    if (fMaxNum==0) RemainingTime = 0;
    else            RemainingTime = DiffTime/DiffNum * (fMaxNum-fCurrentNum);
    char unit_p = Unit(PassedTime);
    char unit_r = Unit(RemainingTime);

    DispWithRestTime(fCurrentNum, fMaxNum, PassedTime,unit_p, RemainingTime, unit_r);
    fOldNum = fCurrentNum;
    fOldTime = CurrentTime;
  }

  if (fCurrentNum==fMaxNum) { // the last event
    Double_t PassedTime    = difftime(CurrentTime, fStartTime);
    char unit_p = Unit(PassedTime);
    DispWithRestTime(fCurrentNum, fCurrentNum, PassedTime,unit_p, 0, 's');
    printf("/n");
  }

  ++fCurrentNum;
}
//_____________________________________________________________________
void TArtCounter::DispWithRestTime(const unsigned int n_curr, 
                                   const unsigned int n_tot,
                                   const double t_pas, const char unit_p, 
                                   const double t_rem , const char unit_r)
{
  std::string bar="";

  if (n_tot!=0){
    int MAX_len=30;
    int len=MAX_len*n_curr/n_tot;
    int i=0;
    while (i<MAX_len){
      if (i<len)
        bar+="=";
      else if (i==len)
        bar+=">";
      else
        bar+=" ";
      i++;
    }
  }

  std::string bar_end;
  bar_end = changeRotstring();

  bar=bar+bar_end;
  printf("\r");
  fprintf(stdout,"%s %10d (%5.1f%c passed, remaining is %5.1f%c)",
          bar.c_str(),n_curr,t_pas,unit_p,t_rem,unit_r);
  fflush(stdout);
}
//_____________________________________________________________________________
std::string TArtCounter::changeRotstring(){
  std::string str;
  switch(fDispIndex){
  case 0:
    str="|";
    break;
  case 1:
    str="/";
    break;
  case 2:
    str="-";
    break;
  case 3:
    str="\\";
    break;
  }
  fDispIndex++;
  if   (fDispIndex>3)  fDispIndex=0;
  else if (fDispIndex<0) fDispIndex=0;

  return str;
}
//_____________________________________________________________________________
char TArtCounter::Unit(double& t){
  char unit;
  if (t>60) {
    t = t / 60.;
    unit = 'm';
  }else 
    unit = 's';

  if (t>60) {
    t = t / 60.;
    unit = 'h';
  }
  if (t>60) {
    t = t / 24.;
    unit = 'd';
  }
  if (t>365) {
    t = t / 365.;
    unit = 'y';
  }
  return unit;
}
//_____________________________________________________________________________
unsigned int TArtCounter::SetMaxNum(const char* ridfname)
{
  struct stat st;

  if(stat(ridfname, &st)){
//    std::cout << "Can't open " << ridfname << std::endl;
//    return 0;
    TArtCore::Warning(__FILE__,"Can't open ridf file, set MaxNum to 0.");
    return 0;
  }

  FILE *fd;
  if(!(fd = fopen(ridfname, "r"))){
//    std::cout << "Can't open " << ridfname << std::endl;
//    return 0;
    TArtCore::Warning(__FILE__,"Can't open ridf file, set MaxNum to 0.");
    return 0;
  }

  ridf_hdst chd;
  char buff[1024];
  fread(buff, 1, sizeof(chd), fd);
  fread(buff, 1, sizeof(buff), fd);

  unsigned int lenum=0;
  int ci=0;
  int ebsz=0;
  int total_ebsz=0;
  int sz=0;
  fseeko(fd,0,SEEK_END);
  while(st.st_size>=total_ebsz){

    fseeko(fd,-4,SEEK_CUR);
    fread(&ebsz, 1, sizeof(int), fd);
    fseeko(fd,-2*ebsz,SEEK_CUR);
    total_ebsz+=2*ebsz;

    ridf_hdst xhd;
    fread(&xhd, 1, sizeof(xhd), fd);
    ci=ridf_ci(xhd);
    sz=ridf_sz(xhd);
    if(ci != 0  && ci != 1 && ci != 2) break;
    if(ridf_sz(xhd) != ebsz) break;

    while(ci!=9){
      fread(&xhd, 1, sizeof(xhd), fd);
      ci=ridf_ci(xhd);
      sz=ridf_sz(xhd);
      if(ci==3||ci==6){
        fread(&lenum, 1, sizeof(int), fd);
        fseeko(fd,2*sz-sizeof(xhd)-sizeof(int),SEEK_CUR);
      }
      else{
        fseeko(fd,2*sz-sizeof(xhd),SEEK_CUR);
      }
    }

    if(lenum != 0) break;

    fread(&ebsz,1, sizeof(int), fd);
    fseeko(fd,-2*ebsz,SEEK_CUR);
    ci = 0;
  }

  fclose(fd);

  fMaxNum = lenum;
  return lenum;
}
//______________________________________________________________________
int TArtCounter::ridf_ci(struct ridf_hdst hd){
  return (hd.hd1 & 0x0fc00000) >> 22;
}
//______________________________________________________________________
int TArtCounter::ridf_sz(struct ridf_hdst hd){
  return hd.hd1 & 0x003fffff;
}
//______________________________________________________________________

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.