Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CalibMergeH4.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <cmath>
3 #include <interface_main/SQRun.h>
6 #include <phool/PHNodeIterator.h>
7 #include <phool/PHIODataNode.h>
8 #include <phool/getClass.h>
9 #include <geom_svc/GeomSvc.h>
10 #include "CalibMergeH4.h"
11 using namespace std;
12 
13 CalibMergeH4::CalibMergeH4(const std::string& name) : SubsysReco(name), m_and_mode(false), m_remove_mode(false)
14 {
15  ;
16 }
17 
19 {
20  ;
21 }
22 
24 {
26 }
27 
29 {
31 }
32 
34 {
35  SQHitVector* hit_vec = findNode::getClass<SQHitVector>(topNode, "SQHitVector");
36  SQHitVector* trig_hit_vec = findNode::getClass<SQHitVector>(topNode, "SQTriggerHitVector");
37  if (!hit_vec || !trig_hit_vec) return Fun4AllReturnCodes::ABORTEVENT;
38  MergeHits( hit_vec);
39  MergeHits(trig_hit_vec);
41 }
42 
43 int CalibMergeH4::MergeHits(SQHitVector* vec_in)
44 {
45  return m_and_mode ? MergeHitsAnd(vec_in) : MergeHitsOr(vec_in);
46 }
47 
48 short CalibMergeH4::FindMergedId(const short id)
49 {
50  if (id == 0) return 0;
51  GeomSvc* geom = GeomSvc::instance();
52  string name = geom->getDetectorName(id);
53  if (name.substr(0, 2) != "H4") return 0;
54  string name2 = (name[2] == 'T' || name[2] == 'B') ? name.substr(0, 3) : name.substr(0, 5);
55  if (name2 == name) return 0; // The original name doesn't have 'u', 'd', 'l' nor 'r'.
56  return geom->getDetectorID(name2);
57 }
58 
59 int CalibMergeH4::MergeHitsOr(SQHitVector* vec_in)
60 {
61  for (unsigned int ih = 0; ih < vec_in->size(); ih++) {
62  SQHit* hit = vec_in->at(ih);
63  short det_new = FindMergedId(hit->get_detector_id());
64  if (det_new == 0) continue;
65  if (m_remove_mode) {
66  hit->set_detector_id(det_new); // Modify ID, which effectively removes the original one.
67  } else {
68  SQHit* hit_new = hit->Clone();
69  hit_new->set_detector_id(det_new);
70  vec_in->push_back(hit_new);
71  }
72  }
73  return 0;
74 }
75 
76 int CalibMergeH4::MergeHitsAnd(SQHitVector* vec_in)
77 {
78  typedef tuple<short, short, short> MergedGroup_t; // <merged det, element, level>
79  typedef map<short, SQHitVector*> MapVec_t; // <det, vector*>
80  typedef map<MergedGroup_t, MapVec_t> MapMapVec_t;
81  MapMapVec_t map_map_vec;
82  // MergedGroup_t represents a group of hits that are merged into one hit.
83  // Per merged group we expect two unmerged det IDs (ex. H4Tu & H4Td per H4T),
84  // which are used as the key of MapVec_t.
85  // Thus MapVec_t usually holds two hit vectors.
86 
88  for (int ih = vec_in->size() - 1; ih >= 0; ih--) {
89  SQHit* hit = vec_in->at(ih);
90  short det_org = hit->get_detector_id();
91  short det_new = FindMergedId(det_org);
92  if (det_new == 0) continue;
93  MapVec_t* map_vec = &map_map_vec[MergedGroup_t(det_new, hit->get_element_id(), hit->get_level())];
94  if (map_vec->find(det_org) == map_vec->end()) {
95  (*map_vec)[det_org] = vec_in->Clone();
96  map_vec->at(det_org)->clear();
97  }
98  map_vec->at(det_org)->push_back(hit);
99  if (m_remove_mode) vec_in->erase(ih);
100  }
101 
103  for (MapMapVec_t::iterator it = map_map_vec.begin(); it != map_map_vec.end(); it++) {
104  short det_new = std::get<0>(it->first);
105  MapVec_t* map_vec = &it->second;
106  int n_det = map_vec->size();
107  if (n_det == 2) { // Good in the "and" mode. Merge all hits.
108  SQHit* hit_push = 0;
109  int nhit = 0;
110  double time = 0;
111  for (MapVec_t::iterator it2 = map_vec->begin(); it2 != map_vec->end(); it2++) {
112  SQHitVector* vec = it2->second;
113  for (SQHitVector::Iter it3 = vec->begin(); it3 != vec->end(); it3++) {
114  SQHit* hit = *it3;
115  time += hit->get_tdc_time();
116  nhit++;
117  if (! hit_push) hit_push = hit;
118  }
119  }
120  hit_push->set_tdc_time(time/nhit); // average
121  hit_push->set_detector_id(det_new);
122  vec_in->push_back(hit_push);
123  } else if (n_det > 2) {
124  cerr << "CalibMergeH4::MergeHitsAnd(): Unexpectedly found " << map_vec->size() << " detectors per merged detector." << endl;
125  }
126 
127  for (MapVec_t::iterator it2 = map_vec->begin(); it2 != map_vec->end(); it2++) {
128  SQHitVector* vec = it2->second;
129  vec->clear();
130  delete vec;
131  }
132  }
133  return 0;
134 }
135 
137 {
139 }
std::string getDetectorName(const int &detectorID) const
Definition: GeomSvc.h:188
virtual void clear()
Definition: SQHitVector.h:51
virtual void set_tdc_time(const float a)
Definition: SQHit.h:55
An SQ interface class to hold one detector hit.
Definition: SQHit.h:20
virtual ConstIter end() const
Definition: SQHitVector.h:59
virtual void set_detector_id(const short a)
Definition: SQHit.h:43
virtual SQHit * Clone() const
Definition: SQHit.h:35
virtual size_t erase(const size_t idkey)
Definition: SQHitVector.h:56
virtual SQHitVector * Clone() const
Definition: SQHitVector.h:47
int process_event(PHCompositeNode *topNode)
Definition: CalibMergeH4.cc:33
std::vector< SQHit * >::iterator Iter
Definition: SQHitVector.h:38
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
virtual void push_back(const SQHit *hit)
Definition: SQHitVector.h:55
virtual float get_tdc_time() const
Return the TDC time (nsec) of this hit.
Definition: SQHit.h:54
virtual ~CalibMergeH4()
Definition: CalibMergeH4.cc:18
int InitRun(PHCompositeNode *topNode)
Definition: CalibMergeH4.cc:28
CalibMergeH4(const std::string &name="CalibMergeH4")
Definition: CalibMergeH4.cc:13
An SQ interface class to hold a list of SQHit objects.
Definition: SQHitVector.h:32
int Init(PHCompositeNode *topNode)
Definition: CalibMergeH4.cc:23
static GeomSvc * instance()
singlton instance
Definition: GeomSvc.cxx:211
virtual size_t size() const
Definition: SQHitVector.h:50
int getDetectorID(const std::string &detectorName) const
Get the plane position.
Definition: GeomSvc.h:184
int End(PHCompositeNode *topNode)
Called at the end of all processing.
virtual short get_level() const
Return the trigger level of this hit. Meaningful only if this hit is of V1495 TDC.
Definition: SQHit.h:51
virtual short get_element_id() const
Return the element ID of this hit.
Definition: SQHit.h:45
virtual ConstIter begin() const
Definition: SQHitVector.h:58