file ExClusterHistograms.cpp ============================ // ----------------------------------------------------------------------------- // Example program to fill histograms in ORCA and write them to file. // ----------------------------------------------------------------------------- // $Date: 2000/06/22 20:10:03 $ // $Revision: 1.1 $ // ----------------------------------------------------------------------------- // Author: Stephan Wynhoff - RWTH-Aachen (Email: Stephan.Wynhoff@cern.ch) // ----------------------------------------------------------------------------- // // In his/her code the user has to provide // - the analysis code which reads information of the reconstructed event // like tracks, clusters, jets, etc.. Here the information can be printed // and/or filled into histograms. (class EcalClusterInfo) // - the definition of the needed histograms and the methods (C++ for // subroutines/functions) to fill them. (class EcalHistos) // You can have more than one analysis class each using one or more histogram // classes if you want to. The histograms of different classes will however be // saved in different files. // ----------------------------------------------------------------------------- // Should be the first include in EVERY .cc/.cpp file! #include "Utilities/Configuration/interface/Architecture.h" // Needed in every program #include "Utilities/Notification/interface/PackageInitializer.h" // here our analysis class is defined #include "Examples/ExCalorimetry/interface/ExClusterInfo.h" // the new (ORCA 4) generic way to register our analysis class to CARF PKBuilder mybuilder("ExClusterInfoBuilder"); file ExClusterInfo.h ==================== // always protect your include files from beeing include more than one time #ifndef EXCLUSTERINFO_H #define EXCLUSTERINFO_H /** \file Examples/Histograms/interface/ExClusterInfo.h * Histogram class used by class ExClusterInfo. */ // The analysis class is an observer. Observers get notified when a new // event arrives. #include "Utilities/Notification/interface/Observer.h" // If a only a pointer to a class is needed and if none of their methods // is called we can forward-declare the class. Then the preprocessor does // not need to open the corresponding header file and compiling is faster. class G3EventProxy; class ExClusterHistos; /** An example analysis class filling calorimetry information to histograms. * This class retrieves clusters from the calorimeters and fill some * information about them into histograms using the EcalHistos class. * The class inherits from ActiveObserver. When a new event is read by ORCA * the private method "upDate" is called for all instantiated observers. * \author Stephan Wynhoff, CERN */ class ExClusterInfo : private ActiveObserver { public: /// default constructor ExClusterInfo(); /// default destructor ~ExClusterInfo(); /// this method will do the user analysis void myAnalysis(G3EventProxy * ev); private: /// don't change the name "upDate" - this method is mandatory. void upDate(G3EventProxy * ev) { if (ev!=0) myAnalysis(ev);} private: unsigned int eventsAnalysed; //!< just to count events that have been analysed unsigned int runsAnalysed; //!< count the runs unsigned int lastrun; //!< number of the last run analysed ExClusterHistos * UserHists; //!< pointer to the Histogram class }; #endif file ExClusterInfo.cc ====================== // should be the first include in EVERY .cc/.cpp file! #include "Utilities/Configuration/interface/Architecture.h" #include "CARF/G3Event/interface/G3EventProxy.h" // we will iterate over CaloClusters #include "CARF/Reco/interface/RecItr.h" #include "Calorimetry/CaloCluster/interface/CaloCluster.h" // the class definition for this example #include "Examples/ExCalorimetry/interface/ExClusterInfo.h" // In ExClusterHistos.h and ExClusterHistos.cc the private // histogram class is defined #include "Examples/ExCalorimetry/interface/ExClusterHistos.h" // ----------------------------------------------------------------------------- // In the constructor we can initialize. It is automatically called when an // instance of the class is created. // ----------------------------------------------------------------------------- ExClusterInfo::ExClusterInfo() { cout << "===========================================================" << endl; cout << "=== Start create new ExClusterInfo ===" << endl; eventsAnalysed = 0; runsAnalysed = 0; lastrun = 0; // Instantiate the user histogram (capitals in filenames are ignored by ZEBRA) UserHists = new ExClusterHistos("exhistogram.hbook"); // UserHists = new ExClusterHistos(); // an alternative, // default filename is calohistos.hbook cout << "=== Done create new ExClusterInfo ===" << endl; cout << "===========================================================" << endl; } // ----------------------------------------------------------------------------- // In the destructor we can print some summary or even fill some histogram // with results calculated. It is automatically called when an instance of // is deleted. // ----------------------------------------------------------------------------- ExClusterInfo::~ExClusterInfo() { cout << "===========================================================" << endl; cout << "=== Start delete ExClusterInfo ===" << endl; cout << " Number of events analysed: " << eventsAnalysed << endl; cout << " Number of runs analysed: " << runsAnalysed << endl; UserHists->FillEventStat(eventsAnalysed,runsAnalysed); // Cleanup the Histograms - here they will be saved to file delete UserHists; cout << "=== Done delete ExClusterInfo ===" << endl; cout << "===========================================================" << endl; } // ----------------------------------------------------------------------------- // This is an example method to do the actual analysis. It gets a pointer // to an event as argument. // ----------------------------------------------------------------------------- void ExClusterInfo::myAnalysis(G3EventProxy * ev) { int nClusters=0; // just to count the clusters // We want to loop over all calorimetric clusters -> get an iterator. // The iterator needs to know about the event to use. // What kind of clusters we use is determined by the libraries specified in // the BuildFile, here: RecItr MyCluster(ev->recEvent(),"EcalFixedWindow_5x5"); // Just print the event number (see CARF/G3Event/interface/G3EventHeader.h) cout << "===========================================================" << endl; cout << "=== Private analysis of event #"<< ev->simTrigger()->id().eventInRun() << " in run #" << ev->simTrigger()->id().runNumber() << endl; eventsAnalysed++; // some statistics: count events and runs processed if (ev->simTrigger()->id().runNumber() != lastrun) { lastrun = (unsigned int) ev->simTrigger()->id().runNumber(); runsAnalysed++; } // Here is the loop over all clusters while (MyCluster.next()) { nClusters++; // count the clusters // Print some of the cluster properties // see Calorimetry/CaloCommon/interface/CaloCluster.h cout << "Cluster " << nClusters <<": E=" << MyCluster->Energy() << ", eta="<< MyCluster->Eta() << ", phi="<< MyCluster->Phi() << endl; // Fill them to (our) histograms. Defined in ExClusterHistos.h UserHists->FillEepCluster(MyCluster->Energy(), MyCluster->Eta(), MyCluster->Phi()); } // Fill (our) histogram for statistics UserHists->FillNCluster(nClusters); cout << "===========================================================" << endl; }; file ExClusterHistos.h ========================= // always protect your include files from beeing include more than one time #ifndef EXCLUSTERHISTOS_H #define EXCLUSTERHISTOS_H /** \file Examples/Histograms/interface/ExClusterHistos.h * Histogram class used by class ExClusterInfo. */ #include "Utilities/CHBook4/interface/HB4Histogrammer.h" #include "Utilities/CHBook4/interface/CHBookHisto.h" #include "Utilities/CHBook4/interface/CHBookHisto2.h" #include "Utilities/CHBook4/interface/CHBookHistoProf.h" #include // needed for the auto_ptr below // This is the class definition - we inherit from HB4Histogrammer // see ( Utilities/CHBOOK4/interface/..) /** This class demonstrates the use of 1-dimensional, 2-dimensional and * profile Histograms. It is used by ExClusterInfo */ class ExClusterHistos : private HB4Histogrammer { public: /** The default constructor with the default arguments. * Missing arguments will be added automatically with the value given here. */ ExClusterHistos(const char * filename = "calohistos.hbook", int lun=10) : HB4Histogrammer(filename, lun) { BaseInit(); } /// The destructor. Here the final cleanup is done. ~ExClusterHistos() { BaseEnd();} // Public methods to access the user histograms /** Fill histograms for the energy \a e , the pseudorapidity \a eta and * the azimuthal angle \a phi .*/ void FillEepCluster(float e, float eta, float phi); /** Fill histograms for the number of clusters \a n . */ void FillNCluster(int n); /** Fill histograms for statistics with the number of events * \a events and runs \a runs analysed. */ void FillEventStat(unsigned int events, unsigned int runs); private: // private methods both are mandatory virtual void book(); //!< book the histograms virtual void hend(); //!< save the histograms to file public: // public data is forbidden by CMS coding rules private: // Define some abbreviation. These are pointers to one and // two-dimensional histograms. Put these into the class. This // avoids conflicts in the global namespace, which you should not // pollute anyway. typedef auto_ptr HP1; typedef auto_ptr HP2; typedef auto_ptr HPP; private: // private data // Since they are private, histograms are accessable only via public methods HP1 hCalo1,hCalo1b, hCalo2, hCalo3, hCalo4, hGlobal; HP2 hCalo5; HPP hCalo6; }; #endif file ExClusterHistos.cc ========================= // ----------------------------------------------------------------------------- // Example histogram class for calo clusters and simple statistics // - used by ExClusterInfo.cc // ----------------------------------------------------------------------------- // $Date: 2000/06/22 20:10:03 $ // $Revision: 1.1 $ // ----------------------------------------------------------------------------- // Author: Stephan Wynhoff - RWTH-Aachen // ----------------------------------------------------------------------------- #include "Utilities/Configuration/interface/Architecture.h" #include "Examples/ExCalorimetry/interface/ExClusterHistos.h" // In this method the user histograms are booked void ExClusterHistos::book() { cout << "===========================================================" << endl; cout << "=== Start booking user histograms ===" << endl; // ID Name Nbins Min Max hCalo1 = HP1(new CHBookHisto (100, "Calo:E(cluster) ", 100, 0., 100. )); hCalo1b = HP1(new CHBookHisto (101, "Calo:E(cluster) ", 50, 0., 500. )); hCalo2 = HP1(new CHBookHisto (200, "Calo:n cluster" , 101, -0.5, 100.5 )); hCalo3 = HP1(new CHBookHisto (300, "Calo:eta(cluster)", 100, -2.5, 2.5 )); hCalo4 = HP1(new CHBookHisto (400, "Calo:phi(cluster)", 100, 0., 3.15 )); hCalo5 = HP2(new CHBookHisto2(500, "Calo:eta_vs_phi (cluster)", 100, -2.5, 2.5, 100, 0., 3.15 )); hCalo6 = HPP(new CHBookHistoProf(600,"Calo",100,-2.5,2.5,0.,3.2, " ")); hGlobal = HP1(new CHBookHisto ( 1, "Events/Runs", 2, 0.5, 2.5 )); cout << "=== Done booking user histograms ===" << endl; cout << "===========================================================" << endl; } // Here one can print the histograms or manipulate them before // they are written to file. void ExClusterHistos::hend() { cout << "===========================================================" << endl; cout << "=== Start cleanup user histograms ===" << endl; hCalo1->print(); hCalo1b->print(); hCalo2->print(); cout << "=== Done cleanup user histograms ===" << endl; cout << "===========================================================" << endl; } // ---------------------------------------------------------------------------- // then follow the methods created by the user to fill the histograms // ---------------------------------------------------------------------------- // fill the histos with Energy, Pseudorapidity and Azimuthal angle void ExClusterHistos::FillEepCluster(float energy, float eta, float phi) { hCalo1->fill(energy); hCalo1b->fill(energy); hCalo3->fill(eta); hCalo4->fill(phi); hCalo5->fill(eta,phi); hCalo6->fill(eta,phi); } // fill the number of Ecal clusters void ExClusterHistos::FillNCluster(int ncl) { hCalo2->fill((float) ncl); } // fill the number of events and runs analysed void ExClusterHistos::FillEventStat(unsigned int events, unsigned int runs) { hGlobal->fill(1.,(float) events); hGlobal->fill(2.,(float) runs); }