Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:18

0001 #include "FWCore/TFWLiteSelectorTest/src/ThingsTSelector2.h"
0002 #include <TCanvas.h>
0003 #include <iostream>
0004 #include "Rtypes.h"
0005 #include "DataFormats/Common/interface/Handle.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "DataFormats/TestObjects/interface/OtherThingCollection.h"
0008 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0009 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0010 
0011 using namespace tfwliteselectortest;
0012 
0013 //Names used in common between the worker and the Selector
0014 static const char* kA = "a";
0015 static const char* kRefA = "refA";
0016 
0017 ThingsWorker::ThingsWorker(const TList*, TList& out) {
0018   std::cout << "begin" << std::endl;
0019   h_a = new TH1F(kA, "a", 100, 0, 20);
0020   out.Add(h_a);
0021 
0022   h_refA = new TH1F(kRefA, "refA", 100, 0, 20);
0023   out.Add(h_refA);
0024 }
0025 
0026 void ThingsWorker::process(const edm::Event& iEvent) {
0027   std::cout << "processing event " << std::endl;
0028   //  chain->GetEntry( entry );
0029   using namespace edmtest;
0030   edm::Handle<OtherThingCollection> hOThings;
0031   // In case of an exception prints the message and aborts the process
0032   CMS_SA_ALLOW try {
0033     iEvent.getByLabel("OtherThing", "testUserTag", hOThings);
0034 
0035     std::cout << ">> other things found:" << hOThings->size() << std::endl;
0036     for (size_t i = 0; i < hOThings->size(); ++i) {
0037       const OtherThing& thing = (*hOThings)[i];
0038       h_refA->Fill(thing.ref->a);
0039       std::cout << ">> ref->a:  " << thing.ref->a << std::endl;
0040     }
0041 
0042     edm::Handle<ThingCollection> hThings;
0043     iEvent.getByLabel("Thing", hThings);
0044     const ThingCollection& things = *hThings;
0045     std::cout << ">> things found:" << things.size() << std::endl;
0046     for (size_t i = 0; i < things.size(); ++i) {
0047       const Thing& thing = things[i];
0048       h_a->Fill(thing.a);
0049       std::cout << ">> a:  " << thing.a << std::endl;
0050     }
0051   } catch (cms::Exception& x) {
0052     std::cout << std::endl << "Failed with cms::Exception: " << std::endl;
0053     std::cout << x.what() << std::endl;
0054     abort();
0055   } catch (std::exception& x) {
0056     std::cout << std::endl << "Failed with std::exception" << std::endl;
0057     std::cout << x.what() << std::endl;
0058     abort();
0059   } catch (...) {
0060     std::cout << std::endl << "Failed with unknown exception" << std::endl;
0061     abort();
0062   }
0063 }
0064 
0065 void ThingsWorker::postProcess(TList&) {}
0066 
0067 void ThingsTSelector2::begin(TList*&) {}
0068 
0069 void ThingsTSelector2::terminate(TList& out) {
0070   std::cout << "terminate" << std::endl;
0071   TCanvas* canvas = new TCanvas();
0072   {
0073     TObject* hist = out.FindObject(kA);
0074     if (nullptr != hist) {
0075       hist->Draw();
0076       canvas->SaveAs("a.jpg");
0077     } else {
0078       std::cout << "no '" << kA << "' histogram" << std::endl;
0079     }
0080   }
0081   std::cout << "refA" << std::endl;
0082   {
0083     TObject* hist = out.FindObject(kRefA);
0084     if (nullptr != hist) {
0085       hist->Draw();
0086       canvas->SaveAs("refA.jpg");
0087     } else {
0088       std::cout << "no '" << kRefA << "' histogram" << std::endl;
0089     }
0090   }
0091   delete canvas;
0092 }