/[PAMELA software]/yoda/event/PamelaRun.cpp
ViewVC logotype

Diff of /yoda/event/PamelaRun.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4.1 by kusanagi, Sat May 28 08:30:01 2005 UTC revision 6.5 by kusanagi, Tue May 30 19:10:02 2006 UTC
# Line 1  Line 1 
1  /** @file  /** @file
2   * $Author: kusanagi $   * $Author: kusanagi $
3   * $Date: 2005/03/06 04:33:01 $   * $Date: 2006/05/30 19:10:01 $
4   * $Revision: 4.0 $   * $Revision: 6.4 $
5   *   *
6   * Implementation of the PamelaRun class.   * Implementation of the PamelaRun class.
7   */   */
# Line 9  Line 9 
9  #include <iomanip>  #include <iomanip>
10  #include <list>  #include <list>
11  #include <exception>  #include <exception>
 //#include <string>  
12  #include <log4cxx/logger.h>  #include <log4cxx/logger.h>
13    
14  #include <TFile.h>  //Substituted by Maurizio 05 Feb 2004  #include <TFile.h>  //Substituted by Maurizio 05 Feb 2004
# Line 52  std::string PamelaRun::GetRunName(int ru Line 51  std::string PamelaRun::GetRunName(int ru
51   * @param int run - Number of the run to check   * @param int run - Number of the run to check
52   * @return int   * @return int
53   */   */
 /*int PamelaRun::RunExists(int run) throw (std::exception){  
  int res;  
  DIR *dirp;  
  std::string pathDir((char*)getenv("YODA_DATA"));  
  pathDir = pathDir + "/";  
  pathDir = pathDir + GetRunName(run);  
 // Open the directory  
 if ((dirp = opendir(pathDir.c_str())) == NULL) {  
 res = 0;  
 } else {  
 closedir(dirp);  
 res = 1;  
 }  
 return res;  
 }*/  
   
   
 /**  
  * Check if the run number is already assigned  
  * @param int run - Number of the run to check  
  * @return int  
  */  
