All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
ClusterCheater5_3.cc
Go to the documentation of this file.
1 #include "ClusterCheater5_3.h"
2 #include <EVENT/LCObject.h>
3 #include <EVENT/LCCollection.h>
4 #include <EVENT/SimCalorimeterHit.h>
5 #include <IMPL/ClusterImpl.h>
6 #include <IMPL/LCCollectionVec.h>
7 #include <IMPL/LCRelationImpl.h>
8 #include <UTIL/LCTOOLS.h>
9 #include <UTIL/LCRelationNavigator.h>
10 #include <iostream>
11 #include "ClusterShapes.h"
12 #include <map>
13 #include <vector>
14 #include <stack>
15 #include <math.h>
16 
17 
18 #include <algorithm>
19 
20 // STUFF needed for GEAR
21 #include <marlin/Global.h>
22 #include <gear/GEAR.h>
23 #include <gear/TPCParameters.h>
24 #include <gear/CalorimeterParameters.h>
25 //#include "MarlinCED.h"
26 //#include "DrowUtil.h"
27 
28 #define MASK_K (unsigned int) 0x3F000000
29 #define SHIFT_K 24
30 #define SHIFT_M 0
31 #define MASK_M (unsigned int) 0x00000007
32 
33 using namespace std ;
34 using namespace lcio ;
35 using namespace marlin ;
36 using namespace UTIL;
37 
38 typedef std::map <MCParticle*,ClusterImpl*> map_MCP_Clust;
39 typedef std::map <MCParticle*,HelixClass*> map_MCP_Helix;
40 bool to_be_saved(const MCParticle * parm , const float cut , const CalorimeterHit* calhit);
42 
43 
44 ClusterCheater5_3::ClusterCheater5_3() : Processor("ClusterCheater5_3") {
45 
46  _description = "Creates true clusters..." ;
47 
48  registerOutputCollection( LCIO::CLUSTER,
49  "TrueClusterCollection",
50  "Collection of True Clusters",
52  std::string("TrueClusters"));
53 
54  registerOutputCollection( LCIO::LCRELATION,
55  "TrueClusterToMCPCollection",
56  "Relation Collection Cluster to MCP",
58  std::string("TrueClusterToMCP"));
59 
60  std::vector<std::string> caloCollections;
61 
62  caloCollections.push_back(std::string("ECAL"));
63  caloCollections.push_back(std::string("HCAL"));
64 
65 
66  registerInputCollections( LCIO::CALORIMETERHIT,
67  "CaloCollections",
68  "Calorimeter Collection Names",
70  caloCollections);
71 
72  registerInputCollection( LCIO::LCRELATION,
73  "RelCollection",
74  "SimCaloHit to CaloHit Relations Collection Name",
76  std::string("RelationCaloHit"));
77 
78  registerInputCollection( LCIO::MCPARTICLE,
79  "MCParticleCollection",
80  "Calorimeter Collection Names",
82  std::string("MCParticle"));
83 
84  registerProcessorParameter("CutBackscatter",
85  "Not to connect hist from backscatter",
86  _backcut ,
87  (int)1 );
88 
89  registerProcessorParameter("MinHitsInCluster",
90  "Minimal number of hits in cluster",
91  _Nmin ,
92  (int)0 );
93 
94 }
95 
97  _nRun = -1;
98  _nEvt = 0;
99  // MarlinCED::init(this) ;
100  _nlost=0;
101  // const gear::CalorimeterParameters& pHcalBarrel = Global::GEAR->getHcalBarrelParameters();
102  const gear::TPCParameters& pTPC = Global::GEAR->getTPCParameters();
103  gearRMax =pTPC.getDoubleVal("tpcOuterRadius");
104  zmax= pTPC.getMaxDriftLength();
105 }
106 
107 
108 void ClusterCheater5_3::processRunHeader( LCRunHeader* /*run*/) {
109  _nRun++ ;
110  _nEvt = 0;
111 }
112 
113 void ClusterCheater5_3::processEvent( LCEvent * evt ) {
114 
115  //float cut =50.0;
116  //float totalUnrecoverableOverlapEcal1max=0.0;
117  //float totalUnrecoverableOverlapEcal2max=0.0;
118  //float totalUnrecoverableOverlapHcalmax=0.0;
119  //float totalUnrecoverableOverlapEcal1min=0.0;
120  //float totalUnrecoverableOverlapEcal2min=0.0;
121  //float totalUnrecoverableOverlapHcalmin=0.0;
122  float noPointerEcal=0.0;
123  //float noPointerHcal=0.0;
124 
125  typedef std::map <MCParticle*,ClusterImpl*> mapMCP2Clust;
126 
127  mapMCP2Clust p2clust;
128 
129  for (unsigned int i=0 ; i < _caloCollections.size(); ++i)
130  {
131 
132  // cout<<"name ="<<_caloCollections[i]<<endl;
133  const LCCollection * relcol ;
134  relcol = evt->getCollection(_relCollection.c_str());
135  LCCollection * col = evt->getCollection(_caloCollections[i].c_str());
136  unsigned int nhits = col->getNumberOfElements();
137 
138 
139 
140 
141 
142  LCRelationNavigator navigate(relcol);
143 
144  for (unsigned int j=0; j < nhits; ++j)
145  {
146  CalorimeterHit * calhit = dynamic_cast<CalorimeterHit*>(col->getElementAt(j));
147  LCObjectVec objectVec = navigate.getRelatedToObjects(calhit);
148 
149  if (objectVec.size() > 0) {
150 
151  vector<MCParticle*> vpar;
152  vector<float>contrib;
153  SimCalorimeterHit * simhit=0;
154  unsigned int ncontrib;
155  vector<MCParticle*>::iterator testp;
156  for( unsigned int k=0;k<objectVec.size();++k)
157  {
158  simhit=dynamic_cast<SimCalorimeterHit*>( objectVec[k]);
159  ncontrib=simhit->getNMCContributions();
160 
161  for ( unsigned int kk=0; kk<ncontrib;++kk)
162  {
163  MCParticle * par=simhit->getParticleCont(kk);
164 
165  int countp=0;
166  testp=find(vpar.begin(),vpar.end(),par);
167  countp=testp-vpar.begin();
168 
169  if( testp == vpar.end())
170  {
171  vpar.push_back(par);
172  contrib.push_back(simhit->getEnergyCont(kk));
173  }else{
174  contrib[countp]+=simhit->getEnergyCont(kk);
175  }
176  }
177  }
178  vector<float>::iterator naj;
179  naj=max_element(contrib.begin(),contrib.end());
180 
181 
182  int max=naj-contrib.begin();
183 
184  MCParticle * par =vpar[max];
185 
186  if ( par !=NULL)
187  {
188 
189  second_jump:
190 
191  if (p2clust[par] == NULL)
192  {
193 
194  float x=par->getVertex()[0];
195  float y=par->getVertex()[1];
196  float z=par->getVertex()[2];
197  float radius=sqrt(x*x+y*y);
198  x=par->getEndpoint()[0];
199  y=par->getEndpoint()[1];
200  float z1=par->getEndpoint()[2];
201  float radius1=sqrt(x*x+y*y);
202 
203 
204  if( ((radius<gearRMax && fabs(z)<zmax) &&
205  (radius1>gearRMax || fabs(z1)>zmax )) ||
206  (par->isBackscatter()&&_backcut) )
207  {
208 
209  ClusterImpl * cluster = new ClusterImpl();
210  p2clust[par]=cluster;
211  // if( to_be_saved(par , cut,calhit ))
212  cluster->addHit(calhit,(float)1.0);
213  }else{
214  if(par->getParents().size()!=0)
215  {
216  par=par->getParents()[0];
217  goto second_jump;
218  }else{
219  ++_nlost;
220  }
221  }
222  }else{
223  ClusterImpl * cluster = p2clust[par];
224  // if( to_be_saved(par , cut,calhit ))
225  cluster->addHit(calhit,(float)1.0);
226  }
227 
228  }else{// par !=0
229 
230  noPointerEcal+=simhit->getEnergyCont(0);
231  }//par !=0
232 
233 
234  }
235 }// over nhits
236 
237 }
238 
239 
240  LCCollectionVec * clscol = new LCCollectionVec(LCIO::CLUSTER);
241  LCCollectionVec * relationcol = new LCCollectionVec(LCIO::LCRELATION);
242 
243  mapMCP2Clust::iterator pos;
244 
245  for (pos = p2clust.begin(); pos != p2clust.end(); ++pos)
246  {
247  MCParticle * mcp = pos->first;
248 
249  ClusterImpl * cluster = pos->second;
250 
251  if(cluster != NULL){
252  CalorimeterHitVec calohitvec = cluster->getCalorimeterHits();
253  int nhcl = (int)calohitvec.size();
254 
255  float totene = 0.0;
256 // float totecal = 0.0;
257 // float tothcal = 0.0;
258  if( nhcl>_Nmin)
259  {
260  float center[3];
261  center[0]=0.0; center[1]=0.0; center[2]=0.0;
262  for (int i=0; i< nhcl; ++i)
263  {
264  CalorimeterHit * calhit = calohitvec[i];
265  float energy=calhit->getEnergy();
266  center[0]+= (calhit->getPosition()[0])*energy;
267  center[1]+= (calhit->getPosition()[1])*energy;
268  center[2]+= (calhit->getPosition()[2])*energy;
269  totene += energy;
270  }
271 
272  center[0]=center[0]/totene;
273  center[1]=center[1]/totene;
274  center[2]=center[2]/totene;
275  cluster->setPosition(center);
276  cluster->setEnergy(totene);
277  clscol->addElement(cluster);
278 
279  LCRelationImpl * rel = new LCRelationImpl(cluster,mcp,(float)1.0);
280  relationcol->addElement( rel );
281  }
282  }
283  }
284  evt->addCollection(clscol,_trueClustCollection.c_str());
285  evt->addCollection(relationcol,_trueClustToMCP.c_str());
286 
287  // cout << " n clusters at the end " << clscol->getNumberOfElements() << endl;
288  _nEvt++;
289 
290 }
291 
292 
293 void ClusterCheater5_3::check( LCEvent * /*evt*/ ) { }
294 
296 {
297 
298  cout << "cluster cheater n hits lost = " << _nlost << endl;
299 }
300 
301 bool to_be_saved(const MCParticle * parm , const float cut,const CalorimeterHit * calhit )
302 {
303 
304 
305  vector<MCParticle*> particlesToRec ;
306 
307  for( unsigned int pp=0;pp<parm->getDaughters().size();++pp)
308  {
309  MCParticle * par =parm->getDaughters()[pp];
310 
311  stack <MCParticle*> krk;
312 
313  if(1)
314 
315  {
316  int testz=count(particlesToRec.begin(),particlesToRec.end(),par);
317  if (testz==0)
318  {
319 
320  // particlesToRec.push_back(par);
321  krk.push(par);
322  }
323  }
324  while(!krk.empty())
325  {
326 
327  par=krk.top();
328  krk.pop();
329 
330  if(1)
331 
332  {
333  int testz=count(particlesToRec.begin(),particlesToRec.end(),par);
334 
335  if (testz==0)
336  {
337  particlesToRec.push_back(par);
338  }
339  for( unsigned int i=0;i<par->getDaughters().size();++i)
340  {
341  krk.push(par->getDaughters()[i]);
342  particlesToRec.push_back(par->getDaughters()[i]);
343  }
344  }
345 
346 
347  }//while
348 
349  }
350 
351  vector<MCParticle*> particlesToBack ;
352  stack <MCParticle*> krk;
353  for ( unsigned int i=0; i< particlesToRec.size();++i)
354  {
355  if( particlesToRec[i]->isBackscatter() )
356  {
357  particlesToBack.push_back( particlesToRec[i]);
358  krk.push(particlesToRec[i]);
359  }
360  }
361 
362  while(!krk.empty())
363  {
364 
365  MCParticle* par=krk.top();
366  krk.pop();
367 
368 
369  int testz=count(particlesToBack.begin(),particlesToBack.end(),par);
370 
371  if (testz==0)
372  {
373  particlesToBack.push_back(par);
374  }
375  for( unsigned int i=0;i<par->getDaughters().size();++i)
376  {
377  krk.push(par->getDaughters()[i]);
378  particlesToBack.push_back(par->getDaughters()[i]);
379  }
380 
381 
382 
383  }//while
384 
385  // cout << "N BCKSCATTER = "<<particlesToBack.size()<< endl;
386  bool test=true;
387  float x1=calhit->getPosition()[0];
388  float y1=calhit->getPosition()[1];
389  float z1=calhit->getPosition()[2];
390  float px=parm->getMomentum()[0];
391  float py=parm->getMomentum()[1];
392  float pz=parm->getMomentum()[2];
393  for ( unsigned int i=0; i< particlesToBack.size();++i)
394  {
395  float x=particlesToBack[i]->getEndpoint()[0];
396  float y=particlesToBack[i]->getEndpoint()[1];
397  float z=particlesToBack[i]->getEndpoint()[2];
398 
399 
400 
401  float dd=px*x1+py*y1+pz*z1;
402  float d=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1));
403 
404  if ( d< cut || dd<0.0)
405  {
406  test=false;
407  }
408  }
409  return test;
410 
411 }
std::string _trueClustToMCP
std::string _relCollection
ClusterCheater5_3 aClusterCheater5_3
virtual void processRunHeader(LCRunHeader *run)
Called for every run.
std::string _trueClustCollection
static const float k
virtual void end()
Called after data processing for clean up.
std::map< MCParticle *, ClusterImpl * > map_MCP_Clust
virtual void processEvent(LCEvent *evt)
Called for every event - the working horse.
virtual void init()
Initialization.
std::vector< std::string > _caloCollections
std::string _MCcollection
std::vector< LCCollection * > LCCollectionVec
Definition: SiStripClus.h:55
virtual void check(LCEvent *evt)
std::map< MCParticle *, HelixClass * > map_MCP_Helix
bool to_be_saved(const MCParticle *parm, const float cut, const CalorimeterHit *calhit)