From 8546e5c3d75d79b71e774bc79dd9da174ebd955a Mon Sep 17 00:00:00 2001
From: John Derek Chapman <chapman@hep.phy.cam.ac.uk>
Date: Mon, 11 Feb 2019 15:01:28 +0000
Subject: [PATCH] Merge branch 'matchingFix' into '21.3'

Small fix in NSW validation alg + update macros

See merge request atlas/athena!21010

(cherry picked from commit 3233692e174813dbb5f8537d32b057a8cf0bbcbb)

b666f0fb Fixing doMuonOnly in matching alg
---
 .../macros/NSWMatching_offline/NSWstudies.h   |  55 +++++++-
 .../NSWMatching_offline/NSWstudies_match.C    | 119 ++++++++----------
 .../macros/NSWMatching_offline/README.md      |  15 +++
 .../macros/NSWMatching_offline/run_matching.C |  16 +--
 .../MuonPRDTest/src/NSWPRDValAlg.cxx          |   5 +-
 5 files changed, 132 insertions(+), 78 deletions(-)
 create mode 100644 MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/README.md

diff --git a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies.h b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies.h
index 484895fdec7..cf996ee2678 100644
--- a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies.h
+++ b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies.h
@@ -30,7 +30,6 @@ public :
    void match_Hits_Digits (Flocalize_collection&, Flocalize_collection&);
    void fillHists (Flocalize_collection& oData, vector< TH1I* >& hist_vec);
    void plotError (Flocalize_collection oPRD, vector<TH1F*> hists_pull);
-
    TTree          *fChain;   //!pointer to the analyzed TTree or TChain
    Int_t           fCurrent; //!current Tree number in a TChain
 
@@ -184,9 +183,14 @@ public :
    vector<int>     *RDO_sTGC_channel;
    vector<int>     *RDO_sTGC_channel_type;
    vector<double>  *RDO_sTGC_time;
-   vector<double>  *RDO_sTGC_charge;
+   vector<unsigned short> *RDO_sTGC_charge;
    vector<unsigned short> *RDO_sTGC_bcTag;
    vector<bool>    *RDO_sTGC_isDead;
+   vector<double>  *RDO_sTGC_localPosX;
+   vector<double>  *RDO_sTGC_localPosY;
+   vector<double>  *RDO_sTGC_globalPosX;
+   vector<double>  *RDO_sTGC_globalPosY;
+   vector<double>  *RDO_sTGC_globalPosZ;
    UInt_t          PRD_sTGC;
    vector<string>  *PRD_sTGC_stationName;
    vector<int>     *PRD_sTGC_stationEta;
@@ -195,6 +199,8 @@ public :
    vector<int>     *PRD_sTGC_gas_gap;
    vector<int>     *PRD_sTGC_channel_type;
    vector<int>     *PRD_sTGC_channel;
+   vector<int>     *PRD_sTGC_charge;
+   vector<unsigned short> *PRD_sTGC_bcTag;
    vector<double>  *PRD_sTGC_globalPosX;
    vector<double>  *PRD_sTGC_globalPosY;
    vector<double>  *PRD_sTGC_globalPosZ;
@@ -298,6 +304,11 @@ public :
    vector<int>     *RDO_MM_channel;
    vector<int>     *RDO_MM_time;
    vector<int>     *RDO_MM_charge;
+   vector<double>  *RDO_MM_localPosX;
+   vector<double>  *RDO_MM_localPosY;
+   vector<double>  *RDO_MM_globalPosX;
+   vector<double>  *RDO_MM_globalPosY;
+   vector<double>  *RDO_MM_globalPosZ;
    UInt_t          PRD_MM;
    vector<string>  *PRD_MM_stationName;
    vector<int>     *PRD_MM_stationEta;
@@ -305,6 +316,7 @@ public :
    vector<int>     *PRD_MM_multiplet;
    vector<int>     *PRD_MM_gas_gap;
    vector<int>     *PRD_MM_channel;
+   vector<int>     *PRD_MM_time;
    vector<double>  *PRD_MM_globalPosX;
    vector<double>  *PRD_MM_globalPosY;
    vector<double>  *PRD_MM_globalPosZ;
@@ -463,6 +475,11 @@ public :
    TBranch        *b_RDO_sTGC_charge;   //!
    TBranch        *b_RDO_sTGC_bcTag;   //!
    TBranch        *b_RDO_sTGC_isDead;   //!
