Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tpc_hits.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file tpc_hits.cc
1 
2 #include "tpc_hits.h"
3 
4 #include <trackbase/TpcDefs.h>
5 #include <trackbase/TrkrDefs.h>
6 #include <trackbase/TrkrHit.h>
7 #include <trackbase/TrkrHitSet.h>
9 
12 #include <trackbase/TrkrHitSetv1.h>
13 #include <trackbase/TrkrHitv2.h>
14 
15 #include <phool/PHCompositeNode.h>
16 #include <phool/PHIODataNode.h> // for PHIODataNode
17 #include <phool/PHNodeIterator.h> // for PHNodeIterator
18 #include <phool/PHObject.h> // for PHObject
19 #include <phool/getClass.h>
20 #include <phool/phool.h>
21 
22 #include <Event/Event.h>
23 #include <Event/EventTypes.h>
24 #include <Event/packet.h>
25 
27 
28 #include <TFile.h>
29 #include <TH2.h>
30 
31 //____________________________________________________________________________..
33  : SubsysReco(name)
34  , hm(nullptr)
35  , _filename("./outputfile.root")
36  {
37  std::cout << "tpc_hits::tpc_hits(const std::string &name)" << std::endl;
38  starting_BCO = -1;
39  rollover_value = 0;
40  current_BCOBIN = 0;
41  M.setMapNames("AutoPad-R1-RevA.sch.ChannelMapping.csv", "AutoPad-R2-RevA-Pads.sch.ChannelMapping.csv", "AutoPad-R3-RevA.sch.ChannelMapping.csv");
42 }
43 
44 //____________________________________________________________________________..
46 {
47  delete hm;
48  std::cout << "tpc_hits::~tpc_hits() Calling dtor" << std::endl;
49 }
50 
51 //____________________________________________________________________________..
52 int tpc_hits::Init(PHCompositeNode * /*topNode*/)
53 {
54  std::cout << "tpc_hits::Init(PHCompositeNode *topNode) Initializing" << std::endl;
55  hm = new Fun4AllHistoManager("HITHIST");
56 
57  _h_hit_XY = new TH2F("_h_hit_XY" ,"_h_hit_XY;X, [m];Y, [m]", 400, -800, 800, 400, -800, 800);
58 
60 
62 }
63 
64 //____________________________________________________________________________..
66 {
67 
68  // get dst node
69  PHNodeIterator iter(topNode);
70  auto dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
71  if (!dstNode)
72  {
73  std::cout << "tpc_hits::InitRun - DST Node missing, doing nothing." << std::endl;
74  exit(1);
75  }
76 
77  // create hitset container if needed
78  auto hitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
79  if (!hitsetcontainer)
80  {
81  std::cout << "tpc_hits::InitRun - creating TRKR_HITSET." << std::endl;
82 
83  // find or create TRKR node
84  PHNodeIterator dstiter(dstNode);
85  auto trkrnode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst("PHCompositeNode", "TRKR"));
86  if (!trkrnode)
87  {
88  trkrnode = new PHCompositeNode("TRKR");
89  dstNode->addNode(trkrnode);
90  }
91 
92  // create container and add to the tree
93  hitsetcontainer = new TrkrHitSetContainerv1;
94  auto newNode = new PHIODataNode<PHObject>(hitsetcontainer, "TRKR_HITSET", "PHObject");
95  trkrnode->addNode(newNode);
96  }
97  topNode->print();
98 
99  // we reset the BCO for the new run
100  starting_BCO = -1;
101  rollover_value = 0;
102  current_BCOBIN = 0;
103 
104  //m_hits = new TrkrHitSetContainerv1();
105 
107 }
108 
109 //____________________________________________________________________________..
111 {
112  //std::cout << "tpc_hits::process_event(PHCompositeNode *topNode) Processing Event" << std::endl;
113  // load relevant nodes
114  // Get the TrkrHitSetContainer node
115  auto trkrhitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
116  assert(trkrhitsetcontainer);
117 
118  Event *_event = findNode::getClass<Event>(topNode, "PRDF");
119  assert( _event );
120 
121  if (_event == nullptr)
122  {
123  std::cout << "tpc_hits::Process_Event - Event not found" << std::endl;
124  return -1;
125  }
126  if (_event->getEvtType() >= 8)
127  {
129  }
130 
131  // check all possible TPC packets that we need to analyze
132  for(int ep=0;ep<2;ep++){
133  for (int sector = 0; sector<=23; sector++)
134  {
135  const int packet = 4000 + sector*10 + ep;
136 
137  // Reading packet
138  Packet *p = _event->getPacket(packet);
139 
140  // Figure out which side
141  int side = 0;
142  if(sector>11) side=1;
143 
144  if (p)
145  {
146  std::cout << "tpc_hits:: Event getPacket: "<< packet << "| Sector"<< sector << "| EndPoint "<< ep << std::endl;
147  }else{
148  continue;
149  }
150 
151  int nr_of_waveforms = p->iValue(0, "NR_WF");
152 
153  for (auto &l : m_hitset)
154  {
155  l = new TrkrHitSetv1();
156 
157  int wf;
158  for (wf = 0; wf < nr_of_waveforms; wf++)
159  {
160  int current_BCO = p->iValue(wf, "BCO") + rollover_value;
161 
162  if (starting_BCO < 0)
163  {
164  starting_BCO = current_BCO;
165  }
166 
167  if (current_BCO < starting_BCO) // we have a rollover
168  {
169  rollover_value += 0x100000;
170  current_BCO = p->iValue(wf, "BCO") + rollover_value;
171  starting_BCO = current_BCO;
172  current_BCOBIN++;
173  }
174  int sampa_nr = p->iValue(wf, "SAMPAADDRESS");
175  int channel = p->iValue(wf, "CHANNEL");
176 
177  int fee = p->iValue(wf, "FEE");
178  int samples = p->iValue( wf, "SAMPLES" );
179  // clockwise FEE mapping
180  //int FEE_map[26]={5, 6, 1, 3, 2, 12, 10, 11, 9, 8, 7, 1, 2, 4, 8, 7, 6, 5, 4, 3, 1, 3, 2, 4, 6, 5};
181  int FEE_R[26]={2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 2, 2, 1, 2, 2, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3};
182  // conter clockwise FEE mapping (From Takao)
183  int FEE_map[26]={3, 2, 5, 3, 4, 0, 2, 1, 3, 4, 5, 7, 6, 2, 0, 1, 0, 1, 4, 5, 11, 9, 10, 8, 6, 7};
184  int pads_per_sector[3] = {96, 128, 192};
185 
186  // setting the mapp of the FEE
187  int feeM = FEE_map[fee]-1;
188  if(FEE_R[fee]==2) feeM += 6;
189  if(FEE_R[fee]==3) feeM += 14;
190  int layer = M.getLayer(feeM, channel);
191 
192  double R = M.getR(feeM, channel);
193  double phi = M.getPhi(feeM, channel) + sector * M_PI / 6 ;
194 
195  TrkrDefs::hitsetkey tpcHitSetKey = TpcDefs::genHitSetKey(layer, sector, side);
196  TrkrHitSetContainer::Iterator hitsetit = trkrhitsetcontainer->findOrAddHitSet(tpcHitSetKey);
197 
198  if( Verbosity() )
199  {
200  int sampaAddress = p->iValue(wf, "SAMPAADDRESS");
201  int sampaChannel = p->iValue(wf, "SAMPACHANNEL");
202  int checksum = p->iValue(wf, "CHECKSUM");
203  int checksumError = p->iValue(wf, "CHECKSUMERROR");
204  std::cout << "tpc_hits::Process_Event Samples "<< samples
205  <<" Chn:"<< channel
206  <<" layer: " << layer
207  << " sampa: "<< sampa_nr
208  << " fee: "<< fee
209  << " Mapped fee: "<< feeM
210  << " sampaAddress: "<< sampaAddress
211  << " sampaChannel: "<< sampaChannel
212  << " checksum: "<< checksum
213  << " checksumError: "<< checksumError
214  << " hitsetkey "<< tpcHitSetKey
215  << " R = " << R
216  << " phi = " << phi
217  << std::endl;
218  }
219  for (int s = 0; s < samples; s++)
220  {
221  int pad = M.getPad(feeM, channel);
222  int t = s + 2 * (current_BCO - starting_BCO);
223  int adc = p->iValue(wf,s);
224  // generate hit key
225  TrkrDefs::hitkey hitkey = TpcDefs::genHitKey((unsigned int) pad + sector*pads_per_sector[FEE_R[sector]-1], (unsigned int) t);
226  // find existing hit, or create
227  auto hit = hitsetit->second->getHit(hitkey);
228 
229  // create hit, assign adc and insert in hitset
230  if (!hit)
231  {
232  // create a new one
233  hit = new TrkrHitv2();
234  hit->setAdc(adc);
235  hitsetit->second->addHitSpecificKey(hitkey, hit);
236  }
237  //else{
238  // hit->setAdc(adc);
239  // hitsetit->second->addHitSpecificKey(hitkey, hit);
240  //}
241 
242  _h_hit_XY->Fill(R*cos(phi),R*sin(phi),adc);
243 
244  }
245 
246  }
247  }
248 
249  }// End of run over packets
250  }//End of ep loop
251  // we skip the mapping to real pads at first. We just say
252  // that we get 16 rows (segment R2) with 128 pads
253  // so each FEE fills 2 rows. Not right, but one step at a time.
254 
256 }
257 
258 //____________________________________________________________________________..
259 //int tpc_hits::ResetEvent(PHCompositeNode * /*topNode*/)
260 //{
261 // std::cout << "tpc_hits::ResetEvent(PHCompositeNode *topNode) Resetting internal structures, prepare for next event" << std::endl;
262 // return Fun4AllReturnCodes::EVENT_OK;
263 //}
264 
265 //____________________________________________________________________________..
266 //int tpc_hits::EndRun(const int runnumber)
267 //{
268 // std::cout << "tpc_hits::EndRun(const int runnumber) Ending Run for Run " << runnumber << std::endl;
269 // return Fun4AllReturnCodes::EVENT_OK;
270 //}
271 
272 //____________________________________________________________________________..
273 int tpc_hits::End(PHCompositeNode * /*topNode*/)
274 {
275  std::cout << "tpc_hits::End(PHCompositeNode *topNode) This is the End..." << std::endl;
276  hm->dumpHistos(_filename, "RECREATE");
277 
279 }
280 
281 //____________________________________________________________________________..
282 //int tpc_hits::Reset(PHCompositeNode * /*topNode*/)
283 //{
284 // std::cout << "tpc_hits::Reset(PHCompositeNode *topNode) being Reset" << std::endl;
285 // return Fun4AllReturnCodes::EVENT_OK;
286 //}
287 
288 //____________________________________________________________________________..
289 //void tpc_hits::Print(const std::string &what) const
290 //{
291 // std::cout << "tpc_hits::Print(const std::string &what) const Printing info for " << what << std::endl;
292 //}