Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:15:17

0001 #include "TreeBuilder.hxx"
0002 #include "Particle.hxx"
0003 #include "Constants.hxx"
0004 #include "DEMPEvent.hxx"
0005 
0006 #include <iostream>
0007 #include <vector>
0008 #include <stdio.h>
0009 #include <string.h>
0010 #include "json/json.h"
0011 
0012 using namespace std;
0013 
0014 TreeBuilder::TreeBuilder(const char * file_name, const char * name) {
0015 
0016   extern Json::Value obj;
0017 
0018   tree_name = name;
0019 
0020 //  string str_copy = "RootFiles/" + obj["file_name"].asString() + ".root";
0021 //  file_name = str_copy.c_str();
0022 //
0023 //  cout << obj["output_file"].asString() << endl;
0024 //  cout << file_name << endl;
0025 //  cout << file_name <<endl;
0026 
0027 
0028   File_Out = new TFile(file_name, "RECREATE");
0029   Tree_Out = new TTree(tree_name, tree_name);
0030 
0031 
0032   Tree_Out->SetAutoSave();
0033 
0034   nParticles = 0;
0035 
0036   nVars = 8;
0037 }
0038 
0039 void TreeBuilder::AddParticle(Particle * p)
0040 {
0041   ParticleList.push_back(p);
0042   vector< double* >* TempVec = new vector<double*>(nVars);
0043   // Vector elements must be initialized to non-null
0044   // pointers
0045   for (int i = 0; i<nVars; i++)
0046     TempVec->at(i) = new double();
0047 
0048   PropList.push_back(TempVec);
0049 
0050   char * p_name = p->GetName();
0051   char b_name[100]; // Branch name
0052   char l_list[100]; // Leaf list
0053 
0054   strcpy(b_name, p_name);
0055   strcat(b_name, "_pid");
0056   strcpy(l_list, b_name);
0057   strcat(l_list, "/D");
0058   Tree_Out->Branch(b_name,TempVec->at(0),l_list);
0059 
0060   strcpy(b_name, p_name);
0061   strcat(b_name, "_Px");
0062   strcpy(l_list, b_name);
0063   strcat(l_list, "/D");
0064   Tree_Out->Branch(b_name,TempVec->at(1),l_list);
0065 
0066   strcpy(b_name, p_name);
0067   strcat(b_name, "_Py");
0068   strcpy(l_list, b_name);
0069   strcat(l_list, "/D");
0070   Tree_Out->Branch(b_name,TempVec->at(2),l_list);
0071 
0072   strcpy(b_name, p_name);
0073   strcat(b_name, "_Pz");
0074   strcpy(l_list, b_name);
0075   strcat(l_list, "/D");
0076   Tree_Out->Branch(b_name,TempVec->at(3),l_list);
0077 
0078 
0079   strcpy(b_name, p_name);
0080   strcat(b_name, "_E");
0081   strcpy(l_list, b_name);
0082   strcat(l_list, "/D");
0083   Tree_Out->Branch(b_name,TempVec->at(4),l_list);
0084 
0085   strcpy(b_name, p_name);
0086   strcat(b_name, "_theta");
0087   strcpy(l_list, b_name);
0088   strcat(l_list, "/D");
0089   Tree_Out->Branch(b_name,TempVec->at(5),l_list);
0090 
0091   strcpy(b_name, p_name);
0092   strcat(b_name, "_phi");
0093   strcpy(l_list, b_name);
0094   strcat(l_list, "/D");
0095   Tree_Out->Branch(b_name,TempVec->at(6),l_list);
0096 
0097   strcpy(b_name, p_name);
0098   strcat(b_name, "_P");
0099   strcpy(l_list, b_name);
0100   strcat(l_list, "/D");
0101   Tree_Out->Branch(b_name,TempVec->at(7),l_list);
0102 
0103   nParticles++;
0104 
0105   //cout << ParticleList[0] << endl;
0106   //cout << TempVec->at(1) << endl;
0107   //cout << PropList.at(0)->at(1) << endl;
0108 }
0109 
0110 void TreeBuilder::Fill()
0111 {
0112   Retrieve();
0113   Tree_Out->Fill();
0114 }
0115 
0116 // Particles have private data members that must be retrieved
0117 // to be copied to an accessible address to be added to the
0118 // TTree.
0119 void TreeBuilder::Retrieve()
0120 {
0121   for (int i = 0; i < nParticles; i++){
0122     //cout << PropList.at(i)->at(1) <<endl;
0123     *PropList.at(i)->at(0) = (double)ParticleList.at(i)->GetPid();
0124     *PropList.at(i)->at(1) = ParticleList.at(i)->Px()/1000;
0125     *PropList.at(i)->at(2) = ParticleList.at(i)->Py()/1000;
0126     *PropList.at(i)->at(3) = ParticleList.at(i)->Pz()/1000;
0127     *PropList.at(i)->at(4) = ParticleList.at(i)->E()/1000;
0128     *PropList.at(i)->at(5) = ParticleList.at(i)->Theta()*constants::DEG;
0129     *PropList.at(i)->at(6) = ParticleList.at(i)->Phi()*constants::DEG;
0130     *PropList.at(i)->at(7) = ParticleList.at(i)->P()/1000;
0131     //cout <<  &PropList.at(i)->at(1) << endl;
0132   }
0133 }
0134 
0135 void TreeBuilder::Save()
0136 {
0137   Tree_Out->AutoSave();
0138   //File_Out->Write();
0139 }
0140 
0141 void TreeBuilder::AddDouble(double* x, const char* name)
0142 {
0143   char b_name[100];
0144   char l_list[100];
0145 
0146   strcpy(b_name, name);
0147   strcpy(l_list, name);
0148   strcat(l_list,"/D");
0149 
0150   Tree_Out->Branch(b_name, x, l_list);
0151 
0152 }
0153 
0154 void TreeBuilder::AddEvent(DEMPEvent * event)
0155 {
0156   this->AddParticle(event->BeamElec);
0157   this->AddParticle(event->ProdPion);
0158   this->AddParticle(event->ProdProt);
0159   this->AddParticle(event->ScatElec);
0160   this->AddParticle(event->TargNeut);
0161   //this->AddParticle(event->VirtPhot);
0162 
0163   /*
0164   this->AddDouble(event->qsq_GeV,"qsq_GeV");
0165   this->AddDouble(event->w_GeV,"w_GeV");
0166   this->AddDouble(event->t_GeV,"t_GeV");
0167   this->AddDouble(event->t_prime_GeV, "t_prime_GeV");
0168   this->AddDouble(event->x_B, "x_B");
0169   this->AddDouble(event->negt, "negt");
0170   
0171   this->AddDouble(event->Vertex_x,"Vertex_x");
0172   this->AddDouble(event->Vertex_y,"Vertex_y");
0173   this->AddDouble(event->Vertex_z,"Vertex_z");
0174   */
0175 }