Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:30:22

0001 #include "SimMuon/Neutron/src/AsciiNeutronReader.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 #include "FWCore/Utilities/interface/EDMException.h"
0004 #include "FWCore/ParameterSet/interface/FileInPath.h"
0005 #include <sstream>
0006 #include <iostream>
0007 
0008 using namespace std;
0009 
0010 AsciiNeutronReader::AsciiNeutronReader(string fileNameBase)
0011     : theFileNameBase(fileNameBase),
0012       //theStreamPos(nChamberTypes+1, 0) //TODO WON"T WORK!  replace with a map
0013       theStreamPos(11, 0) {}
0014 
0015 void AsciiNeutronReader::readNextEvent(int chamberType, edm::PSimHitContainer& result) {
0016   stringstream fileName;
0017   fileName << theFileNameBase << chamberType;
0018   ifstream fin(fileName.str().c_str(), ios::in);
0019   if (!fin.is_open()) {
0020     throw cms::Exception("NeutronReader") << "Muon neutron noise file missing " << fileName.str();
0021   }
0022 
0023   int nhits = read_nhits(fin, chamberType);
0024   for (int ihit = 0; ihit < nhits; ++ihit) {
0025     float entryX, entryY, entryZ, exitX, exitY, exitZ;
0026     float p, tof, eloss, theta, phi;
0027     int type, layer, track;
0028 
0029     fin >> entryX >> entryY >> entryZ >> exitX >> exitY >> exitZ >> p >> tof >> eloss >> type >> layer >> track >>
0030         theta >> phi;
0031     LocalPoint entry(entryX, entryY, entryZ);
0032     LocalPoint exit(exitX, exitY, exitZ);
0033     PSimHit phit(entry, exit, p, tof, eloss, type, layer, track, theta, phi);
0034     result.push_back(phit);
0035   }
0036   theStreamPos[chamberType] = fin.tellg();
0037 }
0038 
0039 int AsciiNeutronReader::read_nhits(ifstream& fin, int chamberType) {
0040   int nhits = 0;
0041   // go to the last saved place
0042   fin.seekg(theStreamPos[chamberType]);
0043   if (fin.eof()) {
0044     resetStreampos(fin, chamberType);
0045   }
0046   LogDebug("NeutronReader") << "starting from pos " << theStreamPos[chamberType] << " EOF " << fin.eof();
0047   fin >> nhits;
0048   if (fin.eof()) {
0049     resetStreampos(fin, chamberType);
0050     fin >> nhits;
0051   }
0052   LogDebug("NeutronReader") << "returning nhits " << nhits;
0053   return nhits;
0054 }
0055 
0056 void AsciiNeutronReader::resetStreampos(ifstream& fin, int chamberType) {
0057   LogDebug("NeutronReader") << "reached EOF, resetting streampos ";
0058   theStreamPos[chamberType] = 0;
0059   fin.clear();
0060   fin.seekg(0, ios::beg);
0061 }