ROOTANA
Loading...
Searching...
No Matches
manalyzer_example_flow.cxx
Go to the documentation of this file.
1//
2// MIDAS analyzer example 3: C++ flow analyzer
3//
4// K.Olchanski
5//
6
7#include <stdio.h>
8
9#include "manalyzer.h"
10#include "midasio.h"
11
12class Object1 : public TAFlowEvent
13{
14public:
15 int fIntValue = 0;
16
17 Object1(TAFlowEvent* flow, int value)
18 : TAFlowEvent(flow)
19 {
20 fIntValue = value;
21 }
22};
23
24class Object2 : public TAFlowEvent
25{
26public:
27 std::string fStringValue;
28
29 Object2(TAFlowEvent* flow, const std::string& stringValue)
30 : TAFlowEvent(flow)
31 {
32 fStringValue = stringValue;
33 }
34};
35
36class Object3 : public TAFlowEvent
37{
38public:
39 double* fPtrValue = NULL;
40
41 Object3(TAFlowEvent* flow, double* doublePtr)
42 : TAFlowEvent(flow)
43 {
44 fPtrValue = doublePtr;
45 }
46
47 ~Object3() // dtor
48 {
49 if (fPtrValue) {
50 delete fPtrValue;
51 fPtrValue = NULL;
52 }
53 }
54};
55
56class Example1: public TARunObject
57{
58public:
60 : TARunObject(runinfo)
61 {
62 printf("Example1::ctor, run %d, file %s\n", runinfo->fRunNo, runinfo->fFileName.c_str());
63 fModuleName = "Example1";
64 fModuleOrder = 1;
65 }
66
68 {
69 printf("Example1::dtor!\n");
70 }
71
72 TAFlowEvent* Analyze(TARunInfo* runinfo, TMEvent* event, TAFlags* flags, TAFlowEvent* flow)
73 {
74 printf("Example1::Analyze, run %d, event serno %d, id 0x%04x, data size %d\n", runinfo->fRunNo, event->serial_number, (int)event->event_id, event->data_size);
75
76 flow = new Object1(flow, 10);
77 flow = new Object2(flow, "some text");
78
79 return flow;
80 }
81
83 {
84 // This function doesn't analyze anything, so we use flags
85 // to have the profiler ignore it
86 *flags |= TAFlag_SKIP_PROFILE;
87 return flow;
88 }
89};
90
91class Example2: public TARunObject
92{
93public:
95 : TARunObject(runinfo)
96 {
97 printf("Example2::ctor, run %d, file %s\n", runinfo->fRunNo, runinfo->fFileName.c_str());
98 fModuleName = "Example2";
99 fModuleOrder = 2;
100 }
101
103 {
104 printf("Example2::dtor!\n");
105 }
106
107 void PreEndRun(TARunInfo* runinfo)
108 {
109 TAFlowEvent* flow = NULL;
110
111 printf("Example2::PreEndRun, run %d\n", runinfo->fRunNo);
112
113 double *dptr = new double;
114 *dptr = 17.1;
115
116 flow = new Object3(flow, dptr);
117
118 runinfo->AddToFlowQueue(flow);
119 }
120
121 TAFlowEvent* Analyze(TARunInfo* runinfo, TMEvent* event, TAFlags* flags, TAFlowEvent* flow)
122 {
123 printf("Example2::Analyze, run %d, event serno %d, id 0x%04x, data size %d\n", runinfo->fRunNo, event->serial_number, (int)event->event_id, event->data_size);
124
125 double *dptr = new double;
126 *dptr = 3.14;
127
128 flow = new Object3(flow, dptr);
129
130 return flow;
131 }
132
134 {
135 printf("Example2::AnalyzeFlowEvent, run %d\n", runinfo->fRunNo);
136
137 // example iterating over flow events
138
139 if (flow) {
140 TAFlowEvent* f = flow;
141 while (f) {
142 Object1* o1 = dynamic_cast<Object1*>(f);
143 Object2* o2 = dynamic_cast<Object2*>(f);
144 Object3* o3 = dynamic_cast<Object3*>(f);
145
146 printf("flow event %p, o1: %p, o2: %p, o3: %p\n", f, o1, o2, o3);
147
148 if (o1)
149 printf("object1 int value: %d\n", o1->fIntValue);
150
151 if (o2)
152 printf("object2 string value: %s\n", o2->fStringValue.c_str());
153
154 if (o3)
155 printf("object3 pointer to double value: %f\n", *o3->fPtrValue);
156
157 f = f->fNext;
158 }
159 }
160
161 // example of profiling a custom time window
162 TAClock start_time = TAClockNow();
163
164 // example direct get of flow events
165 if (flow) {
166 Object1* o1 = flow->Find<Object1>();
167 Object2* o2 = flow->Find<Object2>();
168 Object3* o3 = flow->Find<Object3>();
169
170 if (o1)
171 printf("find object1 int value: %d\n", o1->fIntValue);
172
173 if (o2)
174 printf("find object2 string value: %s\n", o2->fStringValue.c_str());
175
176 if (o3)
177 printf("find object3 pointer to double value: %f\n", *o3->fPtrValue);
178 }
179
180 flow = new TAUserProfilerFlow(flow, "FindObjects", start_time);
181
182 // example of locking threads to execute code that isnt thread safe across modules
183 // At time of writing root (v6.16) fitting tools are not thread safe...
184
185 if (flow) {
186 Object1* o1 = flow->Find<Object1>();
187 if (o1) {
188 //Lock while in the scope of these brackets
189 std::lock_guard<std::mutex> lock(runinfo->fMtInfo->gfLock);
190 printf("Do some function here... maybe some fitting function from root that isn't threadsafe\n");
191 }
192 }
193
194 return flow;
195 }
196};
197
200
201/* emacs
202 * Local Variables:
203 * tab-width: 8
204 * c-basic-offset: 3
205 * indent-tabs-mode: nil
206 * End:
207 */
Example1(TARunInfo *runinfo)
TAFlowEvent * Analyze(TARunInfo *runinfo, TMEvent *event, TAFlags *flags, TAFlowEvent *flow)
TAFlowEvent * AnalyzeFlowEvent(TARunInfo *runinfo, TAFlags *flags, TAFlowEvent *flow)
void PreEndRun(TARunInfo *runinfo)
TAFlowEvent * AnalyzeFlowEvent(TARunInfo *runinfo, TAFlags *flags, TAFlowEvent *flow)
TAFlowEvent * Analyze(TARunInfo *runinfo, TMEvent *event, TAFlags *flags, TAFlowEvent *flow)
Example2(TARunInfo *runinfo)
Object1(TAFlowEvent *flow, int value)
std::string fStringValue
Object2(TAFlowEvent *flow, const std::string &stringValue)
Object3(TAFlowEvent *flow, double *doublePtr)
TAFlowEvent * fNext
Definition manalyzer.h:58
T * Find()
Definition manalyzer.h:64
static std::mutex gfLock
Definition manalyzer.h:204
void AddToFlowQueue(TAFlowEvent *)
std::string fFileName
Definition manalyzer.h:25
int fRunNo
Definition manalyzer.h:24
TAMultithreadHelper * fMtInfo
Definition manalyzer.h:28
std::string fModuleName
Definition manalyzer.h:91
int fModuleOrder
Definition manalyzer.h:92
uint32_t serial_number
MIDAS event serial number.
Definition midasio.h:59
uint32_t data_size
MIDAS event data size.
Definition midasio.h:61
uint16_t event_id
MIDAS event ID.
Definition midasio.h:57
std::chrono::high_resolution_clock::time_point TAClock
Definition manalyzer.h:218
int TAFlags
Definition manalyzer.h:79
TAClock TAClockNow()
Definition manalyzer.h:220
#define TAFlag_SKIP_PROFILE
Definition manalyzer.h:86
static TARegister tar1(new TAFactoryTemplate< Example1 >)
static TARegister tar2(new TAFactoryTemplate< Example2 >)