From 824bc29d85ac083a6f4cba0dbaa068ebfe30be7f Mon Sep 17 00:00:00 2001
From: Dmitry Shchukin <dmitry.shchukin@cern.ch>
Date: Tue, 20 Dec 2022 21:28:31 +0100
Subject: [PATCH] Put back TrackingTools implementation for data events

---
 mkcode.cc | 43 +++++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/mkcode.cc b/mkcode.cc
index 219b951..60126d8 100644
--- a/mkcode.cc
+++ b/mkcode.cc
@@ -568,22 +568,49 @@ int procev(RecoEvent &e0)
 #endif
 #ifdef TRACKINGTOOLS
     // track reconstruction with Tracking Tools
-    double mymom = 9999.;
-    double mychisq = 9999.;
+    double mymom;
+    double mychisq;
+    if(!e.mc) {
+      mymom = -0.5;
+      mychisq = -0.5;
+      bool ifupstream = true;
+      // to be set to false for downstream momentum in the muon runs
+      const std::vector<Track_t> mytracks = GetReconstructedTracks(e, ifupstream);
+      for (unsigned int p = 0; p < mytracks.size(); p++) {
+        Track_t track = mytracks.at(p);
+        mymom = track.momentum;
+        mychisq = track.chisquare;        
+        if(mymom > 159.9999 && mymom < 160.0001) mychisq = 500.; // temporary
+        if(mymom > 99.9999 && mymom < 100.0001) mychisq = 500.; // temporary
+      }
+      if(mytracks.size() > 1) mychisq = 500.;
+      if(mytracks.size() > 1) std::cout << "More than 1 track, N = " << mytracks.size() << std::endl;
+
+      if(e.run >= 8396 && e.run <= 8458) mymom *= 0.5; // FIXME: temporary fix for the hadron run 2022 with mom = 50.
+
+      //detect bad values of chi squared and apply chi squared cut
+      if ( isnan((float)mychisq) || isinf((float)mychisq) ) { ipass = 0; }
+      else {
+        if(MomentumLowerCut > -0.5) {
+          if(mychisq < MomentumChiSqLowerCut) ipass = 0;
+          if(mychisq > MomentumChiSqUpperCut) ipass = 0;
+        }
+      }
+    }
+
     if(e.mc) {
+      mymom = 9999.;
+      mychisq = 9999.;
       const bool debug = false;
       std::vector<std::pair<TVector3, double> > hits;
       FillAbsMMHits(e.MM1, hits);
       FillAbsMMHits(e.MM2, hits);
       FillAbsMMHits(e.MM3, hits);
       FillAbsMMHits(e.MM4, hits);
-
       if(debug)
         for(auto hit : hits)
           cout << "Reco point in MM: (" << hit.first.x() << ", " << hit.first.y() << ", " << hit.first.z() << ")" << endl;
       const std::vector<Track_t> tracks = GetReconstructedTracksFromHits(hits);
-
-      // fill
       if(!tracks.empty()) {
         double _best_chi2(9999);
         double _best_mom(9999);
@@ -596,14 +623,10 @@ int procev(RecoEvent &e0)
         }
         mychisq = _best_chi2;
         mymom = _best_mom;
-
         if (debug) std::cout << "mymom = " << mymom <<std::endl;
         if (debug) std::cout << "mychisq = " << mychisq <<std::endl;
       }
-    } else {
-        std::cout << "Trackingtools not implemented for data in mkcode.exe, exiting..." << std::endl;
-        exit(1);
-      }
+    }
 #endif
   
   if(mymom < MomentumLowerCut) ipass = 0;
-- 
GitLab