CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/SimMuon/Neutron/src/AsciiNeutronReader.cc

Go to the documentation of this file.
00001 #include "SimMuon/Neutron/src/AsciiNeutronReader.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 #include "FWCore/Utilities/interface/EDMException.h"
00004 #include "FWCore/ParameterSet/interface/FileInPath.h"
00005 #include <sstream>
00006 #include<iostream>
00007 
00008 using namespace std;
00009 
00010 AsciiNeutronReader::AsciiNeutronReader(string fileNameBase) :
00011   theFileNameBase(fileNameBase),
00012   //theStreamPos(nChamberTypes+1, 0) //TODO WON"T WORK!  replace with a map 
00013   theStreamPos(11,0)
00014 {
00015 }
00016 
00017 
00018 void AsciiNeutronReader::readNextEvent(int chamberType, edm::PSimHitContainer & result) {
00019   stringstream fileName;
00020   fileName << theFileNameBase << chamberType;
00021   ifstream fin(fileName.str().c_str(), ios::in);
00022   if(!fin.is_open()) {
00023     throw cms::Exception("NeutronReader") 
00024       << "Muon neutron noise file missing " << fileName.str();
00025   }
00026 
00027   int nhits = read_nhits(fin, chamberType);
00028   for(int ihit = 0; ihit < nhits; ++ihit) {
00029     float entryX, entryY, entryZ, exitX, exitY, exitZ;
00030     float p, tof, eloss, theta, phi;
00031     int type, layer, track;
00032 
00033     fin >> entryX >> entryY >> entryZ
00034         >> exitX  >> exitY  >> exitZ
00035         >> p >> tof >> eloss >> type >> layer >> track >> theta >> phi;
00036     LocalPoint entry(entryX, entryY, entryZ);
00037     LocalPoint exit (exitX,  exitY,  exitZ);
00038     PSimHit phit(entry, exit, p, tof, eloss, type, layer, track, theta, phi);
00039     result.push_back(phit);
00040   }
00041   theStreamPos[chamberType] = fin.tellg();
00042 }
00043 
00044 
00045 int AsciiNeutronReader::read_nhits(ifstream & fin, int chamberType) {
00046   int nhits = 0;
00047     // go to the last saved place
00048   fin.seekg(theStreamPos[chamberType]);
00049   if(fin.eof()) {
00050     resetStreampos(fin, chamberType);
00051   }
00052   LogDebug("NeutronReader") << "starting from pos " << theStreamPos[chamberType] 
00053                   << " EOF " << fin.eof();
00054   fin >> nhits;
00055   if(fin.eof()) {
00056     resetStreampos(fin, chamberType);
00057     fin >> nhits;
00058   }
00059   LogDebug("NeutronReader") << "returning nhits " << nhits;
00060   return nhits;
00061 }
00062 
00063 
00064 void AsciiNeutronReader::resetStreampos(ifstream & fin, int chamberType) {
00065   LogDebug("NeutronReader") << "reached EOF, resetting streampos ";
00066   theStreamPos[chamberType] = 0;
00067   fin.clear();
00068   fin.seekg(0, ios::beg);
00069 }
00070