CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dqmAuxFunctions.cc
Go to the documentation of this file.
2 
4 
6 
7 #include <TH1.h>
8 
9 #include <iostream>
10 
12  const std::string& keyword,
13  const std::string& parameter,
14  unsigned minReplacements,
15  unsigned maxReplacements,
16  int& errorFlag) {
17  std::string modSrc = src;
18  unsigned numReplacements = 0;
19  while (modSrc.find(keyword) != std::string::npos) {
20  modSrc.replace(modSrc.find(keyword), keyword.length(), parameter);
21  ++numReplacements;
22  }
23  if ((numReplacements < minReplacements) || (numReplacements > maxReplacements)) {
24  edm::LogError("replace_string") << " Failed to replace parameter = " << parameter << " in src = " << src << ","
25  << " numReplacements = " << numReplacements << " (min = " << minReplacements
26  << ", max = " << maxReplacements << ") !!";
27  errorFlag = 1;
28  }
29  return modSrc;
30 }
31 
32 //
33 //-----------------------------------------------------------------------------------------------------------------------
34 //
35 
36 std::string format_vstring(const std::vector<std::string>& vs) {
37  std::ostringstream os;
38 
39  os << "{ ";
40 
41  unsigned numEntries = vs.size();
42  for (unsigned iEntry = 0; iEntry < numEntries; ++iEntry) {
43  os << vs[iEntry];
44  if (iEntry < (numEntries - 1))
45  os << ", ";
46  }
47 
48  os << " }";
49 
50  return os.str();
51 }
52 
53 //
54 //-----------------------------------------------------------------------------------------------------------------------
55 //
56 
59  //if ( dirName == "" || dirName.find_last_of(dqmSeparator) != (dirName.length() - 1) ) dirName.append(dqmSeparator);
60  //--- add tailing '/'
61  if (!dirName.empty() && dirName.find_last_of(dqmSeparator) != (dirName.length() - 1))
62  dirName.append(dqmSeparator);
63  return dirName;
64 }
65 
67  std::string subDirName = subdirectory;
68  //--- remove characters specifying directory part from name of subdirectory
69  if (subDirName.find(directory) <= 1)
70  subDirName.replace(subDirName.find(directory), directory.length(), "");
71  //--- remove tailing '/'s
72  while (subDirName.find(dqmSeparator) == 0)
73  subDirName.replace(subDirName.find(dqmSeparator), dqmSeparator.length(), "");
74  return subDirName;
75 }
76 
77 //
78 //-----------------------------------------------------------------------------------------------------------------------
79 //
80 
81 void dqmRegisterHistogram(dqm::legacy::DQMStore& dqmStore, TH1* histogram, const std::string& name) {
82  //std::cout << "<dqmRegisterHistogram>:" << std::endl;
83  //histogram->SetName(std::string(histogram->GetName()).append("_copied").data());
84  histogram->SetName(histogram->GetName());
85  if (TH1F* h = dynamic_cast<TH1F*>(histogram)) {
86  //std::cout << " --> calling DQMStore::book1D" << std::endl;
87  dqmStore.book1D(name, h);
88  } else if (TH1S* h = dynamic_cast<TH1S*>(histogram)) {
89  //std::cout << " --> calling DQMStore::book1@" << std::endl;
90  dqmStore.book1S(name, h);
91  } else if (TH2F* h = dynamic_cast<TH2F*>(histogram)) {
92  //std::cout << " --> calling DQMStore::book2D" << std::endl;
93  dqmStore.book2D(name, h);
94  } else if (TH2S* h = dynamic_cast<TH2S*>(histogram)) {
95  //std::cout << " --> calling DQMStore::book2S" << std::endl;
96  dqmStore.book2S(name, h);
97  } else if (TH3F* h = dynamic_cast<TH3F*>(histogram)) {
98  //std::cout << " --> calling DQMStore::book3D" << std::endl;
99  dqmStore.book3D(name, h);
100  } else if (TProfile* h = dynamic_cast<TProfile*>(histogram)) {
101  //std::cout << " --> calling DQMStore::bookProfile" << std::endl;
102  dqmStore.bookProfile(name, h);
103  } else if (TProfile2D* h = dynamic_cast<TProfile2D*>(histogram)) {
104  //std::cout << " --> calling DQMStore::bookProfile2D" << std::endl;
105  dqmStore.bookProfile2D(name, h);
106  }
107 }
108 
110  const std::string& inputDirectory,
111  const std::string& outputDirectory,
112  double scaleFactor,
113  int mode,
114  bool rmInputDirectory) {
115  //std::cout << "<copyRecursively>:" << std::endl;
116  //std::cout << " inputDirectory = " << inputDirectory << std::endl;
117  //std::cout << " outputDirectory = " << outputDirectory << std::endl;
118  //std::cout << " rmInputDirectory = " << rmInputDirectory << std::endl;
119 
120  //--- copy all monitor elements in current inputDirectory to the outputDirectory
121  dqmStore.setCurrentFolder(inputDirectory);
122  std::vector<std::string> meNames = dqmStore.getMEs();
123  for (std::vector<std::string>::const_iterator meName = meNames.begin(); meName != meNames.end(); ++meName) {
124  std::string meName_full = dqmDirectoryName(inputDirectory).append(*meName);
125  //std::cout << " meName_full = " << meName_full << std::endl;
126 
127  dqmStore.setCurrentFolder(inputDirectory);
128  dqm::legacy::MonitorElement* meInput = dqmStore.get(meName_full);
129  //std::cout << " meInput = " << meInput << std::endl;
130  if (!meInput) {
131  edm::LogError("copyRecursively") << " Failed to access meName = " << (*meName) << " in DQMStore"
132  << " --> skipping !!";
133  continue;
134  }
135 
136  TH1* histogram = meInput->getTH1();
137  //std::cout << " histogram = " << histogram << std::endl;
138  if (!histogram) {
139  edm::LogError("copyRecursively") << " Failed to access histogram associated to meName = " << (*meName)
140  << " in DQMStore"
141  << " --> skipping !!";
142  continue;
143  }
144 
145  std::unique_ptr<TH1> clone(dynamic_cast<TH1*>(histogram->Clone()));
146  clone->Scale(scaleFactor);
147 
148  dqmStore.setCurrentFolder(outputDirectory);
149  dqm::legacy::MonitorElement* meOutput = dqmStore.get(dqmDirectoryName(outputDirectory).append(*meName));
150  //std::cout << " meOutput = " << meOutput << std::endl;
151  //--- check if outputHistogram does already exist
152  if (meOutput) {
153  switch (mode) {
154  case 1: // print error message
155  edm::LogError("copyRecursively")
156  << " meName = " << (*meName) << " already exists in outputDirectory = " << outputDirectory
157  << " --> skipping !!";
158  break;
159  case 2: // overwrite outputHistogram
160  dqmRegisterHistogram(dqmStore, clone.release(), *meName);
161  break;
162  case 3: // add histogram to outputHistogram
163  meOutput->getTH1()->Add(clone.get(), scaleFactor);
164  }
165  } else {
166  dqmRegisterHistogram(dqmStore, clone.release(), *meName);
167  }
168  }
169 
170  //--- call function recursively for all sub-directories
171  dqmStore.setCurrentFolder(inputDirectory);
172  std::vector<std::string> dirNames = dqmStore.getSubdirs();
173  for (std::vector<std::string>::const_iterator dirName = dirNames.begin(); dirName != dirNames.end(); ++dirName) {
174  std::string subDirName = dqmSubDirectoryName_merged(inputDirectory, *dirName);
175  //std::cout << " subDirName = " << subDirName << std::endl;
176 
177  std::string inputDirName_full = dqmDirectoryName(inputDirectory).append(subDirName);
178  //std::cout << " inputDirName_full = " << inputDirName_full << std::endl;
179 
180  std::string outputDirName_full = dqmDirectoryName(outputDirectory).append(subDirName);
181  //std::cout << " outputDirName_full = " << outputDirName_full << std::endl;
182 
183  dqmCopyRecursively(dqmStore, inputDirName_full, outputDirName_full, scaleFactor, mode, rmInputDirectory);
184  }
185 }
186 
187 //
188 //-----------------------------------------------------------------------------------------------------------------------
189 //
190 
191 void separateHistogramFromDirectoryName(const std::string& histogramAndDirectoryName,
192  std::string& histogramName,
193  std::string& directoryName) {
194  //std::cout << "<separateHistogramFromDirectoryName>:" << std::endl;
195 
196  std::string tempName = histogramAndDirectoryName;
197 
198  //--- remove DQM root directory from histogram name
199  std::string::size_type dqmRootDirectoryPos = tempName.find(dqmRootDirectory);
200  if (dqmRootDirectoryPos != std::string::npos) {
201  tempName.replace(dqmRootDirectoryPos, dqmRootDirectory.size(), "");
202  }
203 
204  //std::cout << " tempName = " << tempName << std::endl;
205 
206  //--- extract directory from histogram name
207  std::string::size_type lastPos;
208  std::string::size_type nextPos = tempName.find(dqmSeparator);
209  do {
210  lastPos = nextPos;
211  nextPos = tempName.find(dqmSeparator, lastPos + 1);
212  } while (nextPos != std::string::npos);
213 
214  histogramName = (lastPos != std::string::npos) ? std::string(tempName, lastPos + 1, tempName.length()) : tempName;
215  directoryName = (lastPos != std::string::npos) ? std::string(tempName, 0, lastPos) : "";
216 
217  //std::cout << " histogramName = " << histogramName << std::endl;
218  //std::cout << " directoryName = " << directoryName << std::endl;
219 }
MonitorElement * bookProfile2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, double lowZ, double highZ, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:476
MonitorElement * book2S(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:254
void dqmRegisterHistogram(dqm::legacy::DQMStore &dqmStore, TH1 *histogram, const std::string &name)
virtual DQM_DEPRECATED std::vector< std::string > getSubdirs() const
Definition: DQMStore.cc:700
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
void setCurrentFolder(std::string const &fullpath) override
Definition: DQMStore.h:646
std::string replace_string(const std::string &src, const std::string &keyword, const std::string &parameter, unsigned minReplacements, unsigned maxReplacements, int &errorFlag)
Log< level::Error, false > LogError
std::string dqmDirectoryName(const std::string &dqmRootDirectory, const std::string &dqmSubDirectory)
Definition: EwkTauDQM.cc:10
uint16_t size_type
std::string dqmSubDirectoryName_merged(const std::string &directory, const std::string &subdirectory)
MonitorElement * bookProfile(TString const &name, TString const &title, int nchX, double lowX, double highX, int, double lowY, double highY, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:399
Long64_t numEntries(TFile *hdl, std::string const &trname)
Definition: CollUtil.cc:50
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:673
MonitorElement * book1S(TString const &name, TString const &title, int nchX, double lowX, double highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:133
void separateHistogramFromDirectoryName(const std::string &histogramAndDirectoryName, std::string &histogramName, std::string &directoryName)
void dqmCopyRecursively(dqm::legacy::DQMStore &dqmStore, const std::string &inputDirectory, const std::string &outputDirectory, double scaleFactor, int mode, bool rmInputDirectory)
std::string format_vstring(const std::vector< std::string > &vs)
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:212
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
virtual std::vector< std::string > getMEs() const
Definition: DQMStore.cc:720
const std::string dqmRootDirectory
const std::string dqmSeparator
Definition: EwkTauDQM.cc:8
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
MonitorElement * book3D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, int nchZ, double lowZ, double highZ, FUNC onbooking=NOOP())
Definition: DQMStore.h:367
virtual TH1 * getTH1() const