CMS 3D CMS Logo

DQMGenericTnPClient.cc
Go to the documentation of this file.
11 
13 
14 #include<TString.h>
15 #include<TPRegexp.h>
16 
17 using namespace edm;
18 using namespace dqmTnP;
19 using namespace std;
20 
21 
22 using vstring = std::vector<std::string>;
23 
25  public:
27  ~DQMGenericTnPClient() override;
28  void analyze(const edm::Event& event, const edm::EventSetup& eventSetup) override {};
29  void endRun(const edm::Run &run, const edm::EventSetup &setup) override;
30  void calculateEfficiency(const std::string& dirName, const ParameterSet& pset);
31  void findAllSubdirectories (const std::string& dir, std::set<std::string> * myList, TString pattern);
32  private:
34  TFile * plots;
37  bool verbose;
41 };
42 
44  subDirs( pset.getUntrackedParameter<vstring>("subDirs", vstring()) ),
45  myDQMrootFolder( pset.getUntrackedParameter<std::string>("MyDQMrootFolder", "") ),
46  verbose( pset.getUntrackedParameter<bool>("Verbose",false) ),
47  efficiencies( pset.getUntrackedParameter<VParameterSet>("Efficiencies") )
48 {
49  TString savePlotsInRootFileName = pset.getUntrackedParameter<string>("SavePlotsInRootFileName","");
50  plots = savePlotsInRootFileName!="" ? new TFile(savePlotsInRootFileName,"recreate") : nullptr;
53 }
54 
56 
57  TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]");
58 
60  if( !dqmStore ){
61  LogError("DQMGenericTnPClient")<<"Could not find DQMStore service\n";
62  return;
63  }
64  dqmStore->setCurrentFolder(myDQMrootFolder);
65 
66  set<std::string> subDirSet;
67 
68  if (myDQMrootFolder != "")
69  subDirSet.insert(myDQMrootFolder);
70  else {
71  for(auto iSubDir = subDirs.begin();
72  iSubDir != subDirs.end(); ++iSubDir) {
73  std::string subDir = *iSubDir;
74  if ( subDir[subDir.size()-1] == '/' ) subDir.erase(subDir.size()-1);
75  if ( TString(subDir).Contains(metacharacters) ) {
76  const string::size_type shiftPos = subDir.rfind('/');
77  const string searchPath = subDir.substr(0, shiftPos);
78  const string pattern = subDir.substr(shiftPos + 1, subDir.length());
79  findAllSubdirectories (searchPath, &subDirSet, pattern);
80  }
81  else {
82  subDirSet.insert(subDir);
83  }
84  }
85  }
86 
87  for(auto const & dirName : subDirSet) {
88  for(auto const & efficiencie : efficiencies) {
89  calculateEfficiency(dirName, efficiencie);
90  }
91  }
92 
93 }
94 
96  //get hold of numerator and denominator histograms
97  string allMEname = dirName+"/"+pset.getUntrackedParameter<string>("DenominatorMEname");
98  string passMEname = dirName+"/"+pset.getUntrackedParameter<string>("NumeratorMEname");
99  MonitorElement *allME = dqmStore->get(allMEname);
100  MonitorElement *passME = dqmStore->get(passMEname);
101  if(allME==nullptr || passME==nullptr){
102  LogDebug("DQMGenericTnPClient")<<"Could not find MEs: "<<allMEname<<" or "<<passMEname<<endl;
103  return;
104  }
105  TH1 *all = allME->getTH1();
106  TH1 *pass = passME->getTH1();
107  //setup the fitter
108  string fitFunction = pset.getUntrackedParameter<string>("FitFunction");
109  AbstractFitter *fitter = nullptr;
110  if(fitFunction=="GaussianPlusLinear"){
111  GPLfitter->setup(
112  pset.getUntrackedParameter<double>("ExpectedMean"),
113  pset.getUntrackedParameter<double>("FitRangeLow"),
114  pset.getUntrackedParameter<double>("FitRangeHigh"),
115  pset.getUntrackedParameter<double>("ExpectedSigma")
116  );
117  fitter = GPLfitter;
118  }else if(fitFunction=="VoigtianPlusExponential"){
119  VPEfitter->setup(
120  pset.getUntrackedParameter<double>("ExpectedMean"),
121  pset.getUntrackedParameter<double>("FitRangeLow"),
122  pset.getUntrackedParameter<double>("FitRangeHigh"),
123  pset.getUntrackedParameter<double>("ExpectedSigma"),
124  pset.getUntrackedParameter<double>("FixedWidth")
125  );
126  fitter = VPEfitter;
127  }else{
128  LogError("DQMGenericTnPClient")<<"Fit function: "<<fitFunction<<" does not exist"<<endl;
129  return;
130  }
131  //check dimensions
132  int dimensions = all->GetDimension();
133  int massDimension = pset.getUntrackedParameter<int>("MassDimension");
134  if(massDimension>dimensions){
135  LogError("DQMGenericTnPClient")<<"Monitoring Element "<<allMEname<<" has smaller dimension than "<<massDimension<<endl;
136  return;
137  }
138  //figure out directory and efficiency names
139  string effName = pset.getUntrackedParameter<string>("EfficiencyMEname");
140  string effDir = dirName;
141  string::size_type slashPos = effName.rfind('/');
142  if ( string::npos != slashPos ) {
143  effDir += "/"+effName.substr(0, slashPos);
144  effName.erase(0, slashPos+1);
145  }
146  dqmStore->setCurrentFolder(effDir);
147  TString prefix(effDir.c_str());
148  prefix.ReplaceAll('/','_');
149  //calculate and book efficiency
150  if(dimensions==2){
151  TProfile* eff = nullptr;
152  TProfile* effChi2 = nullptr;
153  TString error = fitter->calculateEfficiency((TH2*)pass, (TH2*)all, massDimension, eff, effChi2, plots?prefix+effName.c_str():"");
154  if(error!=""){
155  LogError("DQMGenericTnPClient")<<error<<endl;
156  return;
157  }
158  dqmStore->bookProfile(effName,eff);
159  dqmStore->bookProfile(effName+"Chi2",effChi2);
160  delete eff;
161  delete effChi2;
162  }else if(dimensions==3){
163  TProfile2D* eff = nullptr;
164  TProfile2D* effChi2 = nullptr;
165  TString error = fitter->calculateEfficiency((TH3*)pass, (TH3*)all, massDimension, eff, effChi2, plots?prefix+effName.c_str():"");
166  if(error!=""){
167  LogError("DQMGenericTnPClient")<<error<<endl;
168  return;
169  }
170  dqmStore->bookProfile2D(effName,eff);
171  dqmStore->bookProfile2D(effName+"Chi2",effChi2);
172  delete eff;
173  delete effChi2;
174  }
175 }
176 
178  delete GPLfitter;
179  if(plots){
180  plots->Close();
181  }
182 }
183 
184 void DQMGenericTnPClient::findAllSubdirectories (const std::string& dir, std::set<std::string> * myList, TString pattern = "") {
185  if (!dqmStore->dirExists(dir)) {
186  LogError("DQMGenericTnPClient") << " DQMGenericTnPClient::findAllSubdirectories ==> Missing folder " << dir << " !!!";
187  return;
188  }
189  TPRegexp nonPerlWildcard("\\w\\*|^\\*");
190  if (pattern != "") {
191  if (pattern.Contains(nonPerlWildcard)) pattern.ReplaceAll("*",".*");
192  TPRegexp regexp(pattern);
193  dqmStore->cd(dir);
194  vector <string> foundDirs = dqmStore->getSubdirs();
195  for(auto iDir = foundDirs.begin();
196  iDir != foundDirs.end(); ++iDir) {
197  TString dirName = iDir->substr(iDir->rfind('/') + 1, iDir->length());
198  if (dirName.Contains(regexp))
199  findAllSubdirectories ( *iDir, myList);
200  }
201  }
202  else if (dqmStore->dirExists(dir)){
203  myList->insert(dir);
204  dqmStore->cd(dir);
205  findAllSubdirectories (dir, myList, "*");
206  } else {
207 
208  LogInfo ("DQMGenericClient") << "Trying to find sub-directories of " << dir
209  << " failed because " << dir << " does not exist";
210 
211  }
212  return;
213 }
214 
215 
216 //define this as a plug-in
#define LogDebug(id)
T getUntrackedParameter(std::string const &, T const &) const
GaussianPlusLinearFitter * GPLfitter
vector< string > vstring
Definition: ExoticaDQM.cc:8
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
void endRun(const edm::Run &run, const edm::EventSetup &setup) override
TPRegexp metacharacters("[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]")
void setup(double expectedMean_, double massLow, double massHigh, double expectedSigma_)
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
void calculateEfficiency(const std::string &dirName, const ParameterSet &pset)
uint16_t size_type
void setup(double expectedMean_, double massLow, double massHigh, double expectedSigma_, double width_)
DQMGenericTnPClient(const edm::ParameterSet &pset)
void findAllSubdirectories(const std::string &dir, std::set< std::string > *myList, TString pattern)
VoigtianPlusExponentialFitter * VPEfitter
TH1 * getTH1(void) const
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
TPRegexp nonPerlWildcard("\\w\\*|^\\*")
HLT enums.
dbl *** dir
Definition: mlp_gen.cc:35
const VParameterSet efficiencies
Definition: event.py:1
Definition: Run.h:43