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