File indexing completed on 2024-11-16 09:02:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "eicsmear/erhic/Forester.h"
0011
0012 #include <iomanip>
0013 #include <memory>
0014 #include <stdexcept>
0015 #include <string>
0016
0017 #include <TRefArray.h>
0018 #include <TString.h>
0019
0020 #include "eicsmear/erhic/EventFactory.h"
0021 #include "eicsmear/erhic/File.h"
0022 #include "eicsmear/erhic/ParticleIdentifier.h"
0023
0024 #include "eicsmear/gzstream.h"
0025
0026 namespace erhic {
0027
0028 Forester::Forester()
0029 : mQuit(false)
0030 , mVerbose(false)
0031 , mTree(NULL)
0032 , mEvent(NULL)
0033 , mFile(NULL)
0034 , mRootFile(NULL)
0035 , mMaxNEvents(0)
0036 , mInterval(1)
0037 , mTextFile(NULL)
0038 , mInputName("default.txt")
0039 , mOutputName("default.root")
0040 , mTreeName("EICTree")
0041 , mBranchName("event")
0042 , mFactory(NULL) {
0043 }
0044
0045 Forester::~Forester() {
0046 if (mFile) {
0047 delete mFile;
0048 mFile = NULL;
0049 }
0050 if (mEvent) {
0051 delete mEvent;
0052 mEvent = NULL;
0053 }
0054 if (mFactory) {
0055 delete mFactory;
0056 mFactory = NULL;
0057 }
0058 if (mRootFile) {
0059 delete mRootFile;
0060 mRootFile = NULL;
0061 }
0062
0063
0064
0065
0066
0067
0068 }
0069
0070 Long64_t Forester::Plant() {
0071 try {
0072
0073 OpenInput();
0074 SetupOutput();
0075 if (BeVerbose()) {
0076 std::cout << "\nProcessing " << GetInputFileName() << std::endl;
0077 }
0078
0079
0080
0081
0082 static int i(0);
0083 while (!MustQuit()) {
0084 ++i;
0085 if (BeVerbose() && i % mInterval == 0) {
0086
0087
0088 int width = static_cast<int>(::log10(GetMaxNEvents()) + 1);
0089 std::cout << "Processing event "<< std::setw(width) << i;
0090 if (GetMaxNEvents() > 0) {
0091 std::cout << "/" << std::setw(width) << GetMaxNEvents();
0092 }
0093 std::cout << std::endl;
0094 }
0095
0096 if (mEvent) {
0097 delete mEvent;
0098 mEvent = NULL;
0099 }
0100
0101
0102 try {
0103 mEvent = mFactory->Create();
0104
0105 if (mEvent) {
0106 mTree->Fill();
0107 if (GetMaxNEvents() > 0 && i >= GetMaxNEvents()) {
0108 SetMustQuit(true);
0109 }
0110 mStatus.ModifyEventCount(1);
0111 mStatus.ModifyParticleCount(mEvent->GetNTracks());
0112
0113 } else {
0114 break;
0115 }
0116 }
0117 catch(std::exception& e) {
0118 std::cerr << "Caught exception in Forester::Plant(): "
0119 << e.what() << std::endl;
0120 std::cerr << "Event will be skipped..." << std::endl;
0121 }
0122 }
0123 Finish();
0124 return 0;
0125 }
0126 catch(std::exception& e) {
0127 std::cerr << "Caught exception in Forester::Plant(): "
0128 << e.what() << std::endl;
0129 return -1;
0130 }
0131 }
0132
0133 bool Forester::OpenInput() {
0134 try {
0135
0136 if ( TString(GetInputFileName()).EndsWith("gz", TString::kIgnoreCase) ||
0137 TString(GetInputFileName()).EndsWith("zip", TString::kIgnoreCase)){
0138 auto tmp = std::make_shared<igzstream>();
0139 tmp->open(GetInputFileName().c_str());
0140
0141 if (!tmp->good()) {
0142 std::string message("Unable to open file ");
0143 throw std::runtime_error(message.append(GetInputFileName()));
0144 }
0145 mTextFile = tmp;
0146 } else {
0147 auto tmp = std::make_shared<std::ifstream>();
0148 tmp->open(GetInputFileName().c_str());
0149
0150 if (!tmp->good()) {
0151 std::string message("Unable to open file ");
0152 throw std::runtime_error(message.append(GetInputFileName()));
0153 }
0154 mTextFile = tmp;
0155 }
0156
0157
0158
0159
0160 mFile = erhic::FileFactory::GetInstance().GetFile(mTextFile, GetInputFileName());
0161 if (!mFile) {
0162 throw std::runtime_error(GetInputFileName() +
0163 " is not from a supported generator");
0164 }
0165 mFactory = mFile->CreateEventFactory(*mTextFile);
0166 mFactory->mAdditionalInformation["generator"]=mFile->GetGeneratorName();
0167 return true;
0168 }
0169 catch(std::exception&) {
0170 throw;
0171 }
0172 }
0173
0174 bool Forester::SetupOutput() {
0175 try {
0176
0177 mRootFile = new TFile(GetOutputFileName().c_str(), "RECREATE");
0178 if (!mRootFile->IsOpen()) {
0179 std::string message("Unable to open file ");
0180 throw std::runtime_error(message.append(GetOutputFileName()));
0181 }
0182
0183 mTree = new TTree(GetTreeName().c_str(), "my EIC tree");
0184 if (!mTree) {
0185 std::string message("Error allocating TTree ");
0186 throw std::runtime_error(message.append(GetTreeName()));
0187 }
0188
0189
0190 AllocateEvent();
0191 mTree->Branch(GetBranchName().c_str(), mEvent->ClassName(),
0192 &mEvent, 32000, 99);
0193
0194 mTree->SetAutoSave(500LL * 1024LL * 1024LL);
0195
0196 mFactory->FindFirstEvent();
0197
0198
0199 mStatus.StartTimer();
0200 return true;
0201 }
0202 catch(std::exception&) {
0203 throw;
0204 }
0205 }
0206
0207 void Forester::Finish() {
0208 if (BeVerbose()) {
0209 std::cout << "\nProcessed " << GetInputFileName() << std::endl;
0210 }
0211
0212 mRootFile = mTree->GetCurrentFile();
0213 mRootFile->cd();
0214 mTree->Write();
0215 for ( auto namedobject : mFactory->mObjectsToWriteAtTheEnd ){
0216 namedobject.second->Write(namedobject.first);
0217 }
0218 mRootFile->ls();
0219
0220
0221 Write("forester");
0222
0223 SetMustQuit(false);
0224
0225 mStatus.StopTimer();
0226 if (BeVerbose()) {
0227 GetGetStatus().Print(std::cout);
0228 }
0229 mRootFile->Close();
0230 }
0231
0232 bool Forester::AllocateEvent() {
0233 try {
0234 if (mEvent) {
0235 delete mEvent;
0236 mEvent = NULL;
0237 }
0238 mEvent = mFile->AllocateEvent();
0239 return mEvent;
0240 }
0241
0242 catch(std::exception&) {
0243 throw;
0244 }
0245 }
0246
0247 bool Forester::FindFirstEvent() {
0248
0249
0250
0251
0252 std::getline(*mTextFile, mLine);
0253 std::getline(*mTextFile, mLine);
0254 std::getline(*mTextFile, mLine);
0255 std::getline(*mTextFile, mLine);
0256 std::getline(*mTextFile, mLine);
0257 return true;
0258 }
0259
0260 void Forester::Print(std::ostream& os) const {
0261 os << "Input file: " << mInputName << std::endl;
0262 os << "Output file: " << mOutputName << std::endl;
0263 os << "Output tree: " << mTreeName << std::endl;
0264 os << "Output branch: " << mBranchName << std::endl;
0265 os << "Maximum number of events: " << mMaxNEvents << std::endl;
0266 if (mEvent) {
0267 os << "Event type: " << mEvent->ClassName() << std::endl;
0268 }
0269 }
0270
0271 void Forester::Print(Option_t* ) const {
0272 Print(std::cout);
0273 }
0274
0275 Forester::Status::Status()
0276 : mNEvents(0)
0277 , mNParticles(0) {
0278
0279
0280 std::time(&mStartTime);
0281 mEndTime = mStartTime;
0282 mTimer.Reset();
0283 }
0284
0285 Forester::Status::~Status() { }
0286
0287 std::ostream& Forester::Status::Print(std::ostream& os) const {
0288
0289
0290 os << "Began on " << std::ctime(&mStartTime);
0291 os << "Ended on " << std::ctime(&mEndTime);
0292 os << "Processed " << mNEvents << " events containing "
0293 << mNParticles << " particles in "
0294 << mTimer.RealTime() << " seconds "
0295 << '(' << mTimer.RealTime()/mNEvents <<" sec/event)" << std::endl;
0296 return os;
0297 }
0298
0299 void Forester::Status::StartTimer() {
0300 std::time(&mStartTime);
0301 mTimer.Start();
0302 }
0303
0304 void Forester::Status::StopTimer() {
0305 std::time(&mEndTime);
0306 mTimer.Stop();
0307 }
0308
0309 void Forester::Status::ModifyEventCount(Long64_t count) {
0310 mNEvents += count;
0311 }
0312
0313 void Forester::Status::ModifyParticleCount(Long64_t count) {
0314 mNParticles += count;
0315 }
0316
0317
0318
0319
0320
0321 }