/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/root/buildCaliceCaloHits.cpp File Reference

#include <TRandom.h>
#include <TTree.h>
#include <TFile.h>
#include <TStyle.h>
#include <TKey.h>
#include <Riostream.h>
#include <sstream>
#include <string>
#include "mysql/Mysql.hh"
#include "MicroException.hh"
#include <iostream>
#include <fstream>
#include "tools/Arguments.hh"
#include "root/CaloHit.hh"
#include "root/CaloRun.hh"
#include "root/CaloEvent.hh"
#include "root/MTRun.hh"
#include "root/MTEvent.hh"
#include "root/MTChannel.hh"
#include "root/MTTrack.hh"
#include "root/MTChannelSoftId.hh"

Include dependency graph for buildCaliceCaloHits.cpp:

Go to the source code of this file.

Functions

void selectAndCopyEvent (const string &inputFileName, const string &outputFileName, UInt_t acceptation, UInt_t limit)
bool AddCaloHit (CaloEvent &caloEvent, MTChannel *channel)
void usage (void)
int main (int argc, char **argv)


Function Documentation

void selectAndCopyEvent ( const string &  inputFileName,
const string &  outputFileName,
UInt_t  acceptation,
UInt_t  limit 
)

Definition at line 75 of file buildCaliceCaloHits.cpp.