+   TBranch        *b_RDO_sTGC_localPosX;   //!
+   TBranch        *b_RDO_sTGC_localPosY;   //!
+   TBranch        *b_RDO_sTGC_globalPosX;   //!
+   TBranch        *b_RDO_sTGC_globalPosY;   //!
+   TBranch        *b_RDO_sTGC_globalPosZ;   //!
    TBranch        *b_PRDs_sTGC_n;   //!
    TBranch        *b_PRD_sTGC_stationName;   //!
    TBranch        *b_PRD_sTGC_stationEta;   //!
@@ -471,6 +488,8 @@ public :
    TBranch        *b_PRD_sTGC_gas_gap;   //!
    TBranch        *b_PRD_sTGC_channel_type;   //!
    TBranch        *b_PRD_sTGC_channel;   //!
+   TBranch        *b_PRD_sTGC_charge;   //!
+   TBranch        *b_PRD_sTGC_bcTag;   //!
    TBranch        *b_PRD_sTGC_globalPosX;   //!
    TBranch        *b_PRD_sTGC_globalPosY;   //!
    TBranch        *b_PRD_sTGC_globalPosZ;   //!
@@ -574,6 +593,11 @@ public :
    TBranch        *b_RDO_MM_channel;   //!
    TBranch        *b_RDO_MM_time;   //!
    TBranch        *b_RDO_MM_charge;   //!
+   TBranch        *b_RDO_MM_localPosX;   //!
+   TBranch        *b_RDO_MM_localPosY;   //!
+   TBranch        *b_RDO_MM_globalPosX;   //!
+   TBranch        *b_RDO_MM_globalPosY;   //!
+   TBranch        *b_RDO_MM_globalPosZ;   //!
    TBranch        *b_PRDs_MM_n;   //!
    TBranch        *b_PRD_MM_stationName;   //!
    TBranch        *b_PRD_MM_stationEta;   //!
@@ -581,6 +605,7 @@ public :
    TBranch        *b_PRD_MM_multiplet;   //!
    TBranch        *b_PRD_MM_gas_gap;   //!
    TBranch        *b_PRD_MM_channel;   //!
+   TBranch        *b_PRD_MM_time;   //!
    TBranch        *b_PRD_MM_globalPosX;   //!
    TBranch        *b_PRD_MM_globalPosY;   //!
    TBranch        *b_PRD_MM_globalPosZ;   //!
@@ -793,6 +818,11 @@ void NSWstudies::Init(TTree *tree)
    RDO_sTGC_charge = 0;
    RDO_sTGC_bcTag = 0;
    RDO_sTGC_isDead = 0;
+   RDO_sTGC_localPosX = 0;
+   RDO_sTGC_localPosY = 0;
+   RDO_sTGC_globalPosX = 0;
+   RDO_sTGC_globalPosY = 0;
+   RDO_sTGC_globalPosZ = 0;
    PRD_sTGC_stationName = 0;
    PRD_sTGC_stationEta = 0;
    PRD_sTGC_stationPhi = 0;
@@ -800,6 +830,8 @@ void NSWstudies::Init(TTree *tree)
    PRD_sTGC_gas_gap = 0;
    PRD_sTGC_channel_type = 0;
    PRD_sTGC_channel = 0;
+   PRD_sTGC_charge = 0;
+   PRD_sTGC_bcTag = 0;
    PRD_sTGC_globalPosX = 0;
    PRD_sTGC_globalPosY = 0;
    PRD_sTGC_globalPosZ = 0;
@@ -899,12 +931,18 @@ void NSWstudies::Init(TTree *tree)
    RDO_MM_channel = 0;
    RDO_MM_time = 0;
    RDO_MM_charge = 0;
+   RDO_MM_localPosX = 0;
+   RDO_MM_localPosY = 0;
+   RDO_MM_globalPosX = 0;
+   RDO_MM_globalPosY = 0;
+   RDO_MM_globalPosZ = 0;
    PRD_MM_stationName = 0;
    PRD_MM_stationEta = 0;
    PRD_MM_stationPhi = 0;
    PRD_MM_multiplet = 0;
    PRD_MM_gas_gap = 0;
    PRD_MM_channel = 0;
+   PRD_MM_time = 0;
    PRD_MM_globalPosX = 0;
    PRD_MM_globalPosY = 0;
    PRD_MM_globalPosZ = 0;
@@ -1067,6 +1105,11 @@ void NSWstudies::Init(TTree *tree)
    fChain->SetBranchAddress("RDO_sTGC_charge", &RDO_sTGC_charge, &b_RDO_sTGC_charge);
    fChain->SetBranchAddress("RDO_sTGC_bcTag", &RDO_sTGC_bcTag, &b_RDO_sTGC_bcTag);
    fChain->SetBranchAddress("RDO_sTGC_isDead", &RDO_sTGC_isDead, &b_RDO_sTGC_isDead);
