Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaEvent.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 17/05/2000
2
4//
5// THaEvent
6//
7// This is the base class for "event" objects, which define the
8// structure of the event data to be written to the output DST
9// via ROOT object I/O.
10//
11// Any derived class must implement at least the following:
12//
13// - Define all member variables that are to appear in the output
14// for each event (a la ntuple).
15// - Set up the fDataMap array to link these member variables to
16// global analyzer variables.
17//
18// Data members can be simple types (Double_t etc.) and any ROOT objects
19// capable of object I/O (i.e. with a valid Streamer() class).
20// For any data members other than simple types defined in fDataMap,
21// the Fill() method has to be extended accordingly.
22//
24
25#include "THaEvent.h"
26#include "THaVarList.h"
27#include "THaGlobals.h"
28#include "TClass.h"
29#include <cstring> // for memcpy
30
33
34//_____________________________________________________________________________
35THaEvent::THaEvent() : fInit(false)
36{
37 // Create a THaEvent object.
38 Class()->IgnoreTObjectStreamer();
39}
40
41//_____________________________________________________________________________
43{
44 // Reset
45}
46
47//_____________________________________________________________________________
49{
50 // Copy global variables specified in the data map to the event structure.
51
52 static const char* const here = "Fill()";
53
54 // Initialize datamap if not yet done
55 if( !fInit ) {
56 Int_t status = Init();
57 if( status )
58 return status;
59 }
60
61 // Move the data into the members variables of this Event object
62 //
63 // For basic types, this can be reduced to a memcpy. Knowledge of the
64 // type of the destination is not necessary, BUT the user needs to ensure
65 // the destination type matches the source type -> potential for error!
66 //
67 // For object types, this is no longer that easy (without rewriting the
68 // variable system); we really have to convert the data retrieved via
69 // GetData() to the type of the destination.
70 //
71 // These complications would go away if this class were ever rewritten
72 // with an intelligent streamer.
73 //
74
75 Int_t nvar = 0;
76 for( const auto& datamap : fDataMap ) {
77 if( datamap.ncopy == 0 ) break;
78 if( !datamap.pvar ) continue;
79
80 THaVar* pvar = datamap.pvar;
81 Int_t ncopy = datamap.ncopy;
82 if( ncopy < 0 ) {
83 if( datamap.ncopyvar )
84 ncopy = *datamap.ncopyvar;
85 else {
86 ncopy = pvar->GetLen();
87 if( ncopy == THaVar::kInvalidInt )
88 continue;
89 }
90 }
91 Int_t type = pvar->GetType();
92 size_t size = pvar->GetTypeSize();
93 const void* src = pvar->GetValuePointer();
94 if( !src ) continue;
95 if( pvar->IsBasic() ) {
96 if( type >= kDoubleP ) {
97 // Pointers to pointers - get the pointer they currently point to
98 void** loc = (void**)src;
99 src = (const void*)(*loc);
100 }
101 if( !src ) continue;
102 if( !pvar->IsPointerArray() )
103 memcpy( datamap.dest, src, ncopy*size );
104 else {
105 // For pointer arrays, we need to copy the elements one by one
106 // Type doesn't matter for memcpy, but size does ;)
107 for( int i=0; i<ncopy; i++ ) {
108 const int** psrc = (const int**)src;
109 int* dest = static_cast<int*>( datamap.dest );
110 memcpy( dest+i, *(psrc+i), size );
111 }
112 }
113 } else {
114 // Object variables: Now we have to worry about the type of
115 // the destination. We assume it is the same type as the
116 // source. Ugly and inefficient, but good enough for now I guess.
117 // Note: Function return values are either Long_t or Double_t,
118 // regardless of the actual return value of the function.
119 // This is a ROOT limitation.
120 for( int i=0, j=0; i<ncopy; i++, j++ ) {
121 Double_t val = pvar->GetValue(i);
122 if( val == THaVar::kInvalid )
123 continue;
124 switch( type ) {
125 case kDouble: case kDoubleP:
126 *(((Double_t*)datamap.dest)+j) = val;
127 break;
128 case kFloat: case kFloatP:
129 *(((Float_t*)datamap.dest)+j) = static_cast<Float_t>(val);
130 break;
131 case kInt: case kIntP:
132 *(((Int_t*)datamap.dest)+j) = static_cast<Int_t>(val);
133 break;
134 case kUInt: case kUIntP:
135 *(((UInt_t*)datamap.dest)+j) = static_cast<UInt_t>(val);
136 break;
137 case kShort: case kShortP:
138 *(((Short_t*)datamap.dest)+j) = static_cast<Short_t>(val);
139 break;
140 case kUShort: case kUShortP:
141 *(((UShort_t*)datamap.dest)+j) = static_cast<UShort_t>(val);
142 break;
143 case kLong: case kLongP:
144 *(((Long64_t*)datamap.dest)+j) = static_cast<Long64_t>(val);
145 break;
146 case kULong: case kULongP:
147 *(((ULong64_t*)datamap.dest)+j)= static_cast<ULong64_t>(val);
148 break;
149 case kChar: case kCharP:
150 *(((Char_t*)datamap.dest)+j) = static_cast<Char_t>(val);
151 break;
152 case kByte: case kByteP:
153 *(((Byte_t*)datamap.dest)+j) = static_cast<Byte_t>(val);
154 break;
155 default:
156 Warning( here, "Unknown type for variable %s "
157 "Not filled.", pvar->GetName() );
158 j--; nvar--;
159 break;
160 }
161 }
162 }
163 nvar += ncopy;
164 }
165
166 return nvar;
167}
168
169//_____________________________________________________________________________
171{
172 // Initialize fDataMap. Called automatically by Fill() as necessary.
173
174 if( !gHaVars ) return -2;
175
176 for( auto& datamap : fDataMap ) {
177 if( datamap.ncopy == 0 ) break;
178 if( THaVar* pvar = gHaVars->Find( datamap.name )) {
179 datamap.pvar = pvar;
180 } else {
181 Warning("Init()", "Global variable %s not found. "
182 "Will be filled with zero.", datamap.name );
183 datamap.pvar = nullptr;
184 }
185 }
186 fInit = true;
187 return 0;
188}
189
190//_____________________________________________________________________________
192{
193 // Reset pointers to global variables. Forces Init() to be executed
194 // at next Fill(), which re-associates global variables with the
195 // event structure and histograms.
196
197 fInit = false;
198}
199
200
int Int_t
unsigned int UInt_t
size_t size(const MatrixT &matrix)
unsigned short UShort_t
unsigned char Byte_t
char Char_t
float Float_t
short Short_t
double Double_t
const char Option_t
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
R__EXTERN class THaVarList * gHaVars
Definition THaGlobals.h:11
static const char *const here
Definition THaVar.cxx:64
virtual void Clear(Option_t *opt="")
Definition THaEvent.cxx:42
virtual Int_t Init()
Definition THaEvent.cxx:170
Bool_t fInit
Definition THaEvent.h:72
std::vector< DataMap > fDataMap
Definition THaEvent.h:82
virtual Int_t Fill()
Definition THaEvent.cxx:48
virtual void Reset(Option_t *opt="")
Definition THaEvent.cxx:191
virtual THaVar * Find(const char *name) const
Bool_t IsBasic() const
Definition THaVar.h:63
Bool_t IsPointerArray() const
Definition THaVar.h:66
Double_t GetValue(Int_t i=0) const
Definition THaVar.h:48
VarType GetType() const
Definition THaVar.h:43
Int_t GetLen() const
Definition THaVar.h:39
static const Double_t kInvalid
Definition THaVar.h:24
const void * GetValuePointer() const
Definition THaVar.h:51
static const Long64_t kInvalidInt
Definition THaVar.h:23
size_t GetTypeSize() const
Definition THaVar.h:44
const char * GetName() const override
virtual void Warning(const char *method, const char *msgfmt,...) const
long long Long64_t
unsigned long long ULong64_t
const char * Class
ClassImp(TPyArg)