00001 #include "NCPOTCounter.h"
00002
00003 #include "NCUtils/NCType.h"
00004 using namespace NCType;
00005
00006 #include "MessageService/MsgService.h"
00007
00008 CVSID("$Id: NCPOTCounter.cxx,v 1.33 2009/10/30 17:52:04 rodriges Exp $");
00009
00010 #include "TFile.h"
00011 #include "TH2F.h"
00012 #include "TList.h"
00013 #include "TRegexp.h"
00014 #include "TString.h"
00015 #include "TSystemDirectory.h"
00016 #include "TSystem.h"
00017
00018 #include <cassert>
00019
00020 using std::map;
00021 using std::vector;
00022
00023
00024 NCPOTCounter::Cache NCPOTCounter::fgCache;
00025
00026 NCPOTCounter::NCPOTCounter()
00027 {
00028 }
00029
00030
00031 void NCPOTCounter::Config(const Registry& r,
00032 vector<BeamType::BeamType_t> beamIndex,
00033 vector<NC::RunUtil::ERunType> runsToUse)
00034 {
00035 const char* tmps;
00036 int tmpb, tmpi;
00037
00038
00039 if(r.Get("DataMCPath", tmps)){
00040
00041
00042 TString temp = tmps;
00043
00044 if(temp.Contains("$")){
00045 int pos = temp.Index("/");
00046 temp.Resize(pos);
00047 temp.Remove(TString::kLeading, '$');
00048 fDataMCPath = tmps;
00049
00050 fDataMCPath.Replace(0, pos, gSystem->Getenv(temp));
00051 }
00052 else fDataMCPath = temp;
00053
00054 if(!fDataMCPath.EndsWith("/")) fDataMCPath += "/";
00055 }
00056
00057 if(r.Get("MockDataSet", tmps)) fMockDataSet = tmps;
00058 if(r.Get("UseMCAsData", tmpb)) fMCAsData = tmpb;
00059 if(r.Get("UseMockData", tmpb)) fMockData = tmpb;
00060
00061 if(r.Get("RunLimitNearData", tmpi))
00062 fRunLimits[Detector::kNear][NCType::kData] = tmpi;
00063 if(r.Get("RunLimitFarData", tmpi))
00064 fRunLimits[Detector::kFar][NCType::kData] = tmpi;
00065 if(r.Get("RunLimitNearMC", tmpi))
00066 fRunLimits[Detector::kNear][NCType::kMC] = tmpi;
00067 if(r.Get("RunLimitFarMC", tmpi))
00068 fRunLimits[Detector::kFar][NCType::kMC] = tmpi;
00069
00070 if(r.Get("MockDataSubRun", tmpi)) fMockDataSubRun = tmpi;
00071
00072 fBeamIndex = beamIndex;
00073 fRunsToUse = runsToUse;
00074 }
00075
00076
00077 void NCPOTCounter::SetPOTValues(map<NCBeam::Info, double>& pot,
00078 Detector::Detector_t det,
00079 NCType::EFileType fileType,
00080 NCType::EDataMC dataMC)
00081 {
00082 POTKey key(3);
00083 key[0] = det;
00084 key[1] = fileType;
00085 key[2] = dataMC;
00086
00087 Cache::iterator it = fgCache.find(key);
00088
00089 if(it != fgCache.end()){
00090 pot = it->second;
00091 return;
00092 }
00093
00094
00095 pot.clear();
00096
00097 MSG("NCPOTCounter", Msg::kInfo)
00098 << "NCPOTCounter::SetPOTValues:"
00099 << " fDataMCPath = " << fDataMCPath << endl;
00100
00101
00102
00103
00104
00105 map<NCBeam::Info, double> POTtmp;
00106
00107 for(int r = NC::RunUtil::kRunI;
00108 r <= NC::RunUtil::kMaxRun; ++r){
00109 for(unsigned int j = 0; j < fBeamIndex.size(); ++j){
00110 NCBeam::Info info(fBeamIndex[j], NC::RunUtil::ERunType(r));
00111 POTtmp[info] = 0.;
00112 }
00113 }
00114
00115
00116 for(unsigned int bt = 0; bt < fBeamIndex.size(); ++bt){
00117 for(unsigned int runIdx = 0; runIdx<fRunsToUse.size(); ++runIdx){
00118 NCBeam::Info info(fBeamIndex[bt], fRunsToUse[runIdx]);
00119
00120
00121
00122 int nRunsRead = 0;
00123
00124 const vector<TString> fileList = GetListOfFiles(info, fileType, det, dataMC);
00125
00126 for(unsigned int nf = 0; nf < fileList.size(); ++nf){
00127 const TString fileName = fileList[nf];
00128 const double potToAdd = GetPOTForSingleFile(fileName, fileType,
00129 det, dataMC, bt, nRunsRead);
00130 assert(potToAdd >= 0);
00131
00132
00133
00134 assert(POTtmp.find(info)!=POTtmp.end());
00135 POTtmp[info] += potToAdd;
00136
00137 MSG("NCPOTCounter", Msg::kInfo) << " Adding " << potToAdd << " POT to "
00138 << FileDesignator(fileType, det, dataMC)
00139 << endl;
00140
00141 nRunsRead += GetRunsInFile(fileName);
00142
00143 if(nRunsRead > fRunLimits[det][dataMC]) break;
00144
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 if(POTtmp[info] > 0){
00162 if(dataMC == NCType::kData && fMCAsData){
00163 assert(fFarMCExposures.find(info) != fFarMCExposures.end() &&
00164 "Did you call set_mc_exposure_for_beam for this beam?");
00165 pot[info] = fFarMCExposures[info];
00166 }
00167 else{
00168 pot[info] = POTtmp[info];
00169 }
00170 }
00171
00172 }
00173
00174
00175
00176
00177 double totalPOT=0;
00178
00179 for(unsigned int n = 0; n < fRunsToUse.size(); ++n){
00180 NCBeam::Info info(fBeamIndex[bt], fRunsToUse[n]);
00181 totalPOT+=pot[info];
00182 }
00183
00184 const NCBeam::Info infoAll(fBeamIndex[bt], NC::RunUtil::kRunAll);
00185 pot[infoAll] = totalPOT;
00186
00187 }
00188
00189 for(map<NCBeam::Info, double>::const_iterator it = pot.begin();
00190 it != pot.end(); ++it){
00191 MSG("NCPOTCounter", Msg::kInfo) << "POT INFO: "
00192 << (det == Detector::kFar ? "Far" : "Near")
00193 << " "
00194 << (dataMC == NCType::kMC ? "MC" : "data")
00195 << " from \""
00196 << FileDesignator(fileType, det, dataMC)
00197 << "\" files " << it->first << " has "
00198 << it->second << " POT" << endl;
00199 }
00200
00201 fgCache[key] = pot;
00202 }
00203
00204
00205 double NCPOTCounter::GetPOTValue(Detector::Detector_t det,
00206 NCType::EFileType fileType,
00207 NCType::EDataMC dataMC,
00208 BeamType::BeamType_t beamType,
00209 NC::RunUtil::ERunType run)
00210 {
00211 map<NCBeam::Info, double> pot;
00212
00213 SetPOTValues(pot, det, fileType, dataMC);
00214
00215 const NCBeam::Info beamInfo(beamType, run);
00216
00217 return pot[beamInfo];
00218 }
00219
00220
00221 vector<TString> NCPOTCounter::GetListOfFiles(NCBeam::Info info,
00222 NCType::EFileType fileType,
00223 Detector::Detector_t det,
00224 NCType::EDataMC dataMC) const
00225 {
00226
00227
00228 TDirectory* saveDir = gDirectory;
00229
00230 vector<TString> ret;
00231
00232 const TString fileDesignator = FileDesignator(fileType, det, dataMC);
00233 const TString beamType = BeamType::AsString(info.GetBeamType());
00234 const TString runPeriod= Form("run%d", (int)info.GetRunType());
00235 const TString detName = (det == Detector::kFar) ? "far" : "near";
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 TSystemDirectory dataDir(beamType, fDataMCPath+beamType+"/");
00246 TList* files = dataDir.GetListOfFiles();
00247 assert(files);
00248
00249 for(int de = 0; de < files->GetEntries(); ++de){
00250 TString fileName = files->At(de)->GetName();
00251
00252
00253 if(fileName == "." || fileName == "..") continue;
00254
00255
00256 if(fileName.Contains(beamType) &&
00257 fileName.Contains("strip") &&
00258 fileName.Contains(detName) &&
00259 fileName.Contains(fileDesignator)){
00260
00261
00262 const NC::RunUtil::ERunType fileRunPeriod = NC::RunUtil::FindRunType(fileName);
00263 if(fileRunPeriod != info.GetRunType()) continue;
00264 if(fileType == NCType::kMockFile && fMockDataSubRun != -1){
00265
00266 int subRun = MockFileSubRun(fileName);
00267
00268
00269
00270
00271
00272 if(subRun != -1 && subRun != fMockDataSubRun) continue;
00273 }
00274 ret.push_back(fileName);
00275 }
00276 }
00277
00278 delete files;
00279
00280 saveDir->cd();
00281
00282 return ret;
00283 }
00284
00285 int NCPOTCounter::GetRunsInFile(TString fileName)
00286 {
00287 TFile tf(fileName);
00288 assert(!tf.IsZombie());
00289 TH1F* potHist = (TH1F*)(tf.Get("mcPOT"));
00290 if(!potHist) potHist=(TH1F*)(tf.Get("dataPOT"));
00291 assert(potHist);
00292
00293 return int(potHist->GetEntries());
00294 }
00295
00296
00297 void NCPOTCounter::SetMCExposureForBeam(NCBeam::Info beam, double pot)
00298 {
00299 fFarMCExposures[beam] = pot;
00300 }
00301
00302
00303 double NCPOTCounter::GetPOTForSingleFile(TString fileName,
00304 NCType::EFileType fileType,
00305 Detector::Detector_t det,
00306 NCType::EDataMC dataMC,
00307 int bt,
00308 int nRunsOffset) const
00309 {
00310
00311
00312 TFile tf(fileName);
00313 assert(!tf.IsZombie());
00314 const TString histName = HistName(fileType, dataMC);
00315 const TString fileDesignator = FileDesignator(fileType, det, dataMC);
00316
00317 TH1F* potHist = (TH1F*)(tf.Get(histName));
00318
00319 if(fileType==NCType::kMockFile) {
00320
00321
00322
00323 if (MockFileSubRun(fileName) != -1)
00324 return potHist->Integral(0, potHist->GetNbinsX()+1)*1e12;
00325 else return POTPerRun(bt, det, fileType);
00326 }
00327
00328 MSG("NCPOTCounter", Msg::kInfo) << " Running on "
00329 << fileName << " with "
00330 << potHist->GetEntries()
00331 << " runs" << endl;
00332
00333
00334
00335 if(fileDesignator == "data" || fileDesignator == "blind"){
00336 return potHist->Integral(0, potHist->GetNbinsX()+1)*1e12;
00337 }
00338
00339 const double potPerRun = POTPerRun(bt, det, fileType);
00340
00341 const int runLimit = fRunLimits[det][dataMC]-nRunsOffset;
00342 if(runLimit <= potHist->GetEntries()){
00343 return potPerRun*runLimit;
00344 }
00345 else{
00346 return potHist->Integral(0, potHist->GetNbinsX()+1)*1e12;
00347 }
00348 }
00349
00350
00351 TString NCPOTCounter::FileDesignator(NCType::EFileType fileType,
00352 Detector::Detector_t det,
00353 NCType::EDataMC dataMC) const
00354 {
00355 if(fileType == NCType::kMockFile) return fMockDataSet;
00356
00357 if(dataMC == NCType::kData && !(fMCAsData || fMockData)){
00358 if(det == Detector::kNear) return "data";
00359 else if(det == Detector::kFar) return "blind";
00360 }
00361
00362 if(fileType == NCType::kTauFile) return "tau";
00363
00364 if(fileType == NCType::kElectronFile) return "electron";
00365
00366 return "mc";
00367 }
00368
00369
00370 TString NCPOTCounter::HistName(NCType::EFileType fileType,
00371 NCType::EDataMC dataMC) const
00372 {
00373 if(fileType == NCType::kMockFile ||
00374 (dataMC == NCType::kData && !(fMCAsData || fMockData))) return "dataPOT";
00375
00376 return "mcPOT";
00377 }
00378
00379
00380 double NCPOTCounter::POTPerRun(int bt, Detector::Detector_t det,
00381 NCType::EFileType fileType) const
00382 {
00383
00384
00385
00386 if(det == Detector::kFar){
00387 if(fileType == NCType::kMockFile) {
00388 const double kPotFarMock = 2.5e20;
00389 return kPotFarMock;
00390 }
00391
00392
00393 const double kPotFarMC = 6.5e20;
00394 return kPotFarMC;
00395 }
00396
00397 const double ret = kPOTNear[fBeamIndex[bt]];
00398 assert(ret >= 0);
00399 return ret;
00400 }
00401
00402 int NCPOTCounter::MockFileSubRun(TString fileName) const
00403 {
00404
00405
00406
00407
00408
00409
00410
00411 TRegexp re("_[0-9][0-9][0-9][0-9]\\.uDST");
00412 int stupidROOT;
00413 int index = re.Index(fileName, &stupidROOT);
00414 if(index != kNPOS) {
00415
00416 TString subRunStr(fileName(index+1, 4));
00417 return subRunStr.Atoi();
00418 } else {
00419
00420 return -1;
00421 }
00422 }