Index: /trunk/src/LHCOConverter.cc
===================================================================
--- /trunk/src/LHCOConverter.cc	(revision 315)
+++ /trunk/src/LHCOConverter.cc	(revision 316)
@@ -34,4 +34,5 @@
 #include "ExRootTreeReader.h"
 #include "BlockClasses.h"
+#include "SmearUtil.h"
 
 // root includes
@@ -148,5 +149,5 @@
 
     // 2 muon data
-    BranchReader(branchMuon,line,lhcoMuonID);
+    BranchReaderMuon(branchMuon,branchJet,line);
 
     // 3 tau-jets
@@ -173,18 +174,47 @@
     unsigned int branch_size = branch->GetEntries();
     TRootParticle * particle = 0;
-    for (unsigned int i=0; i< branch_size; i++) {
+
+    // parsing the branch
+    for (unsigned int i=0; i< branch_size; i++) { 
         particle = (TRootParticle*) branch->At(i);
 	TLorentzVector pmu;
 	pmu.SetPtEtaPhiE(particle->PT, particle->Eta, particle->Phi, particle->E);
 	float jmass = pmu.M();
+//if(jmass < 0) cout << "jmass negatif" << particle->PT << " " <<  particle->Eta << " " <<  particle->Phi << " " <<  particle->E << " " << jmass << endl;
 	float ntrk = 0.0;
         float btag =0;
         double ratioE = 0;
 
-        if(lhcoID == lhcoPhotonID){TRootPhoton gam(*((TRootPhoton*) branch->At(i))); jmass=0; ratioE = gam.EHoverEE;}
-        if(lhcoID == lhcoElectronID) { TRootElectron elec(*((TRootElectron*) branch->At(i))); ntrk = elec.Charge; ratioE = elec.EHoverEE;}
-        else if (lhcoID == lhcoMuonID) { TRootMuon muon(*((TRootMuon*) branch->At(i))); ntrk = muon.Charge;  ratioE = muon.EHoverEE;}
-	else if (lhcoID == lhcoTauJetID) { TRootTauJet taujet(*((TRootTauJet*) branch->At(i))); ntrk = taujet.Charge; }
-        else if (lhcoID == lhcoJetID) { TRootJet jet(*((TRootJet*) branch->At(i))); ntrk = jet.NTracks; btag = jet.Btag; ratioE = jet.EHoverEE;}
+	switch(lhcoID) {
+	  case lhcoPhotonID: {
+		TRootPhoton gam(*((TRootPhoton*) branch->At(i))); 
+		jmass=0; ratioE = gam.EHoverEE; 
+	  } break;
+          case lhcoElectronID: {
+		TRootElectron elec(*((TRootElectron*) branch->At(i))); 
+		ntrk = elec.Charge; ratioE = elec.EHoverEE; 
+	  } break;
+	  case lhcoTauJetID: {
+		TRootTauJet taujet(*((TRootTauJet*) branch->At(i))); 
+		ntrk = taujet.Charge;  ratioE = taujet.EHoverEE; 
+	  } break;
+	  case lhcoJetID: {
+		TRootJet jet(*((TRootJet*) branch->At(i))); 
+		ntrk = jet.NTracks; btag = jet.Btag; ratioE = jet.EHoverEE; 
+	  } break;
+	  case lhcoMuonID: {
+	   cout << "ERROR: LHCOConverter: for muons use LHCOConverter::BranchReaderMuon!\n";
+		TRootMuon muon(*((TRootMuon*) branch->At(i))); 
+		ntrk = muon.Charge;  ratioE = muon.EHoverEE; 
+	  } break;
+	  case lhcoETmisID: {
+	   cout << "ERROR: LHCOConverter: for ETmis use LHCOConverter::BranchReaderETmis!\n";
+		TRootMuon muon(*((TRootMuon*) branch->At(i))); 
+		ntrk = muon.Charge;  ratioE = muon.EHoverEE; 
+	  } break;
+
+	  default: break;
+	} // switch
+
 	if(lhcoID != lhcoETmisID) {
         outfile << fixed << setprecision(3)
@@ -200,19 +230,7 @@
 		<< setw(7) << 0.0 << setw(7) << 0.0  // dummy/dummy
                 << endl;
-	} else {
-        outfile << fixed << setprecision(3)
-		<< setw(3) << line++    // line counter
-                << setw(4) << lhcoID << " "   // object ID in lhco format
-                << setw(7) << 0.000 <<" "
-                << setw(7) << particle->Phi <<" "
-                << setw(7) << particle->PT  <<" "
-                << setw(7) << 0. <<" "       // invariant mass
-                << setw(7) << 0  <<" "       // number of tracks
-                << setw(7) << 0.  <<" "
-                << setw(7) << 0. <<" "      // E_had/E_em
-		<< setw(7) << 0.0 << setw(7) << 0.0  // dummy/dummy
-		<< endl;
 	}
-    }
+    } // branch parsing
+
     outfile.close();
     // return outfile;
@@ -232,6 +250,6 @@
             << setw(7) << particle->ET  <<" "
             << setw(7) << 0. <<" "       // invariant mass
-            << setw(7) << 0  <<" "       // number of tracks
-            << setw(7) << 0.  <<" "
+            << setw(7) << 0. <<" "       // number of tracks
+            << setw(7) << 0. <<" "
             << setw(7) << 0. <<" "      // E_had/E_em
             << setw(7) << 0.0 << setw(7) << 0.0  // dummy/dummy
@@ -243,2 +261,42 @@
 
 
+void LHCOConverter::BranchReaderMuon(const TClonesArray* branchMuon, const TClonesArray* branchJet, unsigned int& line) const {
+
+    ofstream outfile( outputfilename_.c_str(),ios_base::app);
+    unsigned int branch_size = branchMuon->GetEntries();
+    TRootMuon * muon = 0;
+    for (unsigned int i=0; i< branch_size; i++) {
+	muon = (TRootMuon*) branchMuon->At(i);
+
+        TLorentzVector pmu; pmu.SetPtEtaPhiE(muon->PT, muon->Eta, muon->Phi, muon->E);
+        float jmass = pmu.M(); if(jmass <0) jmass =0; // small negative values are possible due to smearing. < 0.1 GeV
+
+	// loop on jets, for muon isolation
+	unsigned int closest_jet_ID = 0;
+	float closest_jet_dR = 1E99; // initialisation value, should be high. No further impact
+	for (unsigned int j=0; j< (unsigned int) branchJet->GetEntries(); j++) {
+		TRootJet jet(*((TRootJet*)branchJet->At(j)));
+		float dr_ij = DeltaR(muon->Phi, muon->Eta, jet.Phi, jet.Eta);
+		if( dr_ij < closest_jet_dR ) {
+			closest_jet_ID = j;
+			closest_jet_dR = dr_ij;
+		}
+        } // loop on jets
+
+	// output
+	outfile << fixed << setprecision(3)
+                << setw(3) << line++    // line counter
+                << setw(4) << lhcoMuonID << " "   // object ID in lhco format
+                << setw(7) << muon->Eta <<" "
+                << setw(7) << muon->Phi <<" "
+                << setw(7) << muon->PT  <<" "
+                << setw(7) << jmass <<" "	// invariant mass
+                << setw(7) << (float)muon->Charge  <<" "	// -1 for mu-, +1 for mu+
+                << setw(7) << (float)closest_jet_ID << " "
+                << setw(7) << muon->EHoverEE <<" "	// E_had/E_em
+                << setw(7) << 0.0 << setw(7) << 0.0  // dummy/dummy
+                << endl;
+    } // muon branch loop
+
+    outfile.close();
+}