00076 {
00077  
00078   TFile readFile(inputFileName.c_str(),"READONLY");
00079   TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00080 
00081 
00082 
00083   TIter nextkey(readFile.GetListOfKeys());
00084   TKey *key = NULL;
00085   CaloRun run;
00086   ui32 skipCandidate = 0;
00087   ui32 nbHits = 0;
00088 
00089   UInt_t eventId = 0;
00090   while (key = (TKey*)nextkey()) 
00091   {
00092     //Get tree to read
00093     TTree *readTree = (TTree*)key->ReadObj();
00094     MTEvent *readEvt =  new MTEvent();
00095     TBranch *branch= readTree->GetBranch("MTEvent");
00096     branch->SetAddress(&readEvt);
00097 
00098     //Get RunId
00099     MTRun * readRun  = (MTRun*)readTree->GetUserInfo()->FindObject("MTRun");
00100     int runId = readRun->GetRunId();
00101     cout << "read MTRun id[ " << readRun->GetRunId() << "]" << endl;
00102     float offset_x = 0;
00103     float offset_y = 0;
00104 
00105     // Create a new TTree which will contain  filtered events
00106     stringstream label;
00107     label<< inputFileName << "_" << runId ; 
00108     TTree tree(label.str().c_str(),"Calo tree");
00109     cout << "work on :" << label.str() << endl;
00110     tree.SetMaxTreeSize(40000000000); // 40 GB
00111 
00112 
00113 
00114     CaloEvent *evt = new CaloEvent();
00115     // create a branch with run 
00116     tree.Branch("CaloEvent",&evt,16000,2);
00117     run.SetRunId(runId);
00118     cout << "write CaloRun id[ " << run.GetRunId() << "]" << endl;
00119 
00120     // Loop over event in current input TTree
00121     int nbEntries = readTree->GetEntries();
00122     ui16 evtNum = 0;
00123 
00124     if ( limit == 0 ) { limit = nbEntries; }
00125     for ( evtNum = 0; evtNum <  limit ; evtNum++)
00126     {
00127       UInt_t lastCandidate = 0;
00128   //cout << "----lastCandidate[" << lastCandidate << "]"  << endl;
00129 
00130       // Get input Event as readEvt
00131       readTree->GetEntry(evtNum);
00132 
00133 
00134       if ( readEvt->IsValid() == true  )
00135       {
00136         bool findCaloEvent = false;
00137         int nbChannel = readEvt->GetNchannel();
00138 
00139         // Une fois les hits tries par absolutute time et les dtCanditade tries:
00140         // 1/  on prend le 1er  dtCandidate
00141         // 2/  on boucle sur les hits d'un MTEvent.
00142         //      tant que le hit est a +ou- acceptation du dtCanditate, on ajoute
00143         //        le CalHit correspondant au CaloEvent en cours 
00144         //     Des qu'on trouve un hit en dehors de l acceptation, on enregistre en CaloEvent
00145         //     on passe sur le dtCanditate suivant
00146 
00147         bool endOfCandidate = false;
00148         const std::vector<UInt_t> &dtCanditade = readEvt->GetDtCandidate();
00149         if ( dtCanditade.size() > 0 )
00150         {
00151           vector<UInt_t>::const_iterator iter =  dtCanditade.begin() ; 
00152           UInt_t candidate = *iter;
00153           lastCandidate = candidate;
00154   //cout << "----Candidate[" << candidate << "]"  << endl;
00155   //cout << "----lastCandidate[" << lastCandidate << "]"  << endl;
00156 
00157 
00158           for(int i=0;i<nbChannel && endOfCandidate == false ;i++)
00159           {
00160             MTChannel* channel = (MTChannel*)readEvt->GetChannels()->UncheckedAt(i);
00161             Long64_t dt = channel->GetBcIdDif() - channel->GetBcIdHit();
00162   //cout << "Channel[" << i << "/" << nbChannel <<  "], dt["<< dt << "] bcIdAbs[" << channel->GetBcIdAbs() << "]" <<  endl; 
00163 
00164             if (dt > 0 && fabs(dt-candidate) <= acceptation)
00165             { 
00166               AddCaloHit(*evt,channel);
00167               nbHits++;
00168               findCaloEvent = true;
00169   //cout << "Add CaloHit" << endl;
00170             }
00171             if (  i == nbChannel-1 )
00172             { // last channel
00173               evt->SetEventId(eventId++);
00174               evt->SetDeltaTmax(candidate);
00175               evt->SetBcIdAbs(channel->GetBcIdAbs());
00176   //cout <<  " lst event channel set CalEvent[" << eventId  << "]" << endl;
00177               tree.Fill();
00178               evt->Clear();
00179               findCaloEvent = false;
00180             }
00181             else if ( (fabs(dt-candidate) > acceptation && findCaloEvent == true) ) 
00182             {
00183               evt->SetEventId(eventId++);
00184               evt->SetDeltaTmax(candidate);
00185               evt->SetBcIdAbs(channel->GetBcIdAbs());
00186   //cout <<  "set CalEvent[" << eventId <<  "]  candidate[" << candidate  << "]" << endl;
00187 
00188               tree.Fill();
00189               evt->Clear();
00190               findCaloEvent = false;
00191               iter++;
00192               if ( iter == dtCanditade.end() ) { endOfCandidate = true; }
00193               while ( iter != dtCanditade.end()  &&  *iter <= (lastCandidate + acceptation  ) ) 
00194               {
00195   //cout << "----Skip Candidate[" << *iter << "] <=  (lastCandidate + acceptation )[" << (lastCandidate +  acceptation)  <<  "]"  << endl;
00196 
00197                  skipCandidate++;
00198                  iter++;
00199                  if ( iter == dtCanditade.end() ) { endOfCandidate = true; }
00200               }
00201               if ( ! endOfCandidate )
00202               {
00203                 candidate = *iter;
00204                 lastCandidate = candidate;
00205   //cout << "----Candidate[" << candidate << "]"  << endl;
00206   //cout << "----lastCandidate[" << lastCandidate << "]"  << endl;
00207                 i--;  // on premet au channel courant d'etre ajoute au dt candidat suivant
00208               }
00209             } 
00210             
00211           } // end loop MTChannel
00212         }
00213       }
00214       cout << "Done  entry " << evtNum + 1 << " / " << nbEntries << "\r" <<  endl; // flush;;
00215     }
00216 
00217 
00218     tree.GetUserInfo()->Add(&run); 
00219     TFile *curFile =tree.GetCurrentFile() ;
00220     //curFile->Write("", TObject::kOverwrite);
00221     tree.Write("", TObject::kOverwrite);
00222     cout << "Write TTree" <<endl;
00223     //tree.Write();
00224     delete readEvt;
00225     delete evt;
00226 
00227   }
00228   caloFile.Close();
00229   readFile.Close();
00230   cout << "Done: " << eventId << " events reconstructed." <<  endl;
00231   cout << "nb skipped events "  << skipCandidate <<  endl;
00232   cout << "nb hits "  << nbHits <<  endl;
00233 }

