musrfit 1.10.0
PFindRun.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PFindRun.cpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8***************************************************************************/
9
10/***************************************************************************
11 * Copyright (C) 2007-2026 by Andreas Suter *
12 * andreas.suter@psi.ch *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29
30#include <iostream>
31#include <boost/filesystem.hpp>
32
33#include "PFindRun.h"
34
35//----------------------------------------------------------------------------
40PFindRun::PFindRun(const PStringVector path, const PRunNameTemplateList runNameTemplateList) :
41 fPath(path), fRunNameTemplateList(runNameTemplateList)
42{
43 // nothing to be done here
44}
45
46//----------------------------------------------------------------------------
56PFindRun::PFindRun(const PStringVector path, const PRunNameTemplateList runNameTemplateList,
57 const TString &instrument, const UInt_t year, const UInt_t run,
58 const TString file_format) :
59 fPath(path), fRunNameTemplateList(runNameTemplateList), fInstrument(instrument),
60 fYear(year), fRun(run), fFileFormat(file_format)
61{
62 // nothing to be done here
63}
64
65//----------------------------------------------------------------------------
70{
71 std::cout << "debug> instrument: " << fInstrument << std::endl;
72 std::cout << "debug> year: " << fYear << std::endl;
73 std::cout << "debug> run: " << fRun << std::endl;
74 std::cout << "debug> ++++" << std::endl;
75 std::cout << "debug> run name template list:" << std::endl;
76 for (UInt_t i=0; i<fRunNameTemplateList.size(); i++) {
77 std::cout << i << ": instrument: " << fRunNameTemplateList[i].instrument << ", template: " << fRunNameTemplateList[i].runNameTemplate << std::endl;
78 }
79 std::cout << "debug> ++++" << std::endl;
80}
81
82//----------------------------------------------------------------------------
89TString PFindRun::CreatePathName(const TString path, const TString runNameTemplate)
90{
91 TString pathName{""};
92 TString runName = runNameTemplate;
93 TString yearStr = TString::Format("%d", fYear);
94 TString yyStr;
95 if (fYear < 2000)
96 yyStr = TString::Format("%02d", fYear-1900);
97 else
98 yyStr = TString::Format("%02d", fYear-2000);
99 runName.ReplaceAll("%yyyy%", yearStr);
100 runName.ReplaceAll("%yy%", yyStr);
101
102 // run handling slightly more complicated, since various number of digits possible
103 Int_t idx=-1;
104 TString rr{""};
105 for (Int_t i=2; i<10; i++) {
106 rr ="%";
107 for (Int_t j=0; j<i; j++)
108 rr += "r";
109 rr += "%";
110 if (runName.Index(rr) != -1) {
111 idx = i;
112 break;
113 }
114 }
115 TString format = TString::Format("%%0%dd", idx);
116 TString runStr = TString::Format(format, fRun);
117
118 if (idx != -1)
119 runName.ReplaceAll(rr, runStr);
120
121 pathName = path;
122 pathName += "/";
123 pathName += runName;
124
125 return pathName;
126}
127
128//----------------------------------------------------------------------------
134{
135 // if file format is given, get proper extension
136 TString ext("");
137 if (fFileFormat.Length() != 0) {
138 if (!fFileFormat.CompareTo("MusrRoot") || !fFileFormat.CompareTo("ROOT"))
139 ext = ".root";
140 else if (!fFileFormat.CompareTo("NeXus"))
141 ext = ".nxs";
142 else if (!fFileFormat.CompareTo("PSI-BIN"))
143 ext = ".bin";
144 else if (!fFileFormat.CompareTo("PSI-MDU"))
145 ext = ".mdu";
146 else if (!fFileFormat.CompareTo("MUD"))
147 ext = ".mud";
148 else if (!fFileFormat.CompareTo("WKM"))
149 ext = ".wkm";
150 }
151
152 // find instrument name in path list
153 TString pathName{""};
154 for (Int_t i=0; i<fPath.size(); i++) {
155 if (fPath[i].Index(fInstrument) != -1) {
156 for (Int_t j=0; j<fRunNameTemplateList.size(); j++) {
157 if (fRunNameTemplateList[j].instrument == fInstrument) {
158 pathName = CreatePathName(fPath[i], fRunNameTemplateList[j].runNameTemplate);
159 if (fFileFormat.Length() != 0) {
160 if (!pathName.Contains(ext, TString::kIgnoreCase))
161 continue;
162 }
163 if (boost::filesystem::exists(pathName.Data())) {
164 fPathName = pathName;
165 return true;
166 }
167 }
168 }
169 }
170 }
171 return false;
172}
std::vector< PRunNameTemplate > PRunNameTemplateList
Definition PMusr.h:1454
std::vector< TString > PStringVector
Definition PMusr.h:403
Int_t fYear
Run year (-1 if not specified)
Definition PFindRun.h:143
TString fPathName
Resolved full path (empty until found)
Definition PFindRun.h:146
TString fInstrument
Target instrument name (e.g., "GPS", "LEM")
Definition PFindRun.h:142
void DumpTemplateList()
Debug utility - Prints current search configuration to stdout.
Definition PFindRun.cpp:69
const PStringVector fPath
Search paths for data files.
Definition PFindRun.h:140
Int_t fRun
Run number (-1 if not specified)
Definition PFindRun.h:144
TString fFileFormat
Optional file format filter (empty = any)
Definition PFindRun.h:145
Bool_t FoundPathName()
Searches for the run file using configured templates and paths.
Definition PFindRun.cpp:133
const PRunNameTemplateList fRunNameTemplateList
Template patterns per instrument.
Definition PFindRun.h:141
TString CreatePathName(const TString path, const TString runNameTemplate)
Generates full file path by substituting template placeholders.
Definition PFindRun.cpp:89
PFindRun(const PStringVector path, const PRunNameTemplateList runNameTemplateList)
Default constructor - Creates instance without search parameters.
Definition PFindRun.cpp:40