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

Contents of /yoda/event/PamelaRun.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6.9 - (show annotations) (download)
Mon Oct 16 10:59:12 2006 UTC (18 years, 4 months ago) by mocchiut
Branch: MAIN
CVS Tags: YODA6_3/12
Changes since 6.8: +15 -3 lines
Added branch SoftInfo to store YODA software version in the data

1 /** @file
2 * $Author: pam-rm2 $
3 * $Date: 2006/08/23 14:39:44 $
4 * $Revision: 6.8 $
5 *
6 * Implementation of the PamelaRun class.
7 */
8 #include <iostream>
9 #include <iomanip>
10 #include <list>
11 #include <exception>
12 #include <log4cxx/logger.h>
13
14 #include <TFile.h> //Substituted by Maurizio 05 Feb 2004
15 #include <TTree.h> //Substituted by Maurizio 05 Feb 2004
16 #include <TChain.h> //Substituted by Maurizio 05 Feb 2004
17 #include <TKey.h> //Substituted by Maurizio 05 Feb 2004
18 #include <TList.h> //Substituted by Maurizio 05 Feb 2004
19
20
21 #include "PamelaRun.h"
22 #include "EventHeader.h"
23 #include "Algorithm.h"
24 #include "AlgorithmInfo.h"
25 #include "Exception.h"
26 #include <sys/stat.h>
27
28 extern "C" {
29 #include <dirent.h>
30 #include "DirectoryStructure.h"
31 }
32
33 extern int GetYODAver();
34
35 using namespace pamela;
36
37 static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.PamelaRun"));
38
39 /**
40 * Get the run name according to a certain run number.
41 * @param run Run number.
42 * @return a string with the run name.
43 */
44 std::string PamelaRun::GetRunName(int run) {
45 std::stringstream temp;
46 temp.str("");
47 temp << std::setw( 4 ) << std::setfill( '0' ) << run;
48 return "Run" + temp.str();
49 }
50
51 /**
52 * Check if the run number is already assigned
53 * @param int run - Number of the run to check
54 * @return int
55 */
56 void PamelaRun::RunExists(std::string input) throw (std::exception){
57 int res;
58 DIR *dirp;
59 std::string fileName;
60 //std::string pathDir((char*)getenv("YODA_DATA"));
61 std::string pathDir(GetPath());
62 std::string tempName;
63 std::string::size_type pos;
64
65 pos = input.find_last_of("/");
66 fileName = input.substr(pos+1);
67 pos = fileName.find_last_of(".");
68 fileName = fileName.substr(0,pos);
69 pathDir = pathDir + "/";
70 tempName = pathDir + fileName;
71 oss.str("");
72 oss << Path << "/" << fileName;
73 UnpackPath = oss.str();
74 oss.str("");
75 oss << fileName;
76 Run = oss.str();
77 }
78
79 /**
80 * Create a new Pamela run.
81 * @param run Run number
82 * @param path Base path name to the data
83 */
84 PamelaRun::PamelaRun(std::string fileName, std::string path, bool multiple, short compr):
85 Path(path),
86 Run(fileName),
87 RunNumber(1),//veirificare se รจ possibilie eliminare questa variabile
88 Multiple(multiple),
89 SingleFile(0),
90 compression(compr){
91 logger->debug(_T("Constructor"));
92 //Run = RunExists(fileName);
93 RunExists(fileName);
94 info = RunInfo(this);
95 }
96
97 /**
98 * Get the directory name that contains the files of a certain event
99 * type for this run.
100 * @param type the packet type.
101 * @return the complete path for this event type.
102 */
103 std::string PamelaRun::GetDirName(PacketType const * type) const {
104 //std::stringstream oss;
105 //std::string name = type->GetName();
106 //oss.str("");
107 //oss << Path << "/" << Run << "/" << "pippo";
108 //return oss.str();
109 //return "pippo";
110 //std::string EventType = type->GetName();
111 return Path + "/" + Run + "/" + type->GetName();
112 }
113
114 /**
115 * Get the file name for a certain event type.
116 * @param type subpacket type.
117 * @param name subpacket name.
118 * @return the complete path and file name.
119 */
120 std::string PamelaRun::GetFileName(const SubPacket* type, std::string name) {
121 if (type->IsDetectorSpecific()) {
122 return UnpackPath + "/" + type->GetPacketType()->GetName() + "/"
123 + Run + "." + type->GetPacketType()->GetName() + "."
124 + type->GetSubDetector()->GetName() + "."
125 + name + ".root";
126 } else {
127 return UnpackPath + "/" + type->GetPacketType()->GetName() + "/"
128 + Run + "." + type->GetPacketType()->GetName() + "."
129 + name + ".root";
130 /*
131 return Path + "/" + type->GetPacketType()->GetName() + "/"
132 + Run + "." + type->GetPacketType()->GetName() + "."
133 + name + ".root"; */
134 }
135 }
136
137 /**
138 * Get the file name for a certain event type.
139 * @param type subpacket type.
140 * @return the complete path and file name.
141 */
142 std::string PamelaRun::GetFileName(const SubPacket* type) {
143 //return GetFileName(type, type->GetSubPacketName());
144 return GetFileName(type, "pippo");
145 }
146
147 /**
148 * Get the branch name that corresponds to a certain
149 * subpacket. Usually, this is the name of the subpacket.
150 * @param type subpacket type.
151 * @return the corresponding branch name.
152 */
153 static std::string GetDefaultBranchName(const SubPacket* type) {
154 return type->GetSubPacketName();
155 }
156
157 /**
158 * Get the tree name for a certain subpacket. This is the name of the
159 * subdetector for detector specific subpackets, and the packet name
160 * ("Physics" etc.) for common packets.
161 * @param type the packet type.
162 * @return the corresponding tree name.
163 */
164 static std::string GetTreeName(const SubPacket* type) {
165 if (type->IsDetectorSpecific()) {
166 return type->GetSubDetector()->GetName();
167 } else {
168 return type->GetPacketType()->GetName();
169 }
170 }
171
172 /**
173 * Get a list of all root files from a certain path.
174 * @param path Path name to start the search.
175 * @return A list of all file names.
176 */
177 static std::list<std::string> GetRootFiles(std::string path) throw (std::exception) {
178 std::list<std::string> files;
179 DIR *dir = opendir(path.c_str());
180 if (dir == 0) {
181 logger->debug("Could not open " + path);
182 //throw Exception("Could not open " + path);
183 }
184
185 for (struct dirent *d = readdir(dir); d != NULL; d = readdir(dir)) {
186 if ((strcmp(".",d->d_name) == 0) || (strcmp("..",d->d_name) == 0))
187 continue;
188 std::string filename = path + "/" + d->d_name;
189 struct stat buf;
190 stat(filename.c_str(), &buf);
191 if S_ISDIR(buf.st_mode) {
192 std::list<std::string>toAdd = GetRootFiles(filename);
193 files.insert(files.end(), toAdd.begin(), toAdd.end());
194 } else if ((filename.substr(filename.size()-5, filename.size())
195 == ".root") // correct suffix
196 && (!isdigit(filename[filename.size()-6]))) { // base file
197 std::string nextfilename = filename;
198 nextfilename.insert(filename.size()-5, "_1");
199 if (stat(nextfilename.c_str(), &buf) == 0) {
200 filename.insert(filename.size()-5, "*");
201 }
202 logger->debug("Using " + filename);
203 files.push_back(filename);
204 }
205 }
206 return files;
207 }
208
209 /**
210 * Helper function to open the ROOT TTree of the header within the run
211 * framework.
212 * @param type The packet type.
213 * @return the ROOT TTree.
214 */
215 TChain* PamelaRun::ReadHeaderTree(const PacketType *type)
216 throw (std::exception) {
217 EventHeader header(type);
218 std::string FileName = GetFileName(&header);
219 std::string TreeName = GetTreeName(&header);
220 TChain *tree = new TChain(TreeName.c_str());
221 tree->Add(FileName.c_str());
222 return tree;
223 }
224
225 /**
226 * All add trees of a file as friend to a given tree. The tree name of
227 * each tree found in the file is used as the name of the friend alias.
228 * @param tree The base tree to add all other as friends.
229 * @param FileName The name of the file with other trees.
230 */
231 static void AddAllAsFriend(TChain* tree, std::string FileName) {
232 std::string BaseFileName = FileName;
233 bool HaveChain = false;
234 if (BaseFileName[BaseFileName.size()-6] == '*') {
235 BaseFileName.erase(BaseFileName.size()-6, 1);
236 HaveChain = true;
237 }
238 TFile* File = new TFile(BaseFileName.c_str(), "read");
239 if (!File->IsOpen()) {
240 logger->error("Could not open file " + BaseFileName + " for reading.");
241 return;
242 }
243 TList* list = File->GetListOfKeys();
244 if (HaveChain) {
245 for (TIter i(list); TKey* k = (TKey*)i();) {
246 if (std::string(TTree::Class()->GetName()) == k->GetClassName()) {
247 std::string TreeName = k->GetName();
248 TChain* FriendChain = new TChain(TreeName.c_str());
249 FriendChain->Add(FileName.c_str());
250 tree->AddFriend(FriendChain, TreeName.c_str());
251 //logger->warn("Adding chain " + TreeName + " with " + FriendChain->GetEntries() + " entries as Friend");
252 }
253 }
254 File->Close();
255 } else {
256 for (TIter i(list); TKey* k = (TKey*)i();) {
257 if (std::string(TTree::Class()->GetName()) == k->GetClassName()) {
258 std::string TreeName = k->GetName();
259 TTree* FriendTree = (TTree *)File->Get(TreeName.c_str());
260 tree->AddFriend(FriendTree, TreeName.c_str());
261 //logger->debug("Adding tree " + TreeName + " with " + FriendTree->GetEntries() + " entries as Friend");
262 }
263 }
264 }
265 }
266
267 /**
268 * Read all Root TTrees which belong to a certain event type and mount them
269 * together as "friends".
270 * @param type The packet type.
271 * @return The root trees with the friends.
272 */
273 TTree* PamelaRun::ReadTTree(const PacketType* type) {
274 oss.str("");
275 oss << "Getting root files of " << type->GetName();
276 logger->debug(oss.str().c_str());
277 RootTreeMap::iterator t = TTreeMap.find(type);
278 if (t != TTreeMap.end()) {
279 return t->second;
280 } else {
281 oss.str("");
282 oss << "Reading root files of " << type->GetName();
283 logger->debug(oss.str().c_str());
284 EventHeader header(type);
285 std::string HeaderFileName = GetFileName(&header);
286 TChain* HeaderTree = ReadHeaderTree(type);
287 std::list<std::string> rootfiles = GetRootFiles(GetDirName(type));
288 for (std::list<std::string>::iterator i = rootfiles.begin();
289 i != rootfiles.end(); i++){
290 if (*i == HeaderFileName)
291 continue; // dont add the header tree itself.
292 AddAllAsFriend(HeaderTree, *i);
293 }
294 TTreeMap.insert(RootTreeMap::value_type(type, HeaderTree));
295 return HeaderTree;
296 }
297 }
298
299 /**
300 * Register a certain SubPacket, identified by its name, to be read
301 * from the repository. This function is made for interactive work.
302 * @param subpacket A pointer to the pointer of the packet.
303 * @param name The name of the subpacket
304 */
305 void PamelaRun::ReadSubPacket(void* subpacket, std::string name) {
306 SubPacket *packet = *(SubPacket**)subpacket;
307
308 // look into the map of subpackets if we already read it.
309 std::string FullName = packet->GetPacketType()->GetName() + "." + name;
310 SubPacketMap::iterator i = SubPacketAddresses.find(FullName);
311 if (i != SubPacketAddresses.end()) { // it is in the map
312 *(SubPacket**)subpacket = i->second;
313 return;
314 } else { // not found in the map of used subpackets
315 TTree* tree = ReadTTree(packet->GetPacketType());
316 TBranch* branch = tree->GetBranch(name.c_str());
317 if (branch != 0) {
318 branch->SetAddress(subpacket);
319 SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
320 } else {
321 oss.str("");
322 oss << "Could not find data for " << packet->GetPacketType()->GetName() << "/" << name ;
323 logger->error(oss.str().c_str());
324 }
325 }
326 }
327
328 /**
329 * Register a certain SubPacket, identified by its default name, to be
330 * read from the repository. This function is made for
331 * interactive work.
332 * @param subpacket A pointer to the pointer of the packet.
333 */
334 void PamelaRun::ReadSubPacket(void* subpacket) {
335 SubPacket *packet = *(SubPacket**)subpacket;
336 ReadSubPacket(subpacket, GetDefaultBranchName(packet));
337 }
338
339 /**
340 * Register a certain SubPacket with its default name, to be read from
341 * the repository. This functions is for use from the algorithm.
342 * @param algo Algorithm that needs this SubPacket.
343 * @param subpacket A pointer to the pointer of the packet.
344 */
345 void PamelaRun::ReadSubPacket(const Algorithm* algo, void* subpacket) {
346 //:TODO: store the request of the algorithm here.
347 ReadSubPacket(subpacket);
348 }
349
350 /**
351 * Register a certain SubPacket, with a custom name, to
352 * be read from the repository. This functions is for use from
353 * the algorithm.
354 * @param algo Algorithm that needs this SubPacket.
355 * @param subpacket A pointer to the pointer of the packet.
356 * @param name The name of the subpacket
357 */
358 void PamelaRun::ReadSubPacket(const Algorithm* algo, void* subpacket,
359 std::string name) {
360 //:TODO: store the request of the algorithm here.
361 ReadSubPacket(subpacket, name);
362 }
363
364 /**
365 * Helper function to create a ROOT TTree within the run framework.
366 * @param algo Algorithm that creates this SubPacket.
367 * @param packet subpacket type
368 * @param name the name of the subpacket
369 * @return the ROOT TTree.
370 */
371 TTree* PamelaRun::CreateTTree(Algorithm* algo, const SubPacket* packet,
372 std::string name)
373 throw (std::exception) {
374 std::string FileName = "";
375 std::string EventType = packet->GetPacketType()->GetName();
376 TFile* File = 0;
377 std::string TreeName = GetTreeName(packet);
378 TTree *tree = 0;
379 FileName = GetFileName(packet, name);
380 CreateDirectoryStructure(FileName.c_str());
381 File = new TFile(FileName.c_str(), "create");
382 tree = new TTree(TreeName.c_str(), algo->GetAlgorithmName().c_str());
383 WritingRootTrees[packet->GetPacketType()].push_back(tree);
384
385 File->SetCompressionLevel(compression);
386 if (!File->IsOpen()) {
387 logger->error("Could not open file " + FileName);
388 //throw Exception("Could not open file " + FileName);
389 }
390 logger->debug("Creating file " + FileName + " with Tree " + TreeName);
391 return tree;
392 }
393
394
395 /**
396 * Register a certain SubPacket to be written to the repository. A
397 * usual call sequence for this function ist
398 * MyEvent *event = new MyEvent();
399 * run->WriteSubPacket(this, &event, event->Class()
400 * @param algo Algorithm that produces this SubPacket.
401 * @param subpacket A pointer to the pointer of the subpacket.
402 * @param c The class the subpacket belongs to.
403 * @param name The name of the packet.
404 */
405 void PamelaRun::WriteSubPacket(Algorithm *algo, void* subpacket,
406 const TClass *c, std::string name) {
407 SubPacket *packet = *(SubPacket **)subpacket;
408 oss.str("");
409 oss << "Register: " << name << " for " << algo->GetAlgorithmName() << " (writing)";
410 logger->debug(oss.str().c_str());
411 TTree* HeaderTree = ReadTTree(packet->GetPacketType());
412 std::string FullName = packet->GetPacketType()->GetName() + "." + name;
413 if (Multiple) {
414 TTree* tree = CreateTTree(algo, packet, name);
415 oss.str("");
416 oss << "Branch: " << name << " Class: " << c->GetName();
417 logger->debug(oss.str().c_str());
418 tree->Branch(name.c_str(), c->GetName(), subpacket);
419 HeaderTree->AddFriend(tree, tree->GetName());
420 SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
421 } else {
422 std::string nameBranch(name);
423 HeaderTree->Branch(nameBranch.c_str(), c->GetName(), subpacket);
424 SubPacketAddresses.insert(SubPacketMap::value_type(FullName, packet));
425 }
426 }
427
428 /**
429 * Register a certain SubPacket with its default name to be written to
430 * the repository. A usual call sequence for this function ist
431 * MyEvent *event = new MyEvent();
432 * run->WriteSubPacket(this, &event, event->Class()
433 * @param algo Algorithm that produces this SubPacket.
434 * @param subpacket A pointer to the pointer of the subpacket.
435 * @param c The class the subpacket belongs to.
436 */
437 void PamelaRun::WriteSubPacket(Algorithm *algo, void* subpacket,
438 const TClass *c) {
439 SubPacket *packet = *(SubPacket **)subpacket;
440 WriteSubPacket(algo, subpacket, c, GetDefaultBranchName(packet));
441 }
442
443 /**
444 * Write the header packet to all ROOT files in the tree. Intended to
445 * be used for raw data readers that create the initial event structure.
446 * @param algo Algorithm that produces this SubPacket.
447 * @param subpacket A pointer to the pointer of the header packet.
448 */
449 void PamelaRun::WriteHeaders(Algorithm* algo, EventHeader** subpacket) {
450 WriteHeader(algo, subpacket, PacketType::PhysEndRun);
451 WriteHeader(algo, subpacket, PacketType::CalibCalPulse1);
452 WriteHeader(algo, subpacket, PacketType::CalibCalPulse2);
453 WriteHeader(algo, subpacket, PacketType::Physics);
454 WriteHeader(algo, subpacket, PacketType::CalibTrkBoth);
455 WriteHeader(algo, subpacket, PacketType::CalibTrk1);
456 WriteHeader(algo, subpacket, PacketType::CalibTrk2);
457 WriteHeader(algo, subpacket, PacketType::CalibTof);
458 WriteHeader(algo, subpacket, PacketType::CalibS4);
459 WriteHeader(algo, subpacket, PacketType::CalibCalPed);
460 WriteHeader(algo, subpacket, PacketType::Calib1_Ac1);
461 WriteHeader(algo, subpacket, PacketType::Calib1_Ac2);
462 WriteHeader(algo, subpacket, PacketType::Calib2_Ac1);
463 WriteHeader(algo, subpacket, PacketType::Calib2_Ac2);
464 WriteHeader(algo, subpacket, PacketType::RunHeader);
465 WriteHeader(algo, subpacket, PacketType::RunTrailer);
466 WriteHeader(algo, subpacket, PacketType::CalibHeader);
467 WriteHeader(algo, subpacket, PacketType::CalibTrailer);
468 WriteHeader(algo, subpacket, PacketType::InitHeader);
469 WriteHeader(algo, subpacket, PacketType::InitTrailer);
470 WriteHeader(algo, subpacket, PacketType::EventTrk);
471 WriteHeader(algo, subpacket, PacketType::Log);
472 WriteHeader(algo, subpacket, PacketType::VarDump);
473 WriteHeader(algo, subpacket, PacketType::ArrDump);
474 WriteHeader(algo, subpacket, PacketType::TabDump);
475 WriteHeader(algo, subpacket, PacketType::Tmtc);
476 WriteHeader(algo, subpacket, PacketType::Mcmd);
477 WriteHeader(algo, subpacket, PacketType::ForcedFECmd);
478 WriteHeader(algo, subpacket, PacketType::Ac1Init);
479 WriteHeader(algo, subpacket, PacketType::CalInit);
480 WriteHeader(algo, subpacket, PacketType::TrkInit);
481 WriteHeader(algo, subpacket, PacketType::TofInit);
482 WriteHeader(algo, subpacket, PacketType::TrgInit);
483 WriteHeader(algo, subpacket, PacketType::NdInit);
484 WriteHeader(algo, subpacket, PacketType::S4Init);
485 WriteHeader(algo, subpacket, PacketType::Ac2Init);
486 WriteHeader(algo, subpacket, PacketType::CalAlarm);
487 WriteHeader(algo, subpacket, PacketType::Ac1Alarm);
488 WriteHeader(algo, subpacket, PacketType::TrkAlarm);
489 WriteHeader(algo, subpacket, PacketType::TrgAlarm);
490 WriteHeader(algo, subpacket, PacketType::TofAlarm);
491 WriteHeader(algo, subpacket, PacketType::S4Alarm);
492 WriteHeader(algo, subpacket, PacketType::Ac2Alarm);
493 WriteHeader(algo, subpacket, PacketType::TsbT);
494 WriteHeader(algo, subpacket, PacketType::TsbB);
495 }
496
497 /**
498 * Write the ROOT files to disk.
499 */
500 void PamelaRun::WriteFiles(void) {
501 // Workaround: unlink all friend trees first top avoid to store
502 // the links in the header tree file.
503 for (RootTreeMap::iterator i = TTreeMap.begin(); i != TTreeMap.end(); i++) {
504 if (i->second->GetListOfFriends() != 0) {
505 i->second->GetListOfFriends()->Delete();
506 }
507 }
508
509 TTree *softinfo = 0;
510 Int_t version=GetYODAver();
511 //Int_t version= 60312;
512 //printf("version = %f \n",version);
513 softinfo = new TTree("SoftInfo","YODA software info");
514 softinfo->Branch("SoftInfo",&version,"version/I");
515 softinfo->Fill();
516
517 for (TTreeListMap::iterator i = WritingRootTrees.begin();
518 i != WritingRootTrees.end(); i++) {
519 for (TTreeList::iterator j = i->second.begin();
520 j != i->second.end(); j++) {
521 (*j)->GetCurrentFile()->Write(0,TObject::kOverwrite);
522 }
523 }
524
525 softinfo->Delete();
526 }
527
528 /**
529 * Fill all ROOT trees of a certain type that were opened for writing.
530 * @param type the package type of the trees to fill.
531 */
532 void PamelaRun::FillTrees(const PacketType* type) {
533 try{
534 for (TTreeList::iterator j = WritingRootTrees[type].begin();
535 j != WritingRootTrees[type].end(); j++) {
536 (*j)->Fill();
537 }
538 } catch(Exception exc){
539 logger->fatal("ORRORE!!!");
540 }
541 }
542
543
544 /**
545 * Write the header packet of a specified packet type. This is mainly used
546 * for the raw reader to create the base for the event structure.
547 * @param algo Algorithm that produces this SubPacket.
548 * @param subpacket A pointer to the pointer of the packet.
549 * @param type The packet type.
550 */
551 void PamelaRun::WriteHeader(Algorithm* algo, EventHeader** subpacket,
552 const PacketType* type) {
553 EventHeader header(type);
554 std::string EventType = (&header)->GetPacketType()->GetName();
555
556 std::string FileName = "";
557 TFile* File = 0;
558 Long64_t maxsize = 5000000000LL;
559 TTree::SetMaxTreeSize(maxsize);
560 TTree *tree = 0;
561 if ( Multiple ){
562 FileName = GetFileName(&header, GetDefaultBranchName(&header));
563 CreateDirectoryStructure(FileName.c_str());
564 File = new TFile(FileName.c_str(), "create");
565 File->SetCompressionLevel(compression);
566 tree = new TTree("Pscu", algo->GetAlgorithmName().c_str());
567 WritingRootTrees[(&header)->GetPacketType()].push_back(tree);
568 tree->GetCurrentFile()->cd();
569 tree->Branch(GetDefaultBranchName(&header).c_str(),
570 (*subpacket)->Class()->GetName(), subpacket);
571 TTreeMap.insert(RootTreeMap::value_type(type, tree));
572 } else {
573 if (!Multiple && (SingleFile == NULL)){
574 //std::string pathDir((char*)getenv("YODA_DATA"));
575 SingleFile = new TFile((GetPath() + "/" + Run + ".root").c_str(), "update");
576 SingleFile->SetCompressionLevel(compression);
577 }
578 tree = new TTree(EventType.c_str(), algo->GetAlgorithmName().c_str());
579 WritingRootTrees[type].push_back(tree);
580 tree->GetCurrentFile()->cd();
581 tree->Branch(GetDefaultBranchName(&header).c_str(),
582 (*subpacket)->Class()->GetName(), subpacket);
583 TTreeMap.insert(RootTreeMap::value_type(type, tree));
584 }
585 }

  ViewVC Help
Powered by ViewVC 1.1.23