Fork me on GitHub

Changeset 275 in svn for trunk/src


Ignore:
Timestamp:
Feb 16, 2009, 1:17:12 PM (16 years ago)
Author:
Xavier Rouby
Message:

more things; not yet finished

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/LHCOConverter.cc

    r271 r275  
    3030***********************************************************************/
    3131
     32// local includes
    3233#include "LHCOConverter.h"
     34#include "ExRootTreeReader.h"
     35#include "BlockClasses.h"
     36
     37// root includes
     38#include "TFile.h"
     39#include "TChain.h"
     40#include "TClonesArray.h"
     41
     42// c++ includes
    3343#include <iostream>
    3444#include <string>
    3545#include <fstream>
    3646#include <sstream>
     47#include <iomanip>
    3748using namespace std;
    3849
     
    4556  if (inputfilename_ == "") {valid_ = false;}
    4657  else {
    47     const unsigned int r_find = outputfilename_.rfind(".root");
    48     if(r_find > outputfilename_.size()) {
     58    TFile ftest(inputfilename_.c_str(),"READ");
     59    if (!ftest.IsOpen()) { cout << "ERROR: file " << inputfilename_ << " could not be opened\n"; valid_=false;
     60    }
     61    else {
     62        ftest.Close();
     63        const unsigned int r_find = outputfilename_.rfind(".root");
     64        if(r_find > outputfilename_.size()) {
    4965           cout << "ERROR: the extension of the input file is not '.root'. Exiting...\n"; valid_=false;
     66        }
     67        else {
     68                outputfilename_.replace(r_find,5,".lhco");
     69                ofstream outfile( outputfilename_.c_str());
     70                outfile.close();
     71                cout << "INFO: " << outputfilename_ << " has been created\n";
     72        }
    5073    }
    51     outputfilename_.replace(r_find,5,".lhco");
    52     ofstream outfile( outputfilename_.c_str());
    53     outfile.close();
    54     cout << "INFO: " << outputfilename_ << " has been created\n";
    5574  }
    5675}
    5776
    5877void LHCOConverter::CopyRunLogFile() {
    59   if(!valid_) {cout << "Nothing done\n"; return; }
     78  if(!valid_) {/*cout << "Nothing done\n";*/ return; }
    6079  if(inputlogname_ == "") {
    6180        // re-creates the logrun file name from the rootfile name
     
    7291  string linereader;
    7392  while ( getline(infile,linereader) ) {
    74     outfile << "## " << linereader << endl;
     93    outfile << "  # " << linereader << endl;
    7594  }
    76   outfile << "##" << endl;
     95  outfile << "  #" << endl;
    7796  infile.close();
    7897  outfile.close();
     
    8099
    81100void LHCOConverter::ConvertExRootAnalysisToLHCO() {
    82   if(!valid_) {cout << "Nothing done\n"; return; }
    83   cout << "Creating " << outputfilename_ << endl;
     101  if(!valid_) {/*cout << "Nothing done\n";*/ return; }
    84102
     103  //output file
     104  ofstream outfile( outputfilename_.c_str(),ios_base::app);
     105  //#  typ      eta    phi      pt    jmas  ntrk  btag   had/em  dum1  dum2
     106  outfile << " ## More info on LHCO files: http://cp3wks05.fynu.ucl.ac.be/Manual/lhco.html\n  #" << endl;
     107  outfile << "  #  typ    eta    phi      pt    jmas  ntrk  btag   had/em  dum1  dum2" << endl;
     108
     109  // input files
     110  TChain analysisChain("Analysis");
     111  TChain triggerChain("Trigger");
     112  analysisChain.Add(inputfilename_.c_str());
     113  triggerChain.Add(inputfilename_.c_str());
     114  ExRootTreeReader *analysisTree = new ExRootTreeReader(&analysisChain);
     115  ExRootTreeReader *triggerTree  = new ExRootTreeReader(&triggerChain);
     116
     117  TClonesArray *branchPhoton = analysisTree->UseBranch("Photon");
     118  TClonesArray *branchElectron = analysisTree->UseBranch("Electron");
     119  TClonesArray *branchMuon = analysisTree->UseBranch("Muon");
     120  TClonesArray *branchTauJet = analysisTree->UseBranch("TauJet");
     121  TClonesArray *branchJet = analysisTree->UseBranch("Jet");
     122  TClonesArray *branchETmis = analysisTree->UseBranch("ETmis");
     123
     124  Long64_t Nevents = analysisTree->GetEntries();
     125  cout << "** TTree 'Analysis' contains " << Nevents << " events" << endl;
     126  Nevents = min(Nevents,triggerTree->GetEntries());
     127  if (Nevents != analysisTree->GetEntries())
     128     cout << "** WARNING: not the same number of entries in 'Analysis' and      **\n**          'Trigger' trees\n";
     129  for(Long64_t event = 0; event < Nevents; ++event) {
     130    analysisTree->ReadEntry(event);
     131    triggerTree->ReadEntry(event);
     132 //TRIGT->GetGlobalResult(branchElecTrig, branchMuonTrig,branchJetTrig, branchTauJetTrig,branchPhotonTrig, branchETmisTrig,branchTrigger);
     133    unsigned int triginfo = 0;
     134    unsigned int line =0;
     135    outfile << setw(3) << line++  << setw(4) << " " << setw(7) << event << setw(7) << triginfo << endl;
     136
     137    // 0 photon data
     138    BranchReader(branchPhoton,line,lhcoPhotonID);
     139
     140    // 1 electron/positron data
     141    BranchReader(branchElectron,line,lhcoElectronID);
     142
     143    // 2 muon data
     144    BranchReader(branchMuon,line,lhcoMuonID);
     145
     146    // 3 tau-jets
     147    BranchReader(branchTauJet,line,lhcoTauJetID);
     148
     149    // 4 jets
     150    BranchReader(branchJet,line,lhcoJetID);
     151
     152    // 6 MET
     153    BranchReader(branchETmis,line,lhcoETmisID);
     154
     155  } // event loop
     156
     157  outfile.close();
     158  delete triggerTree;
     159  delete analysisTree;
    85160}
     161
     162
     163void LHCOConverter::BranchReader(const TClonesArray* branch, unsigned int& line, unsigned short int lhcoID) const {
     164  ofstream outfile( outputfilename_.c_str(),ios_base::app);
     165   
     166    unsigned int branch_size = branch->GetEntries();
     167    TRootParticle * particle = 0;
     168    for (unsigned int i=0; i< branch_size; i++) {
     169        double jmass =0;
     170        unsigned int ntrk = 0;
     171        unsigned short int btag =0;
     172        double ratioE = 0;
     173        particle = (TRootParticle*) branch->At(i);
     174        outfile << setw(3) << line++    // line counter
     175                << setw(4) << lhcoID    // object ID in lhco format
     176                << setw(7) << particle->Eta
     177//                << setw(7) << fixed(3) << particle->Eta
     178                << setw(7) << particle->Phi
     179                << setw(7) << particle->PT
     180                << setw(7) << jmass     // invariant mass
     181                << setw(7) << ntrk      // number of tracks
     182                << setw(7) << btag     
     183                << setw(7) << ratioE    // E_had/E_em
     184                << endl;
     185    }
     186  outfile.close();
     187}
Note: See TracChangeset for help on using the changeset viewer.