1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 07/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2008
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MParEmulated
|
---|
28 | //
|
---|
29 | // Storage Container for emulated branches
|
---|
30 | //
|
---|
31 | // Thanks to roots streaming mechanism simple branches can be recreated
|
---|
32 | // from a file. To read these kind of foreign branches the root system
|
---|
33 | // allocates memory. We can get a pointer to this memory and the
|
---|
34 | // offsets to the data, thus allowing to use this data and ecapsulate
|
---|
35 | // it into the MARS environment. This is done using MParEmulated.
|
---|
36 | //
|
---|
37 | /////////////////////////////////////////////////////////////////////////////
|
---|
38 | #include "MParEmulated.h"
|
---|
39 |
|
---|
40 | #include <TPRegexp.h>
|
---|
41 | #include <TMethodCall.h>
|
---|
42 | #include <TStreamerElement.h>
|
---|
43 |
|
---|
44 | #include "MLog.h"
|
---|
45 | #include "MLogManip.h"
|
---|
46 |
|
---|
47 | ClassImp(MParEmulated);
|
---|
48 |
|
---|
49 | using namespace std;
|
---|
50 |
|
---|
51 | // --------------------------------------------------------------------------
|
---|
52 | //
|
---|
53 | // Default constructor.
|
---|
54 | //
|
---|
55 | MParEmulated::MParEmulated(const char *name, const char *title)
|
---|
56 | : fPtr(0)
|
---|
57 | {
|
---|
58 | fName = name ? name : "MParEmulated";
|
---|
59 | fTitle = title ? title : "Parameter container emulating a class";
|
---|
60 | }
|
---|
61 |
|
---|
62 | // --------------------------------------------------------------------------
|
---|
63 | //
|
---|
64 | // The default is to print all emulated data members. If an option is
|
---|
65 | // given it is interpreted as TPRegexp (regular expression) and only
|
---|
66 | // data members matching this regex are printed.
|
---|
67 | //
|
---|
68 | void MParEmulated::Print(Option_t *o) const
|
---|
69 | {
|
---|
70 | TString opt(o);
|
---|
71 | if (opt.IsNull())
|
---|
72 | opt = ".*";
|
---|
73 |
|
---|
74 | TPRegexp regex(opt);
|
---|
75 | Print(regex, fClassName, "", 0);
|
---|
76 | }
|
---|
77 |
|
---|
78 | // --------------------------------------------------------------------------
|
---|
79 | //
|
---|
80 | // Get the class with name clsname and its corresponding streamer info
|
---|
81 | //
|
---|
82 | TStreamerInfo *MParEmulated::GetStreamerInfo(const TString &clsname) const
|
---|
83 | {
|
---|
84 | TClass *cls = gROOT->GetClass(clsname);
|
---|
85 | if (!cls)
|
---|
86 | {
|
---|
87 | *fLog << err << dbginf << "ERROR - Class " << clsname << " not in dictionary." << endl;
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 |
|
---|
91 | TStreamerInfo *info = cls->GetStreamerInfo();
|
---|
92 | if (!info)
|
---|
93 | {
|
---|
94 | *fLog << err << dbginf << "ERROR - No TStreamerInfo for class " << clsname << "." << endl;
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | return info;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // --------------------------------------------------------------------------
|
---|
102 | //
|
---|
103 | // Get the method call for the given method and offset, add method to
|
---|
104 | // MParContainer::fgListmethodCall
|
---|
105 | //
|
---|
106 | TMethodCall *MParEmulated::GetMethodCall(const char *get, Int_t offset) const
|
---|
107 | {
|
---|
108 | TMethodCall *call = new TMethodCall(MParEmulated::Class(), get, Form("%d", offset));;
|
---|
109 | fgListMethodCall.Add(call);
|
---|
110 | return call;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // --------------------------------------------------------------------------
|
---|
114 | //
|
---|
115 | // Get the getter method for the given data member. Since we have no real
|
---|
116 | // getter methods and no real data members we have to fake the TMethodCall.
|
---|
117 | //
|
---|
118 | TMethodCall *MParEmulated::GetterMethod(const char *name, TString clsname, Int_t offset) const
|
---|
119 | {
|
---|
120 | TStreamerInfo *info = GetStreamerInfo(clsname);
|
---|
121 | if (!info)
|
---|
122 | return 0;
|
---|
123 |
|
---|
124 | const TString arg(name);
|
---|
125 |
|
---|
126 | const Ssiz_t p = arg.First('.');
|
---|
127 |
|
---|
128 | const TString nam = p<0 ? arg : arg(0, p);
|
---|
129 |
|
---|
130 | Int_t off;
|
---|
131 | TStreamerElement *el = info->GetStreamerElement(nam, off);
|
---|
132 | if (!el)
|
---|
133 | {
|
---|
134 | *fLog << err << dbginf << "ERROR - No TStreamerInfo for " << nam << " [" << clsname << "]" << endl;
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | const TString type = el->GetTypeNameBasic();
|
---|
139 |
|
---|
140 | if (type=="int")
|
---|
141 | return GetMethodCall("GetInt", offset+off);
|
---|
142 |
|
---|
143 | if (type=="double")
|
---|
144 | return GetMethodCall("GetDouble", offset+off);
|
---|
145 |
|
---|
146 | if (p<0)
|
---|
147 | {
|
---|
148 | *fLog << err << dbginf << "ERROR - No TStreamerInfo for " << nam << "." << type << " [" << clsname << "]" << endl;
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | const TString var = arg(p+1, arg.Length());
|
---|
153 | return GetterMethod(var, type, offset+off);
|
---|
154 | }
|
---|
155 |
|
---|
156 | // --------------------------------------------------------------------------
|
---|
157 | //
|
---|
158 | // Print the requested data from our memory using the streamer info.
|
---|
159 | //
|
---|
160 | void MParEmulated::Print(TPRegexp ®ex, TString clsname, TString prefix, Int_t offset) const
|
---|
161 | {
|
---|
162 | TStreamerInfo *info = GetStreamerInfo(clsname);
|
---|
163 | if (!info)
|
---|
164 | return;
|
---|
165 |
|
---|
166 | TIter Next(info->GetElements());
|
---|
167 | TStreamerElement *el = 0;
|
---|
168 | while ((el=(TStreamerElement*)Next()))
|
---|
169 | {
|
---|
170 | const TString str = prefix+el->GetName();
|
---|
171 |
|
---|
172 | if (str(regex).IsNull())
|
---|
173 | continue;
|
---|
174 |
|
---|
175 | if (el->InheritsFrom(TStreamerBasicType::Class()))
|
---|
176 | {
|
---|
177 | const TString type(el->GetTypeNameBasic());
|
---|
178 |
|
---|
179 | cout << fName << "." << str << "[" << type << "] \t";
|
---|
180 | if (type=="int")
|
---|
181 | cout << GetInt(el->GetOffset()+offset);
|
---|
182 |
|
---|
183 | if (type=="double")
|
---|
184 | cout << GetDouble(el->GetOffset()+offset);
|
---|
185 |
|
---|
186 | cout << endl;
|
---|
187 | continue;
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (el->InheritsFrom(TStreamerObjectAny::Class()))
|
---|
191 | {
|
---|
192 | Print(regex, el->GetTypeNameBasic(), str+".",
|
---|
193 | el->GetOffset()+offset);
|
---|
194 | continue;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|