BOSS 7.0.7
BESIII Offline Software System
Loading...
Searching...
No Matches
OfflineRevise.cxx
Go to the documentation of this file.
1#include <string>
2#include <vector>
3#include <map>
4#include <sstream>
5#include <typeinfo>
6#include <exception>
7
9{
10public:
11 typedef std::vector<SniperJSON>::const_iterator vec_iterator;
12 typedef std::map<std::string, SniperJSON>::const_iterator map_iterator;
13
14 // default constructor
15 SniperJSON();
16
17 // construct from a json string
18 SniperJSON(const std::string &jstr);
19
20 // whether this is a scalar or string
21 bool isScalar() const { return m_type > 2; }
22
23 // whether this is a vector
24 bool isVector() const { return m_type == 2; }
25
26 // whether this is a map
27 bool isMap() const { return m_type == 1; }
28
29 /* get a C++ object from this element, T can be
30 * type[1]: C++ scalar and std::string
31 * type[2]: std::vector< type[1,2,3] >
32 * type[3]: std::map< type[1], type[1,2,3] >
33 */
34 template <typename T>
35 T get() const;
36
37 // push_back an element in case of a vector
38 bool push_back(const SniperJSON &var);
39
40 // insert a key/value pair in case of a map
41 bool insert(const std::string &key, const SniperJSON &val);
42
43 // clear all the contents of this element
44 void reset();
45
46 // get the array/vector or object/map size
47 int size() const;
48
49 // get the vector begin iterator
50 vec_iterator vec_begin() const { return m_jvec.begin(); }
51 // get the vector end iterator
52 vec_iterator vec_end() const { return m_jvec.end(); }
53
54 // get the object in vector via index
55 SniperJSON &operator[](int index) { return m_jvec.at(index); }
56 // get the object in vector via index
57 const SniperJSON &operator[](int index) const { return m_jvec.at(index); }
58
59 // get the map begin iterator
60 map_iterator map_begin() const { return m_jmap.begin(); }
61 // get the map end iterator
62 map_iterator map_end() const { return m_jmap.end(); }
63 // find the iterator via key
64 map_iterator find(const std::string &key) const { return m_jmap.find(key); }
65
66 // get the object in map via key
67 SniperJSON &operator[](const std::string &key);
68 // get the object in map via key
69 const SniperJSON &operator[](const std::string &key) const;
70
71 // load json from an input stream
72 static SniperJSON load(std::istream &is);
73
74 // load json from a string
75 static SniperJSON loads(const std::string &jstr);
76
77private:
78 typedef std::string::size_type StrCursor;
79
80 // 0:invalid, 1:object/map, 2:array/vector, 3:string, 9:other scalars
81 int m_type;
82 // scalar or std::string element holder
83 std::string m_jvar;
84 // array/vector element holder
85 std::vector<SniperJSON> m_jvec;
86 // object/map element holder
87 std::map<std::string, SniperJSON> m_jmap;
88
89 // control string for space charactors
90 static const std::string SPACES;
91 // control string for delimit charactors
92 static const std::string DELIMITS;
93
94 // construct a sub-element from a json string
95 SniperJSON(const std::string &jstr, StrCursor &cursor);
96
97 // initialize the json element from a string
98 void init(const std::string &jstr, StrCursor &cursor);
99
100 // get next valid charactor
101 char getValidChar(const std::string &jstr, StrCursor &cursor);
102
103 // read the content of a json object/map to m_jmap;
104 void readObjectMap(const std::string &jstr, StrCursor &cursor);
105
106 // read the content of a json array/vector to m_jvec
107 void readArrayVec(const std::string &jstr, StrCursor &cursor);
108
109 // read the content of a json string to m_jvar
110 void readStringStr(const std::string &jstr, StrCursor &cursor);
111
112 // read the content of other scalars to m_jvar
113 void readScalarStr(const std::string &jstr, StrCursor &cursor);
114
115 // function template helps to access type[1]
116 template <typename T>
117 inline void toCppVar(T &var) const;
118
119 // function template helps to access type[2]
120 template <typename T>
121 inline void toCppVar(std::vector<T> &var) const;
122
123 // function template helps to access type[3]
124 template <typename K, typename V>
125 inline void toCppVar(std::map<K, V> &var) const;
126
127 // an inner class for any exception while json parsing
128 class Exception : public std::exception
129 {
130 public:
131 Exception(const std::string &msg);
132 Exception(const std::string &jstr, int cursor);
133 virtual ~Exception() throw();
134 const char *what() const throw();
135
136 private:
137 std::string m_msg;
138 };
139};
140
141template <typename T>
142T SniperJSON::get() const
143{
144 T v;
145 toCppVar(v);
146 return v;
147}
148
149template <typename T>
150inline void SniperJSON::toCppVar(T &var) const
151{
152 if (m_type == 9)
153 {
154 std::stringstream ss;
155 ss << m_jvar;
156 ss >> var;
157
158 if (ss.eof())
159 {
160 return;
161 }
162 }
163
164 throw Exception(
165 std::string("cannot set <") + typeid(T).name() + "> with '" + m_jvar + "'");
166}
167
168template <>
169inline void SniperJSON::toCppVar<bool>(bool &var) const
170{
171 if (m_type == 9)
172 {
173 if (m_jvar == "true")
174 {
175 var = true;
176 return;
177 }
178 else if (m_jvar == "false")
179 {
180 var = false;
181 return;
182 }
183 }
184
185 throw Exception(std::string("cannot set <bool> with '") + m_jvar + "'");
186}
187
188template <>
189inline void SniperJSON::toCppVar<std::string>(std::string &var) const
190{
191 if (m_type == 3)
192 {
193 var = m_jvar.substr(1, m_jvar.size() - 2);
194 return;
195 }
196
197 throw Exception(std::string("cannot set <std::string> with '") + m_jvar + "'");
198}
199
200template <typename T>
201inline void SniperJSON::toCppVar(std::vector<T> &var) const
202{
203 if (m_type == 2)
204 {
205 for (vec_iterator it = m_jvec.begin(); it != m_jvec.end(); ++it)
206 {
207 var.push_back(it->get<T>());
208 }
209 return;
210 }
211
212 throw Exception(std::string("not a valid vector"));
213}
214
215//This implementaiton should be replaced by "std::is_same" when c++11 is valid
216namespace PreCXX11 {
217 struct true_value {
218 static bool value;
219 };
220 bool true_value::value = true;
221
222 struct false_value {
223 static bool value;
224 };
225 bool false_value::value = false;
226
227 template<typename, typename>
228 struct is_same : public false_value {};
229
230 template<typename T>
231 struct is_same<T, T> : public true_value {};
232}
233
234template <typename K, typename V>
235inline void SniperJSON::toCppVar(std::map<K, V> &var) const
236{
237 if (m_type == 1)
238 {
239 for (map_iterator it = m_jmap.begin(); it != m_jmap.end(); ++it)
240 {
241 var.insert(std::make_pair(
243 PreCXX11::is_same<K, std::string>::value ? it->first : it->first.substr(1, it->first.size() - 2))
244 .get<K>(),
245 it->second.get<V>()));
246 }
247 return;
248 }
249
250 throw Exception(std::string("not a valid map"));
251}
252
253const std::string SniperJSON::SPACES(" \n\t\r");
254const std::string SniperJSON::DELIMITS(", \n]}\t\r");
255
257 : m_type(0)
258{
259}
260
261SniperJSON::SniperJSON(const std::string &jstr)
262 : m_type(0)
263{
264 StrCursor cursor = 0;
265 init(jstr, cursor);
266
267 cursor = jstr.find_first_not_of(SniperJSON::SPACES, cursor);
268 if (cursor != std::string::npos)
269 {
270 throw Exception(jstr, cursor);
271 }
272}
273
274SniperJSON::SniperJSON(const std::string &jstr, StrCursor &cursor)
275 : m_type(0)
276{
277 init(jstr, cursor);
278}
279
281{
282 if (m_type == 2 || m_type == 0)
283 {
284 m_jvec.push_back(var);
285 m_type = 2;
286 return true;
287 }
288 return false;
289}
290
291bool SniperJSON::insert(const std::string &key, const SniperJSON &val)
292{
293 if (m_type == 1 || m_type == 0)
294 {
295 std::string _key = '"' + key + '"';
296 m_jmap.insert(std::make_pair(_key, val));
297 m_type = 1;
298 return true;
299 }
300 return false;
301}
302
304{
305 m_type = 0;
306 m_jvar.clear();
307 m_jvec.clear();
308 m_jmap.clear();
309}
310
312{
313 if (m_type == 1)
314 {
315 return m_jmap.size();
316 }
317 else if (m_type == 2)
318 {
319 return m_jvec.size();
320 }
321
322 return -1;
323}
324
326{
327 return m_jmap.at('"' + key + '"');
328}
329
330const SniperJSON &SniperJSON::operator[](const std::string &key) const
331{
332 return m_jmap.at('"' + key + '"');
333}
334
336{
337 std::ostringstream oss;
338 oss << is.rdbuf();
339
340 return loads(oss.str());
341}
342
343SniperJSON SniperJSON::loads(const std::string &jstr)
344{
345 return SniperJSON(jstr);
346}
347
348void SniperJSON::init(const std::string &jstr, StrCursor &cursor)
349{
350 switch (getValidChar(jstr, cursor))
351 {
352 case '{': //object
353 readObjectMap(jstr, cursor);
354 m_type = 1;
355 break;
356 case '[': //array
357 readArrayVec(jstr, cursor);
358 m_type = 2;
359 break;
360 case '"': //string
361 readStringStr(jstr, cursor);
362 m_type = 3;
363 break;
364 default: //number, true, false or null
365 readScalarStr(jstr, cursor);
366 m_type = 9;
367 }
368}
369
370char SniperJSON::getValidChar(const std::string &jstr, StrCursor &cursor)
371{
372 cursor = jstr.find_first_not_of(SPACES, cursor);
373 if (cursor != std::string::npos)
374 {
375 return jstr.at(cursor);
376 }
377 throw Exception(jstr, cursor);
378}
379
380void SniperJSON::readObjectMap(const std::string &jstr, StrCursor &cursor)
381{
382 bool status = true;
383
384 if (getValidChar(jstr, ++cursor) != '}')
385 {
386 --cursor; //reset the cursor just before the first key
387 do
388 {
389 readStringStr(jstr, ++cursor);
390 const std::string &key = m_jvar;
391 if (key.size() == 2 || getValidChar(jstr, cursor) != ':')
392 {
393 status = false;
394 break;
395 }
396 m_jmap.insert(std::make_pair(key, SniperJSON(jstr, ++cursor)));
397 } while (getValidChar(jstr, cursor) == ',');
398 }
399
400 m_jvar.clear();
401
402 if (status && getValidChar(jstr, cursor) == '}')
403 {
404 ++cursor;
405 return;
406 }
407
408 throw Exception(jstr, cursor);
409}
410
411void SniperJSON::readArrayVec(const std::string &jstr, StrCursor &cursor)
412{
413 if (getValidChar(jstr, ++cursor) != ']')
414 {
415 --cursor; //reset the cursor just before the first variable
416 do
417 {
418 m_jvec.push_back(SniperJSON(jstr, ++cursor));
419 } while (getValidChar(jstr, cursor) == ',');
420 }
421
422 if (getValidChar(jstr, cursor) == ']')
423 {
424 ++cursor;
425 return;
426 }
427
428 throw Exception(jstr, cursor);
429}
430
431void SniperJSON::readStringStr(const std::string &jstr, StrCursor &cursor)
432{
433 if (getValidChar(jstr, cursor) == '"')
434 {
435 StrCursor pstart = cursor;
436 cursor = jstr.find('"', pstart + 1);
437 if (cursor != std::string::npos)
438 {
439 ++cursor;
440 m_jvar = jstr.substr(pstart, cursor - pstart);
441 return;
442 }
443 }
444
445 throw Exception(jstr, cursor);
446}
447
448void SniperJSON::readScalarStr(const std::string &jstr, StrCursor &cursor)
449{
450 StrCursor pstart = cursor;
451 cursor = jstr.find_first_of(DELIMITS, pstart + 1);
452 if (cursor != std::string::npos || pstart == 0)
453 {
454 m_jvar = jstr.substr(pstart, cursor - pstart);
455 return;
456 }
457
458 throw Exception(jstr, cursor);
459}
460
461SniperJSON::Exception::Exception(const std::string &msg)
462 : m_msg("json error: ")
463{
464 m_msg += msg;
465}
466
467SniperJSON::Exception::Exception(const std::string &jstr, int cursor)
468 : m_msg("invalid json:\n")
469{
470 m_msg += jstr.substr(0, cursor + 1);
471 m_msg += " <<< parsing error";
472}
473
474SniperJSON::Exception::~Exception() throw()
475{
476}
477
478const char *SniperJSON::Exception::what() const throw()
479{
480 return m_msg.c_str();
481}
482
484#include <fstream>
485#include <iostream>
486
487OfflineRevise::OfflineRevise(const std::string& conf)
488 : m_lastRun(-1),
489 m_lastFlag(true)
490{
491 std::ifstream f(conf.c_str());
493
494 {
495 SniperJSON& c1 = json["Ets1MissingSecond"];
496 for (SniperJSON::vec_iterator it = c1["RunRanges"].vec_begin();
497 it != c1["RunRanges"].vec_end();
498 ++it)
499 {
500 m_runRanges.push_back(std::make_pair(
501 (*it)["From"].get<int>(),
502 (*it)["To"].get<int>() ));
503 }
504
505 m_runs = c1["Runs"].get<std::vector<int> >();
506
507 //for ( std::vector<std::pair<int, int> >::iterator it = m_runRanges.begin();
508 // it != m_runRanges.end(); ++it) {
509 // std::cout << "RunRange: " << it->first << ", " << it->second << std::endl;
510 //}
511 //for (std::vector<int>::iterator it = m_runs.begin();
512 // it != m_runs.end(); ++it) {
513 // std::cout << "Run: " << *it << std::endl;
514 //}
515
516 m_tRoundSwitch = json["TimeRoundSwitch"].get<int>();
517 }
518}
519
521{
522}
523
525{
526 bool goodFlag = true;
527 int run = header->runNumber();
528
529 if ( run == m_lastRun ) {
530 goodFlag = m_lastFlag;
531 }
532 else {
533 for ( std::vector<std::pair<int, int> >::iterator it = m_runRanges.begin();
534 it != m_runRanges.end();
535 ++it)
536 {
537 if ( run >= it->first && run <= it->second ) {
538 goodFlag = false;
539 break;
540 }
541 }
542
543 if ( goodFlag && std::find(m_runs.begin(), m_runs.end(), run) != m_runs.end() ) {
544 goodFlag = false;
545 }
546
547 m_lastFlag = goodFlag;
548 m_lastRun = run;
549 m_lastEvent = header->eventNumber();
550 m_t0Sec = header->time();
551 m_t0NanoShift0 = -1;
552 m_t0NanoShift1 = -1;
553 m_lastEts1 = 0;
554 m_lastEts2Old = 0;
555 m_lastEts2New = 0;
556 }
557
558 if ( ! goodFlag ) {
559 fixEts1(header);
560 fixEts2(header);
561 }
562}
563
564void OfflineRevise::fixEts1(Event::EventHeader* header)
565{
566 long tNow = header->time() - m_t0Sec;
567 unsigned long _ets1 = header->etsT1();
568 long ets1NanoSec = _ets1 % 2000000;
569
570 if ( tNow == 0 ) {
571 long _shift0 = ets1NanoSec % 2000000;
572 if ( _shift0 > m_t0NanoShift0 ) {
573 m_t0NanoShift0 = _shift0;
574 }
575 }
576 else if ( tNow == 1 ) {
577 if ( m_t0NanoShift1 == -1 ) {
578 if ( ets1NanoSec > m_t0NanoShift0 ) {
579 m_t0NanoShift1 = ets1NanoSec;
580 //std::cout << "Shift " << m_t0NanoShift0 << " " << m_t0NanoShift1 << std::endl;
581 }
582 }
583 }
584
585 if ( m_t0NanoShift1 > 0 && ets1NanoSec >= m_t0NanoShift1 ) {
586 --tNow;
587 }
588
589 unsigned long ets1 = tNow*2000000 + ets1NanoSec;
590
591 int evtDiff = header->eventNumber() - m_lastEvent;
592
593 if ( labs(ets1-m_lastEts1) < 1000000 && abs(evtDiff) < 100 ) {
594 //m_lastEts1 = ets1;
595 }
596 else {
597 if ( ets1NanoSec > m_t0NanoShift0-200000 ) {
598 long tDiff = ets1NanoSec - m_lastEts1%2000000;
599 if ( tDiff > 1000000 ) {
600 if ( evtDiff < 100 ) {
601 tNow = m_lastEts1/2000000 - 1;
602 }
603 }
604 else if ( tDiff < -1000000 ) {
605 if ( evtDiff > 100 ) {
606 tNow = m_lastEts1/2000000 + 1;
607 }
608 }
609 else {
610 tNow = m_lastEts1/2000000;
611 }
612 ets1 = tNow*2000000 + ets1NanoSec;
613 }
614 }
615
616 header->setEtsT1(ets1);
617 m_lastEvent = header->eventNumber();
618 if ( abs(evtDiff) < 100 ) {
619 m_lastEts1 = ets1;
620 }
621}
622
623void OfflineRevise::fixEts2(Event::EventHeader* header)
624{
625 unsigned long _ets2 = header->etsT2();
626
627 if ( _ets2 != 0 ) {
628 if ( _ets2 == m_lastEts2Old ) {
629 header->setEtsT2(m_lastEts2New);
630 }
631 else {
632 unsigned long ets1 = header->etsT1();
633 long ets1NanoSec = ets1%2000000;
634 long tNow = ets1/2000000;
635
636 long ets2NanoSec = _ets2 % 2000000;
637
638 long tDiff = ets2NanoSec - ets1NanoSec;
639 if ( tDiff > 1000000 ) {
640 --tNow;
641 }
642 else if ( tDiff < -1000000 ) {
643 ++tNow;
644 }
645
646 unsigned long ets2 = tNow*2000000 + ets2NanoSec;
647
648 header->setEtsT2(ets2);
649 m_lastEts2Old = _ets2;
650 m_lastEts2New = ets2;
651 }
652 }
653}
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
#define private
*************DOUBLE PRECISION m_pi *DOUBLE PRECISION m_HvecTau2 DOUBLE PRECISION m_HvClone2 DOUBLE PRECISION m_gamma1 DOUBLE PRECISION m_gamma2 DOUBLE PRECISION m_thet1 DOUBLE PRECISION m_thet2 INTEGER m_IFPHOT *COMMON c_Taupair $ !Spin Polarimeter vector first Tau $ !Spin Polarimeter vector second Tau $ !Clone Spin Polarimeter vector first Tau $ !Clone Spin Polarimeter vector second Tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning st tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !Random Euler angle for cloning nd tau $ !phi of HvecTau1 $ !theta of HvecTau1 $ !phi of HvecTau2 $ !theta of HvecTau2 $ !super key
Definition: Taupair.h:42
int eventNumber() const
Retrieve event number.
Definition: EventHeader.h:37
int runNumber() const
Retrieve run number.
Definition: EventHeader.h:42
unsigned long etsT2()
Definition: EventHeader.h:65
unsigned int time() const
Definition: EventHeader.h:46
void setEtsT2(unsigned long value)
Definition: EventHeader.h:71
void setEtsT1(unsigned long value)
Update ETS.
Definition: EventHeader.h:69
unsigned long etsT1()
Retrieve ETS.
Definition: EventHeader.h:63
virtual ~OfflineRevise()
void fixHeader(Event::EventHeader *header)
OfflineRevise(const std::string &conf)
bool insert(const std::string &key, const SniperJSON &val)
T get() const
bool isVector() const
std::vector< SniperJSON >::const_iterator vec_iterator
static SniperJSON loads(const std::string &jstr)
map_iterator find(const std::string &key) const
int size() const
std::map< std::string, SniperJSON >::const_iterator map_iterator
map_iterator map_begin() const
bool push_back(const SniperJSON &var)
static SniperJSON load(std::istream &is)
SniperJSON & operator[](int index)
vec_iterator vec_end() const
map_iterator map_end() const
bool isMap() const
vec_iterator vec_begin() const
const SniperJSON & operator[](int index) const
bool isScalar() const
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")