00001
00002
00003
00004
00005
00012
00013
00014
00015
00016
00017
00018
00019 #include "DataFormats/Provenance/interface/Provenance.h"
00020 #include "FWCore/Framework/interface/EDAnalyzer.h"
00021 #include "FWCore/Framework/interface/Event.h"
00022 #include "FWCore/Framework/interface/GenericHandle.h"
00023 #include "FWCore/Framework/interface/MakerMacros.h"
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00026 #include "FWCore/ParameterSet/interface/ParameterDescriptionNode.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00029 #include "FWCore/Utilities/interface/Algorithms.h"
00030
00031 #include "Reflex/Member.h"
00032
00033
00034 #include <algorithm>
00035 #include <iomanip>
00036 #include <iostream>
00037 #include <map>
00038 #include <sstream>
00039 #include <string>
00040 #include <vector>
00041
00042 namespace edm {
00043 class ConfigurationDescriptions;
00044 namespace {
00045 std::string formatClassName(std::string const& iName) {
00046 return std::string("(")+iName+")";
00047 }
00048
00049 char const* kNameValueSep = "=";
00051 template<typename T>
00052 void doPrint(std::string const& iName, Reflex::Object const& iObject, std::string const& iIndent) {
00053 LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << *reinterpret_cast<T*>(iObject.Address());
00054 }
00055
00056 template<>
00057 void doPrint<char>(std::string const& iName, Reflex::Object const& iObject, std::string const& iIndent) {
00058 LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << static_cast<int>(*reinterpret_cast<char*>(iObject.Address()));
00059 }
00060
00061 template<>
00062 void doPrint<unsigned char>(std::string const& iName, Reflex::Object const& iObject, std::string const& iIndent) {
00063 LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << static_cast<unsigned int>(*reinterpret_cast<unsigned char*>(iObject.Address()));
00064 }
00065
00066 template<>
00067 void doPrint<bool>(std::string const& iName, Reflex::Object const& iObject, std::string const& iIndent) {
00068 LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << ((*reinterpret_cast<bool*>(iObject.Address()))?"true":"false");
00069 }
00070
00071 typedef void(*FunctionType)(std::string const&, Reflex::Object const&, std::string const&);
00072 typedef std::map<std::string, FunctionType> TypeToPrintMap;
00073
00074 template<typename T>
00075 void addToMap(TypeToPrintMap& iMap) {
00076 iMap[typeid(T).name()] = doPrint<T>;
00077 }
00078
00079 bool printAsBuiltin(std::string const& iName,
00080 Reflex::Object const& iObject,
00081 std::string const& iIndent) {
00082 typedef void(*FunctionType)(std::string const&, Reflex::Object const&, std::string const&);
00083 typedef std::map<std::string, FunctionType> TypeToPrintMap;
00084 static TypeToPrintMap s_map;
00085 static bool isFirst = true;
00086 if(isFirst) {
00087 addToMap<bool>(s_map);
00088 addToMap<char>(s_map);
00089 addToMap<short>(s_map);
00090 addToMap<int>(s_map);
00091 addToMap<long>(s_map);
00092 addToMap<unsigned char>(s_map);
00093 addToMap<unsigned short>(s_map);
00094 addToMap<unsigned int>(s_map);
00095 addToMap<unsigned long>(s_map);
00096 addToMap<float>(s_map);
00097 addToMap<double>(s_map);
00098 isFirst = false;
00099 }
00100 TypeToPrintMap::iterator itFound = s_map.find(iObject.TypeOf().TypeInfo().name());
00101 if(itFound == s_map.end()) {
00102
00103 return false;
00104 }
00105 itFound->second(iName, iObject, iIndent);
00106 return true;
00107 }
00108
00109 bool printAsContainer(std::string const& iName,
00110 Reflex::Object const& iObject,
00111 std::string const& iIndent,
00112 std::string const& iIndentDelta);
00113
00114 void printObject(std::string const& iName,
00115 Reflex::Object const& iObject,
00116 std::string const& iIndent,
00117 std::string const& iIndentDelta) {
00118 std::string printName = iName;
00119 Reflex::Object objectToPrint = iObject;
00120 std::string indent(iIndent);
00121 if(iObject.TypeOf().IsPointer()) {
00122 LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << formatClassName(iObject.TypeOf().Name()) << std::hex << iObject.Address() << std::dec;
00123 Reflex::Type pointedType = iObject.TypeOf().ToType();
00124 if(Reflex::Type::ByName("void") == pointedType ||
00125 pointedType.IsPointer() ||
00126 iObject.Address() == 0) {
00127 return;
00128 }
00129 return;
00130
00131
00132 objectToPrint = Reflex::Object(pointedType, iObject.Address());
00133
00134 objectToPrint = Reflex::Object(objectToPrint.CastObject(objectToPrint.DynamicType()));
00135 printName = std::string("*")+iName;
00136 indent += iIndentDelta;
00137 }
00138 std::string typeName(objectToPrint.TypeOf().Name());
00139 if(typeName.empty()) {
00140 typeName = "<unknown>";
00141 }
00142
00143
00144 if(objectToPrint.TypeOf().IsTypedef()) {
00145 objectToPrint = Reflex::Object(objectToPrint.TypeOf().ToType(), objectToPrint.Address());
00146 }
00147 if(printAsBuiltin(printName, objectToPrint, indent)) {
00148 return;
00149 }
00150 if(printAsContainer(printName, objectToPrint, indent, iIndentDelta)) {
00151 return;
00152 }
00153
00154 LogAbsolute("EventContent") << indent << printName << " " << formatClassName(typeName);
00155 indent += iIndentDelta;
00156
00157 for(Reflex::Member_Iterator itMember = objectToPrint.TypeOf().DataMember_Begin();
00158 itMember != objectToPrint.TypeOf().DataMember_End();
00159 ++itMember) {
00160
00161 try {
00162 printObject(itMember->Name(),
00163 itMember->Get(objectToPrint),
00164 indent,
00165 iIndentDelta);
00166 }catch(std::exception& iEx) {
00167 LogAbsolute("EventContent") << indent << itMember->Name() << " <exception caught(" << iEx.what() << ")>\n";
00168 }
00169 }
00170 }
00171
00172 bool printAsContainer(std::string const& iName,
00173 Reflex::Object const& iObject,
00174 std::string const& iIndent,
00175 std::string const& iIndentDelta) {
00176 Reflex::Object sizeObj;
00177 try {
00178 size_t temp;
00179 sizeObj = Reflex::Object(Reflex::Type::ByTypeInfo(typeid(size_t)), &temp);
00180 iObject.Invoke("size", &sizeObj);
00181 assert(iObject.TypeOf().FunctionMemberByName("size").TypeOf().ReturnType().TypeInfo() == typeid(size_t));
00182
00183 assert(sizeObj.TypeOf().FinalType().TypeInfo() == typeid(size_t));
00184 size_t size = *reinterpret_cast<size_t*>(sizeObj.Address());
00185 Reflex::Member atMember;
00186 try {
00187 atMember = iObject.TypeOf().MemberByName("at");
00188 } catch(std::exception const& x) {
00189
00190 return false;
00191 }
00192 LogAbsolute("EventContent") << iIndent << iName << kNameValueSep << "[size=" << size << "]";
00193 Reflex::Object contained;
00194 std::string indexIndent = iIndent + iIndentDelta;
00195 Reflex::Type atReturnType = atMember.TypeOf().ReturnType();
00196
00197
00198
00199
00200
00201
00202 bool const isRef = atReturnType.IsReference();
00203 void* refMemoryBuffer = 0;
00204 size_t index = 0;
00205
00206
00207
00208 std::vector<void*> args;
00209 args.push_back(&index);
00210 for(; index != size; ++index) {
00211 std::ostringstream sizeS;
00212 sizeS << "[" << index << "]";
00213 if(isRef) {
00214 Reflex::Object refObject(atReturnType, &refMemoryBuffer);
00215 atMember.Invoke(iObject, &refObject, args);
00216
00217
00218 contained = Reflex::Object(atReturnType, refMemoryBuffer);
00219 } else {
00220 contained = atReturnType.Construct();
00221 atMember.Invoke(iObject, &contained, args);
00222 }
00223
00224 try {
00225 printObject(sizeS.str(), contained, indexIndent, iIndentDelta);
00226 } catch(std::exception& iEx) {
00227 LogAbsolute("EventContent") << indexIndent << iName << " <exception caught("
00228 << iEx.what() << ")>\n";
00229 }
00230 if(!isRef) {
00231 contained.Destruct();
00232 }
00233 }
00234 return true;
00235 } catch(std::exception const& x) {
00236
00237 return false;
00238 }
00239 return false;
00240 }
00241
00242 void printObject(Event const& iEvent,
00243 std::string const& iClassName,
00244 std::string const& iModuleLabel,
00245 std::string const& iInstanceLabel,
00246 std::string const& iProcessName,
00247 std::string const& iIndent,
00248 std::string const& iIndentDelta) {
00249 try {
00250 GenericHandle handle(iClassName);
00251 }catch(edm::Exception const&) {
00252 LogAbsolute("EventContent") << iIndent << " \"" << iClassName << "\"" << " is an unknown type" << std::endl;
00253 return;
00254 }
00255 GenericHandle handle(iClassName);
00256 iEvent.getByLabel(InputTag(iModuleLabel, iInstanceLabel, iProcessName), handle);
00257 std::string className = formatClassName(iClassName);
00258 printObject(className, *handle, iIndent, iIndentDelta);
00259 }
00260 }
00261
00262 class EventContentAnalyzer : public EDAnalyzer {
00263 public:
00264 explicit EventContentAnalyzer(ParameterSet const&);
00265 ~EventContentAnalyzer();
00266
00267 virtual void analyze(Event const&, EventSetup const&);
00268 virtual void endJob();
00269
00270 static void fillDescriptions(ConfigurationDescriptions& descriptions);
00271
00272 private:
00273
00274
00275 std::string indentation_;
00276 std::string verboseIndentation_;
00277 std::vector<std::string> moduleLabels_;
00278 bool verbose_;
00279 std::vector<std::string> getModuleLabels_;
00280 bool getData_;
00281 int evno_;
00282 std::map<std::string, int> cumulates_;
00283 bool listContent_;
00284 };
00285
00286
00287
00288
00289 EventContentAnalyzer::EventContentAnalyzer(ParameterSet const& iConfig) :
00290 indentation_(iConfig.getUntrackedParameter("indentation", std::string("++"))),
00291 verboseIndentation_(iConfig.getUntrackedParameter("verboseIndentation", std::string(" "))),
00292 moduleLabels_(iConfig.getUntrackedParameter("verboseForModuleLabels", std::vector<std::string>())),
00293 verbose_(iConfig.getUntrackedParameter("verbose", false) || moduleLabels_.size()>0),
00294 getModuleLabels_(iConfig.getUntrackedParameter("getDataForModuleLabels", std::vector<std::string>())),
00295 getData_(iConfig.getUntrackedParameter("getData", false) || getModuleLabels_.size()>0),
00296 evno_(1),
00297 listContent_(iConfig.getUntrackedParameter("listContent", true)){
00298
00299 sort_all(moduleLabels_);
00300 }
00301
00302 EventContentAnalyzer::~EventContentAnalyzer() {
00303
00304
00305
00306
00307 }
00308
00309
00310
00311
00312
00313
00314 void
00315 EventContentAnalyzer::analyze(Event const& iEvent, EventSetup const&) {
00316 typedef std::vector<Provenance const*> Provenances;
00317 Provenances provenances;
00318
00319 iEvent.getAllProvenance(provenances);
00320
00321 if(listContent_) {
00322 LogAbsolute("EventContent") << "\n" << indentation_ << "Event " << std::setw(5) << evno_ << " contains "
00323 << provenances.size() << " product" << (provenances.size() == 1 ? "" : "s")
00324 << " with friendlyClassName, moduleLabel, productInstanceName and processName:"
00325 << std::endl;
00326 }
00327
00328 std::string startIndent = indentation_+verboseIndentation_;
00329 for(Provenances::iterator itProv = provenances.begin(), itProvEnd = provenances.end();
00330 itProv != itProvEnd;
00331 ++itProv) {
00332 std::string const& className = (*itProv)->className();
00333
00334 std::string const& friendlyName = (*itProv)->friendlyClassName();
00335
00336
00337 std::string const& modLabel = (*itProv)->moduleLabel();
00338
00339
00340 std::string const& instanceName = (*itProv)->productInstanceName();
00341
00342
00343 std::string const& processName = (*itProv)->processName();
00344
00345 bool doVerbose = verbose_ && (moduleLabels_.empty() ||
00346 binary_search_all(moduleLabels_, modLabel));
00347
00348 if(listContent_ || doVerbose) {
00349 LogAbsolute("EventContent") << indentation_ << friendlyName
00350 << " \"" << modLabel
00351 << "\" \"" << instanceName << "\" \""
00352 << processName << "\""
00353 << " (productId = " << (*itProv)->productID() << ")"
00354 << std::endl;
00355 }
00356
00357 std::string key = friendlyName
00358 + std::string(" + \"") + modLabel
00359 + std::string("\" + \"") + instanceName + "\" \"" + processName + "\"";
00360 ++cumulates_[key];
00361
00362 if(doVerbose) {
00363
00364 printObject(iEvent,
00365 className,
00366 modLabel,
00367 instanceName,
00368 processName,
00369 startIndent,
00370 verboseIndentation_);
00371 continue;
00372 }
00373 if(getData_) {
00374 if(getModuleLabels_.empty() ||
00375 binary_search_all(getModuleLabels_, modLabel)) {
00376 try {
00377 GenericHandle handle(className);
00378 } catch(edm::Exception const&) {
00379 LogAbsolute("EventContent") << startIndent << " \"" << className << "\"" << " is an unknown type" << std::endl;
00380 return;
00381 }
00382 GenericHandle handle(className);
00383 iEvent.getByLabel(InputTag(modLabel,
00384 instanceName,
00385 processName),
00386 handle);
00387 }
00388 }
00389 }
00390
00391 ++evno_;
00392 }
00393
00394
00395 void
00396 EventContentAnalyzer::endJob() {
00397 typedef std::map<std::string, int> nameMap;
00398
00399 LogAbsolute("EventContent") << "\nSummary for key being the concatenation of friendlyClassName, moduleLabel, productInstanceName and processName" << std::endl;
00400 for(nameMap::const_iterator it = cumulates_.begin(), itEnd = cumulates_.end();
00401 it != itEnd;
00402 ++it) {
00403 LogAbsolute("EventContent") << std::setw(6) << it->second << " occurrences of key " << it->first << std::endl;
00404 }
00405
00406
00407
00408
00409
00410 }
00411
00412 void
00413 EventContentAnalyzer::fillDescriptions(ConfigurationDescriptions& descriptions) {
00414
00415 descriptions.setComment("This plugin will print a list of all products in the event "
00416 "provenance. It also has options to print and/or get each product.");
00417
00418 ParameterSetDescription desc;
00419
00420 ParameterDescriptionNode* np;
00421
00422 std::string defaultString("++");
00423 np = desc.addOptionalUntracked<std::string>("indentation", defaultString);
00424 np->setComment("This string is printed at the beginning of every line printed during event processing.");
00425
00426 np = desc.addOptionalUntracked<bool>("verbose", false);
00427 np->setComment("If true, the contents of products are printed using Reflex.");
00428
00429 defaultString = " ";
00430 np = desc.addOptionalUntracked<std::string>("verboseIndentation", defaultString);
00431 np->setComment("This string is used to further indent lines when printing the contents of products in verbose mode.");
00432
00433 std::vector<std::string> defaultVString;
00434
00435 np = desc.addOptionalUntracked<std::vector<std::string> >("verboseForModuleLabels", defaultVString);
00436 np->setComment("If this vector is not empty, then only products with module labels on this list are printed using Reflex.");
00437
00438 np = desc.addOptionalUntracked<bool>("getData", false);
00439 np->setComment("If true the products will be retrieved using getByLabel.");
00440
00441 np = desc.addOptionalUntracked<std::vector<std::string> >("getDataForModuleLabels", defaultVString);
00442 np->setComment("If this vector is not empty, then only products with module labels on this list are retrieved by getByLabel.");
00443
00444 np = desc.addOptionalUntracked<bool>("listContent", true);
00445 np->setComment("If true then print a list of all the event content.");
00446
00447
00448 descriptions.add("printContent", desc);
00449 }
00450 }
00451
00452 using edm::EventContentAnalyzer;
00453 DEFINE_FWK_MODULE(EventContentAnalyzer);