bool AddCaloHit ( CaloEvent caloEvent,
MTChannel channel 
)

Definition at line 238 of file buildCaliceCaloHits.cpp.

Referenced by selectAndCopyEvent().

00239 {
00240   //loop over hits
00241 //  int nchannel = evt.GetNchannel();
00242 
00243 //  for (int j=0;j<nchannel;j++)
00244 //  {
00245 //    MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00246 
00247     Long64_t dt = channel->GetBcIdDif() - channel->GetBcIdHit();
00248 
00249     //if ( dt > 0 && fabs(dt-dtCanditade) <= acceptation)
00250     //{
00251       int digit = channel->GetDigitalValue();
00252       int xpos = (int)channel->GetX();
00253       int ypos = (int)channel->GetY();
00254       int zpos = (int)channel->GetZ();
00255       int layer = channel->GetChamberId();
00256 
00257       const MTChannelSoftId &soft = channel->GetSoftId();
00258 
00259       CaloHit hit;
00260 
00261       hit.SetX(xpos);
00262       hit.SetY(ypos);
00263       hit.SetZ(zpos);
00264       hit.SetRowInChamber(channel->GetRowInChamber());
00265       hit.SetColInChamber(channel->GetColInChamber());
00266       hit.SetDeltaT(dt);
00267       hit.SetThreshold(digit);
00268       hit.SetLayer(layer);
00269       hit.SetSoftId(soft);
00270 
00271 
00272       caloEvent.AddHit(hit);
00273     //}
00274  // }
00275   return true;
00276 }

void usage ( void   ) 

Definition at line 33 of file buildCaliceCaloHits.cpp.

00034 {
00035     cout  << "     : buildCaloHits inputfilename outputfileName acceptation -l eventNum" << endl;
00036 }

int main ( int  argc,
char **  argv 
)

Definition at line 38 of file buildCaliceCaloHits.cpp.

00038                                 {
00039 
00040   
00041   Arguments arg(argc,argv,"buildCaloHits inputfilename outputfileName acceptation -l eventNum");
00042   if (arg.getNbOfArgs() != 3 || arg.getNbOfOptions() > 1 )
00043   {
00044     arg.usage();
00045     return -1;
00046   }
00047 
00048   // Get input file name in a input file
00049   string outputFileName = arg.getArgument(2);
00050 
00051   // RECREATE Root file to clear its contains if it alread exist
00052   TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00053   caloFile.Write();
00054   caloFile.Close();
00055 
00056   string inputRootFile = arg.getArgument(1);
00057   cout<<"Read ROOT file : "<<inputRootFile<<endl;
00058   UInt_t acceptation = atoi(arg.getArgument(3).c_str());
00059 
00060   unsigned int limit = 0;
00061   if ( arg.getOption("-l").size() != 0 )
00062   {
00063     limit = atoi(arg.getOption("-l").c_str());
00064   } 
00065 
00066   cout << inputRootFile << endl;
00067   cout << outputFileName << endl;
00068 
00069   selectAndCopyEvent(inputRootFile,outputFileName,acceptation,limit);
00070   return 0;
00071 }


Generated on Mon Jan 7 13:17:09 2013 for MicromegasFramework by  doxygen 1.4.7