Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NCPOTCounter.cxx

Go to the documentation of this file.
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   // TODO - this code is copy-pasted from NCExtrapolationModule
00039   if(r.Get("DataMCPath", tmps)){
00040     //for some reason the TSystemDirectory constructor can't resolve
00041     //environmental variables, so just take care of it here
00042     TString temp = tmps;
00043     // Assume the form $ENVVAR/path
00044     if(temp.Contains("$")){
00045       int pos = temp.Index("/"); // find the first /
00046       temp.Resize(pos); // and truncate at it
00047       temp.Remove(TString::kLeading, '$'); // drop the $ off the front
00048       fDataMCPath = tmps; // Copy the original string
00049       // and then replace the part that we think is the environment variable
00050       fDataMCPath.Replace(0, pos, gSystem->Getenv(temp));
00051     }
00052     else fDataMCPath = temp;
00053     // Add a trailing slash so that concatenating the path later will work
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   // Make POT map with an entry for each run/beam type
00103   // combination. If we actually have nonzero POTs for a particular
00104   // combination, it'll get added to the 'pot' map at the end.
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   //loop over the beams/runperiods we're using and fill the pot values
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       // The total number of runs we've read from files,
00121       // so we can apply the file limits
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         //const int runType = NC::RunUtil::FindRunType(fileName);
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         // If we've already hit the file limit, stop
00143         if(nRunsRead > fRunLimits[det][dataMC]) break;
00144 
00145       } // end for nf
00146 
00147       
00148       /*
00149         const TString beamType = BeamType::AsString(fBeamIndex[bt]);
00150         MSG("NCPOTCounter", Msg::kInfo) << "POT INFORMATION("
00151         << (det == Detector::kFar ? "far" : "near")
00152         << "): " << endl
00153         << FileDesignator(fileType, det, dataMC)
00154         << " run I POT[" << beamType << "] = "
00155         << POTtmp[0].at(bt)
00156         << " run II POT[" << beamType << "] = "
00157         << POTtmp[1].at(bt)
00158         << endl;
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       }//end if pot > 0 for this combination of run type and beam      
00171      
00172     }//end loop over run types 
00173 
00174     // Now we've looped over all the run periods, add their total to
00175     // the RunAll NCBeam for this beamtype
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   }//end loop over beam types
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 //   MSG("NCPOTCounter", Msg::kInfo) << "GetListOfFiles(" << bt << ", " << fileType
00227 //                                   << ", " << det << ", " << dataMC << endl;
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   //have a path to the relevant uDST's.  each uDST has the
00238   //BeamType::AsString() for the beam in its name, also has near
00239   //or far in the name.
00240   //make a TSystemDirectory object to get a list of files in the
00241   //path for the data and mc
00242 
00243   //the files for different beam configurations are in uDST/LXXXzYYYi
00244   //directories.  that is directories named for the beam type
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     //Do nothing for "." and ".." entries
00253     if(fileName == "." || fileName == "..") continue;
00254 
00255     //check that this file is the right kind
00256     if(fileName.Contains(beamType) &&
00257        fileName.Contains("strip") &&
00258        fileName.Contains(detName) &&
00259        fileName.Contains(fileDesignator)){
00260 
00261       // Check it's the right run type - this should work for both data and mc
00262       const NC::RunUtil::ERunType fileRunPeriod = NC::RunUtil::FindRunType(fileName);
00263       if(fileRunPeriod != info.GetRunType()) continue;
00264       if(fileType == NCType::kMockFile && fMockDataSubRun != -1){
00265         // For mock data, only add files with subrun requested
00266         int subRun = MockFileSubRun(fileName);
00267         // If it's not the one we want, don't add it to the chain.
00268         //
00269         // subRun==-1 if the file is the one-uDST-per-run type. In
00270         // that case, the fileDesignator check above is enough to tell
00271         // us that we want to use this file, so don't skip it
00272         if(subRun != -1 && subRun != fMockDataSubRun) continue;
00273       }
00274       ret.push_back(fileName);
00275     }//end if the right detector and beam and designator
00276   }//end loop over data files
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   // Hopefully this cast does what I want. Why does GetEntries() not return an int? Gah
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   //  TDirectory* saveDir = gDirectory;
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     // The one-subrun-per-uDST files have the correct POT stored in
00321     // the dataPOT histogram. The one-run-per-uDST files have to use
00322     // the hardcoded value from POTPerRun
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   //use all pot's for data
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   //really need to do something better than the next few lines
00384   //to account for changes in MC version
00385 
00386   if(det == Detector::kFar){
00387     if(fileType == NCType::kMockFile) {
00388       const double kPotFarMock = 2.5e20;
00389       return kPotFarMock;
00390     }
00391 
00392     // TODO: Is this a good name for it? If so should go in NCType
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   // Filenames look like:
00405   // "far_mock_L010z185i_F21910001_0000.uDST_strip.root"
00406   //
00407   // ROOT is useless and doesn't support character classes, or
00408   // specifying a number of times to match, so we get this RE
00409   // to find the subrun from the filename.
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     // We found a subrun string in the filename, so use that.
00416     TString subRunStr(fileName(index+1, 4));
00417     return subRunStr.Atoi();
00418   } else {
00419     // This will happen for one-uDST-per-run files
00420     return -1;
00421   }
00422 }

Generated on Mon Feb 15 11:07:04 2010 for loon by  doxygen 1.3.9.1