Belle II Software  release-08-01-10
Utilities.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #define TRG_SHORT_NAMES
10 
11 #include <sstream>
12 #include <fstream>
13 #include <iostream>
14 #include <iomanip>
15 #include "trg/trg/Utilities.h"
16 
17 using namespace std;
18 
19 namespace Belle2 {
25  string
26  TRGUtilities::dateString(void)
27  {
28  time_t t;
29  time(& t);
30  struct tm* tl;
31  tl = localtime(& t);
32  char ts1[80];
33  strftime(ts1, sizeof(ts1), "%Y/%m/%d %H:%M %Z", tl);
34  return (ts1);
35  }
36 
37  string
38  TRGUtilities::dateStringF(void)
39  {
40  time_t t;
41  time(& t);
42  struct tm* tl;
43  tl = localtime(& t);
44  char ts0[80];
45  strftime(ts0, sizeof(ts0), "%Y%m%d_%H%M", tl);
46  return string(ts0);
47  }
48 
49  std::string
50  TRGUtilities::itostring(int i)
51  {
52  // std::ostringstream s;
53  // s << i;
54  // return s.str();
55 
56  return std::to_string(i);
57  }
58 
59  std::string
60  TRGUtilities::dtostring(double d, unsigned int precision)
61  {
62  std::ostringstream s;
63  s << std::setprecision(precision) << d;
64  return s.str();
65  }
66 
67  std::string
68  TRGUtilities::carstring(const std::string& s)
69  {
70  std::string ret;
71 // const char * p = str;
72 // while ( *p && isspace(*p) ) p++;
73 // while ( *p && !isspace(*p) ) ret += *(p++);
74  int i;
75  int len = s.length();
76  for (i = 0; i < len; i++) {
77  if (!isspace(s[i])) break;
78  }
79  for (; i < len; i++) {
80  if (!isspace(s[i])) {
81  ret += s[i];
82  } else break;
83  }
84  return ret;
85  }
86 
87  std::string
88  TRGUtilities::cdrstring(const std::string& s)
89  {
90 // const char * p = str;
91 // while ( *p && isspace(*p) ) p++;
92 // while ( *p && !isspace(*p) ) p++;
93 // while ( *p && isspace(*p) ) p++;
94  int i;
95  int len = s.length();
96  for (i = 0; i < len; i++) {
97  if (!isspace(s[i])) break;
98  }
99  for (; i < len; i++) {
100  if (isspace(s[i])) break;
101  }
102  for (; i < len; i++) {
103  if (!isspace(s[i])) break;
104  }
105  return s.substr(i);
106  }
107 
108  void
109  TRGUtilities::bitDisplay(unsigned val)
110  {
111  bitDisplay(val, 31, 0);
112  }
113 
114  void
115  TRGUtilities::bitDisplay(unsigned val, unsigned f, unsigned l)
116  {
117  unsigned i;
118  for (i = 0; i < f - l; i++) {
119  if ((i % 8) == 0) cout << " ";
120  cout << (val >> (f - i)) % 2;
121  }
122  }
123 
124  string
125  TRGUtilities::streamDisplay(unsigned val, unsigned f, unsigned l)
126  {
127  string s;
128  for (unsigned i = f; i < l + 1; i++) {
129  if ((i % 8) == 0) s += " ";
130  s += itostring((val >> i) % 2);
131  }
132  return s;
133  }
134 
135  string
136  TRGUtilities::streamDisplay(unsigned val)
137  {
138  return streamDisplay(val, 0, 63);
139  }
140 
142 } // namespace Belle2
143 
Abstract base class for different kinds of events.