testVectorIO.cxx

Go to the documentation of this file.
00001 
00002 //
00003 // Cint macro to test I/O of mathcore Lorentz Vectors in a Tree and compare with a 
00004 // TLorentzVector. A ROOT tree is written and read in both using either a XYZTVector or /// a TLorentzVector. 
00005 // 
00006 //  To execute the macro type in: 
00007 //
00008 // root[0]: .x  mathcoreVectorIO.C
00009 
00010 
00011 #ifdef USE_REFLEX
00012 #include "Cintex/Cintex.h"
00013 #include "Reflex/Reflex.h"
00014 #endif
00015 
00016 #include "TRandom3.h"
00017 #include "TStopwatch.h"
00018 #include "TSystem.h"
00019 #include "TFile.h"
00020 #include "TTree.h"
00021 #include "TH1D.h"
00022 #include "TCanvas.h"
00023 
00024 #include <iostream>
00025 
00026 #include "TLorentzVector.h"
00027 
00028 #include "Math/Vector4D.h"
00029 #include "Math/Vector3D.h"
00030 #include "Math/Point3D.h"
00031 
00032 #include "Track.h"
00033 
00034 
00035 #define DEBUG
00036 
00037 
00038 //#define READONLY
00039 
00040 //#define USE_REFLEX
00041 
00042 #define DEFVECTOR4D(TYPE) \
00043 typedef TYPE AVector4D; \
00044 const std::string vector4d_type = #TYPE ;
00045 
00046 #define DEFVECTOR3D(TYPE) \
00047 typedef TYPE AVector3D; \
00048 const std::string vector3d_type = #TYPE ;
00049 
00050 #define DEFPOINT3D(TYPE) \
00051 typedef TYPE APoint3D; \
00052 const std::string point3d_type = #TYPE ;
00053 
00054 
00055 
00056 //const double tol = 1.0E-16;
00057 const double tol = 1.0E-6; // or doublr 32 or float
00058 
00059 DEFVECTOR4D(ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<Double32_t> >);
00060 //DEFVECTOR4D(ROOT::Math::LorentzVector<ROOT::Math::PtEtaPhiM4D<Double32_t> >);
00061 
00062 DEFVECTOR3D(ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<Double32_t> >); 
00063 
00064 DEFPOINT3D(ROOT::Math::PositionVector3D<ROOT::Math::Cartesian3D<Double32_t> >); 
00065 
00066 
00067 // DEFVECTOR4D(ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<Double32_t> >) 
00068 
00069 //using namespace ROOT::Math;
00070 
00071 template<class Vector> 
00072 inline double getMag2(const Vector & v) { 
00073   return v.mag2();
00074 }
00075 
00076 // inline double getMag2(const VecTrackD & v) { 
00077 //    // print the read points
00078 //    std::cout << "VecTRackD " << std::endl;
00079 //    for (VecTrackD::It itr = v.begin() ; itr != v.end(); ++itr) 
00080 //       std::cout << (*itr).Pos() << std::endl; 
00081 
00082 //   return v.mag2();
00083 // }
00084 
00085 
00086 inline double getMag2(const TVector3 & v) { 
00087   return v.Mag2();
00088 }
00089 
00090 inline double getMag2(const TLorentzVector & v) { 
00091   return v.Mag2();
00092 }
00093 
00094 template<class U> 
00095 inline void setValues(ROOT::Math::DisplacementVector3D<U> & v, const double x[] ) { 
00096     v.SetXYZ(x[0],x[1],x[2]); 
00097 }
00098 
00099 template<class U> 
00100 inline void setValues(ROOT::Math::PositionVector3D<U> & v, const double x[]) { 
00101     v.SetXYZ(x[0],x[1],x[2]); 
00102 }
00103 
00104 template<class U> 
00105 inline void setValues(ROOT::Math::LorentzVector<U> & v, const double x[]) { 
00106     v.SetXYZT(x[0],x[1],x[2],x[3]); 
00107 }
00108 // specialization for T -classes 
00109 inline void setValues(TVector3 & v, const double x[]) { 
00110     v.SetXYZ(x[0],x[1],x[2]); 
00111 }
00112 inline void setValues(TLorentzVector & v, const double x[]) { 
00113     v.SetXYZT(x[0],x[1],x[2],x[3]); 
00114 }
00115 
00116 
00117 template <class Vector> 
00118 double testDummy(int n) { 
00119 
00120   TRandom3 R(111);
00121 
00122   TStopwatch timer;
00123 
00124   Vector v1;
00125   double s = 0; 
00126   double p[4];
00127 
00128   timer.Start();
00129   for (int i = 0; i < n; ++i) { 
00130         p[0] = R.Gaus(0,10);
00131         p[1] = R.Gaus(0,10);
00132         p[2] = R.Gaus(0,10);
00133         p[3] = R.Gaus(100,10);
00134         setValues(v1,p);
00135         s += getMag2(v1); 
00136   }
00137 
00138   timer.Stop();
00139 
00140   double sav = std::sqrt(s/double(n));
00141 
00142   std::cout << " Time for Random gen " << timer.RealTime() << "  " << timer.CpuTime() << std::endl; 
00143   int pr = std::cout.precision(18);  std::cout << "Average : " << sav << std::endl;   std::cout.precision(pr);
00144 
00145   return sav; 
00146 }
00147 
00148 //----------------------------------------------------------------
00149 /// writing
00150 //----------------------------------------------------------------
00151 
00152 template<class Vector> 
00153 double write(int n, const std::string & file_name, const std::string & vector_type, int compress = 0) { 
00154 
00155   TStopwatch timer;
00156 
00157   TRandom3 R(111);
00158 
00159   std::cout << "writing a tree with " << vector_type << std::endl; 
00160 
00161   std::string fname = file_name + ".root";
00162   TFile f1(fname.c_str(),"RECREATE","",compress);
00163 
00164   // create tree
00165   std::string tree_name="Tree with" + vector_type; 
00166   TTree t1("t1",tree_name.c_str());
00167 
00168   Vector *v1 = new Vector();
00169   std::cout << "typeID written : " << typeid(*v1).name() << std::endl;
00170 
00171   t1.Branch("Vector branch",vector_type.c_str(),&v1);
00172 
00173   timer.Start();
00174   double p[4]; 
00175   double s = 0; 
00176   for (int i = 0; i < n; ++i) { 
00177         p[0] = R.Gaus(0,10);
00178         p[1] = R.Gaus(0,10);
00179         p[2] = R.Gaus(0,10);
00180         p[3] = R.Gaus(100,10);
00181         //CylindricalEta4D<double> & c = v1->Coordinates();
00182         //c.SetValues(Px,pY,pZ,E);
00183         setValues(*v1,p);
00184         t1.Fill();
00185         s += getMag2(*v1); 
00186   }
00187 
00188   f1.Write();
00189   timer.Stop();
00190 
00191   double sav = std::sqrt(s/double(n));
00192 
00193 
00194 #ifdef DEBUG
00195   t1.Print();
00196   std::cout << " Time for Writing " << file_name << "\t: " << timer.RealTime() << "  " << timer.CpuTime() << std::endl; 
00197   int pr = std::cout.precision(18);  std::cout << "Average : " << sav << std::endl;   std::cout.precision(pr);
00198 #endif
00199   
00200   return sav; 
00201 }
00202 
00203 
00204 //----------------------------------------------------------------
00205 /// reading
00206 //----------------------------------------------------------------
00207 
00208 template<class Vector> 
00209 double read(const std::string & file_name) { 
00210 
00211   TStopwatch timer;
00212 
00213   std::string fname = file_name + ".root";
00214 
00215   TFile f1(fname.c_str());
00216   if (f1.IsZombie() ) { 
00217     std::cout << " Error opening file " << file_name << std::endl; 
00218     return -1; 
00219   }
00220 
00221   //TFile f1("mathcoreVectorIO_D32.root");
00222 
00223   // create tree
00224   TTree *t1 = (TTree*)f1.Get("t1");
00225 
00226   Vector *v1 = 0;
00227 
00228   std::cout << "reading typeID  : " << typeid(*v1).name() << std::endl;
00229 
00230   t1->SetBranchAddress("Vector branch",&v1);
00231 
00232   timer.Start();
00233   int n = (int) t1->GetEntries();
00234   std::cout << " Tree Entries " << n << std::endl; 
00235   double s=0;
00236   for (int i = 0; i < n; ++i) { 
00237     t1->GetEntry(i);
00238     s += getMag2(*v1);
00239   }
00240 
00241 
00242   timer.Stop();
00243 
00244   double sav = std::sqrt(s/double(n));
00245 
00246 #ifdef DEBUG
00247   std::cout << " Time for Reading " << file_name << "\t: " << timer.RealTime() << "  " << timer.CpuTime() << std::endl; 
00248   int pr = std::cout.precision(18);  std::cout << "Average : " << sav << std::endl;   std::cout.precision(pr);
00249 #endif
00250 
00251   return sav; 
00252 }
00253 
00254 template<class TrackType>
00255 double writeTrack(int n, const std::string & file_name, int compress = 0) { 
00256 
00257   std::cout << "\n";
00258   std::cout << "  Test writing .." << file_name << " .....\n";
00259   std::cout << "**************************************************\n";
00260 
00261   TRandom3 R(111);
00262 
00263   TStopwatch timer;
00264 
00265   //std::cout << "writing a tree with " << vector_type << std::endl; 
00266 
00267   std::string fname = file_name + ".root";
00268   TFile f1(fname.c_str(),"RECREATE","",compress);
00269 
00270   // create tree
00271   std::string tree_name="Tree with TrackD"; 
00272   TTree t1("t1",tree_name.c_str());
00273 
00274   TrackType *track = new TrackType();
00275   std::cout << "typeID written : " << typeid(*track).name() << std::endl;
00276 
00277   
00278   //t1.Branch("Vector branch",&track,16000,0);
00279   // default split model
00280   t1.Branch("Vector branch",&track);
00281 
00282   timer.Start();
00283   double s = 0; 
00284   ROOT::Math::XYZTVector q; 
00285   ROOT::Math::XYZPoint p; 
00286   typedef typename TrackType::VectorType V;
00287   typedef typename TrackType::PointType  P;
00288 
00289   for (int i = 0; i < n; ++i) { 
00290     q.SetXYZT( R.Gaus(0,10),
00291                R.Gaus(0,10),
00292                R.Gaus(0,10),
00293                R.Gaus(100,10) ); 
00294     p.SetXYZ( q.X(), q.Y(), q.Z() ); 
00295         
00296     track->Set( V(q), P(p) ); 
00297     
00298     t1.Fill();
00299     s += getMag2( *track ); 
00300   }
00301 
00302   f1.Write();
00303   timer.Stop();
00304 
00305   double sav = std::sqrt(s/double(n));
00306 
00307 
00308 #ifdef DEBUG
00309   t1.Print();
00310   std::cout << " Time for Writing " << file_name << "\t: " << timer.RealTime() << "  " << timer.CpuTime() << std::endl; 
00311   int pr = std::cout.precision(18);  std::cout << "Average : " << sav << std::endl;   std::cout.precision(pr);
00312 #endif
00313   
00314   return sav; 
00315 
00316 }
00317 
00318 
00319 
00320 int testResult(double w1, double r1, const std::string & type) { 
00321 
00322   int iret = 0; 
00323   std::cout << w1 << "  r " << r1 << std::endl; 
00324   // do like this to avoid nan's
00325   if (!( fabs(w1-r1)  < tol)) { 
00326     std::cout << "\nERROR: Differeces found  when reading " << std::endl;
00327     int pr = std::cout.precision(18);  std::cout << w1 << "   !=    " << r1 << std::endl; std::cout.precision(pr);
00328     iret = -1;
00329   }
00330 
00331   std::cout << "\n*********************************************************************************************\n"; 
00332   std::cout << "Test :\t " << type << "\t\t";
00333   if (iret ==0) 
00334     std::cout << "OK" << std::endl; 
00335   else 
00336     std::cout << "FAILED" << std::endl; 
00337   std::cout << "********************************************************************************************\n\n"; 
00338 
00339   return iret; 
00340 }
00341 
00342 
00343 
00344 int testVectorIO(bool readOnly = false) { 
00345 
00346   int iret = 0; 
00347 
00348 // #ifdef __CINT__
00349 //   gSystem->Load("libMathCore");  
00350 //   gSystem->Load("libPhysics");  
00351 //   using namespace ROOT::Math;
00352 // #endif
00353 
00354 #ifdef USE_REFLEX
00355 
00356 // #ifdef __CINT__
00357 //   gSystem->Load("libReflex");  
00358 //   gSystem->Load("libCintex");  
00359 // #endif
00360 
00361   std::cout << "Use Reflex dictionary " << std::endl; 
00362 
00363   gSystem->Load("libReflex");  
00364   gSystem->Load("libCintex");  
00365 
00366   ROOT::Cintex::Cintex::SetDebug(1);
00367   ROOT::Cintex::Cintex::Enable();
00368 
00369 
00370 
00371   //iret |= gSystem->Load("libSmatrixRflx");  
00372   //iret |= gSystem->Load("libMathAddRflx");  
00373   //iret |= gSystem->Load("libMathRflx");  
00374   if (iret |= 0) { 
00375     std::cerr <<"Failing to Load Reflex dictionaries " << std::endl;
00376     return iret; 
00377   }
00378 
00379 #endif   // endif on using reflex
00380 
00381   
00382   int nEvents = 100000;
00383   //int nEvents = 100;
00384 
00385   double w1, r1 = 0;
00386   std::string fname; 
00387   
00388   testDummy<AVector4D>(nEvents);
00389  
00390   fname = "lorentzvector";
00391   if (readOnly) {
00392      w1 = 98.995527276968474;
00393      fname += "_prev";
00394   }
00395   else
00396      w1 = write<AVector4D>(nEvents,fname,vector4d_type);
00397 
00398   r1 = read<AVector4D>(fname);
00399   iret |= testResult(w1,r1,vector4d_type); 
00400 
00401   fname = "displacementvector";
00402   if (readOnly) {
00403      w1 = 17.3281570633214095; 
00404      fname += "_prev";
00405   }
00406   else 
00407      w1 = write<AVector3D>(nEvents,fname,vector3d_type);
00408 
00409   r1 = read<AVector3D>(fname);
00410   iret |= testResult(w1,r1,vector3d_type); 
00411 
00412   fname = "positionvector";
00413   if (readOnly)
00414      fname += "_prev";
00415   else 
00416      w1 = write<APoint3D>(nEvents,fname,point3d_type);
00417   
00418   r1 = read<APoint3D>(fname);
00419   iret |= testResult(w1,r1,point3d_type); 
00420 
00421 
00422   // test TrackD
00423   fname = "track";
00424   // load track dictionary 
00425   gSystem->Load("libTrackDict");  
00426 
00427   //nEvents = 10000; 
00428 
00429   if (readOnly) {
00430      fname += "_prev";
00431   }
00432   else 
00433     w1 = writeTrack<TrackD>( nEvents,fname); 
00434   
00435   r1 = read<TrackD>(fname);
00436   iret |= testResult(w1,r1,"TrackD"); 
00437 
00438   // test TrackD32
00439 
00440   fname = "trackD32";
00441   // load track dictionary 
00442   //  gSystem->Load("libTrackDict");  
00443 
00444   if (readOnly) {
00445      fname += "_prev";
00446   }
00447   else 
00448     w1 = writeTrack<TrackD32>( nEvents,fname); 
00449   
00450   r1 = read<TrackD32>(fname);
00451   iret |= testResult(w1,r1,"TrackD32"); 
00452 
00453 
00454   // test vector of tracks
00455   fname = "vectrack";
00456 
00457   nEvents = 10000; 
00458 
00459   if (readOnly) {
00460      fname += "_prev";
00461   }
00462   else 
00463     w1 = writeTrack<VecTrackD>( nEvents,fname); 
00464   
00465   r1 = read<VecTrackD>(fname);
00466   iret |= testResult(w1,r1,"VecTrackD"); 
00467 
00468   // test ClusterD (cotaining a vector of points) 
00469   fname = "cluster";
00470 
00471   nEvents = 10000; 
00472 
00473   if (readOnly) {
00474      fname += "_prev";
00475   }
00476   else 
00477     w1 = writeTrack<ClusterD>( nEvents,fname); 
00478   
00479   r1 = read<ClusterD>(fname);
00480   iret |= testResult(w1,r1,"ClusterD"); 
00481   
00482   return iret; 
00483 }
00484 
00485 int main(int argc, char ** ) { 
00486 
00487    bool readOnly = false;
00488    if (argc > 1) readOnly = true; 
00489 
00490    int iret =  testVectorIO(readOnly);
00491    if (iret != 0) 
00492       std::cerr << "testVectorIO:\t FAILED ! " << std::endl;
00493    else 
00494       std::cerr << "testVectorIO:\t OK ! " << std::endl;
00495    
00496    return iret; 
00497 
00498 }
00499   

Generated on Tue Jul 5 14:33:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1