gbl_example.cxx
Go to the documentation of this file.
1 /*
2  * this is an example of the usage
3  * it's based on the example1.cpp in GBL by Claus Kleinwort
4  */
5 
6 #define AIDATT_USE_DD4HEP 1
7 #define USE_LCIO 1
8 #define USE_GBL 1
9 
10 // c++
11 #include <iostream>
12 #include <map>
13 #include <string>
14 
15 // lcio
16 #include "lcio.h"
17 #include "IO/LCWriter.h"
18 #include "EVENT/LCEvent.h"
19 #include "EVENT/LCCollection.h"
20 #include "EVENT/SimTrackerHit.h"
21 #include "UTIL/LCTrackerConf.h"
22 
23 #include <IMPL/LCCollectionVec.h>
24 #include "IMPL/TrackImpl.h"
25 
26 // DD4hep
27 #include "DD4hep/Detector.h"
28 #include "DD4hep/DD4hepUnits.h"
29 #include "DDRec/SurfaceHelper.h"
30 #include "DDRec/SurfaceManager.h"
31 #include "DDRec/Surface.h"
32 #include "DDRec/CellIDPositionConverter.h"
33 
34 
35 
36 // aidaTT
37 #include "aidaTT/DD4hepGeometry.hh"
38 #include "aidaTT/AidaTT.hh"
39 #include "aidaTT/ConstantSolenoidBField.hh"
40 #include "aidaTT/analyticalPropagation.hh"
41 #include "aidaTT/simplifiedPropagation.hh"
42 #include "aidaTT/GBLInterface.hh"
43 #include "aidaTT/fitResults.hh"
44 
45 using namespace std;
46 using namespace lcio;
47 
48 
49 int main(int argc, char** argv)
50 {
51  if(argc < 2)
52  {
53  std::cout << " usage: ./gbl_example ILDEx.xml ILDExSimu.slcio" << std::endl ;
54  return 1;
55  }
56 
58  std::string inFile = argv[1] ;
59 
61  dd4hep::Detector& theDet = dd4hep::Detector::getInstance();
62  theDet.fromCompact(inFile);
63 
64  dd4hep::DetElement world = theDet.world() ;
65 
66  aidaTT::DD4hepGeometry geom(theDet);
67 
68  const auto& surfaces = geom.getSurfaces() ;
69 
70  dd4hep::rec::CellIDPositionConverter cellid_converter(theDet);
71 
72  dd4hep::rec::SurfaceManager& surfMan = *theDet.extension<dd4hep::rec::SurfaceManager>() ;
73  //auto surfMap = surfMan.map( "world" ) ;
74 
75  // create map of surfaces
76  std::map< long64, const aidaTT::ISurface* > surfMap ;
77  for(const auto& surf : surfaces)
78  {
79  surfMap[surf->id() ] = (surf) ;
80  //surfMap[surf->id() ]->setProperty(dd4hep::rec::SurfaceType::OrthogonalToZ,true);
81  }
82 
83 
84 
86  std::string lcioFileName = argv[2] ;
87 
88  LCReader* rdr = LCFactory::getInstance()->createLCReader() ;
89  rdr->open(lcioFileName) ;
90 
91  std::string outFileName = "trackTest.slcio";
92 
93  LCWriter* wrt = LCFactory::getInstance()->createLCWriter() ;
94  wrt->open(outFileName) ;
95 
96  LCEvent* evt = 0 ;
97 
98  std::vector< std::string > colNames ;
99  colNames.push_back("SiTrackerBarrelHits") ;
100  //colNames.push_back("SiVertexBarrelHits") ;
101  //colNames.push_back("SiTrackerEndcapHits") ;
102  //colNames.push_back("SiVertexEndcapHits") ;
103 
104  UTIL::BitField64 idDecoder(LCTrackerCellID::encoding_string()) ;
105 
106 
107  // the aidaTT stuff
108 
112  // with B = 3.5T => omega = 3* 10^(-4) * 3.5 / sqrt(6)
113  aidaTT::trackParameters bogusTP;
114 
115  //~ const double initialOmega = 3.5*1e-5;
116  //~ const double initialTanLambda = 0.;
117  //~ const double initialPhi = 5*M_PI_4;
118 
119  const double initialOmega = 1.5 * 1e-4;
120  const double initialTanLambda = 1.;
121  const double initialPhi = M_PI_2;
122 
123  bogusTP.setTrackParameters(aidaTT::Vector5(initialOmega, initialTanLambda, initialPhi, 0., 0.));
124 
125  // create the different objects needed for fitting
126  // first a constant field parallel to z, 1T
127  aidaTT::ConstantSolenoidBField* bfield = new aidaTT::ConstantSolenoidBField(3.5);
128 
129  // create the propagation object
130  aidaTT::analyticalPropagation* propagation = new aidaTT::analyticalPropagation();
131  //aidaTT::simplifiedPropagation* propagation = new aidaTT::simplifiedPropagation();
132 
133  // create the fitter object
134  aidaTT::GBLInterface* fitter = new aidaTT::GBLInterface();
135 
136 
137  while((evt = rdr->readNextEvent()) != 0)
138  {
139 
140  IMPL::LCCollectionVec* outputTrackCollection = new IMPL::LCCollectionVec(EVENT::LCIO::TRACK);
141  std::string outName = "trytracks";
142 
143  std::cout << " reading an event " << std::endl;
144  // now create a trajectory object to be fitted
145  aidaTT::trajectory theMaster(bogusTP, fitter, propagation, &geom);
146 
147  for(unsigned icol = 0, ncol = colNames.size() ; icol < ncol ; ++icol)
148  {
149  LCCollection* col = NULL;
150  try
151  {
152  col = evt->getCollection(colNames[ icol ]) ;
153  }
154  catch(DataNotAvailableException &e)
155  {
156  continue;
157  }
158 
159  int nHit = col->getNumberOfElements() ;
160 
161  for(int i = 0 ; i < nHit ; ++i)
162  {
163  SimTrackerHit* sHit = (SimTrackerHit*) col->getElementAt(i) ;
164  long64 id = sHit->getCellID0() ;
165  //idDecoder.setValue(id) ;
166 
167  //const auto si = surfMap->find( cellid_converter.findContext(id)->identifier );
168  // // Get the Surface (http://test-dd4hep.web.cern.ch/test-dd4hep/doxygen/html/classdd4hep_1_1rec_1_1_i_surface.html)
169  //std::cout << surfMap->count( cellid_converter.findContext(id)->identifier ) << "\n";
170  //dd4hep::rec::ISurface* surf = (si != surfMap->end() ? si->second : nullptr);
171 
172  const aidaTT::ISurface* surf = surfMap[ id ] ;
173 
174  dd4hep::rec::Vector3D pos1;
175  pos1 = surf->origin();
176  double recalcPos[3] = {pos1.x(), pos1.y(), pos1.z()}; // could it be more ugly?
177  if(surf->type().isSensitive()) {
178 
179  std::vector<double> precisionDummy;
180  precisionDummy.push_back(1. / 0.0001);
181  precisionDummy.push_back(1. / 0.00012);
182  //for(unsigned int i = 0; i < 3; ++i)
183  // recalcPos[i] = sHit->getPosition()[i] * dd4hep::mm;
184 
185  theMaster.addMeasurement(recalcPos, precisionDummy, *surf, sHit);
186  }
187  }
188 
189  theMaster.prepareForFitting();
190  theMaster.fit();
191 
192  const aidaTT::fitResults& result = *(theMaster.getFitResults());
193 
194  TrackImpl* theTrack = new TrackImpl();
195  theTrack->setOmega(result.estimatedParameters()(0)) ;
196  theTrack->setTanLambda(result.estimatedParameters()(1));
197  theTrack->setPhi(result.estimatedParameters()(2));
198  theTrack->setD0(result.estimatedParameters()(3));
199  theTrack->setZ0(result.estimatedParameters()(4));
200 
201  TrackImpl* theInitialTrack = new TrackImpl();
202  theInitialTrack->setOmega(initialOmega) ;
203  theInitialTrack->setTanLambda(initialTanLambda);
204  theInitialTrack->setPhi(initialPhi);
205  theInitialTrack->setD0(0.);
206  theInitialTrack->setZ0(0.);
207 
208 
209  outputTrackCollection->addElement(theTrack);
210  outputTrackCollection->addElement(theInitialTrack);
211 
212  }
213  evt->addCollection(outputTrackCollection , outName);
214 
215  wrt->writeEvent(evt);
216 
217  }
218 
219 
220  return 0;
221 }
222 
Detector
Definition: DDG4.py:69
int main(int argc, char **argv)
Definition: gbl_example.cxx:49