-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detector.cxx
126 lines (111 loc) · 2.72 KB
/
Detector.cxx
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Simple detector base class
#include "Detector.h"
#include "Decoder.h"
#include "Variable.h"
#include <iostream>
#include <algorithm>
using namespace std;
Detector::Detector( string _name, int _imod )
: name(std::move(_name))
, type("--baseclass--")
, imod(_imod-1)
, fVars(nullptr)
{
if( imod<0 ) {
cerr << "\"" << name << "\": "
<< "Warning: invalid module number = " << _imod << endl;
}
}
// Clear event-by-event data
void Detector::Clear()
{
data.clear();
}
// Initialize detector
// If 'shared' is true, initialize data shared among all instances
int Detector::Init( bool shared )
{
int ret = ReadDatabase(shared);
if( !shared )
DefineVariables( kDefine );
return ret;
}
// Generic detector decoding
int Detector::Decode( Decoder& evdata )
{
auto ndata = evdata.GetNdata(imod);
if( debug > 1 )
Print();
if( debug > 2 )
cout << " Decode: ndata = " << ndata;
if( ndata > 0 ) {
if( debug > 3 )
cout << ", data = ";
double* pdata = evdata.GetDataBuf(imod);
data.assign( pdata, pdata+ndata );
if( debug > 3 ) {
for( decltype(ndata) i = 0; i < ndata; ++i ) {
cout << evdata.GetData(imod, i);
if( i+1 != ndata )
cout << ", ";
}
}
}
if( debug > 2 )
cout << endl;
return 0;
}
void Detector::Print() const
{
cout << "DET(" << type << "): " << name << endl;
}
// Define variables. The default implementation does nothing.
int Detector::DefineVariables( bool /* remove */ )
{
return 0;
}
// Read database. The default implementation does nothing.
int Detector::ReadDatabase( bool /* shared */ ) {
return 0;
}
// Declared in Podd.h
int DefineVarsFromList( const std::vector<VarDef_t>& defs,
const std::string& prefix,
const shared_ptr<varlst_t>& varlst, bool remove )
{
if( defs.empty() or (remove and (not varlst or varlst->empty())) )
return 0;
if( not varlst ) {
cerr << "Missing variable list. Cannot define any variables" << endl;
return 0;
}
auto& vars = *varlst;
int ndef = 0;
for( const auto& def : defs ) {
string varname(prefix);
if( !prefix.empty() ) {
varname += '.';
}
varname.append(def.name);
auto it = find_if( ALL(vars), [&varname](auto& var) {
return (var && var->GetName() == varname);
});
if( remove ) {
if( it != vars.end() ) {
vars.erase(it);
++ndef;
}
} else {
if( it != vars.end() ) {
cerr << "Variable " << varname << " already exists, skipped" << endl;
} else if( !def.loc ) {
cerr << "Invalid location pointer for variable " << varname
<< ", skipped " << endl;
} else {
vars.push_back( make_unique<Variable>(varname, def.note, def.loc) );
++ndef;
}
}
}
return ndef;
}