Index: trunk/interface/LHCOConverter.h
===================================================================
--- trunk/interface/LHCOConverter.h	(revision 274)
+++ trunk/interface/LHCOConverter.h	(revision 275)
@@ -39,4 +39,8 @@
 using namespace std;
 
+class TClonesArray;
+
+enum {lhcoPhotonID=0,  lhcoElectronID, lhcoMuonID, lhcoTauJetID, lhcoJetID, lhcoETmisID=6};
+
 class LHCOConverter {
 public:
@@ -49,4 +53,5 @@
 
 private:
+  void BranchReader(const TClonesArray*, unsigned int&, unsigned short int ) const; // put it as a operator<<
   const string inputfilename_;
   string inputlogname_;
Index: trunk/src/LHCOConverter.cc
===================================================================
--- trunk/src/LHCOConverter.cc	(revision 274)
+++ trunk/src/LHCOConverter.cc	(revision 275)
@@ -30,9 +30,20 @@
 ***********************************************************************/
 
+// local includes
 #include "LHCOConverter.h"
+#include "ExRootTreeReader.h"
+#include "BlockClasses.h"
+
+// root includes
+#include "TFile.h"
+#include "TChain.h"
+#include "TClonesArray.h"
+
+// c++ includes
 #include <iostream>
 #include <string>
 #include <fstream>
 #include <sstream>
+#include <iomanip>
 using namespace std;
 
@@ -45,17 +56,25 @@
   if (inputfilename_ == "") {valid_ = false;}
   else { 
-    const unsigned int r_find = outputfilename_.rfind(".root");
-    if(r_find > outputfilename_.size()) {
+    TFile ftest(inputfilename_.c_str(),"READ");
+    if (!ftest.IsOpen()) { cout << "ERROR: file " << inputfilename_ << " could not be opened\n"; valid_=false; 
+    }
+    else {
+	ftest.Close();
+    	const unsigned int r_find = outputfilename_.rfind(".root");
+	if(r_find > outputfilename_.size()) {
 	   cout << "ERROR: the extension of the input file is not '.root'. Exiting...\n"; valid_=false; 
+    	}
+    	else {
+    		outputfilename_.replace(r_find,5,".lhco");
+	    	ofstream outfile( outputfilename_.c_str());
+    		outfile.close();
+	    	cout << "INFO: " << outputfilename_ << " has been created\n";
+	}
     }
-    outputfilename_.replace(r_find,5,".lhco");
-    ofstream outfile( outputfilename_.c_str());
-    outfile.close();
-    cout << "INFO: " << outputfilename_ << " has been created\n";
   }
 }
 
 void LHCOConverter::CopyRunLogFile() {
-  if(!valid_) {cout << "Nothing done\n"; return; }
+  if(!valid_) {/*cout << "Nothing done\n";*/ return; }
   if(inputlogname_ == "") {
 	// re-creates the logrun file name from the rootfile name
@@ -72,7 +91,7 @@
   string linereader;
   while ( getline(infile,linereader) ) {
-    outfile << "## " << linereader << endl;
+    outfile << "  # " << linereader << endl;
   }
-  outfile << "##" << endl;
+  outfile << "  #" << endl;
   infile.close();
   outfile.close();
@@ -80,6 +99,89 @@
 
 void LHCOConverter::ConvertExRootAnalysisToLHCO() {
-  if(!valid_) {cout << "Nothing done\n"; return; }
-  cout << "Creating " << outputfilename_ << endl;
+  if(!valid_) {/*cout << "Nothing done\n";*/ return; }
 
+  //output file
+  ofstream outfile( outputfilename_.c_str(),ios_base::app);
+  //#  typ      eta    phi      pt    jmas  ntrk  btag   had/em  dum1  dum2
+  outfile << " ## More info on LHCO files: http://cp3wks05.fynu.ucl.ac.be/Manual/lhco.html\n  #" << endl;
+  outfile << "  #  typ    eta    phi      pt    jmas  ntrk  btag   had/em  dum1  dum2" << endl;
+
+  // input files
+  TChain analysisChain("Analysis");
+  TChain triggerChain("Trigger");
+  analysisChain.Add(inputfilename_.c_str());
+  triggerChain.Add(inputfilename_.c_str());
+  ExRootTreeReader *analysisTree = new ExRootTreeReader(&analysisChain);
+  ExRootTreeReader *triggerTree  = new ExRootTreeReader(&triggerChain);
+
+  TClonesArray *branchPhoton = analysisTree->UseBranch("Photon");
+  TClonesArray *branchElectron = analysisTree->UseBranch("Electron");
+  TClonesArray *branchMuon = analysisTree->UseBranch("Muon");
+  TClonesArray *branchTauJet = analysisTree->UseBranch("TauJet");
+  TClonesArray *branchJet = analysisTree->UseBranch("Jet");
+  TClonesArray *branchETmis = analysisTree->UseBranch("ETmis");
+
+  Long64_t Nevents = analysisTree->GetEntries();
+  cout << "** TTree 'Analysis' contains " << Nevents << " events" << endl;
+  Nevents = min(Nevents,triggerTree->GetEntries());
+  if (Nevents != analysisTree->GetEntries()) 
+     cout << "** WARNING: not the same number of entries in 'Analysis' and      **\n**          'Trigger' trees\n";
+  for(Long64_t event = 0; event < Nevents; ++event) {
+    analysisTree->ReadEntry(event);
+    triggerTree->ReadEntry(event);
+ //TRIGT->GetGlobalResult(branchElecTrig, branchMuonTrig,branchJetTrig, branchTauJetTrig,branchPhotonTrig, branchETmisTrig,branchTrigger);
+    unsigned int triginfo = 0;
+    unsigned int line =0;
+    outfile << setw(3) << line++  << setw(4) << " " << setw(7) << event << setw(7) << triginfo << endl;
+
+    // 0 photon data
+    BranchReader(branchPhoton,line,lhcoPhotonID);
+
+    // 1 electron/positron data
+    BranchReader(branchElectron,line,lhcoElectronID);
+
+    // 2 muon data
+    BranchReader(branchMuon,line,lhcoMuonID);
+
+    // 3 tau-jets
+    BranchReader(branchTauJet,line,lhcoTauJetID);
+
+    // 4 jets
+    BranchReader(branchJet,line,lhcoJetID);
+
+    // 6 MET
+    BranchReader(branchETmis,line,lhcoETmisID);
+
+  } // event loop
+
+  outfile.close();
+  delete triggerTree;
+  delete analysisTree;
 }
+
+
+void LHCOConverter::BranchReader(const TClonesArray* branch, unsigned int& line, unsigned short int lhcoID) const {
+  ofstream outfile( outputfilename_.c_str(),ios_base::app);
+    
+    unsigned int branch_size = branch->GetEntries();
+    TRootParticle * particle = 0;
+    for (unsigned int i=0; i< branch_size; i++) {
+	double jmass =0;
+	unsigned int ntrk = 0;
+	unsigned short int btag =0;
+	double ratioE = 0;
+        particle = (TRootParticle*) branch->At(i);
+        outfile << setw(3) << line++    // line counter
+                << setw(4) << lhcoID    // object ID in lhco format
+                << setw(7) << particle->Eta
+//                << setw(7) << fixed(3) << particle->Eta
+                << setw(7) << particle->Phi
+                << setw(7) << particle->PT
+		<< setw(7) << jmass 	// invariant mass
+		<< setw(7) << ntrk	// number of tracks
+		<< setw(7) << btag	
+		<< setw(7) << ratioE	// E_had/E_em
+                << endl;
+    }
+  outfile.close();
+}
