CMS 3D CMS Logo

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