Diana Software
MNeighboursPacker.cc
Go to the documentation of this file.
1 #include "MNeighboursPacker.hh"
2 #include "QEventAssembler.hh"
3 #include "QEventList.hh"
4 #include <sstream>
5 #include "QBaseType.hh"
6 #define NSPACER "_"
7 
9 
10 using namespace Diana;
11 
12 
14 {
15  fMaxNeighbours = GetInt("MaximumNumberOfNeighbours",2);
16  if(fMaxNeighbours < 1) Panic("MaximumNumberOfNeighbours = %d not allowed",fMaxNeighbours);
17  fLabels.clear();
18  fLabels.resize(fMaxNeighbours);
19 
20  // retreive aliases from event and from files
21  QAliases& aliases = eva.GetAliases();
22  std::map<std::string, QVdt*> wparams = GetModuleParameters(GetSequence().GetWriterName(), 0);
23 
24  if(wparams.find("AliasFileName") != wparams.end()) {
25  aliases.FillFromFile(wparams["AliasFileName"]->GetString());
26  }
27  if(wparams.find("CustomAliasFileName") != wparams.end()) {
28  aliases.FillFromFile(wparams["CustomAliasFileName"]->GetString());
29  }
30  eva.GetEvent().Add<QInt>("NumberOfNeighbours");
31 
32  // add objects of neigbours events
33  std::vector<QEventLabel> originalObjects = eva.GetLabels();
34  for(size_t i = 0; i < originalObjects.size(); i++) {
35 
36  const QEventLabel& label = originalObjects[i];
37  if(!GetBool(label,false,false)) continue;
38  QObject** object = eva.Get(label.owner.c_str(),label.name.c_str());
39  (*object)->SetIsRequired();
40 
41  for(size_t n = 0; n < (size_t)fMaxNeighbours; n++) {
42  // create new label
43  std::stringstream newOwner;
44  newOwner<<std::string(GetString("Prefix","N",false));
45  newOwner<<n+1;
46  newOwner<<NSPACER;
47  newOwner<<label.owner;
48  QEventLabel newLabel(newOwner.str(),label.name);
49  QObject** newObject = eva.Get(newOwner.str().c_str(),label.name.c_str());
50  if(newObject == 0) {
51  // create new object
52  newObject = new (QObject*);
53  *newObject = (*object)->Duplicate();
54  (*object)->FullyFill(*newObject);
55  (*newObject)->SetIsJustAdded();
56 
57  // add new object to the QEvent
58  eva.Add(newLabel.owner.c_str(),newLabel.name.c_str(),newObject);
59  } else {
60  (*newObject)->SetIsJustAdded();
61  }
62 
63  // store label of new object
64  ObjectLabels labels;
65  labels.fLabel = newLabel;
66  labels.fOriginalLabel = label;
67  fLabels[n].push_back(labels);
68 
69  // set aliases for new object in the form N1#alias
70  std::map<std::string,std::string> thisAliases = aliases.Find(label);
71  std::map<std::string,std::string>::const_iterator aiter = thisAliases.begin();
72  while(aiter != thisAliases.end()) {
73  std::stringstream nalias;
74  nalias<<std::string(GetString("Prefix","N"));
75  nalias<<n+1;
76  nalias<<NSPACER;
77  nalias<<aiter->first;
78  aliases.Add(newLabel,aiter->second,nalias.str());
79  aiter++;
80  }
81 
82  }
83  }
84  if(fLabels[0].empty()) {
85  Error("No QObject from neighbours will be packed");
86  } else {
87  std::stringstream msg;
88  msg<<"Packing: ";
89  for(size_t n = 0; n < fLabels[0].size(); n++) msg<<fLabels[0][n].fOriginalLabel.GetStringLabel()<<" ";
90  msg<<std::endl;
91  Info("%s",msg.str().c_str());
92  }
93 
94  fMaxNeighboursAchieved = 0;
95  return ACT_NEXTEV;
96 }
97 
99 {
100  for(size_t n = 0; n < neigh.Size(); n++) {
101  if(n < (size_t)fMaxNeighbours) {
102  for(size_t l = 0; l < fLabels[n].size(); l++) {
103  const QEventLabel& label = fLabels[n][l].fLabel;
104  const QEventLabel& originalLabel = fLabels[n][l].fOriginalLabel;
105  ReadHandle<QObject> rHandle(originalLabel.name.c_str());
106  neigh[n].Get(originalLabel.owner.c_str(),rHandle);
107  if(rHandle.IsValid()) {
108  const QObject* originalObject = &rHandle.Get();
109  QObject** object = eva.Get(label.owner.c_str(),label.name.c_str());
110  originalObject->FullyFill(*object);
111  (*object)->SetIsJustAdded();
112  }
113  }
114  }
115  }
116 
117  eva.GetEvent().Get<QInt>("NumberOfNeighbours") = neigh.Size();
118 
119  if(neigh.Size() > fMaxNeighboursAchieved) fMaxNeighboursAchieved = neigh.Size();
120 
121  return ACT_NEXTEV;
122 }
123 
125 {
126  if(fMaxNeighboursAchieved > (size_t)fMaxNeighbours)
127  Warn("Only %d neighbours per event have been packed, while the maximum number available was %d", fMaxNeighbours, fMaxNeighboursAchieved);
128  else if(fMaxNeighboursAchieved < (size_t)fMaxNeighbours)
129  Warn("Could not pack %d neighbours per event, since maximum number available was only %d",fMaxNeighbours, fMaxNeighboursAchieved);
130  GlobalHandle<QInt> nHandle("NumberOfNeighbours");
131  nHandle.Set(fMaxNeighbours);
132  GlobalData().Set(&nHandle,"CurrentWriter");
133  return ACT_NEXTEV;
134 }
QRunDataHandle rHandle(753)
#define NSPACER
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
Internal class to save new (NX_owner@name) and original (owner@name) label of a QObject.
Save user selected QObjects from neighbours QEvents into the main QEvent.
Action Do(Diana::QEventAssembler &eva, const Diana::QEventList &neigh)
Do.
Action Init(Diana::QEventAssembler &eva)
Init.
QError FillFromFile(const std::string &filename)
Definition: QAliases.cc:63
void Add(const QEventLabel &label, const std::string &path, const std::string &alias)
Definition: QAliases.cc:50
std::map< std::string, std::string > Find(const QEventLabel &label) const
Definition: QAliases.cc:25
base types wrapped into a QObject. Currently implemented types are QInt QDouble and QFloat....
Definition: QBaseType.hh:17
class to store ActionId and fEventNumber (in case fActionId=ACT_GOTOEV)
Definition: QDriver.hh:59
Visitor class of QEvent that provides full handling of QEvent.
QEvent & GetEvent()
Get the QEvent.
std::vector< QEventLabel > GetLabels() const
Get the list of all QObject labels in the event.
void Get(const char *owner, WriteHandle< Q > &handle)
Get QObject from the event in write mode. This method has to be called in the event loop,...
const QAliases & GetAliases() const
get aliases (const version)
void Add(const char *owner, WriteHandle< Q > &handle)
Add QObject to the event. This method has to be called before the event loop, e.g....
label for QObject in the QEvent
Definition: QEventLabel.hh:23
std::string owner
owner of the QObject
Definition: QEventLabel.hh:60
std::string name
name of the QObject
Definition: QEventLabel.hh:62
list of references to const QEvent (s)
Definition: QEventList.hh:21
size_t Size() const
number of QEvent (s)
Definition: QEventList.hh:36
void Get(const char *owner, ReadHandle< Q > &handle) const
Get a QObject Handle in read mode.
Definition: QEvent.hh:74
void Add(WriteHandle< Q > &handle)
Add a QObject to the event.
Definition: QEvent.hh:193
read handle to access QEvent QObject's.
Definition: QHandle.hh:126
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...