Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/TFWLiteSelectorTest/src/ThingsTSelector.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 static const char* kA = "a";
0014 static const char* kRefA = "refA";
0015 void ThingsTSelector::begin(TList*&) {}
0016 
0017 void ThingsTSelector::preProcessing(const TList*, TList& out) {
0018   if (nullptr != h_a) {
0019     out.Remove(h_a);
0020     delete h_a.get();
0021     h_a = nullptr;
0022   }
0023   h_a = new TH1F(kA, "a", 100, 0, 20);
0024   out.Add(h_a);
0025 
0026   if (nullptr != h_refA) {
0027     out.Remove(h_refA);
0028     delete h_refA.get();
0029     h_refA = nullptr;
0030   }
0031   h_refA = new TH1F(kRefA, "refA", 100, 0, 20);
0032   out.Add(h_refA);
0033 }
0034 
0035 void ThingsTSelector::process(const edm::Event& iEvent) {
0036   std::cout << "processing event " << std::endl;
0037   //  chain->GetEntry( entry );
0038   using namespace edmtest;
0039   edm::Handle<OtherThingCollection> hOThings;
0040   // In case of an exception prints the message and aborts the process
0041   CMS_SA_ALLOW try {
0042     iEvent.getByLabel("OtherThing", "testUserTag", hOThings);
0043     std::cout << ">> other things found:" << hOThings->size() << std::endl;
0044 
0045     for (size_t i = 0; i < hOThings->size(); ++i) {
0046       const OtherThing& thing = (*hOThings)[i];
0047       h_refA->Fill(thing.ref->a);
0048       std::cout << ">> ref->a:  " << thing.ref->a << std::endl;
0049     }
0050 
0051     edm::Handle<ThingCollection> hThings;
0052     iEvent.getByLabel("Thing", hThings);
0053     const ThingCollection& things = *hThings;
0054     std::cout << ">> things found:" << things.size() << std::endl;
0055     for (size_t i = 0; i < things.size(); ++i) {
0056       const Thing& thing = things[i];
0057       h_a->Fill(thing.a);
0058       std::cout << ">> a:  " << thing.a << std::endl;
0059     }
0060   } catch (cms::Exception& x) {
0061     std::cout << std::endl << "Failed with cms::Exception: " << std::endl;
0062     std::cout << x.what() << std::endl;
0063     abort();
0064   } catch (std::exception& x) {
0065     std::cout << std::endl << "Failed with std::exception" << std::endl;
0066     std::cout << x.what() << std::endl;
0067     abort();
0068   } catch (...) {
0069     std::cout << std::endl << "Failed with unknown exception" << std::endl;
0070     abort();
0071   }
0072 }
0073 
0074 void ThingsTSelector::postProcessing(TList&) {}
0075 
0076 void ThingsTSelector::terminate(TList& out) {
0077   std::cout << "terminate" << std::endl;
0078   TCanvas* canvas = new TCanvas();
0079   {
0080     TObject* hist = out.FindObject(kA);
0081     if (nullptr != hist) {
0082       hist->Draw();
0083       canvas->SaveAs("a.jpg");
0084     } else {
0085       std::cout << "no '" << kA << "' histogram" << std::endl;
0086     }
0087   }
0088   {
0089     TObject* hist = out.FindObject(kRefA);
0090     if (nullptr != hist) {
0091       hist->Draw();
0092       canvas->SaveAs("refA.jpg");
0093     } else {
0094       std::cout << "no '" << kRefA << "' histogram" << std::endl;
0095     }
0096   }
0097   delete canvas;
0098 }