+   fChain->SetBranchAddress("RDO_sTGC_localPosX", &RDO_sTGC_localPosX, &b_RDO_sTGC_localPosX);
+   fChain->SetBranchAddress("RDO_sTGC_localPosY", &RDO_sTGC_localPosY, &b_RDO_sTGC_localPosY);
+   fChain->SetBranchAddress("RDO_sTGC_globalPosX", &RDO_sTGC_globalPosX, &b_RDO_sTGC_globalPosX);
+   fChain->SetBranchAddress("RDO_sTGC_globalPosY", &RDO_sTGC_globalPosY, &b_RDO_sTGC_globalPosY);
+   fChain->SetBranchAddress("RDO_sTGC_globalPosZ", &RDO_sTGC_globalPosZ, &b_RDO_sTGC_globalPosZ);
    fChain->SetBranchAddress("PRD_sTGC", &PRD_sTGC, &b_PRDs_sTGC_n);
    fChain->SetBranchAddress("PRD_sTGC_stationName", &PRD_sTGC_stationName, &b_PRD_sTGC_stationName);
    fChain->SetBranchAddress("PRD_sTGC_stationEta", &PRD_sTGC_stationEta, &b_PRD_sTGC_stationEta);
@@ -1075,6 +1118,8 @@ void NSWstudies::Init(TTree *tree)
    fChain->SetBranchAddress("PRD_sTGC_gas_gap", &PRD_sTGC_gas_gap, &b_PRD_sTGC_gas_gap);
    fChain->SetBranchAddress("PRD_sTGC_channel_type", &PRD_sTGC_channel_type, &b_PRD_sTGC_channel_type);
    fChain->SetBranchAddress("PRD_sTGC_channel", &PRD_sTGC_channel, &b_PRD_sTGC_channel);
+   fChain->SetBranchAddress("PRD_sTGC_charge", &PRD_sTGC_charge, &b_PRD_sTGC_charge);
+   fChain->SetBranchAddress("PRD_sTGC_bcTag", &PRD_sTGC_bcTag, &b_PRD_sTGC_bcTag);
    fChain->SetBranchAddress("PRD_sTGC_globalPosX", &PRD_sTGC_globalPosX, &b_PRD_sTGC_globalPosX);
    fChain->SetBranchAddress("PRD_sTGC_globalPosY", &PRD_sTGC_globalPosY, &b_PRD_sTGC_globalPosY);
    fChain->SetBranchAddress("PRD_sTGC_globalPosZ", &PRD_sTGC_globalPosZ, &b_PRD_sTGC_globalPosZ);
@@ -1178,6 +1223,11 @@ void NSWstudies::Init(TTree *tree)
    fChain->SetBranchAddress("RDO_MM_channel", &RDO_MM_channel, &b_RDO_MM_channel);
    fChain->SetBranchAddress("RDO_MM_time", &RDO_MM_time, &b_RDO_MM_time);
    fChain->SetBranchAddress("RDO_MM_charge", &RDO_MM_charge, &b_RDO_MM_charge);
+   fChain->SetBranchAddress("RDO_MM_localPosX", &RDO_MM_localPosX, &b_RDO_MM_localPosX);
+   fChain->SetBranchAddress("RDO_MM_localPosY", &RDO_MM_localPosY, &b_RDO_MM_localPosY);
+   fChain->SetBranchAddress("RDO_MM_globalPosX", &RDO_MM_globalPosX, &b_RDO_MM_globalPosX);
+   fChain->SetBranchAddress("RDO_MM_globalPosY", &RDO_MM_globalPosY, &b_RDO_MM_globalPosY);
+   fChain->SetBranchAddress("RDO_MM_globalPosZ", &RDO_MM_globalPosZ, &b_RDO_MM_globalPosZ);
    fChain->SetBranchAddress("PRD_MM", &PRD_MM, &b_PRDs_MM_n);
    fChain->SetBranchAddress("PRD_MM_stationName", &PRD_MM_stationName, &b_PRD_MM_stationName);
    fChain->SetBranchAddress("PRD_MM_stationEta", &PRD_MM_stationEta, &b_PRD_MM_stationEta);
@@ -1185,6 +1235,7 @@ void NSWstudies::Init(TTree *tree)
    fChain->SetBranchAddress("PRD_MM_multiplet", &PRD_MM_multiplet, &b_PRD_MM_multiplet);
    fChain->SetBranchAddress("PRD_MM_gas_gap", &PRD_MM_gas_gap, &b_PRD_MM_gas_gap);
    fChain->SetBranchAddress("PRD_MM_channel", &PRD_MM_channel, &b_PRD_MM_channel);