54   void PamelaRun::RunExists(std::string input) throw (std::exception){   void PamelaRun::RunExists(std::string input) throw (std::exception){
55   int res;   int res;
56   DIR *dirp;   DIR *dirp;
57   std::string fileName;   std::string fileName;
58   std::string pathDir((char*)getenv("YODA_DATA"));   //std::string pathDir((char*)getenv("YODA_DATA"));
59     std::string pathDir(GetPath());
60   std::string tempName;   std::string tempName;
61   std::string::size_type pos;   std::string::size_type pos;
62    
63   pos = input.find_last_of("/");   pos = input.find_last_of("/");
  //if ((pos == std::string::npos) && !input.empty()) ;//throw Exception("Directories not allowed....");  
64   fileName = input.substr(pos+1);   fileName = input.substr(pos+1);
65   pos = fileName.find_last_of(".");   pos = fileName.find_last_of(".");
66   fileName = fileName.substr(0,pos);   fileName = fileName.substr(0,pos);
67   pathDir = pathDir + "/";   pathDir = pathDir + "/";
68   tempName = pathDir + fileName + "00";   tempName = pathDir + fileName;
69   //oss << pathDir << fileName << std::setw( 2 ) << std::setfill( '0' );   oss.str("");
70   oss.str(tempName.c_str());   oss << Path << "/" << fileName;
71   int i = 0;   UnpackPath = oss.str();
  while (res){  
     //if ((dirp = opendir(tempName.c_str())) == NULL) {  
     if ((dirp = opendir(oss.str().c_str())) == NULL) {  
         res = 0;  
     } else {  
         closedir(dirp);  
         oss.str("");  
         oss << pathDir << fileName << std::setw( 2 ) << std::setfill( '0' ) << ++i;  
         res = 1;  
     }  
  }  
  oss.str("");  
  oss << Path << "/" << fileName << std::setw( 2 ) << std::setfill( '0' ) << i;  
  Path = oss.str();  
72   oss.str("");   oss.str("");
73   oss << fileName << std::setw( 2 ) << std::setfill( '0' ) << i;   oss << fileName;
74   Run = oss.str();   Run = oss.str();
 //return (oss.str());  
75  }  }
76    
77  /**  /**
# Line 117  return res; Line 79  return res;
79   * @param run Run number   * @param run Run number
80   * @param path Base path name to the data   * @param path Base path name to the data
81   */   */
82  PamelaRun::PamelaRun(std::string fileName, std::string path):  PamelaRun::PamelaRun(std::string fileName, std::string path, bool multiple, short compr):
83    Path(path),    Path(path),
84    Run(fileName),    Run(fileName),
85    RunNumber(1){ //veirificare se è possibilie eliminare questa variabile    RunNumber(1),//veirificare se è possibilie eliminare questa variabile
86      Multiple(multiple),
87      SingleFile(0),
88      compression(compr){
89    logger->debug(_T("Constructor"));    logger->debug(_T("Constructor"));
90    //Run = RunExists(fileName);    //Run = RunExists(fileName);
91    RunExists(fileName);    RunExists(fileName);
# Line 152  std::string PamelaRun::GetDirName(Packet Line 117  std::string PamelaRun::GetDirName(Packet
117   */   */
118  std::string PamelaRun::GetFileName(const SubPacket* type, std::string name) {  std::string PamelaRun::GetFileName(const SubPacket* type, std::string name) {
119    if (type->IsDetectorSpecific()) {    if (type->IsDetectorSpecific()) {
120      return Path + "/" + type->GetPacketType()->GetName() + "/"      return UnpackPath + "/" + type->GetPacketType()->GetName() + "/"
121        + Run + "." + type->GetPacketType()->GetName() + "."        + Run + "." + type->GetPacketType()->GetName() + "."
122        + type->GetSubDetector()->GetName() + "."        + type->GetSubDetector()->GetName() + "."
123        + name + ".root";        + name + ".root";
124    } else {    } else {
125      return Path + "/" + type->GetPacketType()->GetName() + "/"      return UnpackPath + "/" + type->GetPacketType()->GetName() + "/"
126        + Run + "." + type->GetPacketType()->GetName() + "."        + Run + "." + type->GetPacketType()->GetName() + "."
127        + name + ".root";        + name + ".root";
128     /*     /*
# Line 404  void PamelaRun::ReadSubPacket(const Algo Line 369  void PamelaRun::ReadSubPacket(const Algo
369  TTree* PamelaRun::CreateTTree(Algorithm* algo, const SubPacket* packet,  TTree* PamelaRun::CreateTTree(Algorithm* algo, const SubPacket* packet,
370                                std::string name)                                std::string name)
371    throw (std::exception) {    throw (std::exception) {
372    std::string FileName = GetFileName(packet, name);    std::string FileName = "";
373    std::string EventType = packet->GetPacketType()->GetName();    std::string EventType = packet->GetPacketType()->GetName();
374      TFile* File = 0;
375      std::string TreeName = GetTreeName(packet);
376      TTree *tree = 0;
377      FileName = GetFileName(packet, name);
378    CreateDirectoryStructure(FileName.c_str());    CreateDirectoryStructure(FileName.c_str());
379    TFile* File = new TFile(FileName.c_str(), "create");    File = new TFile(FileName.c_str(), "create");
380    File->SetCompressionLevel(5);    tree = new TTree(TreeName.c_str(), algo->GetAlgorithmName().c_str());
381      WritingRootTrees[packet->GetPacketType()].push_back(tree);
382      
383      File->SetCompressionLevel(compression);
384    if (!File->IsOpen()) {    if (!File->IsOpen()) {
385      logger->error("Could not open file " + FileName);      logger->error("Could not open file " + FileName);
386      //throw Exception("Could not open file " + FileName);      //throw Exception("Could not open file " + FileName);
387    }    }
   std::string TreeName = GetTreeName(packet);  
   TTree *tree = new TTree(TreeName.c_str(), algo->GetAlgorithmName().c_str());  
   WritingRootTrees[packet->GetPacketType()].push_back(tree);  
   tree->GetCurrentFile()->cd();  
   AlgorithmInfo ai(algo);  
   ai.Write();  
   info.Write();  
388    logger->debug("Creating file " + FileName + " with Tree " + TreeName);    logger->debug("Creating file " + FileName + " with Tree " + TreeName);
389    return tree;    return tree;
390  }  }
# Line 442  void PamelaRun::WriteSubPacket(Algorithm Line 407  void PamelaRun::WriteSubPacket(Algorithm
407    oss <<  "Register: " << name << " for " << algo->GetAlgorithmName() << " (writing)";    oss <<  "Register: " << name << " for " << algo->GetAlgorithmName() << " (writing)";
408    logger->debug(oss.str().c_str());    logger->debug(oss.str().c_str());
409    TTree* HeaderTree = ReadTTree(packet->GetPacketType());    TTree* HeaderTree = ReadTTree(packet->GetPacketType());
   TTree* tree = CreateTTree(algo, packet, name);  
   oss.str("");  
   oss <<  "Branch: " << name << " Class: " << c->GetName();  
   logger->debug(oss.str().c_str());  
   tree->Branch(name.c_str(), c->GetName(),  subpacket);  
   HeaderTree->AddFriend(tree, tree->GetName());  
   
410    std::string FullName = packet->GetPacketType()->GetName() + "." + name;    std::string FullName = packet->GetPacketType()->GetName() + "." + name;
411    SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));    if (Multiple) {
412            TTree* tree = CreateTTree(algo, packet, name);
413            oss.str("");
414            oss <<  "Branch: " << name << " Class: " << c->GetName();
415            logger->debug(oss.str().c_str());
416            tree->Branch(name.c_str(), c->GetName(),  subpacket);
417            HeaderTree->AddFriend(tree, tree->GetName());
418            SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
419      } else {
420            std::string nameBranch(name);
421            HeaderTree->Branch(nameBranch.c_str(), c->GetName(),  subpacket);
422            SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
423      }
424  }  }
425    
426  /**  /**
# Line 469  void PamelaRun::WriteSubPacket(Algorithm Line 439  void PamelaRun::WriteSubPacket(Algorithm
439  }  }
440    
441  /**  /**
  * Write the header packet of a specified packet type. This is mainly used  
  * for the raw reader to create the base for the event structure.  
  * @param algo Algorithm that produces this SubPacket.  
  * @param subpacket A pointer to the pointer of the packet.  
  * @param type The packet type.  
  */  
 void PamelaRun::WriteHeader(Algorithm* algo, EventHeader** subpacket,  
                             const PacketType* type) {  
   EventHeader header(type);  
   std::string FileName = GetFileName(&header, GetDefaultBranchName(&header));  
   std::string EventType = (&header)->GetPacketType()->GetName();  
   CreateDirectoryStructure(FileName.c_str());  
   TFile* File = new TFile(FileName.c_str(), "create");  
   if (!File->IsOpen()) {  
     logger->error("Could not open file " + FileName);  
     //throw Exception("Could not open file " + FileName);  
   }  
   //std::string TreeName = GetTreeName(packet);  
   TTree *tree = new TTree("Pscu", algo->GetAlgorithmName().c_str());  
   WritingRootTrees[(&header)->GetPacketType()].push_back(tree);  
   tree->GetCurrentFile()->cd();  
   AlgorithmInfo ai(algo);  
   ai.Write();  
   info.Write();  
   std::stringstream temp;  
   //temp.str("");  
   //temp << "Created file" << FileName.c_str() << " with Tree Pscu" ;  
   //logger->debug(temp.str());  
   tree->Branch(GetDefaultBranchName(&header).c_str(),  
                (*subpacket)->Class()->GetName(), subpacket);  
   TTreeMap.insert(RootTreeMap::value_type(type, tree));  
 }  
   
   
 /**  
442   * Write the header packet to all ROOT files in the tree. Intended to   * Write the header packet to all ROOT files in the tree. Intended to
443   * be used for raw data readers that create the initial event structure.   * be used for raw data readers that create the initial event structure.
444   * @param algo Algorithm that produces this SubPacket.   * @param algo Algorithm that produces this SubPacket.
445   * @param subpacket A pointer to the pointer of the header packet.   * @param subpacket A pointer to the pointer of the header packet.
446   */   */
447  void PamelaRun::WriteHeaders(Algorithm* algo, EventHeader** subpacket) {  void PamelaRun::WriteHeaders(Algorithm* algo, EventHeader** subpacket) {
   
448      WriteHeader(algo, subpacket, PacketType::PhysEndRun);      WriteHeader(algo, subpacket, PacketType::PhysEndRun);
449      WriteHeader(algo, subpacket, PacketType::CalibCalPulse1);      WriteHeader(algo, subpacket, PacketType::CalibCalPulse1);
450      WriteHeader(algo, subpacket, PacketType::CalibCalPulse2);      WriteHeader(algo, subpacket, PacketType::CalibCalPulse2);
451      WriteHeader(algo, subpacket, PacketType::Physics);      WriteHeader(algo, subpacket, PacketType::Physics);
452        WriteHeader(algo, subpacket, PacketType::CalibTrkBoth);
453      WriteHeader(algo, subpacket, PacketType::CalibTrk1);      WriteHeader(algo, subpacket, PacketType::CalibTrk1);
454      WriteHeader(algo, subpacket, PacketType::CalibTrk2);      WriteHeader(algo, subpacket, PacketType::CalibTrk2);
455      WriteHeader(algo, subpacket, PacketType::CalibTof);      WriteHeader(algo, subpacket, PacketType::CalibTof);
# Line 531  void PamelaRun::WriteHeaders(Algorithm* Line 466  void PamelaRun::WriteHeaders(Algorithm*
466      WriteHeader(algo, subpacket, PacketType::InitHeader);      WriteHeader(algo, subpacket, PacketType::InitHeader);
467      WriteHeader(algo, subpacket, PacketType::InitTrailer);      WriteHeader(algo, subpacket, PacketType::InitTrailer);
468      WriteHeader(algo, subpacket, PacketType::EventTrk);      WriteHeader(algo, subpacket, PacketType::EventTrk);
     WriteHeader(algo, subpacket, PacketType::TestTrk);  
     WriteHeader(algo, subpacket, PacketType::TestTof);  
469      WriteHeader(algo, subpacket, PacketType::Log);      WriteHeader(algo, subpacket, PacketType::Log);
470      WriteHeader(algo, subpacket, PacketType::VarDump);      WriteHeader(algo, subpacket, PacketType::VarDump);
471      WriteHeader(algo, subpacket, PacketType::ArrDump);      WriteHeader(algo, subpacket, PacketType::ArrDump);
# Line 549  void PamelaRun::WriteHeaders(Algorithm* Line 482  void PamelaRun::WriteHeaders(Algorithm*
482      WriteHeader(algo, subpacket, PacketType::S4Init);          WriteHeader(algo, subpacket, PacketType::S4Init);    
483      WriteHeader(algo, subpacket, PacketType::Ac2Init);      WriteHeader(algo, subpacket, PacketType::Ac2Init);
484      WriteHeader(algo, subpacket, PacketType::CalAlarm);          WriteHeader(algo, subpacket, PacketType::CalAlarm);    
485      WriteHeader(algo, subpacket, PacketType::AcAlarm);          WriteHeader(algo, subpacket, PacketType::Ac1Alarm);    
486      WriteHeader(algo, subpacket, PacketType::TrkAlarm);          WriteHeader(algo, subpacket, PacketType::TrkAlarm);    
487      WriteHeader(algo, subpacket, PacketType::TrgAlarm);          WriteHeader(algo, subpacket, PacketType::TrgAlarm);    
488      WriteHeader(algo, subpacket, PacketType::TofAlarm);      WriteHeader(algo, subpacket, PacketType::TofAlarm);
489      WriteHeader(algo, subpacket, PacketType::S4Alarm);      WriteHeader(algo, subpacket, PacketType::S4Alarm);
490        WriteHeader(algo, subpacket, PacketType::Ac2Alarm);    
491      WriteHeader(algo, subpacket, PacketType::TsbT);      WriteHeader(algo, subpacket, PacketType::TsbT);
492      WriteHeader(algo, subpacket, PacketType::TsbB);      WriteHeader(algo, subpacket, PacketType::TsbB);
493  }  }
# Line 574  void PamelaRun::WriteFiles(void) { Line 508  void PamelaRun::WriteFiles(void) {
508         i != WritingRootTrees.end(); i++) {         i != WritingRootTrees.end(); i++) {
509      for (TTreeList::iterator j = i->second.begin();      for (TTreeList::iterator j = i->second.begin();
510           j != i->second.end(); j++) {           j != i->second.end(); j++) {
511        (*j)->GetCurrentFile()->Write();        (*j)->GetCurrentFile()->Write(0,TObject::kOverwrite);
512      }      }
513    }    }
514  }  }
# Line 595  void PamelaRun::FillTrees(const PacketTy Line 529  void PamelaRun::FillTrees(const PacketTy
529  }  }
530    
531    
532    /**
533     * Write the header packet of a specified packet type. This is mainly used
534     * for the raw reader to create the base for the event structure.
535     * @param algo Algorithm that produces this SubPacket.
536     * @param subpacket A pointer to the pointer of the packet.
537     * @param type The packet type.
538     */
539    void PamelaRun::WriteHeader(Algorithm* algo, EventHeader** subpacket,
540                                const PacketType* type) {
541      EventHeader header(type);
542      std::string EventType = (&header)->GetPacketType()->GetName();
543    
544      std::string FileName = "";
545      TFile* File = 0;
546      TTree *tree = 0;
547      if ( Multiple ){
548            FileName = GetFileName(&header, GetDefaultBranchName(&header));
549            CreateDirectoryStructure(FileName.c_str());
550            File = new TFile(FileName.c_str(), "create");
551            File->SetCompressionLevel(compression);
552            tree = new TTree("Pscu", algo->GetAlgorithmName().c_str());
553            WritingRootTrees[(&header)->GetPacketType()].push_back(tree);
554            tree->GetCurrentFile()->cd();
555            tree->Branch(GetDefaultBranchName(&header).c_str(),
556                   (*subpacket)->Class()->GetName(), subpacket);
557            TTreeMap.insert(RootTreeMap::value_type(type, tree));
558      } else {
559            if (!Multiple && (SingleFile == NULL)){
560                    //std::string pathDir((char*)getenv("YODA_DATA"));
561                    SingleFile = new TFile((GetPath() + "/" + Run + ".root").c_str(), "update");
562                    SingleFile->SetCompressionLevel(compression);
563            }
564            tree = new TTree(EventType.c_str(), algo->GetAlgorithmName().c_str());
565            WritingRootTrees[type].push_back(tree);
566            tree->GetCurrentFile()->cd();
567            tree->Branch(GetDefaultBranchName(&header).c_str(),
568                   (*subpacket)->Class()->GetName(), subpacket);
569            TTreeMap.insert(RootTreeMap::value_type(type, tree));
570      }
571    }

Legend:
Removed from v.4.1  
changed lines
  Added in v.6.5

  ViewVC Help
Powered by ViewVC 1.1.23