Diana Software
MGuiDriver.cc
Go to the documentation of this file.
1 #include "MGuiDriver.hh"
2 #include "QGDUtils.hh"
3 #include "QEventAssembler.hh"
4 #include "QRunData.hh"
5 #include "QRunDataHandle.hh"
6 #include "QRawEvent.hh"
7 #include <iostream>
8 #include "TSocket.h"
9 #include "TMessage.h"
10 #include "TMap.h"
11 #include "TObjString.h"
12 #include <sstream>
13 
14 using namespace Diana;
15 
17 
19 {
20  fSocket = 0;
21  fEva = new QEventAssembler();
22 }
23 
25 {
26  delete fEva;
27  // called once in a sequence
28 }
29 
31 {
32 
33  // open network connection to GUI if not already done
34  if (fSocket == 0) {
35  std::string add = GetString("GuiHost","localhost");
36  int port = GetInt("GuiPort",1130);
37  fSocket = new TSocket(add.c_str(),port);
38  }
39  fRunData.Clear();
40  // called before event loop
41  Action act = Menu(ML_Init);
42  fEva->Clear();
43  return act;
44 }
45 
47 {
48 
49  QEvent& ev = eva.GetEvent();
50  // get event number and channel id of this event
51 
52  const QHeader& hdr = ev.Get<QHeader>("","Header");;
53  const QPulseInfo& pi = ev.Get<QPulseInfo>("","PulseInfo");;
54  Debug("Current event: %d, Channel %d",hdr.GetEventNumber(), pi.GetChannelId());
55  fEva->Assign(ev);
56  fDm = &GlobalData();
57  if(fRunData.fNumber != hdr.GetRun())
58  //fRunData = GlobalData().Get<QRunData>("","RunData","");
59  {
60  QRunDataHandle rh(hdr.GetRun());
61  GlobalData().Get("DAQ",&rh,"");
62  if(rh.IsValid()) fRunData = rh.Get();
63  }
64 
65  Action act = Menu(ML_Do);
66  return act;
67 }
68 
70 {
71  // called at the end of the event loop
72  Action act = Menu(ML_Done);
73  return act;
74 }
75 
77  QDriver::Action act;
78  act.fActionId = ACT_NONE;
79  while(act.fActionId == ACT_NONE) {
80  SendCommands(level);
81  QGDCommand cmd = GetCommand(level);
82  act = HandleCommand(cmd);
83  }
84  Debug("Performing action %d",act.fActionId);
85  return act;
86 }
87 
88 
90 {
91  QDriver::Action act;
92  switch( cmd.fCommandId )
93  {
94  case QGDCommand::CmdNext:
95  act.fActionId = ACT_NEXTEV;
96  break;
98  act.fActionId = ACT_PREVEV;
99  break;
100  case QGDCommand::CmdJump:
101  act.fActionId = ACT_GOTOEV;
103  break;
104  case QGDCommand::CmdQuit:
105  act.fActionId = ACT_QUIT;
106  break;
108  act.fActionId = ACT_RERUN;
109  break;
111  ListModules();
112  act.fActionId = ACT_NONE;
113  break;
115  ListModuleParameters(QGDUtils::ParameterToQVdt(cmd.fParameter1).GetInt());
116  act.fActionId = ACT_NONE;
117  break;
119  ChangeModuleParameter(QGDUtils::ParameterToQVdt(cmd.fParameter1).GetInt(),
121  act.fActionId = ACT_NONE;
122  break;
125  act.fActionId = ACT_NONE;
126  break;
127  case QGDCommand::CmdNone:
128  default:
129  act.fActionId = ACT_NONE;
130  break;
131  }
132  return act;
133 }
134 
136 {
137  QGDCommand cmd;
138  if(fSocket && fSocket->IsValid()) {
139  TMessage* mess = NULL;
140  Debug("Waiting for command");
141  fSocket->Recv(mess);
142  if(mess) {
143  TObject* obj = mess->ReadObject(mess->GetClass());
144  if(QGDCommand* myCmd = dynamic_cast<QGDCommand*>(obj)) {
145  cmd = *myCmd;
146  Debug("Command received: %d",cmd.fCommandId);
147  return cmd;
148  } else {
149  Warn("Object received is not a QGDCommand, Ignored");
150  }
151  }
152  }
153  Panic("Connection error, command not received");
154  return cmd;
155 }
156 
157 
158 
160 {
161  Debug("Sending avalaible commands");
162  QGDCommandList cl;
163  cl.fCommands.clear();
164  QGDCommand command;
165  if(level == ML_Init) {
166  cl.fCommands.push_back(QGDCommand::CmdNone);
167  cl.fCommands.push_back(QGDCommand::CmdNext);
168  cl.fCommands.push_back(QGDCommand::CmdJump);
169  cl.fCommands.push_back(QGDCommand::CmdQuit);
170  cl.fCommands.push_back(QGDCommand::CmdRerun);
171  cl.fCommands.push_back(QGDCommand::CmdListMod);
172  cl.fCommands.push_back(QGDCommand::CmdListPar);
173  cl.fCommands.push_back(QGDCommand::CmdChangePar);
174  } else if(level == ML_Do) {
175  cl.fCommands.push_back(QGDCommand::CmdNone);
176  cl.fCommands.push_back(QGDCommand::CmdNext);
177  cl.fCommands.push_back(QGDCommand::CmdPrev);
178  cl.fCommands.push_back(QGDCommand::CmdJump);
179  cl.fCommands.push_back(QGDCommand::CmdQuit);
180  cl.fCommands.push_back(QGDCommand::CmdRerun);
181  cl.fCommands.push_back(QGDCommand::CmdListMod);
182  cl.fCommands.push_back(QGDCommand::CmdListPar);
183  cl.fCommands.push_back(QGDCommand::CmdChangePar);
184  cl.fCommands.push_back(QGDCommand::CmdSendData);
185  } else if(level == ML_Done) {
186  cl.fCommands.push_back(QGDCommand::CmdNone);
187  cl.fCommands.push_back(QGDCommand::CmdRerun);
188  cl.fCommands.push_back(QGDCommand::CmdListMod);
189  cl.fCommands.push_back(QGDCommand::CmdListPar);
190  cl.fCommands.push_back(QGDCommand::CmdChangePar);
191  }
192  SendTObject(&cl);
193 
194 }
195 
196 void MGuiDriver::SendTObject(const TObject* obj)
197 {
198  if(fSocket && fSocket->IsValid()) {
199  TMessage mess(kMESS_OBJECT);
200  mess.WriteObject( obj );
201  fSocket->Send( mess );
202  Debug("Object sent");
203  } else {
204  Panic("Connection error, could not send data");
205  }
206 }
207 
208 
210 {
211 
212  const std::vector<QBaseModule*>& modList = GetModulesList();
213  QGDModuleList eModList;
214  for(size_t m = 0; m < modList.size(); m++) {
215  QBaseModule *mod = modList[m];
216  std::string fullPath = mod->GetFullPath();
217  eModList.fModules.push_back(fullPath);
218  }
219  SendTObject(&eModList);
220 }
221 
223 {
224  const std::vector<QBaseModule*>& modList = GetModulesList();
225  if(modIndex <= 0 && modIndex > (int)modList.size()-1) {
226  Error("Module index (%d) out of range",modIndex);
227  return;
228  }
229  const QBaseModule* thisMod = modList[(size_t)modIndex];
230  std::string moduleName = thisMod->GetName();
231  int occurrence = thisMod->GetOccurrence();
232  std::map<std::string, QVdt*> params = GetModuleParameters(moduleName, occurrence);
233  std::map<std::string, QVdt*>::iterator p1 = params.begin();
235  while(p1 != params.end()) {
236  QGDParameter qparam;
237  qparam = QGDUtils::QVdtToParameter(*(p1->second));
238  qparam.fName = p1->first;
239  pList.fParameters.push_back(qparam);
240  p1++;
241  }
242  SendTObject(&pList);
243 }
244 
245 void MGuiDriver::ChangeModuleParameter(int modIndex, const QVdt& newVal)
246 {
247  const std::vector<QBaseModule*>& modList = GetModulesList();
248  if(modIndex <= 0 && modIndex > (int)modList.size()-1) {
249  Error("Module index (%d) out of range",modIndex);
250  return;
251  }
252  const QBaseModule* thisMod = modList[(size_t)modIndex];
253  std::string moduleName = thisMod->GetName();
254  int occurrence = thisMod->GetOccurrence();
255  std::map<std::string,QVdt*> parameters = GetModuleParameters(moduleName,occurrence);
256  std::string parName = newVal.GetName();
257  std::map<std::string,QVdt*>::iterator thisPar = parameters.find(parName);
258  if(thisPar == parameters.end())
259  Warn("Parameter %s not found, setting it anyway",parName.c_str());
260  *(parameters[parName]) = newVal;
261 }
262 
263 void MGuiDriver::SendData(const std::string& type)
264 {
265  if( type == "QRunData") {
266  SendTObject(&fRunData);
267  } else if (type == "QGlobal") {
268  TMap* data = new TMap();
269  data->SetName("QGlobal");
270  data->SetOwner(1);
271  std::vector<QGlobalLabel> labels = fDm->GetWriteLabels();
272  for(size_t l = 0; l < labels.size(); l++) {
273  QGlobalLabel lab(labels[l]);
274  const QObject* obj = fDm->GetByLabel(lab);
275  if(obj ) {
276  QObject* dobj = obj->Duplicate();
277  obj->FullyFill(dobj);
278  TObjString* strlab = new TObjString(lab.GetStringLabel().c_str());
279  data->Add(strlab, dobj);
280  }
281  }
282  SendTObject(data);
283  delete data;
284  } else {
285  TMap* data = new TMap();
286  data->SetName("QEvent");
287  data->SetOwner(1);
288  std::vector<QEventLabel> labels = fEva->GetWriteLabels();
289  for(size_t l = 0; l < labels.size(); l++) {
290  QEventLabel lab(labels[l]);
291  QObject** obj = fEva->Get(lab.owner.c_str(),lab.name.c_str());
292  if(obj && *obj) {
293  QObject* dobj = (*obj)->Duplicate();
294  (*obj)->FullyFill(dobj);
295  TObjString* strlab = new TObjString(lab.GetStringLabel().c_str());
296  data->Add(strlab, dobj);
297  }
298  }
299  SendTObject(data);
300  delete data;
301  }
302 }
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
driver module for Interactive GUI, interfaced via TSocket with any class inerithing QGDMessageHandler
Definition: MGuiDriver.hh:46
void ChangeModuleParameter(int modIndex, const QVdt &val)
change module parameter
Definition: MGuiDriver.cc:245
Action Done()
Done method.
Definition: MGuiDriver.cc:69
void SendTObject(const TObject *obj)
general purpose function used to send TObjects
Definition: MGuiDriver.cc:196
Action HandleCommand(QGDCommand cmd)
HandleCommand method. Execute a command.
Definition: MGuiDriver.cc:89
void ListModuleParameters(int modIndex)
Send List of module parameter.
Definition: MGuiDriver.cc:222
Action Menu(MenuLevel level)
Menu method. Handle Menu logic.
Definition: MGuiDriver.cc:76
~MGuiDriver()
destructor
Definition: MGuiDriver.cc:24
void SendCommands(MenuLevel level)
Send Avalaible commands to the server.
Definition: MGuiDriver.cc:159
QGDCommand GetCommand(MenuLevel level)
Get command from standard input.
Definition: MGuiDriver.cc:135
Action Do(Diana::QEventAssembler &eva)
Do method.
Definition: MGuiDriver.cc:46
void ListModules()
Send List of avalaible modules.
Definition: MGuiDriver.cc:209
Action Init(Diana::QEventAssembler &eva)
Init method.
Definition: MGuiDriver.cc:30
void SendData(const std::string &type)
send data
Definition: MGuiDriver.cc:263
class to store ActionId and fEventNumber (in case fActionId=ACT_GOTOEV)
Definition: QDriver.hh:59
int fEventNumber
Definition: QDriver.hh:64
ActionId fActionId
Definition: QDriver.hh:63
Visitor class of QEvent that provides full handling of QEvent.
QEvent & GetEvent()
Get the QEvent.
label for QObject in the QEvent
Definition: QEventLabel.hh:23
std::string GetStringLabel() const
get string in the format "owner@name"
Definition: QEventLabel.cc:48
std::string owner
owner of the QObject
Definition: QEventLabel.hh:60
std::string name
name of the QObject
Definition: QEventLabel.hh:62
diana event
Definition: QEvent.hh:46
void Get(const char *owner, ReadHandle< Q > &handle) const
Get a QObject Handle in read mode.
Definition: QEvent.hh:74
std::vector< QGDCommand::Id > fCommands
Definition: QGDInclude.hh:54
QGDParameter fParameter1
Definition: QGDInclude.hh:45
QGDParameter fParameter2
Definition: QGDInclude.hh:46
std::vector< std::string > fModules
Definition: QGDInclude.hh:61
std::vector< QGDParameter > fParameters
Definition: QGDInclude.hh:67
std::string fName
Definition: QGDInclude.hh:19
Label for global QObject's.
Definition: QGlobalLabel.hh:19
std::string GetStringLabel() const
convert label to string
Raw event: basic information like run number and time.
Definition: QHeader.hh:16
int GetRun() const
destructor
Definition: QHeader.hh:22
int GetEventNumber() const
get EventNumber
Definition: QHeader.hh:25
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
const int & GetChannelId() const
Get ChannelId.
Definition: QPulseInfo.hh:22
global handle for QRunData
Variable Data Type.
Definition: QVdt.hh:26
const std::string & GetString() const
Definition: QVdt.cc:225
std::string GetName() const
Definition: QVdt.hh:68
long int GetInt() const
Definition: QVdt.cc:213
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...
QVdt ParameterToQVdt(QGDParameter param)
Definition: QGDUtils.cc:47
QGDParameter QVdtToParameter(const QVdt &vdt)
Definition: QGDUtils.cc:54