forked from pwittich/mictest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.h
90 lines (75 loc) · 2.7 KB
/
Debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef _debug_
#define _debug_
#ifdef DEBUG
/*
Usage: DEBUG must be defined before this header file is included, typically
#define DEBUG
#include "Debug.h"
This defines macros dprint(), dcall() and dprintf();
dprint(x) is equivalent to std::cout << x << std::endl;
example: dprint("Hits in layer=" << ilayer);
dcall(x) simply calls x
example: dcall(pre_prop_print(ilay, mkfp));
dprintf(x) is equivalent to printf(x)
example: dprintf("Bad label for simtrack %d -- %d\n", itrack, track.label());
All printouts are also controlled by a bool variable "debug"
bool debug = true; is declared as a file global in an anonymous
namespace, and thus can be overridden within any interior scope
as needed, so one could change the global to false and only set
a local to true within certain scopes.
All are protected by a file scope mutex to avoid mixed printouts.
This mutex can also be acquired within a block via dmutex_guard:
if (debug) {
dmutex_guard;
[do complicated stuff]
}
The mutex is not reentrant, so avoid using dprint et al. within a scope
where the mutex has already been acquired, as doing so will deadlock.
*/
#include <mutex>
#define dmutex_guard std::lock_guard<std::mutex> dlock(debug_mutex)
#define dprint(x) if (debug) { dmutex_guard; std::cout << x << std::endl; }
#define dcall(x) if (debug) { dmutex_guard; x; }
#define dprintf(...) if (debug) { dmutex_guard; printf(__VA_ARGS__); }
namespace {
bool debug = true; // default, can be overridden locally
std::mutex debug_mutex;
}
static void print(const TrackState& s)
{
std::cout << "x: " << s.parameters[0]
<< " y: " << s.parameters[1]
<< " z: " << s.parameters[2] << std::endl
<< "px: " << s.parameters[3]
<< " py: " << s.parameters[4]
<< " pz: " << s.parameters[5] << std::endl
<< "valid: " << s.valid << " errors: " << std::endl;
dumpMatrix(s.errors);
std::cout << std::endl;
}
static void print(std::string label, int itrack, const Track& trk)
{
std::cout << std::endl << label << ": " << itrack << " hits: " << trk.nFoundHits() << " State" << std::endl;
print(trk.state());
}
static void print(std::string label, const TrackState& s)
{
std::cout << label << std::endl;
print(s);
}
static void print(std::string label, const MeasurementState& s)
{
std::cout << label << std::endl;
std::cout << "x: " << s.parameters()[0]
<< " y: " << s.parameters()[1]
<< " z: " << s.parameters()[2] << std::endl
<< "errors: " << std::endl;
dumpMatrix(s.errors());
std::cout << std::endl;
}
#else
#define dprint(x) (void(0))
#define dcall(x) (void(0))
#define dprintf(...) (void(0))
#endif
#endif