+   fChain->SetBranchAddress("PRD_MM_time", &PRD_MM_time, &b_PRD_MM_time);
    fChain->SetBranchAddress("PRD_MM_globalPosX", &PRD_MM_globalPosX, &b_PRD_MM_globalPosX);
    fChain->SetBranchAddress("PRD_MM_globalPosY", &PRD_MM_globalPosY, &b_PRD_MM_globalPosY);
    fChain->SetBranchAddress("PRD_MM_globalPosZ", &PRD_MM_globalPosZ, &b_PRD_MM_globalPosZ);
diff --git a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies_match.C b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies_match.C
index fc59bc4428a..218a8d422a8 100644
--- a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies_match.C
+++ b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/NSWstudies_match.C
@@ -12,8 +12,6 @@
 #include <cmath>
 #include <iostream>
 
-#include "../efficiency.C"
-
 #include "NSWstudies_match.h"
 #include "NSWstudies.h"
 
@@ -24,11 +22,15 @@ void init_hists (vector< TH1I* >& hist_vec, bool isMM, string datatype, string m
 bool doEvt (int evtnr);
 void write_and_delete (vector< TH1I* > vec);
 void init_hist_pull (vector< TH1F*>& hist_pull);
+void efficiency (double missers, double total);
 
 //Inputs
-int min_mismatched_channel = 0;
+bool doMuonOnly = false;
 int max_diff = 3;
 
+// For SDO <> PRD matching one can also look at the pull
+bool doPull = false;
+
 // log settings
 bool quiet = true;
 bool printDigits = false;
@@ -37,9 +39,8 @@ bool printHits = false;
 // Helpers in development:
 // Validate hits beforehand
 bool digi_test = false;
-// Does the electron check do anything?
-bool doElectronCheck = true;
-
+// Check hits and SDO for electron, plan to replace MuonOnly
+bool doElectroncheck = false;
 
 // To test two data objects versus each other change the input of match_Hits_Digits and fillHists functions!!
 void NSWstudies::Loop()
@@ -47,7 +48,7 @@ void NSWstudies::Loop()
    bool doMM = 1;
    bool dosTGC = 1;
 
-   TFile *outFile = new TFile("RootFiles/NSWVal_Hists.root", "recreate");
+   TFile *outFile = new TFile("NSWMatching_Hists.root", "recreate");
    vector< TH1I* > hist_MM_digits;
    vector< TH1I* > hist_MM_hits;
    vector< TH1I* > hist_sTGC_digits;
@@ -58,31 +59,8 @@ void NSWstudies::Loop()
    TH2D *hist_sTGC_global_hits = new TH2D ("sTGC_Global_pos_mismatched_hits", "sTGC_Global_pos_mismatched_hits", 50, -6000., 6000., 50, -6000., 6000.);
 
    vector< TH1F* > hist_pull;
-   init_hist_pull(hist_pull);
-
-//   In a ROOT session, you can do:
-//      root> .L NSWstudies.C
-//      root> NSWstudies t
-//      root> t.GetEntry(12); // Fill t data members with entry number 12
-//      root> t.Show();       // Show values of entry 12
-//      root> t.Show(16);     // Read and show values of entry 16
-//      root> t.Loop();       // Loop on all entriess
-//
-
-//     This is the loop skeleton where:
-//    jentry is the global entry number in the chain
-//    ientry is the entry number in the current Tree
-//  Note that the argument to GetEntry must be:
-//    jentry for TChain::GetEntry
-//    ientry for TTree::GetEntry and TBranch::GetEntry
-//
-//       To read only selected branches, Insert statements like:
-// METHOD1:
-//    fChain->SetBranchStatus("*",0);  // disable all branches
-//    fChain->SetBranchStatus("branchname",1);  // activate branchname
-// METHOD2: replace line
-//    fChain->GetEntry(jentry);       //read all branches
-//by  b_branchname->GetEntry(ientry); //read only this branch
+   if (doPull) { init_hist_pull(hist_pull); }
+
    if (fChain == 0) return;
 
    Long64_t nentries = fChain->GetEntriesFast();
@@ -96,22 +74,18 @@ void NSWstudies::Loop()
    for (jentry=firstentry; jentry<nentries;jentry++) {
       Long64_t ientry = LoadTree(jentry);
       if (ientry < 0) break;
+
       nb = fChain->GetEntry(jentry);   nbytes += nb;
       if (!doEvt(eventNumber)) {continue;}
       // if (Cut(ientry) < 0) continue;
-     
+
       //only muon events:
       bool allMu = true;
       int mu_pdg = 13;
       for (int pdg : *TruthParticle_Pdg) { allMu &= (abs(pdg) == mu_pdg); }
-      if (!allMu) { continue; }
+      if (!allMu && doMuonOnly) { continue; }
       
-      if (!quiet) {
-         printf("\n**** Event: %d ****\n", eventNumber);
-       /*  printf("\nMM's\n");printf("number of truthparticles: %lu\n", TruthParticle_Pdg->size());
-            for (int pdg : *TruthParticle_Pdg) { printf("   namely: %d\n", pdg);}
-            for (double Pt : *TruthParticle_Pt) { printf("  Pt: %f\n", Pt); }*/
-      }
+      if (!quiet) { printf("\n**** Event: %d ****\n", eventNumber);}
 
       if (dosTGC) {
          Flocalize_collection oHits_sTGC ("Hits", Hits_sTGC_off_stationName, Hits_sTGC_off_stationEta, Hits_sTGC_off_stationPhi, Hits_sTGC_off_multiplet, Hits_sTGC_off_gas_gap, Hits_sTGC_off_channel, Hits_sTGC_off_channel_type);
@@ -120,23 +94,23 @@ void NSWstudies::Loop()
          Flocalize_collection oSDO_sTGC ("SDOs", SDO_sTGC_stationName, SDO_sTGC_stationEta, SDO_sTGC_stationPhi, SDO_sTGC_multiplet, SDO_sTGC_gas_gap, SDO_sTGC_channel, SDO_sTGC_channel_type);
          Flocalize_collection oRDO_sTGC ("RDOs", RDO_sTGC_stationName, RDO_sTGC_stationEta, RDO_sTGC_stationPhi, RDO_sTGC_multiplet, RDO_sTGC_gas_gap, RDO_sTGC_channel, RDO_sTGC_channel_type);
          Flocalize_collection oPRD_sTGC ("PRDs", PRD_sTGC_stationName, PRD_sTGC_stationEta, PRD_sTGC_stationPhi, PRD_sTGC_multiplet, PRD_sTGC_gas_gap, PRD_sTGC_channel, PRD_sTGC_channel_type);
-         match_Hits_Digits(oRDO_sTGC, oPRD_sTGC);
-         fillHists(oRDO_sTGC, hist_sTGC_hits);
-         fillHists(oPRD_sTGC, hist_sTGC_digits);
+         match_Hits_Digits(oHits_sTGC, oDigits_sTGC);
+         fillHists(oHits_sTGC, hist_sTGC_hits);
+         fillHists(oDigits_sTGC, hist_sTGC_digits);
 
-         plotError (oPRD_sTGC, hist_pull);
+         if (doPull) { plotError (oPRD_sTGC, hist_pull); }
          
          /*/Digits to Hits 2D:
          for (unsigned int k = 0; k < oDigits_sTGC.size(); ++k) {
             int diff = oDigits_sTGC.matchedchannel.at(k) - oDigits_sTGC.channel->at(k);
-            if ((oDigits_sTGC.matchedchannel.at(k) < 0 || abs(diff) > 3) && oDigits_sTGC.channel->at(k) > min_mismatched_channel) {
+            if (oDigits_sTGC.matchedchannel.at(k) < 0 || abs(diff) > 3) {
                hist_sTGC_global_digits->Fill(Digits_sTGC_globalPosX->at(k), Digits_sTGC_globalPosY->at(k));
             }
          }
          //Hits to Digits 2D:
          for (unsigned int k = 0; k < oHits_sTGC.size(); ++k) {
             int diff = oHits_sTGC.matchedchannel.at(k) - oHits_sTGC.channel->at(k);
-            if ((oHits_sTGC.matchedchannel.at(k) < 0 || abs(diff) > 3) && oHits_sTGC.channel->at(k) > min_mismatched_channel) {
+            if (oHits_sTGC.matchedchannel.at(k) < 0 || abs(diff) > 3) {
                hist_sTGC_global_hits->Fill(Hits_sTGC_detector_globalPositionX->at(k), Hits_sTGC_detector_globalPositionY->at(k));
             }
          }*/
@@ -149,23 +123,23 @@ void NSWstudies::Loop()
          Flocalize_collection oSDO_MM ("SDOs", SDO_MM_stationName, SDO_MM_stationEta, SDO_MM_stationPhi, SDO_MM_multiplet, SDO_MM_gas_gap, SDO_MM_channel);
          Flocalize_collection oRDO_MM ("RDOs", RDO_MM_stationName, RDO_MM_stationEta, RDO_MM_stationPhi, RDO_MM_multiplet, RDO_MM_gas_gap, RDO_MM_channel);
          Flocalize_collection oPRD_MM ("PRDs", PRD_MM_stationName, PRD_MM_stationEta, PRD_MM_stationPhi, PRD_MM_multiplet, PRD_MM_gas_gap, PRD_MM_channel);
-         match_Hits_Digits(oRDO_MM, oPRD_MM);
-         fillHists(oRDO_MM, hist_MM_hits);
-         fillHists(oPRD_MM, hist_MM_digits);
+         match_Hits_Digits(oHits_MM, oDigits_MM);
+         fillHists(oHits_MM, hist_MM_hits);
+         fillHists(oDigits_MM, hist_MM_digits);
 
-         plotError (oPRD_MM, hist_pull);
+         if (doPull) { plotError (oPRD_MM, hist_pull); }
 
          /*/Digits to Hits 2D:
          for (unsigned int k = 0; k < oDigits_MM.sizes(); ++k) {
             int diff = oDigits_MM.matchedchannel.at(k) - oDigits_MM.channel->at(k);
-            if ((oDigits_MM.matchedchannel.at(k) < 0 || abs(diff) > 3) && oDigits_MM.channel->at(k) > min_mismatched_channel) {
+            if (oDigits_MM.matchedchannel.at(k) < 0 || abs(diff) > 3) {
                hist_MM_global_digits->Fill(Digits_MM_globalPosX->at(k), Digits_MM_globalPosY->at(k));
             }
          }/
          // //Hits to Digits 2D:
          for (unsigned int k = 0; k < oHits_MM.size(); ++k) {
             int diff = oHits_MM.matchedchannel.at(k) - oHits_MM.channel->at(k);
-            if ((oHits_MM.matchedchannel.at(k) < 0 || abs(diff) > 3) && oHits_MM.channel->at(k) > min_mismatched_channel) {
+            if (oHits_MM.matchedchannel.at(k) < 0 || abs(diff) > 3) {
                hist_MM_global_hits->Fill(Hits_MM_detector_globalPositionX->at(k), Hits_MM_detector_globalPositionY->at(k));
             }
          }*/
@@ -293,19 +267,23 @@ void NSWstudies::fillHists (Flocalize_collection& oData, vector< TH1I* >& hist_v
       if (abs(diff) > 19 ) { diff = -20; /*printf("Matchedchannel more then 20 strips away! Matchedchannel: %d\n", oData.matchedchannel.at(i));*/ }
       //Check if hit inside volume & only muon:
       if (oData.matchedchannel.at(i) == -100) { continue; }
-      if (oData.name == "Hits" && !digi_test && doElectronCheck) {
+      if (oData.name == "Hits" && !digi_test && doElectroncheck) {
          if (oData.isMM) { 
             bool accept_hit = Hits_MM_isInsideBounds->at(i) * (abs(Hits_MM_particleEncoding->at(i)) == 13);
-            //diff = accept_hit * diff; 
             if (!accept_hit) { continue; }
          } else { 
-         	// For the wire digits, channel numbers can only go up to 59. However I made it so that hits placed in the dead region are given the channelnumber 63. As 63 isn't a valid channel number, the digit isn't added.
+         	// quote: "For the wire digits, channel numbers can only go up to 59. However I made it so that hits placed in the dead region are given the channelnumber 63. As 63 isn't a valid channel number, the digit isn't added.""
             bool accept_hit = Hits_sTGC_isInsideBounds->at(i) * 
             						(abs(Hits_sTGC_particleEncoding->at(i)) == 13) * 
             						(oData.channel_type->at(i) != 2 || oData.channel->at(i) != 63);
-            //diff = accept_hit * diff; 
             if (!accept_hit) { continue; }
          }
+      } else if (oData.name == "SDOs" && !digi_test && doElectroncheck) {
+         if (oData.isMM) { 
+            if (SDO_MM_barcode->at(i) == 0) { continue; }
+         } else { 
+            if (SDO_sTGC_barcode->at(i) == 0) { continue; }
+         }
       }
       //
       hist_diff->Fill(diff); 
@@ -324,7 +302,7 @@ void NSWstudies::fillHists (Flocalize_collection& oData, vector< TH1I* >& hist_v
       for (unsigned int j = 0; j < oData.matchedindices.at(i).size(); ++j) { 
          hist_match->Fill(nextbin);
       }
-      if (abs(diff) > max_diff && oData.channel->at(i) > min_mismatched_channel) { 
+      if (abs(diff) > max_diff) { 
          hist_missmatch->Fill(nextbin);
          hist_missmatched_chc->Fill(oData.channel->at(i));
          hasMissed = true;
@@ -348,18 +326,18 @@ void init_hists (vector< TH1I* >& hist_vec, bool isMM, string datatype, string m
       ndigits = 100;
       type = "sTGC";
    }
-   title.Form("%s_Nearest_matched(%s)channel_minus_Channel_%s", type, matchedwith.c_str(), obj);
+   title.Form("%s_matched_strip_distance_%s_%s", type, obj, matchedwith.c_str());
    hist_vec.push_back( new TH1I (title.Data(), title.Data(), 40, -20, 20) );
    hist_vec[0]->GetXaxis()->SetTitle("Difference(strips) (-20 = out of range)");
-   title.Form("%s_Occurence_of_%s", type, obj);
+   title.Form("%s_position_%s", type, obj);
    hist_vec.push_back( new TH1I (title.Data(), title.Data(), ndigits, 0, ndigits) );
    title.Form("%s_%s_per_%s",type, obj, matchedwith.c_str());
    hist_vec.push_back( new TH1I (title.Data(), title.Data(), ndigits, 0, ndigits) );
-   title.Form("%s_Nr_mismatched_%s", type, obj);
+   title.Form("%s_position_mismatched_%s", type, obj);
    hist_vec.push_back( new TH1I (title.Data(), title.Data(), ndigits, 0, ndigits) );
-   title.Form("%s_Mismatched_events_%s", type, obj);
+   title.Form("%s_events_mismatched_%s", type, obj);
    hist_vec.push_back( new TH1I (title.Data(), title.Data(), 1000, 0, 1000) );
-   title.Form("%s_Mismatched_channel_%s", type, obj);
+   title.Form("%s_channel_mismatched_%s", type, obj);
    hist_vec.push_back( new TH1I (title.Data(), title.Data(), 100, 0, 500) );
 }
 
@@ -379,8 +357,10 @@ void NSWstudies::plotError (Flocalize_collection oPRD, vector<TH1F*> hists_pull)
 	      sTGC_truthX = SDO_sTGC_localPosX->at(j);
 	      sTGC_pull = (sTGC_locX - sTGC_truthX) / sTGC_error;
 
-	      printf("PRD sTGC channel: %d, matched with SDO channel: %d\n", PRD_sTGC_channel->at(i), SDO_sTGC_channel->at(j));
-	      printf("PRD sTGC locX: %f, SDO locx: %f\n\n", sTGC_locX, sTGC_truthX);
+	      if (!quiet) {
+            printf("PRD sTGC channel: %d, matched with SDO channel: %d\n", PRD_sTGC_channel->at(i), SDO_sTGC_channel->at(j));
+            printf("PRD sTGC locX: %f, SDO locx: %f\n\n", sTGC_locX, sTGC_truthX); 
+         }
 
 	      //printf("sTGC1\n");
 	      chTy = oPRD.channel_type->at(i);
@@ -397,8 +377,10 @@ void NSWstudies::plotError (Flocalize_collection oPRD, vector<TH1F*> hists_pull)
 	      MM_truthX = SDO_MM_localPosX->at(j);
 	      MM_pull = (MM_locX - MM_truthX) / MM_error;
 
-	      printf("PRD MM channel: %d, matched with SDO channel: %d\n", PRD_MM_channel->at(i), SDO_MM_channel->at(j));
-	      printf("PRD MM locX: %f, SDO locx: %f\n\n", MM_locX, MM_truthX);
+         if (!quiet) {
+   	      printf("PRD MM channel: %d, matched with SDO channel: %d\n", PRD_MM_channel->at(i), SDO_MM_channel->at(j));
+   	      printf("PRD MM locX: %f, SDO locx: %f\n\n", MM_locX, MM_truthX);
+         }
 
 	      gg = oPRD.gas_gap->at(i) + 5;
 	      hists_pull[gg]->Fill((MM_locX - MM_truthX));
@@ -445,7 +427,10 @@ void init_hist_pull (vector< TH1F*>& hist_pull) {
 
 }
 
-
+void efficiency (double missers, double total) {
+   double eff = ((total - missers)/total);
+   printf("Efficiency: %g/%g = %g %% \n", missers, total, (eff*100.));
+}
 
 
 
diff --git a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/README.md b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/README.md
new file mode 100644
index 00000000000..cd81ebf78cf
--- /dev/null
+++ b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/README.md
@@ -0,0 +1,15 @@
+This folder contains files used to makes histograms comparing various EDM objects of the NSW digitization chain. 
+The underlying matching method is integrated in the NSW PRD Test, these files are to document the histogram production. 
+This is to allow others to reproduce results produced with these files.
+
+To use: 
+Copy the contents of this folder to any directory you like, it works offline.
+A NSW Ntuple, as produced by the NSWValAlg, needs to be present in the same directory.
+(By default the filename is hardcoded to "NSWPRDValAlg.reco.ntuple.root", to allow a different name, change the creator in NSWstudies.h)
+To run: simply run this file (terminal: root -l -q run_loop.C)
+
+By default the program will check RDO<->PRD for both sTGC and MM. 
+To test other objects, change the input of the match_Hits_Digits and fillHists in the Loop function of NSWstudies_match.C
+
+In the beginning of the "NSWstudies_match.C" file, the input variables of the NSW Matching algorithm can be set.
+(These have the same default values as the NSWPRDTest version)
\ No newline at end of file
diff --git a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/run_matching.C b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/run_matching.C
index 9e5a3eb1195..d064e8f590a 100644
--- a/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/run_matching.C
+++ b/MuonSpectrometer/MuonValidation/MuonPRDTest/macros/NSWMatching_offline/run_matching.C
@@ -4,7 +4,7 @@
 
 #include "TROOT.h"
 
-void run_loop () {
+void run_matching () {
 	gROOT->ProcessLine(".L NSWstudies_match.C++");
 	gROOT->ProcessLine("NSWstudies t;");
 	gROOT->ProcessLine("t.Loop()");
@@ -12,18 +12,18 @@ void run_loop () {
 
 /*
 This folder contains files used to makes histograms comparing various EDM objects of the NSW digitization chain. 
-The underlying matchin method is integrated in the NSW PRD Test, these files are to document the histogram production. 
+The underlying matching method is integrated in the NSW PRD Test, these files are to document the histogram production. 
 This is to allow others to reproduce results produced with these files.
 
 To use: 
-Copy the containts of this folder to any directory you like, it works offline.
-A NSW Ntuple, as produced by the NSWPRDTest, needs to be present in the same directory.
+Copy the contents of this folder to any directory you like, it works offline.
+A NSW Ntuple, as produced by the NSWValAlg, needs to be present in the same directory.
 (By default the filename is hardcoded to "NSWPRDValAlg.reco.ntuple.root", to allow a different name, change the creator in NSWstudies.h)
-You will need to crate an extra directory named "Rootfiles", where the output histograms will be placed
-To run simply run this file (terminal: root -l -q run_loop.C)
-In the beginning of the "NSWstudies_match.C" file, the input variables of the NSW Matching algorithm can be set.
-(These have the same default values as the NSWPRDTest version)
+To run: simply run this file (terminal: root -l -q run_loop.C)
 
 By default the program will check RDO<->PRD for both sTGC and MM. 
 To test other objects, change the input of the match_Hits_Digits and fillHists in the Loop function of NSWstudies_match.C
+
+In the beginning of the "NSWstudies_match.C" file, the input variables of the NSW Matching algorithm can be set.
+(These have the same default values as the NSWPRDTest version)
 */
\ No newline at end of file
diff --git a/MuonSpectrometer/MuonValidation/MuonPRDTest/src/NSWPRDValAlg.cxx b/MuonSpectrometer/MuonValidation/MuonPRDTest/src/NSWPRDValAlg.cxx
index 7a3d9365a62..767e48f58cb 100644
--- a/MuonSpectrometer/MuonValidation/MuonPRDTest/src/NSWPRDValAlg.cxx
+++ b/MuonSpectrometer/MuonValidation/MuonPRDTest/src/NSWPRDValAlg.cxx
@@ -390,7 +390,10 @@ StatusCode NSWPRDValAlg::NSWMatchingAlg (EDM_object data0, EDM_object data1) {
 
   // Prepare Muon only check
   std::vector<int>* TruthParticle_Pdg;
-  if ( m_doNSWMatchingMuon ) { m_tree->SetBranchAddress("TruthParticle_Pdg", &TruthParticle_Pdg); }
+  if ( m_doNSWMatchingMuon ) { 
+    TruthParticle_Pdg = 0;
+    m_tree->SetBranchAddress("TruthParticle_Pdg", &TruthParticle_Pdg); 
+  }
 
   Long64_t nEntries = m_tree->GetEntriesFast();
   for (Long64_t i_entry = 0; i_entry < nEntries; ++i_entry) {
-- 
GitLab