Belle II Software  release-05-01-25
Utilities.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : Utilities.cc
5 // Section : TRG
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : Utility functions
10 //-----------------------------------------------------------------------------
11 // $Log$
12 //-----------------------------------------------------------------------------
13 
14 #define TRG_SHORT_NAMES
15 
16 #include <sstream>
17 #include <fstream>
18 #include <iostream>
19 #include <iomanip>
20 #include "trg/trg/Utilities.h"
21 
22 using namespace std;
23 
24 namespace Belle2 {
30  string
31  TRGUtil::dateString(void)
32  {
33  time_t t;
34  time(& t);
35  struct tm* tl;
36  tl = localtime(& t);
37  char ts1[80];
38  strftime(ts1, sizeof(ts1), "%Y/%m/%d %H:%M %Z", tl);
39  return (ts1);
40  }
41 
42  string
43  TRGUtil::dateStringF(void)
44  {
45  time_t t;
46  time(& t);
47  struct tm* tl;
48  tl = localtime(& t);
49  char ts0[80];
50  strftime(ts0, sizeof(ts0), "%Y%m%d_%H%M", tl);
51  return string(ts0);
52  }
53 
54  std::string
55  TRGUtil::itostring(int i)
56  {
57  // std::ostringstream s;
58  // s << i;
59  // return s.str();
60 
61  return std::to_string(i);
62  }
63 
64  std::string
65  TRGUtil::dtostring(double d, unsigned int precision)
66  {
67  std::ostringstream s;
68  s << std::setprecision(precision) << d;
69  return s.str();
70  }
71 
72  std::string
73  TRGUtil::carstring(const std::string& s)
74  {
75  std::string ret;
76 // const char * p = str;
77 // while ( *p && isspace(*p) ) p++;
78 // while ( *p && !isspace(*p) ) ret += *(p++);
79  int i;
80  int len = s.length();
81  for (i = 0; i < len; i++) {
82  if (!isspace(s[i])) break;
83  }
84  for (; i < len; i++) {
85  if (!isspace(s[i])) {
86  ret += s[i];
87  } else break;
88  }
89  return ret;
90  }
91 
92  std::string
93  TRGUtil::cdrstring(const std::string& s)
94  {
95 // const char * p = str;
96 // while ( *p && isspace(*p) ) p++;
97 // while ( *p && !isspace(*p) ) p++;
98 // while ( *p && isspace(*p) ) p++;
99  int i;
100  int len = s.length();
101  for (i = 0; i < len; i++) {
102  if (!isspace(s[i])) break;
103  }
104  for (; i < len; i++) {
105  if (isspace(s[i])) break;
106  }
107  for (; i < len; i++) {
108  if (!isspace(s[i])) break;
109  }
110  return s.substr(i);
111  }
112 
113  void
114  TRGUtil::bitDisplay(unsigned val)
115  {
116  bitDisplay(val, 31, 0);
117  }
118 
119  void
120  TRGUtil::bitDisplay(unsigned val, unsigned f, unsigned l)
121  {
122  unsigned i;
123  for (i = 0; i < f - l; i++) {
124  if ((i % 8) == 0) cout << " ";
125  cout << (val >> (f - i)) % 2;
126  }
127  }
128 
129  string
130  TRGUtil::streamDisplay(unsigned val, unsigned f, unsigned l)
131  {
132  string s;
133  for (unsigned i = f; i < l + 1; i++) {
134  if ((i % 8) == 0) s += " ";
135  s += itostring((val >> i) % 2);
136  }
137  return s;
138  }
139 
140  string
141  TRGUtil::streamDisplay(unsigned val)
142  {
143  return streamDisplay(val, 0, 63);
144  }
145 
147 } // namespace Belle2
148 
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19