Belle II Software prerelease-11-00-00d
StoreEntry.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/datastore/StoreEntry.h>
9#include <framework/dataobjects/RelationContainer.h>
10#include <framework/logging/Logger.h>
11
12#include <TClass.h>
13#include <TClonesArray.h>
14
15#include <utility>
16
17using namespace Belle2;
18
19StoreEntry::StoreEntry(bool isArray_, TClass* cl, std::string name_, bool dontWriteOut_):
20 isArray(isArray_),
21 dontWriteOut(dontWriteOut_),
22 objClass(cl),
23 object(nullptr),
24 ptr(nullptr),
25 name(std::move(name_))
26{
28}
29
31{
32 if (object)
33 return;
34 if (isArray) {
35 // The TClonesArray is created with a small initial capacity instead of the
36 // ROOT default of 1000: it grows geometrically as soon as it is filled, so
37 // nothing is lost for the arrays which are actually used, while resetting
38 // an array (TClonesArray::Delete(), see resetForGetEntry()) costs time
39 // proportional to the *capacity* and not to the number of stored objects.
40 // Most of the DataStore entries are empty most of the time, so this makes
41 // clearing them (e.g. in PruneDataStore or at the end of each event)
42 // essentially free.
43 // Same argument applies to the RelationContainer dataobject.
44 object = new TClonesArray(objClass, 10);
45 } else {
46 // Oh dear, where to begin. So we want to create a new object of the class
47 // we have and we require this class to be inheriting from TObject. Fine,
48 // but there could be classes with multiple inheritance where the TObject
49 // is not the first base class. In this case the memory layout puts the
50 // TObject not at the beginning of the instance but at an offset. The
51 // compiler knows this so a static_cast<> or c-style cast from one to the
52 // other will correctly modify the pointing address to point to the start
53 // of TObject, but TClass::New() gives us a void* pointer so the compiler
54 // doesn't know about that. So to be on the safe side we have to manually
55 // fix the pointer address using the BaseClassOffset from TClass. And since
56 // pointer arithmetic on void* is forbidden we have to go to char* first.
57 auto* rawPtr = reinterpret_cast<char*>(objClass->New());
58 int offset = objClass->GetBaseClassOffset(TObject::Class());
59 if (offset < 0) B2FATAL("Class " << objClass->GetName() << " does not inherit from TObject");
60 object = reinterpret_cast<TObject*>(rawPtr + offset);
61 }
62}
63
65{
66 if (isArray) {
67 static_cast<TClonesArray*>(object)->Delete();
68 } else if (object->IsA() == RelationContainer::Class()) {
69 static_cast<RelationContainer*>(object)->Clear();
70 } else {
71 //we don't know anything about object, so we just delete it here (and recreate later)
72 delete object;
73 object = nullptr;
74 }
75}
76
78{
80 if (object == nullptr)
82
83 ptr = object;
84}
85
87{
88 recreate();
89 ptr = nullptr;
90}
91
92TClonesArray* StoreEntry::getPtrAsArray() const
93{
94 if (!isArray)
95 return nullptr;
96 return static_cast<TClonesArray*>(ptr);
97}
Class to store relations between StoreArrays in the DataStore.
Abstract base class for different kinds of events.
STL namespace.
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on whether the object...
Definition StoreEntry.h:52
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition StoreEntry.cc:92
bool dontWriteOut
Flag that indicates whether the object should be written to the output by default.
Definition StoreEntry.h:41
StoreEntry()
default constructor.
Definition StoreEntry.h:24
TObject * object
The pointer to the actual object.
Definition StoreEntry.h:49
void invalidate()
invalidate entry for next event.
Definition StoreEntry.cc:86
bool isArray
Flag that indicates whether the object is a TClonesArray.
Definition StoreEntry.h:40
std::string name
Name of the entry.
Definition StoreEntry.h:54
void recoverFromNullObject()
Recreate object if null.
Definition StoreEntry.cc:30
void resetForGetEntry()
Reset stored object to defaults, or nullptr.
Definition StoreEntry.cc:64
void recreate()
Reset stored object to defaults, set ptr to new object.
Definition StoreEntry.cc:77
TClass * objClass
class of object.
Definition StoreEntry.h:42