Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DPTriggerAnalyzer.cxx
Go to the documentation of this file.
1 #include "DPTriggerAnalyzer.h"
2 #include <interface_main/SQRun.h>
5 #include <geom_svc/GeomSvc.h>
6 #include <fun4all/Fun4AllBase.h>
8 #include <phool/PHNodeIterator.h>
10 #include <phool/PHIODataNode.h>
11 #include <phool/getClass.h>
12 #include <TSystem.h>
13 #include <iomanip>
14 #include <fstream>
15 #include <sstream>
16 #include <string>
17 using namespace std;
18 
19 DPTriggerRoad::DPTriggerRoad() : roadID(0), sigWeight(0.), bkgRate(0.), pXmin(0.)
20 {
21  uniqueTrIDs.clear();
22 }
23 
24 DPTriggerRoad::DPTriggerRoad(const std::list<int>& path) : roadID(0), sigWeight(0.), bkgRate(0.), pXmin(0.)
25 {
26  uniqueTrIDs.clear();
27  for(std::list<int>::const_iterator iter = path.begin(); iter != path.end(); ++iter)
28  {
29  if(*iter < 0) continue;
30  uniqueTrIDs.push_back(*iter);
31  }
32 }
33 
35 {
36  int TB = 0;
37  for(unsigned int i = 0; i < NTRPLANES; ++i)
38  {
39  int detectorID = getTrDetectorID(i);
40  TB = TB + (((detectorID & 1) == 0) ? 1 : -1);
41  }
42 
43  if(TB == 4) return 1;
44  if(TB == -4) return -1;
45 
46  return 0;
47 }
48 
50 {
51  int corr = getTB();
52  for(unsigned int i = 0; i < NTRPLANES; ++i)
53  {
54  int detectorID = getTrDetectorID(i);
55  int elementID = getTrElementID(i);
56 
57  detectorID -= corr;
58  uniqueTrIDs[i] = detectorID*1000 + elementID;
59  }
60 }
61 
63 {
64  for(unsigned int i = 0; i < NTRPLANES; ++i)
65  {
66  if(uniqueTrIDs[i] != elem.uniqueTrIDs[i]) return false;
67  }
68 
69  return true;
70 }
71 
73 {
74  return sigWeight < elem.sigWeight;
75 }
76 
78 {
79  TString sid;
80  for(unsigned int i = 0; i < uniqueTrIDs.size(); ++i)
81  {
82  sid = sid + Form("%06d", uniqueTrIDs[i]);
83  }
84 
85  return sid;
86 }
87 
88 std::ostream& operator << (std::ostream& os, const DPTriggerRoad& road)
89 {
90  os << "Trigger Road ID = " << road.roadID << ", signal = " << road.sigWeight << ", bkg = " << road.bkgRate << "\n ";
91  for(unsigned int i = 0; i < NTRPLANES; ++i)
92  {
93  os << road.uniqueTrIDs[i] << " == ";
94  }
95 
96  return os;
97 }
98 
100 
101 DPTriggerAnalyzer::DPTriggerAnalyzer(const std::string& name) :
102  SubsysReco(name),
103  _road_set_file_name("trigger_67.txt"),
104  _mode_nim1(NIM_AND),
105  _mode_nim2(NIM_AND),
106  _run_header(nullptr),
107  _event_header(nullptr),
108  _hit_vector(nullptr)
109 {
110 }
111 
113 {
114  deleteMatrix(matrix[0]);
115  deleteMatrix(matrix[1]);
116 }
117 
118 void DPTriggerAnalyzer::set_nim_mode(const NimMode nim1, const NimMode nim2)
119 {
120  _mode_nim1 = nim1;
121  _mode_nim2 = nim2;
122 }
123 
130 {
131  //Load the trigger roads
132  int charge = (int) -1e3, roadID = -1;
133  int uIDs[NTRPLANES] = { -1, -1, -1, -1 };
134  double pXmin = -1.e6, sigWeight = -1.e6, bkgRate = -1.e6;
135  string line = "";
136  std::ifstream fin(gSystem->ExpandPathName(_road_set_file_name.c_str()), std::ifstream::in);
137  if (fin.is_open()) {
138  while (getline(fin, line)) {
139  stringstream ss(line);
140 
141  ss >> charge >> roadID;
142  for (int i = 0; i < NTRPLANES; ++i)
143  ss >> uIDs[i];
144  ss >> pXmin >> sigWeight >> bkgRate;
145 
146  DPTriggerRoad road;
147  road.setRoadID(roadID);
148  road.setSigWeight(sigWeight);
149  road.setBkgRate(bkgRate);
150  road.setPxMin(pXmin);
151 
152  for (int i = 0; i < NTRPLANES; ++i)
153  road.addTrElement(uIDs[i]);
154  roads[(-charge + 1) / 2].insert(
155  std::map<TString, DPTriggerRoad>::value_type(road.getStringID(),
156  road));
157  }
158  } else {
159  NIMONLY = true;
160  LogInfo("NIM Only mode NOT supported now.");
162  }
163  //build the search matrix
165 
167 }
168 
170 {
171  return GetNodes(topNode);
172 }
173 
175  GeomSvc* geomSvc = GeomSvc::instance();
176 
177  //NIM trigger first
179  map<int, int> nhit_det; // [det_id] = counts
180  for (Int_t ihit = 0; ihit < _hit_vector->size(); ++ihit) {
181  nhit_det[ _hit_vector->at(ihit)->get_detector_id() ]++;
182  // Note: GeomSvc::getTriggerLv() was used in the past version, but it should not be used since the "trigger level" was not a standard variable.
183  }
184 
185  bool HXB = nhit_det[ geomSvc->getDetectorID("H1B" ) ] > 0 &&
186  nhit_det[ geomSvc->getDetectorID("H2B" ) ] > 0 &&
187  nhit_det[ geomSvc->getDetectorID("H3B" ) ] > 0 &&
188  nhit_det[ geomSvc->getDetectorID("H4B" ) ] > 0 ;
189  bool HXT = nhit_det[ geomSvc->getDetectorID("H1T" ) ] > 0 &&
190  nhit_det[ geomSvc->getDetectorID("H2T" ) ] > 0 &&
191  nhit_det[ geomSvc->getDetectorID("H3T" ) ] > 0 &&
192  nhit_det[ geomSvc->getDetectorID("H4T" ) ] > 0 ;
193  bool HYL = nhit_det[ geomSvc->getDetectorID("H1L" ) ] > 0 &&
194  nhit_det[ geomSvc->getDetectorID("H2L" ) ] > 0 &&
195  nhit_det[ geomSvc->getDetectorID("H4Y1L") ] > 0 &&
196  nhit_det[ geomSvc->getDetectorID("H4Y2L") ] > 0 ;
197  bool HYR = nhit_det[ geomSvc->getDetectorID("H1R" ) ] > 0 &&
198  nhit_det[ geomSvc->getDetectorID("H2R" ) ] > 0 &&
199  nhit_det[ geomSvc->getDetectorID("H4Y1R") ] > 0 &&
200  nhit_det[ geomSvc->getDetectorID("H4Y2R") ] > 0 ;
201  bool nim1_on = _mode_nim1 == NIM_AND ? HYL && HYR : HYL || HYR;
202  bool nim2_on = _mode_nim2 == NIM_AND ? HXT && HXB : HXT || HXB;
203  _event_header->set_trigger(SQEvent::NIM1, nim1_on);
204  _event_header->set_trigger(SQEvent::NIM2, nim2_on);
205 
206  //For FPGA trigger, build the internal hit pattern first
207  data.clear();
208  data.resize(NTRPLANES);
209  for(Int_t ihit = 0; ihit < _hit_vector->size(); ++ihit) {
210  SQHit *hit=_hit_vector->at(ihit);
211  int index = geomSvc->getHodoStation(hit->get_detector_id()) - 1;
212  if (0 <= index && index < NTRPLANES) {
213  data[index].insert( hit->get_detector_id()*1000 + hit->get_element_id() );
214  }
215  }
216  bool all_planes_have_hits = true;
217  for (int i = 0; i < NTRPLANES; ++i) {
218  if (data[i].empty()) {
219  all_planes_have_hits = false;
220  break;
221  }
222  }
223 
224  //do the tree DFS search
225  for(int i = 0; i < 2; ++i) {
226  path.clear();
227  roads_found[i].clear();
228  if (all_planes_have_hits) searchMatrix(matrix[i], 0, i);
229  }
230 
231  //FPGA singles trigger
232  int nPlusTop = 0;
233  int nPlusBot = 0;
234  int nMinusTop = 0;
235  int nMinusBot = 0;
236  int nHiPxPlusTop = 0;
237  int nHiPxPlusBot = 0;
238  int nHiPxMinusTop = 0;
239  int nHiPxMinusBot = 0;
240 
241  for (std::list<DPTriggerRoad>::iterator iter = roads_found[0].begin();
242  iter != roads_found[0].end(); ++iter) {
243  if (iter->getTB() > 0) {
244  ++nPlusTop;
245  if (iter->getPxMin() > 3.) ++nHiPxPlusTop;
246  } else {
247  ++nPlusBot;
248  if (iter->getPxMin() > 3.) ++nHiPxPlusBot;
249  }
250  }
251 
252  for (std::list<DPTriggerRoad>::iterator iter = roads_found[1].begin();
253  iter != roads_found[1].end(); ++iter) {
254  if (iter->getTB() > 0) {
255  ++nMinusTop;
256  if (iter->getPxMin() > 3.) ++nHiPxMinusTop;
257  } else {
258  ++nMinusBot;
259  if (iter->getPxMin() > 3.) ++nHiPxMinusBot;
260  }
261  }
262 
263  bool fpga1_on = (nPlusTop > 0 && nMinusBot > 0) || (nPlusBot > 0 && nMinusTop > 0);
264  bool fpga2_on = (nPlusTop > 0 && nMinusTop > 0) || (nPlusBot > 0 && nMinusBot > 0);
265  bool fpga3_on = (nPlusTop > 0 && nPlusBot > 0) || (nMinusTop > 0 && nMinusBot > 0);
266  bool fpga4_on = nPlusTop > 0 || nMinusTop > 0 || nPlusBot > 0 || nMinusBot > 0 ;
267  bool fpga5_on = nHiPxPlusTop > 0 || nHiPxMinusTop > 0 || nHiPxPlusBot > 0 || nHiPxMinusBot > 0;
268  _event_header->set_trigger(SQEvent::MATRIX1, fpga1_on);
269  _event_header->set_trigger(SQEvent::MATRIX2, fpga2_on);
270  _event_header->set_trigger(SQEvent::MATRIX3, fpga3_on);
271  _event_header->set_trigger(SQEvent::MATRIX4, fpga4_on);
272  _event_header->set_trigger(SQEvent::MATRIX5, fpga5_on);
273 
275 }
276 
279 }
280 
281 int DPTriggerAnalyzer::GetNodes(PHCompositeNode* topNode) {
282 
283  _event_header = findNode::getClass<SQEvent>(topNode, "SQEvent");
284  if (!_event_header) {
285  LogInfo("!_event_header");
287  }
288 
289  _hit_vector = findNode::getClass<SQHitVector>(topNode, "SQHitVector");
290  if (!_hit_vector) {
291  LogInfo("!_hit_vector");
293  }
294 
296 }
297 
299 
303  for (int i = 0; i < 2; ++i) {
304  matrix[i] = new MatrixNode(-1);
305  for (std::map<TString, DPTriggerRoad>::iterator iter = roads[i].begin(); iter != roads[i].end(); ++iter) {
306  MatrixNode* parentNode[NTRPLANES + 1]; //NOTE: the last entry is useless, just to keep the following code simpler
307  parentNode[0] = matrix[i];
308  for (int j = 0; j < NTRPLANES; ++j) {
309  int uniqueID = iter->second.getTrID(j);
310  bool isNewNode = true;
311  for (std::list<MatrixNode*>::iterator jter = parentNode[j]->children.begin(); jter != parentNode[j]->children.end(); ++jter) {
312  if (uniqueID == (*jter)->uniqueID) {
313  parentNode[j + 1] = *jter;
314  isNewNode = false;
315 
316  break;
317  }
318  }
319 
320  if (isNewNode) {
321  MatrixNode* newNode = new MatrixNode(uniqueID);
322  parentNode[j]->add(newNode);
323  parentNode[j + 1] = newNode;
324  }
325  }
326  }
327  }
328 }
329 
332 
333 void DPTriggerAnalyzer::searchMatrix(MatrixNode* node, int level, int index) {
334  path.push_back(node->uniqueID);
335  if (node->children.empty()) {
336  //printPath();
337  DPTriggerRoad road_found(path);
338  if (roads[index].find(road_found.getStringID()) != roads[index].end())
339  roads_found[index].push_back(road_found);
340  path.pop_back();
341 
342  return;
343  }
344 
345  for (std::list<MatrixNode*>::iterator iter = node->children.begin(); iter != node->children.end(); ++iter) {
346  if (data[level].find((*iter)->uniqueID) == data[level].end())
347  continue;
348  searchMatrix(*iter, level + 1, index);
349  }
350  path.pop_back();
351 }
352 
354  if (node == NULL) return;
355 
356  for (std::list<MatrixNode*>::iterator iter = node->children.begin(); iter != node->children.end(); ++iter) {
357  deleteMatrix(*iter);
358  }
359 
360  delete node;
361 }
362 
364  for (unsigned int i = 1; i < NTRPLANES; ++i) {
365  std::cout << "Lv. " << i << ": ";
366  for (std::set<int>::iterator iter = data[i].begin(); iter != data[i].end(); ++iter) {
367  std::cout << *iter << " ";
368  }
369  std::cout << std::endl;
370  }
371 }
372 
374  std::cout << "Found one road: " << std::endl;
375  for (std::list<int>::iterator iter = path.begin(); iter != path.end(); ++iter) {
376  std::cout << *iter << " === ";
377  }
378  std::cout << std::endl;
379 }
380 
382 
384 {
385  children.clear();
386 }
387 
389 {
390  children.push_back(child);
391 }
bool operator<(const DPTriggerRoad &elem) const
void printHitPattern()
Helper functions to print various things.
void setBkgRate(double rate)
int Init(PHCompositeNode *topNode)
void buildTriggerMatrix()
Build the trigger matrix by the input roads list.
void add(MatrixNode *child)
add a child
int End(PHCompositeNode *topNode)
Called at the end of all processing.
int InitRun(PHCompositeNode *topNode)
bool operator==(const DPTriggerRoad &elem) const
comparison
DPTriggerAnalyzer(const std::string &name="DPTriggerAnalyzer")
An SQ interface class to hold one detector hit.
Definition: SQHit.h:20
int getTrElementID(unsigned int i) const
void searchMatrix(MatrixNode *node, int level, int index)
Test the trigger pattern.
void setSigWeight(double weight)
void flipTB()
flip the LR or TB
void deleteMatrix(MatrixNode *node)
Tree deletion.
#define NULL
Definition: Pdb.h:9
void addTrElement(int uniqueID)
add one hit into the road
int process_event(PHCompositeNode *topNode)
std::basic_ostream< E, T > & operator<<(std::basic_ostream< E, T > &os, shared_ptr< Y > const &p)
Definition: shared_ptr.hpp:573
std::list< MatrixNode * > children
virtual void set_trigger(const SQEvent::TriggerMask i, const bool a)=0
virtual const SQHit * at(const size_t idkey) const
Definition: SQHitVector.h:53
virtual short get_detector_id() const
Return the detector ID of this hit.
Definition: SQHit.h:42
int getTB()
Get the sign of LR or TB.
static GeomSvc * instance()
singlton instance
Definition: GeomSvc.cxx:211
virtual size_t size() const
Definition: SQHitVector.h:50
#define LogInfo(message)
Definition: DbSvc.cc:14
int getDetectorID(const std::string &detectorName) const
Get the plane position.
Definition: GeomSvc.h:184
virtual short get_element_id() const
Return the element ID of this hit.
Definition: SQHit.h:45
void set_nim_mode(const NimMode nim1, const NimMode nim2)
void setPxMin(double pxmin)
void setRoadID(int id)
Sets.
int getTrDetectorID(unsigned int i) const
int getHodoStation(const int detectorID) const
Definition: GeomSvc.cxx:897
#define NTRPLANES