Warning, /tutorial-jana2/_episodes/03-end-user-plugin.md is written in an unsupported language. File is not indexed.
0001 ---
0002 title: "Creating a plugin to make custom histograms/trees"
0003 teaching: 15
0004 exercises: 20
0005 questions:
0006 - "Why should a I make a custom plugin?"
0007 - "How do I create a custom plugin?"
0008 objectives:
0009 - "Understand when one should make a plugin and when they should just use a ROOT macro."
0010 - "Understand how to create a new, stand-alone plugin with your own custom histograms."
0011 keypoints:
0012 - "Plugins can be used to generate custom histograms by attaching directly to the reconstruction process."
0013 - "Plugins can be used for monitoring or custom analysis."
0014 ---
0015
0016 ~~~
0017 Note: The following episode presents a somewhat outdated view, and some commands may not function.
0018 If you are only interested in analyzing already-reconstructed data, then there is no requirement
0019 to use a plugin as described below; **just analyze output ROOT file directly instead**.
0020 ~~~
0021 {: .error}
0022
0023 Plugins are the basic building blocks when it comes to analyzing data. They request objects and perform actions, such as making histograms, or writing out certain objects to files. When your plugin requests objects (e.g. clusters) the factory responsible for the requested object is loaded and run (We will dive into factories in the next exciting episode of how to use JANA). When running EICrecon you will configure it to use some number of plugins (each potentially with their own set of configuration parameters). Now, let us begin constructing a new plugin.
0024
0025 To do this we will use the eicmkplugin.py script that comes with EICrecon. This utility should be your "go-to" for jumpstarting your work with EICrecon/JANA when it comes to data. To put eicmkplugin.py in your path, you can do the following:
0026 ~~~
0027 source EICrecon/bin/eicrecon-this.sh
0028 ~~~
0029
0030 The eickmkplugin script can be called simply by typing: "eicmkplugin.py" followed by the name of the plugin. Let's begin by calling:
0031 ~~~
0032 eicmkplugin.py myFirstPlugin
0033 ~~~
0034
0035 You should now have terminal output that looks like this:
0036 ~~~
0037 Writing myFirstPlugin/CMakeLists.txt ...
0038 Writing myFirstPlugin/myFirstPluginProcessor.h ...
0039 Writing myFirstPlugin/myFirstPluginProcessor.cc ...
0040
0041 Created plugin myFirstPlugin.
0042 Build with:
0043
0044 cmake -S myFirstPlugin -B myFirstPlugin/build
0045 cmake --build myFirstPlugin/build --target install
0046 ~~~
0047 There should now exist a new folder labeled "myPlugin". That directory contains 2 files: a CMakelists.txt file (needed for compiling our new plugin) and the source code for the plugin itself.
0048
0049 Inside the source code for your plugin is a fairly simple class. The private data members should contain the necessary variables to successfully run your plugin; this will likely include any histograms, canvases, fits or other accoutrement. The public section contains the required Constructor, Init, Process, and Finish functions. In init we get the application, as well as initialize any variables or histograms, etc. The Process function typically gets objects from the event and does something with them (e.g. fill the histogram of cluster energy). And finally Finish is called where we clean up and do final things before ending the run of our plugin.
0050
0051 Before we compile our plugins we need to tell JANA about where the plugins will be found. Start by setting your EICrecon_MY environment variable to a directory where you have write permission. The build instructions will install the plugin to that directory. When eicrecon is run, it will also look for plugins in the $EICrecon_MY directory and the EICrecon build you are using. This step is easy to overlook but necessary for the plugin to be found once compiled. Let's do this now before we forget:
0052
0053 ~~~
0054 mkdir EICrecon_MY
0055 export EICrecon_MY=${PWD}/EICrecon_MY
0056 ~~~
0057
0058 To compile your plugin, let's follow the guidance given and type:
0059 ~~~
0060 cmake -S myFirstPlugin -B myFirstPlugin/build
0061 cmake --build myFirstPlugin/build --target install
0062 ~~~
0063
0064 You can test plugin installed and can load correctly by runnign eicrecon with it:
0065 ~~~
0066 eicrecon -Pplugins=myFirstPlugin,JTest -Pjana:nevents=10
0067 ~~~
0068
0069 The second plugin, JTest, just supplies dummy events, ensuring your plugin is properly compiled and found. To generate your first histograms, let's edit the myFirstPluginProcessor.cc and myFirstPluginProcessor.h files (located in the myFirstPlugin directory). Start by modifying myFirstPluginProcessor.h. In the end it should look similar to the one below:
0070
0071 ```c++
0072 #include <JANA/JEventProcessorSequentialRoot.h>
0073 #include <TH2D.h>
0074 #include <TFile.h>
0075
0076 #include <edm4hep/SimCalorimeterHit.h>
0077
0078 class myFirstPluginProcessor: public JEventProcessorSequentialRoot {
0079 private:
0080
0081 // Data objects we will need from JANA e.g.
0082 PrefetchT<edm4hep::SimCalorimeterHit> rawhits = {this, "EcalBarrelHits"};
0083
0084 // Declare histogram and tree pointers here. e.g.
0085 TH1D* hEraw = nullptr;
0086
0087 public:
0088 myFirstPluginProcessor() { SetTypeName(NAME_OF_THIS); }
0089
0090 void InitWithGlobalRootLock() override;
0091 void ProcessSequential(const std::shared_ptr<const JEvent>& event) override;
0092 void FinishWithGlobalRootLock() override;
0093 };
0094 ```
0095 Next, edit the myFirstPluginProcessor.cc file to the following:
0096 ```c++
0097 #include "myFirstPluginProcessor.h"
0098 #include <services/rootfile/RootFile_service.h>
0099
0100 // The following just makes this a JANA plugin
0101 extern "C" {
0102 void InitPlugin(JApplication *app) {
0103 InitJANAPlugin(app);
0104 app->Add(new myFirstPluginProcessor);
0105 }
0106 }
0107
0108 //-------------------------------------------
0109 // InitWithGlobalRootLock
0110 //-------------------------------------------
0111 void myFirstPluginProcessor::InitWithGlobalRootLock(){
0112
0113 // This ensures the histograms created here show up in the same root file as
0114 // other plugins attached to the process. Place them in dedicated directory
0115 // to avoid name conflicts.
0116 auto rootfile_svc = GetApplication()->GetService<RootFile_service>();
0117 auto rootfile = rootfile_svc->GetHistFile();
0118 rootfile->mkdir("myFirstPlugin")->cd();
0119
0120 hEraw = new TH1D("Eraw", "BEMC hit energy (raw)", 100, 0, 0.075);
0121 }
0122
0123 //-------------------------------------------
0124 // ProcessSequential
0125 //-------------------------------------------
0126 void myFirstPluginProcessor::ProcessSequential(const std::shared_ptr<const JEvent>& event) {
0127
0128 for( auto hit : rawhits() ) hEraw->Fill( hit->getEnergy() );
0129 }
0130
0131 //-------------------------------------------
0132 // FinishWithGlobalRootLock
0133 //-------------------------------------------
0134 void myFirstPluginProcessor::FinishWithGlobalRootLock() {
0135
0136 // Do any final calculations here.
0137 }
0138 ```
0139 Before we continue, stop for a moment and remember that plugins are compiled objects. Thus it is imperative we rebuild our plugin after making any changes. To do this, we can simply run the same commands we used to build the plugin in the first place:
0140
0141 ~~~
0142 cmake -S myFirstPlugin -B myFirstPlugin/build
0143 cmake --build myFirstPlugin/build --target install
0144 ~~~
0145
0146 You can test the plugin using the following simulated data file:
0147
0148 ~~~bash
0149 wget https://eicaidata.s3.amazonaws.com/2022-09-26_ncdis10x100_minq2-1_200ev.edm4hep.root
0150 eicrecon -Pplugins=myFirstPlugin 2022-09-26_ncdis10x100_minq2-1_200ev.edm4hep.root
0151 ~~~
0152
0153 You should now have a root file, eicrecon.root, with a single directory: "myFirstPlugin" containing the resulting hEraw histogram.
0154
0155
0156 _____________________________________________________________________________________________________________
0157
0158 As exercises try (make sure you rebuild everytime you change your plugin):
0159
0160 1) Plot the X,Y positions of all the hits.
0161
0162 2) Repeat only for hits with energy greater than 0.005 GeV.
0163
0164 3) Try to plot similar histograms from the EcalEndcapN.
0165
0166 Feel free to play around with other objects and their properties (hint: when you ran eicrecon, you should have seen a list of all the objects that were available to you. You can also see this list by typing: eicrecon -Pplugins=myFirstPlugin -Pjana:nevents=0)
0167
0168 Note: very shortly you will be adding a factory. After you do come back to this plugin and access your newly created objects
0169
0170
0171 {% include links.md %}
0172